package com.ruoyi.project.common.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.document.domain.TPlantproglist;
import com.ruoyi.project.document.service.ITPlantproglistService;
import com.ruoyi.project.ehs.domain.*;
import com.ruoyi.project.ehs.service.ITEnvironapprovalService;
import com.ruoyi.project.ehs.service.ITFireapprovalService;
import com.ruoyi.project.ehs.service.ITHealthapprovalService;
import com.ruoyi.project.ehs.service.ITSafetyapprovalService;
import com.ruoyi.project.training.domain.TTraining;
import com.ruoyi.project.training.mapper.TWorkcertificateCbpsMapper;
import com.ruoyi.project.training.newstaff.domain.TTnNew;
import com.ruoyi.project.training.service.ITTrainingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/cbps/home")
public class CBPSHomeDataController extends BaseController {
    @Autowired
    private ITSafetyapprovalService tSafetyapprovalService;

    @Autowired
    private ITFireapprovalService tFireapprovalService;

    @Autowired
    private ITEnvironapprovalService tEnvironapprovalService;

    @Autowired
    private ITHealthapprovalService tHealthapprovalService;

    @Autowired
    private ITPlantproglistService tPlantproglistService;

    @Autowired
    private ITTrainingService tTrainingService;
    @Resource
    private TWorkcertificateCbpsMapper tWorkcertificateCbpsMapper;

    @GetMapping("/countNum")
    public AjaxResult getCountNum() {
        HashMap<String, Integer> result = new HashMap<>();
        List<TSafetyapproval> safeList = tSafetyapprovalService.selectTSafetyapprovalList(new TSafetyapproval());
        int safeWarnNum = 0;
        for (TSafetyapproval t: safeList
             ) {
            if (t.getNextreviewdate() != null) {
                long now = System.currentTimeMillis();
                long diff = t.getNextreviewdate().getTime() - now;
                long nd = 1000 * 24 * 60 * 60;
                long editDifference = diff / nd;
                if (editDifference < 30) {
                    safeWarnNum ++;
                    continue;
                }
            }
        }
        result.put("safeNum", safeList.size());
        result.put("safeWarnNum", safeWarnNum);

        List<TFireapproval> fireList = tFireapprovalService.selectTFireapprovalList(new TFireapproval());
        int fireWarnNum = 0;
        for (TFireapproval t: fireList
        ) {
            if (t.getNextreviewdate() != null) {
                long now = System.currentTimeMillis();
                long diff = t.getNextreviewdate().getTime() - now;
                long nd = 1000 * 24 * 60 * 60;
                long editDifference = diff / nd;
                if (editDifference < 30) {
                    fireWarnNum ++;
                    continue;
                }
            }
        }
        result.put("fireNum", fireList.size());
        result.put("fireWarnNum", fireWarnNum);
        List<TEnvironapproval> envList = tEnvironapprovalService.selectTEnvironapprovalList(new TEnvironapproval());
        int envWarnNum = 0;
        for (TEnvironapproval t: envList
        ) {
            if (t.getNextreviewdate() != null) {
                long now = System.currentTimeMillis();
                long diff = t.getNextreviewdate().getTime() - now;
                long nd = 1000 * 24 * 60 * 60;
                long editDifference = diff / nd;
                if (editDifference < 30) {
                    envWarnNum ++;
                    continue;
                }
            }
        }
        result.put("envNum", envList.size());
        result.put("envWarnNum", envWarnNum);

        List<THealthapproval> healthList = tHealthapprovalService.selectTHealthapprovalList(new THealthapproval());
        int healthWarnNum = 0;
        for (TEnvironapproval t: envList
        ) {
            if (t.getNextreviewdate() != null) {
                long now = System.currentTimeMillis();
                long diff = t.getNextreviewdate().getTime() - now;
                long nd = 1000 * 24 * 60 * 60;
                long editDifference = diff / nd;
                if (editDifference < 30) {
                    healthWarnNum ++;
                    continue;
                }
            }
        }
        result.put("healthNum", healthList.size());
        result.put("healthWarnNum", healthWarnNum);
        return AjaxResult.success(result);
    }

    @GetMapping("/procedureCount")
    public AjaxResult procedureCount() {
        TPlantproglist tPlantproglist = new TPlantproglist();
        HashMap<String, Object> result = new HashMap<>();
        tPlantproglist.setItem(2L);
        List<TPlantproglist> list = tPlantproglistService.selectTPlantproglistList(tPlantproglist);
        result.put("appNum", list.size());
        result.put("app", "程序总汇");

        tPlantproglist.setItem(3L);
        list = tPlantproglistService.selectTPlantproglistList(tPlantproglist);
        result.put("fingerpostNum", list.size());
        result.put("fingerpost", "指南总汇");

        tPlantproglist.setItem(4L);
        list = tPlantproglistService.selectTPlantproglistList(tPlantproglist);
        result.put("guideNum", list.size());
        result.put("guide", "指导书总汇");

        tPlantproglist.setItem(5L);
        list = tPlantproglistService.selectTPlantproglistList(tPlantproglist);
        result.put("fileNum", list.size());
        result.put("file", "附件总汇");

        tPlantproglist.setItem(1L);
        list = tPlantproglistService.selectTPlantproglistList(tPlantproglist);
        result.put("excelNum", list.size());
        result.put("excel", "表格总汇");

        tPlantproglist.setItem(6L);
        list = tPlantproglistService.selectTPlantproglistList(tPlantproglist);
        result.put("trainNum", list.size());
        result.put("train", "培训材料");
        return AjaxResult.success(result);
    }

//    @GetMapping("/trainCount")
//    public AjaxResult getTrainCount() {
//        TTraining tTraining = new TTraining();
//        tTraining.setDeptId(10012l);
//        Calendar cal = Calendar.getInstance();
//        tTraining.setYear(String.valueOf(cal.get(Calendar.YEAR)));
//        return AjaxResult.success(tTrainingService.countTTraining(tTraining));
//    }

    @GetMapping("/trainCount")
    public AjaxResult getTrainCount(TTraining tTraining) {
        tTraining.setDeptId(10012l);
        tTraining.setIsfinish(1l);
        List<TTraining> list1 = tTrainingService.selectTTrainingList(tTraining);
        tTraining.setIsfinish(0l);
        List<TTraining> list0 = tTrainingService.selectTTrainingList(tTraining);
        tTraining.setNonParticipants("未参培");
        List<TTraining> list2 = tTrainingService.selectTTrainingList(tTraining);
        HashMap<String, Object> result = new HashMap<>();
        result.put("count1", list1.size());
        result.put("count1Name", "已完成");
        result.put("count0", list0.size() - list2.size());
        result.put("count0Name", "未完成");
        result.put("count2", list2.size());
        result.put("count2Name", "未参培");
        return AjaxResult.success(result);
    }

    @GetMapping("/getLine")
    public AjaxResult getLine() {

        TPlantproglist tPlantproglist = new TPlantproglist();
        tPlantproglist.setDeptId(10012l);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM");
        Calendar cal = Calendar.getInstance();
        Map<String, List<TPlantproglist>> result = new HashMap<>();
        List<TPlantproglist> nextEdits = new ArrayList<>();
        List<TPlantproglist> nextReviews = new ArrayList<>();
        for (int i = 1; i < 8; i++) {
            tPlantproglist.setStartDate(sdf.format(cal.getTime()));
            cal.add(Calendar.MONTH, 1);
            nextEdits.addAll(tPlantproglistService.countNextEditDate(tPlantproglist));
            nextReviews.addAll(tPlantproglistService.countNextReviewDate(tPlantproglist));
        }
        result.put("nextEdit", nextEdits);
        result.put("nextReview",nextReviews);
        return AjaxResult.success(result);
    }

    @GetMapping("/getWorkcetificate")
    public AjaxResult getWorkcetificate() {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> map = tWorkcertificateCbpsMapper.selectDateNotNull();
        Map<String, Object> map1 = tWorkcertificateCbpsMapper.selectDateWarn();
        result.put("cerAll" , map);
        result.put("cerWarn" , map1);

        return AjaxResult.success(result);


    };


}
