package com.flossom.miniProgram.controller;

import com.flossom.common.core.constant.Constants;
import com.flossom.common.core.domain.R;
import com.flossom.common.core.domain.SysFile;
import com.flossom.common.core.domain.entity.WxClockLog;
import com.flossom.common.core.domain.entity.WxClockStatistics;
import com.flossom.common.core.domain.req.WxClockLogReq;
import com.flossom.common.core.domain.ret.WxClockLogRet;
import com.flossom.common.core.exception.ServiceException;
import com.flossom.common.core.web.controller.BaseController;
import com.flossom.common.core.web.page.TableDataInfo;
import com.flossom.common.security.utils.SecurityUtils;
import com.flossom.miniProgram.service.IWxClockLogService;
import com.flossom.system.api.RemoteFileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户打卡Controller
 *
 * @author flossom
 * @date 2024-01-27
 */
@RestController
@RequestMapping("/clock")
public class WxClockLogController extends BaseController {

    @Autowired
    private IWxClockLogService wxClockLogService;

    @Autowired
    private RemoteFileService remoteFileService;

    /**
     * 添加用户使用仪器记录表
     */
    @GetMapping("/addClockInstrument")
    public R addClockInstrument(@NotNull(message = "请选择仪器ID") @RequestParam("instrumentId") Long instrumentId) {
        wxClockLogService.addClockInstrument(instrumentId);
        return R.ok();
    }

    /**
     * 获取用户当天最新的一条打卡记录
     */
    @GetMapping("/latestClockRecord")
    public R latestClockRecord() {
        return R.ok(wxClockLogService.latestClockRecord());
    }

    /**
     * 分页查询用户的打卡记录
     */
    @GetMapping("/list")
    public R list(@RequestParam(value = "year") Integer year, @RequestParam(value = "month") Integer month) {
        WxClockLog queryClockLog = new WxClockLog();
        queryClockLog.setUserId(SecurityUtils.getLoginUser().getWxUserMember().getId());
        YearMonth targetYearMonth = YearMonth.of(year, month);
        queryClockLog.getParams().put("beginTime", targetYearMonth.atDay(1).atStartOfDay());
        queryClockLog.getParams().put("endTime", targetYearMonth.atEndOfMonth().atTime(LocalTime.MAX));
        return R.ok(wxClockLogService.selectWxClockLogList(queryClockLog));
    }

    /**
     * 上传打卡图片
     */
    @PostMapping("/upload")
    public R insertClockLog(@RequestParam(value = "clockImg") MultipartFile clockImg) {
        return remoteFileService.upload(clockImg);
    }


    /**
     * 新增用户打卡
     * 需要记录当天使用过的全部仪器（打卡前后的全部仪器）
     * firstClockImg 第一张图片是必须的
     */
    @PostMapping("/insertClockLog")
    public R insertClockLog(@Validated @RequestBody WxClockLogReq wxClockLogReq) {
        if (wxClockLogReq.getClockImageList() == null || wxClockLogReq.getClockImageList().size() == 0) {
            logger.error("请上传打卡图片");
            throw new ServiceException("请上传打卡图片");
        }
        if (wxClockLogReq.getClockImageList().size() > 3) {
            logger.error("打卡图片只能上传3张以下");
            throw new ServiceException("打卡图片只能上传3张以下");
        }
        wxClockLogService.insertWxClockLog(wxClockLogReq);
        return R.ok();
    }


    /**
     * 获取打卡统计
     */
    @GetMapping("/clockStatistics")
    public R clockStatistics(@RequestParam(value = "year") Integer year) {
        return R.ok(wxClockLogService.clockStatistics(year));
    }


    /**
     * TODO： 后期迁移到 system 项目中
     * 定时任务接口
     * 每月第一天将上个月在redis中的数据同步到数据库中，计算打卡比率
     */
    @GetMapping("/clockStatisticsTimedTask")
    public void clockStatisticsTimedTask(@RequestParam(value = "userIdList", required = false) List<Long> userIdList,
                                         @RequestParam(value = "year", required = false) Integer year,
                                         @RequestParam(value = "month", required = false) Integer month) {
        wxClockLogService.clockStatisticsTimedTask(userIdList, year, month);
    }

}
