package com.propertyManagement.web.controller.business.api;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.propertyManagement.business.domain.WxClock;
import com.propertyManagement.business.domain.bo.WxClockBo;
import com.propertyManagement.business.domain.vo.WxClockVo;
import com.propertyManagement.business.domain.vo.WxClockVo2;
import com.propertyManagement.business.mapper.WxClockMapper;
import com.propertyManagement.business.service.IWxClockService;
import com.propertyManagement.business.support.util.AuthUtil;
import com.propertyManagement.common.annotation.RepeatSubmit;
import com.propertyManagement.common.core.controller.BaseController;
import com.propertyManagement.common.core.domain.R;
import com.propertyManagement.common.core.validate.AddGroup;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 打卡
 *
 * @author liushuai
 * @date 2025-05-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/clock")
public class ApiWxClockController extends BaseController {

    private final IWxClockService iWxClockService;
    private final WxClockMapper wxClockMapper;

    /**
     * 查询打卡列表
     */
    @GetMapping("/listAll")
    public R<WxClockVo2> list() {
        Long accountId = AuthUtil.getWxUser().getAccountId();
        WxClockVo2 wxClockVo2 = new WxClockVo2();
        // 获取本月第一天和最后一天
        DateTime beginOfMonth = DateUtil.beginOfMonth(DateUtil.date());
        DateTime endOfMonth = DateUtil.endOfMonth(DateUtil.date());
        // 生成本月所有日期列表
        List<DateTime> dateTimes = DateUtil.rangeToList(beginOfMonth, endOfMonth, DateField.DAY_OF_MONTH);
        // 并行处理提高效率
        List<WxClockVo2.ClockRecord> clockRecordList = dateTimes.parallelStream()
            .map(item -> {
                String dateStr = DateUtil.format(item, "yyyy-MM-dd");
                int count = Math.toIntExact(wxClockMapper.selectCount(Wrappers.<WxClock>lambdaQuery()
                    .apply("DATE(clock_time) = {0}", dateStr)
                    .eq(WxClock::getAccountId, accountId)));
                return new WxClockVo2.ClockRecord(dateStr, count > 0 ? 1 : 0);
            })
            .collect(Collectors.toList());
        // 计算打卡和缺卡天数
        long clockDays = clockRecordList.stream().filter(r -> r.getClockState() == 1).count();
        wxClockVo2.setClockDay((int) clockDays);
        wxClockVo2.setNoRecordDay(clockRecordList.size() - (int) clockDays);
        wxClockVo2.setClockRecordList(clockRecordList);
        return R.ok(wxClockVo2);
    }

    /**
     * 获取打卡详细信息
     *
     * @param date 日期
     */
    @GetMapping("/record/{date}")
    public R<List<WxClockVo>> getInfo(@NotEmpty(message = "日期不能为空") @PathVariable String date) {
        LambdaQueryWrapper<WxClock> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.apply("DATE(clock_time) = {0}", date).eq(WxClock::getAccountId, AuthUtil.getWxUser().getAccountId());
        return R.ok(wxClockMapper.selectVoList(queryWrapper));
    }

    /**
     * 新增打卡
     */
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxClockBo bo) {
        return toAjax(iWxClockService.add(bo));
    }
}
