package com.czg.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.czg.account.entity.ShopConfig;
import com.czg.account.service.ShopConfigService;
import com.czg.market.dto.AttendanceConfigDTO;
import com.czg.market.service.AttendanceService;
import com.czg.market.vo.DingAttendanceStatsVO;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Map;

/**
 * 考勤打卡数据
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/attendance")
public class AttendanceController {
    @Resource
    private AttendanceService attendanceService;
    @DubboReference
    private ShopConfigService shopConfigService;

    /**
     * 配置获取
     * @return 配置信息
     */
    @GetMapping("/config")
    public CzgResult<Map<String, String>> getConfig() {
        ShopConfig config = shopConfigService.getById(StpKit.USER.getShopId());
        return CzgResult.success(Map.of(
                "dingAppKey", config == null ? "" : config.getDingAppKey(),
                "dingAppSecret", config == null ? "" :config.getDingAppSecret()
        ));
    }

    /**
     * 修改配置
     */
    @PutMapping("/config")
    public CzgResult<Boolean> editConfig(@Validated @RequestBody AttendanceConfigDTO dto) {
        ShopConfig shopConfig = BeanUtil.copyProperties(dto, ShopConfig.class);
        shopConfig.setId(StpKit.USER.getShopId());
        return CzgResult.success(shopConfigService.editConfig(StpKit.USER.getShopId(), shopConfig));
    }


    /**
     * 列表
     * @param name 姓名
     * @param startTime 开始时间
     * @param endTime 结束时间
     */
    @GetMapping
    public CzgResult<ArrayList<DingAttendanceStatsVO>> getList(
                                                               @RequestParam(required = false) String name, @RequestParam(required = false) String startTime,
                                                               @RequestParam(required = false) String endTime) {
        return CzgResult.success(attendanceService.list(StpKit.USER.getShopId(),  name, startTime, endTime, null));
    }


    /**
     * 用户打卡详情
     * @param userId 返回的userId
     * @param startTime 开始时间
     * @param endTime 结束时间
     */
    @GetMapping("/detail")
    public CzgResult<Map<String, Object>> detail(
            @RequestParam String userId, @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime, @RequestParam(required = false, defaultValue = "0") Integer weekNum) {
        return CzgResult.success(attendanceService.detail(StpKit.USER.getShopId(),  userId, startTime, endTime, weekNum));
    }
}
