package com.ruoyi.api.controller.clock;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.api.controller.common.AbstractApiController;
import com.ruoyi.clock.domain.Employee;
import com.ruoyi.clock.domain.bo.ActivityAgentBo;
import com.ruoyi.clock.domain.bo.ClockCalendarBo;
import com.ruoyi.clock.domain.bo.ClockRecordBo;
import com.ruoyi.clock.domain.bo.SaleStatisticBo;
import com.ruoyi.clock.domain.vo.*;
import com.ruoyi.clock.service.IClockRecordService;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotNull;
import java.util.List;


/**
 * 打卡记录信息Controller
 *
 * @author pengc
 * @date 2026-03-04
 */
@Validated
@Api(value = "打卡记录信息控制器", tags = {"打卡记录信息管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/clock/clockRecord")
public class ApiClockRecordController extends AbstractApiController {

    private final IClockRecordService iClockRecordService;

    /**
     * 分页查询打卡记录信息列表
     */
    @ApiOperation("查询打卡记录信息列表")
    @GetMapping("/page")
    public TableDataInfo<ClockRecordVo> page(@Validated(QueryGroup.class) ClockRecordBo bo, PageQuery pageQuery) {
        return iClockRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取打卡记录信息详细信息
     */
    @ApiOperation("获取打卡记录信息详细信息")
    @GetMapping("/info/{recordId}")
    public R<ClockRecordVo> getInfo(@ApiParam("主键")
                                                  @NotNull(message = "主键不能为空")
                                                  @PathVariable("recordId") Long recordId) {
        return R.ok(iClockRecordService.queryById(recordId));
    }


    /**
     * 首页查看打卡统计信息
     */
    @ApiOperation("首页查看打卡统计信息")
    @GetMapping("/index/statistic")
    public R<ClockStatisticRecordVo> statistic() {
        Employee employee = getLoginUser();
        if(ObjectUtil.isNull(employee)){
           return R.ok(new ClockStatisticRecordVo());
        }
        return R.ok(iClockRecordService.queryClockStatisticRecord(employee.getEmployeeId()));
    }

    /**
     * 查询活动销量打卡排行
     */
    @ApiOperation("查询活动销量打卡排行")
    @PostMapping("/saleRankList")
    public R<List<SaleStatisticRecordVo>> saleRankList(@Validated(QueryGroup.class) @RequestBody SaleStatisticBo bo) {
        return R.ok(iClockRecordService.querySaleStatisticRecordList(bo));
    }


    /**
     * 查询我的打卡销量排名
     */
    @ApiOperation("查询活动销量打卡排行")
    @PostMapping("/mySaleRank")
    public R<MySaleStatisticRecordVo> mySaleRank(@Validated(QueryGroup.class) @RequestBody SaleStatisticBo bo) {
        Employee loginUser = getLoginUser();
        if(ObjectUtil.isNull(loginUser)){
            return R.ok(new MySaleStatisticRecordVo());
        }
        bo.setEmployeeId(loginUser.getEmployeeId());
        return R.ok(iClockRecordService.mySaleRank(bo));
    }


    /**
     * 查询我的活动打卡统计
     */
    @ApiOperation("查询我的活动打卡统计")
    @PostMapping("/myActivityStatistic")
    public R<MyActivityStatisticVo> myActivityStatistic(@Validated(QueryGroup.class) @RequestBody SaleStatisticBo bo) {
        Employee loginUser = getLoginUser();
        if(ObjectUtil.isNull(loginUser)){
            return R.ok(new MyActivityStatisticVo());
        }
        bo.setEmployeeId(loginUser.getEmployeeId());
        return R.ok(iClockRecordService.myActivityStatistic(bo));
    }

    /**
     * 打卡（晒单打卡 / 销量打卡，每天只能打卡一次，已打卡则更新）
     * 活动类型：0-晒单打卡  1-销量打卡
     * 销量打卡时需传 productList
     */
    @ApiOperation("打卡（晒单/销量，每天限一次，支持修改）")
    @RepeatSubmit()
    @PostMapping("/submit")
    public R<Long> submitClock(@Validated(AddGroup.class) @RequestBody ClockRecordBo bo) {
        Long employeeId = getUserId();
        return R.ok(iClockRecordService.submitClock(bo, employeeId));
    }

    /**
     * 查询打卡日历
     * 传入活动id、年、月，返回该月每天的打卡状态及详情
     */
    @ApiOperation("查询打卡日历")
    @GetMapping("/calendar")
    public R<List<ClockCalendarDayVo>> calendar(@Validated(QueryGroup.class) ClockCalendarBo bo) {
        Long employeeId = getUserId();
        return R.ok(iClockRecordService.queryCalendar(bo, employeeId));
    }

}
