package jnpf.cultivate.controller.web.identify;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.constant.MsgCode;
import jnpf.cultivate.FtbCultivateIdentifyApi;
import jnpf.cultivate.service.CultivateIdentifyApplyService;
import jnpf.cultivate.service.CultivateIdentifyTableService;
import jnpf.enums.cultivate.ApplySourceEnum;
import jnpf.model.cultivate.dto.identify.*;
import jnpf.model.cultivate.dto.statistics.FtbCultivateStatisticsDTO;
import jnpf.model.cultivate.vo.identify.*;
import jnpf.util.CustomTenantUtil;
import jnpf.util.FtbUtil;
import jnpf.util.NoDataSourceBind;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * web实操鉴定
 *
 * @author mouzeping
 */
@Slf4j
@RestController
@RequestMapping(value = "/cultivate")
public class CultivateCultivateIdentifyController implements FtbCultivateIdentifyApi {
    @Autowired
    private CustomTenantUtil customTenantUtil;
    @Resource
    private CultivateIdentifyTableService tableService;
    @Resource
    private CultivateIdentifyApplyService applyService;

    /**
     * 鉴定表/列表
     *
     * @param req 帅选条件
     * @return
     * @
     */
    @Operation(summary = "列表")
    @GetMapping(value = "/identify/list")
    public ActionResult<PageListVO<IdentifyTableListVo>> getPageList(@Valid IdentifyTableListDto req) {
        log.info("列表，入参=>{}", JSONObject.toJSON(req));
        long st = System.currentTimeMillis();
        PageInfo<IdentifyTableListVo> page = tableService.getPageList(req);
        long ent = System.currentTimeMillis();
        log.info("列表，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.page(page.getList(), FtbUtil.getPagination(page));
    }

    /**
     * 鉴定表/详情
     *
     * @return
     * @
     */
    @Operation(summary = "详情")
    @GetMapping(value = "/identify/info")
    public ActionResult<IdentifyTableInfoVo> getInfo(@Valid IdentifyTableInfoDto req) {
        log.info("详情，入参=>{}", JSONObject.toJSON(req));
        long st = System.currentTimeMillis();
        IdentifyTableInfoVo tableInfoVo = tableService.getInfo(req);
        long ent = System.currentTimeMillis();
        log.info("详情，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(tableInfoVo);
    }

    /**
     * 鉴定表/新增
     *
     * @return
     * @
     */
    @Operation(summary = "新增")
    @PostMapping(value = "/identify/save")
    public ActionResult save(@Valid @RequestBody IdentifyTableSaveDto req) {
        log.info("鉴定表/新增，入参=>{}", JSONObject.toJSON(req));
        long st = System.currentTimeMillis();
        tableService.saveData(req);
        long ent = System.currentTimeMillis();
        log.info("鉴定表/新增，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(MsgCode.SU018.get());
    }

    /**
     * 鉴定表/修改
     *
     * @return
     * @
     */
    @Operation(summary = "修改")
    @PutMapping(value = "/identify/update")
    public ActionResult update(@Valid @RequestBody IdentifyTableUpdateDto req) {
        log.info("鉴定表/修改，入参=>{}", JSONObject.toJSON(req));
        long st = System.currentTimeMillis();
        tableService.updateData(req);
        long ent = System.currentTimeMillis();
        log.info("鉴定表/修改，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(MsgCode.SU004.get());
    }


    /**
     * 鉴定表/删除
     *
     * @return
     * @
     */
    @Operation(summary = "删除")
    @DeleteMapping(value = "/identify/delete")
    public ActionResult<IdentifyTableDeleteVo> delete(@Valid IdentifyTableInfoDto req) {
        IdentifyTableDeleteVo tableDeleteVo = tableService.deleteData(req);
        if (CollectionUtil.isNotEmpty(tableDeleteVo.getManuallyInitiateVoList()) ||
                CollectionUtil.isNotEmpty(tableDeleteVo.getPostAndCourseVoList())) {
            ActionResult<IdentifyTableDeleteVo> actionResult = new ActionResult<>();
            actionResult.setCode(400);
            if (CollectionUtil.isNotEmpty(tableDeleteVo.getManuallyInitiateVoList())) {
                actionResult.setMsg("删除失败");
            }
            actionResult.setData(tableDeleteVo);
            return actionResult;
        } else {
            return ActionResult.success("删除成功");
        }
    }


    /**
     * 实操鉴定申请/列表
     *
     * @param req 帅选条件
     * @return
     * @
     */
    @Operation(summary = "列表")
    @GetMapping(value = "/apply/list")
    public ActionResult<PageListVO<IdentifyApplyListVo>> getPageApplyList(@Valid IdentifyApplyListDto req) {
        log.info("实操鉴定申请/列表，入参=>{}", JSONObject.toJSON(req));
        long st = System.currentTimeMillis();
        PageInfo<IdentifyApplyListVo> page = applyService.getPageApplyList(req);
        long ent = System.currentTimeMillis();
        log.info("实操鉴定申请/列表，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.page(page.getList(), FtbUtil.getPagination(page));
    }


    /**
     * 实操鉴定申请/详情(基本信息)
     *
     * @param req 帅选条件
     * @return
     * @
     */
    @Operation(summary = "详情(基本信息)")
    @GetMapping(value = "/apply/basicInfo")
    public ActionResult<IdentifyApplyBasicInfoVo> getApplyBasicInfo(@Valid IdentifyTableInfoDto req) {
        log.info("实操鉴定申请/详情(基本信息)，入参=>{}", JSONObject.toJSON(req));
        long st = System.currentTimeMillis();
        IdentifyApplyBasicInfoVo basicInfo = applyService.getApplyBasicInfo(req);
        long ent = System.currentTimeMillis();
        log.info("实操鉴定申请/详情(基本信息)，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(basicInfo);
    }

    /**
     * 实操鉴定申请/详情(鉴定详情)
     *
     * @param req 帅选条件
     * @return
     * @
     */
    @Operation(summary = "详情(鉴定详情)")
    @GetMapping(value = "/apply/info")
    public ActionResult<IdentifyApplyInfoVo> getApplyInfo(@Valid IdentifyTableInfoDto req) {
        log.info("实操鉴定申请/详情(鉴定详情)，入参=>{}", JSONObject.toJSON(req));
        long st = System.currentTimeMillis();
        IdentifyApplyInfoVo infoVo = applyService.getApplyInfo(req);
        long ent = System.currentTimeMillis();
        log.info("实操鉴定申请/详情(鉴定详情)，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(infoVo);
    }

    /**
     * 实操鉴定申请/新增
     *
     * @param req 帅选条件
     * @return
     * @
     */
    @PostMapping(value = "/apply/save")
    public ActionResult applyDataSave(@Valid @RequestBody BatchIdentifyApplySaveDto req) {
        req.setSource(ApplySourceEnum.SDFQ.getCode());
        applyService.applyDataSaveBatch(req);
        return ActionResult.success(MsgCode.SU018.get());
    }


    /**
     * 实操鉴定申请/推送
     *
     * @param req 帅选条件
     * @return
     * @
     */
    @Operation(summary = "新增")
    @PostMapping(value = "/apply/push")
    public ActionResult applyDataSave(@Valid @RequestBody IdentifyApplyDataPushDto req) {
        log.info("实操鉴定申请/推送，入参=>{}", JSONObject.toJSON(req));
        long st = System.currentTimeMillis();
        applyService.applyDataPush(req);
        long ent = System.currentTimeMillis();
        log.info("实操鉴定申请/推送，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(MsgCode.SU018.get());
    }

    /**
     * 实操鉴定申请/重新鉴定
     *
     * @param req 帅选条件
     * @return
     * @
     */
    @Operation(summary = "重新鉴定")
    @PutMapping(value = "/apply/reIdentify")
    public ActionResult applyDataReIdentify(@Valid @RequestBody IdentifyApplyReIdentifyDto req) {
        log.info("实操鉴定申请/重新鉴定，入参=>{}", JSONObject.toJSON(req));
        long st = System.currentTimeMillis();
        applyService.applyDataReIdentify(req);
        long ent = System.currentTimeMillis();
        log.info("实操鉴定申请/重新鉴定，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(MsgCode.SU005.get());
    }

    /**
     * 实操鉴定申请/删除检验
     *
     * @param req 帅选条件
     * @return
     * @
     */
    @Operation(summary = "删除检验")
    @GetMapping(value = "/apply/deleteCheck")
    public ActionResult<List<IdentifyApplyListVo>> applyDataDeleteCheck(@Valid IdentifyTableInfoDto req) {
        log.info("实操鉴定申请/删除检验，入参=>{}", JSONObject.toJSON(req));
        long st = System.currentTimeMillis();
        List<IdentifyApplyListVo> applyListVoList = applyService.applyDataDeleteCheck(req);
        if (applyListVoList.size() > 0) {
            ActionResult<List<IdentifyApplyListVo>> actionResult = new ActionResult<>();
            actionResult.setCode(400);
            actionResult.setMsg("检验失败");
            actionResult.setData(applyListVoList);
            long ent = System.currentTimeMillis();
            log.info("实操鉴定申请/删除检验，耗时=>{}", ent - st + " 毫秒");
            return actionResult;
        } else {
            return ActionResult.success("检验成功");
        }
    }

    /**
     * 实操鉴定申请/删除
     *
     * @param req 帅选条件
     * @return
     * @
     */
    @Operation(summary = "删除")
    @DeleteMapping(value = "/apply/delete")
    public ActionResult applyDataDelete(@Valid IdentifyTableInfoDto req) {
        try {
            log.info("实操鉴定申请/删除，入参=>{}", JSONObject.toJSON(req));
            long st = System.currentTimeMillis();
            applyService.applyDataDelete(req);
            long ent = System.currentTimeMillis();
            log.info("实操鉴定申请/删除，耗时=>{}", ent - st + " 毫秒");
            return ActionResult.success(MsgCode.SU003.get());
        } catch (Exception e) {
            e.printStackTrace();
            return ActionResult.fail(e.getMessage());
        }
    }

    /**
     * 数据看板/统计（鉴定）
     *
     * @param req 帅选条件
     * @return
     * @
     */
    @Operation(summary = "统计（鉴定）")
    @GetMapping(value = "/apply/statistics")
    public ActionResult getIdentifyStatistics(@Valid FtbCultivateStatisticsDTO req) {
        try {
            log.info("数据看板/统计（鉴定），入参=>{}", JSONObject.toJSON(req));
            long st = System.currentTimeMillis();
            IdentifyStatisticsVo statisticsVo = applyService.getIdentifyStatistics(req);
            long ent = System.currentTimeMillis();
            log.info("数据看板/统计（鉴定），耗时=>{}", ent - st + " 毫秒");
            return ActionResult.success(statisticsVo);
        } catch (Exception e) {
            e.printStackTrace();
            return ActionResult.fail(e.getMessage());
        }
    }

    /**
     * 数据看板/排名（鉴定）
     *
     * @param req 帅选条件
     * @return
     * @
     */
    @Operation(summary = "排名（鉴定）")
    @GetMapping(value = "/apply/top")
    public ActionResult<List<IdentifyTopVo>> getIdentifyTop(@Valid FtbCultivateStatisticsDTO req) {
        try {
            log.info("数据看板/排名（鉴定），入参=>{}", JSONObject.toJSON(req));
            long st = System.currentTimeMillis();
            List<IdentifyTopVo> topVoList = applyService.getIdentifyTop(req);
            long ent = System.currentTimeMillis();
            log.info("数据看板/排名（鉴定），耗时=>{}", ent - st + " 毫秒");
            return ActionResult.success(topVoList);
        } catch (Exception e) {
            e.printStackTrace();
            return ActionResult.fail(e.getMessage());
        }
    }

    /**
     * 处理鉴定逾期数据
     *
     * @return java.lang.Boolean
     */
    @Override
    @PostMapping(value = "/apply/setIdentifyBeOverdue")
    @NoDataSourceBind
    public Boolean setIdentifyBeOverdue(@RequestParam(value = "tenantId") String tenantId) {
        customTenantUtil.checkOutTenant(tenantId);
        return applyService.setIdentifyBeOverdue();
    }

    /**
     * 计划鉴定时间提醒
     *
     * @return java.lang.Boolean
     */
    @Override
    @PostMapping(value = "/apply/setIdentifyRemind")
    @NoDataSourceBind
    public Boolean setIdentifyRemind(@RequestParam(value = "tenantId") String tenantId) {
        customTenantUtil.checkOutTenant(tenantId);
        return applyService.setIdentifyRemind(tenantId);
    }
}


