package jnpf.cultivate.controller.web.statistics;

import jnpf.base.ActionResult;
import jnpf.cultivate.FtbCultivateStoreStatisticApi;
import jnpf.cultivate.service.FtbCultivateStatisticsService;
import jnpf.cultivate.service.FtbCultivateStoreStatisticService;
import jnpf.cultivate.utils.UserApiV2Util;
import jnpf.model.cultivate.dto.statistics.FtbCultivateStatisticsDTO;
import jnpf.model.cultivate.dto.storestatistics.*;
import jnpf.model.cultivate.vo.position.*;
import jnpf.model.cultivate.vo.statistics.NumberofAppSessions;
import jnpf.model.thousandsfaces.TodayWorkVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Objects;
import java.util.Calendar;
import java.util.Date;


/**
 * 培训店长统计模块
 *
 * @author xgl
 */
@RestController
@RequestMapping("/web/store-index-statistics")
@Validated
@Slf4j
public class FtbCultivateStoreStatisticController implements FtbCultivateStoreStatisticApi {

    @Autowired
    private FtbCultivateStoreStatisticService ftbCultivateStoreStatisticService;

    @Resource
    private FtbCultivateStatisticsService ftbCultivateStatisticsService;

    /**
     * 店长页面 培训统计数量
     */
    @Override
    @PostMapping("/get-cultivate-count")
    public ActionResult<FtbCultivateStoreCountVO> getCultivateCount(@RequestBody FtbCultivateStoreStatisticsReq req) {
        FtbCultivateStoreCountVO vo = ftbCultivateStoreStatisticService.getCultivateCount(req);
        return ActionResult.success(vo);
    }


    /**
     * 查询我的考试列表
     *
     * @param req
     * @return
     */
    @Override
    @PostMapping("/queryMyExamList")
    public ActionResult<List<TodayWorkVo>> queryMyExamList(@RequestBody StoreStatisticsMyExamReq req) {
        List<TodayWorkVo> list = ftbCultivateStoreStatisticService.queryMyExamList(req);
        return ActionResult.success(list);
    }

    /**
     * 待我批阅(下属的)
     *
     * @param req
     * @return
     */
    @Override
    @PostMapping("/queryWaitMyReadOver")
    public ActionResult<List<TodayWorkVo>> queryWaitMyReadOver(@RequestBody StoreStatisticsWaitMyCheckExamReq req) {
        List<TodayWorkVo> list = ftbCultivateStoreStatisticService.queryWaitMyReadOver(req);
        return ActionResult.success(list);
    }

    /**
     * 今日工作-待参与及参与中的线下培训<负责人及参与人均属于相关人员>列表
     *
     * @param req
     * @return
     */
    @Override
    @PostMapping("/queryOfflineTrainList")
    public ActionResult<List<TodayWorkVo>> queryOfflineTrainList(@RequestBody StoreOfflineTrainReq req) {
        List<TodayWorkVo> list = ftbCultivateStoreStatisticService.queryOfflineTrainList(req);
        return ActionResult.success(list);
    }


    /**
     * 今日工作-我的任务（当日开始的培训任务）列表
     *
     * @param req
     * @return
     */
    @Override
    @PostMapping("/myTaskList")
    public ActionResult<List<TodayWorkVo>> storeMyTaskList(@RequestBody StoreOfflineTrainReq req) {
        List<TodayWorkVo> list = ftbCultivateStoreStatisticService.storeMyTaskList(req);
        return ActionResult.success(list);
    }


    /**
     * 今日工作-鉴定他人（当日的待鉴定）列表
     *
     * @param req
     * @return
     */
    @Override
    @PostMapping("/myIdentityList")
    public ActionResult<List<TodayWorkVo>> storeMyIdentityList(@RequestBody StoreIdentityReq req) {
        List<TodayWorkVo> list = ftbCultivateStoreStatisticService.storeMyIdentityList(req);
        return ActionResult.success(list);
    }

    /**
     * 员工界面 查询未学习的通用课程数量
     *
     * @param req
     * @return
     */
    @Override
    @PostMapping("/get-worker-cultivate-count")
    public ActionResult<FtbCultivateWorkerCountVO> getWorkerCultivateCount(@RequestBody FtbCultivateStoreStatisticsReq req) {
        FtbCultivateWorkerCountVO vo = ftbCultivateStoreStatisticService.getWorkerCultivateCount(req);
        return ActionResult.success(vo);
    }

    /**
     * 工作台-员工
     */
    @Override
    @PostMapping("/person/training-statistics")
    public ActionResult<FtbPersonTrainingStatisticsVO> personTrainingStatistics(@RequestBody FtbCultivateStoreStatisticsReq req) {
        FtbPersonTrainingStatisticsVO vo = ftbCultivateStoreStatisticService.personTrainingStatistics(req);
        return ActionResult.success(vo);
    }


    /**
     * 工作台-店长
     */

    @PostMapping("/store-manager/training-statistics")
    public ActionResult<FtbStoreManagerTrainingStatisticsVO> storeManagerTrainingStatistics(@RequestBody FtbCultivateStoreStatisticsReq req) {
        FtbStoreManagerTrainingStatisticsVO vo = ftbCultivateStoreStatisticService.storeManagerTrainingStatistics(req);
        return ActionResult.success(vo);
    }

    /**
     * 工作台-管理层
     */

    @PostMapping("/manager/training-statistics")
    public ActionResult<FtbManagerTrainingStatisticsVO> managerTrainingStatistics(@RequestBody FtbCultivateStoreStatisticsReq req) {
        FtbManagerTrainingStatisticsVO vo = new FtbManagerTrainingStatisticsVO();
        vo.initData();
        FtbCultivateStatisticsDTO dto = new FtbCultivateStatisticsDTO();
        dto.setOrgId(req.getStoreId());
        dto.setOrgIds(req.getStoreIds());
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date startTime = calendar.getTime();
        dto.setEndTime(new Date());
        dto.setStartTime(startTime);
        NumberofAppSessions numberofAppSessions = ftbCultivateStatisticsService.trainingStatisticsV2(dto);
        if (Objects.nonNull(numberofAppSessions)) {
            BigDecimal identPass = numberofAppSessions.getNumberOfOperation();
            BigDecimal identTotal = numberofAppSessions.getTotalNumberOfAppraisals();
            BigDecimal examPass = numberofAppSessions.getNumberOfExam();
            BigDecimal examTotal = new BigDecimal(numberofAppSessions.getTotalNumberExam());
            if (identTotal.compareTo(BigDecimal.ZERO) != 0) {
                vo.setIdentificationPassRate(UserApiV2Util.dealWithRate(identPass.divide(identTotal, 4, RoundingMode.HALF_UP), ""));
            }
            if (examTotal.compareTo(BigDecimal.ZERO) != 0) {
                vo.setExamPassRate(UserApiV2Util.dealWithRate(examPass.divide(examTotal, 4, RoundingMode.HALF_UP), ""));
            }
        }
        return ActionResult.success(vo);
    }

}
