package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StoreClockIn;
import shop.alien.entity.store.vo.StoreClockInVo;
import shop.alien.store.service.StoreClockInService;

@Slf4j
@Api(tags = {"店铺打卡"})
@CrossOrigin
@RestController
@RequestMapping("/storeClockIn")
@RequiredArgsConstructor
public class StoreClockInController {

    private final StoreClockInService storeClockInService;

    @ApiOperation("打卡")
    @ApiOperationSupport(order = 1)
    @PostMapping("/addStoreClockIn")
    public R<StoreClockIn> addStoreClockIn(@RequestBody StoreClockIn storeClockIn) {
        log.info("StoreClockInController.addStoreClockIn?storeClockIn={}", storeClockIn.toString());
        return R.data(storeClockInService.addStoreClockIn(storeClockIn));
    }

    @ApiOperation("打卡列表")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "分页页数", dataType = "Integer", defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "分页条数", dataType = "Integer", defaultValue = "10", paramType = "query"),
            @ApiImplicitParam(name = "phoneId", value = "'user_' + 手机号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "mySelf", value = "是否只查看自己（0-否  1-是）", dataType = "Integer", defaultValue = "0", paramType = "query"),
            @ApiImplicitParam(name = "storeId", value = "店铺id", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/getStoreClockInList")
    public R<IPage<StoreClockInVo>> getStoreClockInList(Integer userId, int page, int size, String phoneId, int mySelf, @RequestParam(required = false) Integer storeId) {
        log.info("StoreClockInController.getStoreClockInList?userId={},page={},size={},phoneId={},mySelf={},storeId={}", userId, page, size, phoneId, mySelf,storeId);
        return R.data(storeClockInService.getStoreClockInList(userId, page, size, phoneId, mySelf,storeId));
    }

    @ApiOperation("删除打卡记录")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id", dataType = "Integer", paramType = "query")})
    @GetMapping("/deleteClockIn")
    public R<IPage<StoreClockInVo>> deleteClockIn(Integer id) {
        log.info("StoreClockInController.deleteClockIn?id={}", id);
        int result = storeClockInService.deleteClockIn(id);
        return 1 == result ? R.success("删除成功") : R.fail("删除失败");
    }

    @ApiOperation("设置权限")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id", dataType = "int", paramType = "query"), @ApiImplicitParam(name = "id", value = "主键id", dataType = "Integer", paramType = "query"), @ApiImplicitParam(name = "permission", value = "权限  1-所有人可见  2-仅自己可见", dataType = "int", paramType = "query")})
    @GetMapping("/setPermission")
    public R<IPage<StoreClockInVo>> setPermission(int userId, Integer id, int permission) {
        log.info("StoreClockInController.setPermission?userId={},id={},permission={}", userId, id, permission);
        int result = storeClockInService.setPermission(userId, id, permission);
        return 1 <= result ? R.success("设置成功") : R.fail("设置失败");
    }

    @ApiOperation("设置图片")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id", dataType = "Integer", paramType = "query"), @ApiImplicitParam(name = "img", value = "打卡图片", dataType = "String", paramType = "query")})
    @GetMapping("/setImg")
    public R<IPage<StoreClockInVo>> setImg(int id, String img) {
        log.info("StoreClockInController.setImg?id={},img={}", id, img);
        int result = storeClockInService.setImg(id, img);
        return 1 == result ? R.success("设置成功") : R.fail("设置失败");
    }

}
