package com.rongwei.sfcommon.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rongwe.scentity.domian.*;
import com.rongwe.scentity.vo.AccidentTrackPlanVo;
import com.rongwe.scentity.vo.AccidentUserVo;
import com.rongwe.scentity.vo.AccidentVo;
import com.rongwe.scentity.vo.AspAccidentReportVo;
import com.rongwei.commonservice.serial.service.CommonDictService;
import com.rongwei.commonservice.serial.service.SysSerialNumberService;
import com.rongwei.rwcommon.base.R;
import com.rongwei.rwcommon.utils.SecurityUtil;
import com.rongwei.rwcommon.utils.StringUtils;
import com.rongwei.rwcommon.vo.generalsql.GeneralApiParameter;
import com.rongwei.rwcommonentity.commonservers.vo.SysSerialVo;
import com.rongwei.sfcommon.sys.dao.AccidentDao;
import com.rongwei.sfcommon.sys.service.*;
import com.rongwei.sfcommon.utils.MlConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

@Service("accidentService")
public class AccidentServiceImpl extends ServiceImpl<AccidentDao, AccidentDo> implements AccidentService {

    @Autowired
    private AccidentService accidentService;
    @Autowired
    private AccidentDao accidentDao;
    @Autowired
    private AccidentTrackPlanService accidentTrackPlanService;
    @Autowired
    private HiddenDangerTrackService hiddenDangerTrackService;
    @Autowired
    private SysSerialNumberService sysSerialNumberService;

    @Autowired
    private WordExportUtil wordExportUtil;

    @Autowired
    private CommonDictService dictService;

    @Autowired
    private AccidentUserService accidentUserService;

    @Autowired
    private AccidentObjectService accidentObjectService;
    @Autowired
    private AspAccidentReportService aspAccidentReportService;


    @Override
    public R getCode(Map<String, Object> map) {
        try {
            //数据结构适配
            GeneralApiParameter generalApiParameter = BeanUtil.mapToBeanIgnoreCase(map, GeneralApiParameter.class, true);
            AccidentDo accidentDo = BeanUtil.mapToBeanIgnoreCase(generalApiParameter.getMasterPar(), AccidentDo.class, true);
            //如果是新增，则在订单评价中间表中新增评价项
            if (accidentDo.getAccidentcode() == null) {
                //设置订单流水
                SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
                String date = formatter.format(new Date());
                String code = accidentService.getCodeMAx();
                AccidentDo accidentDo1 = new AccidentDo();
                accidentDo1.setId(accidentDo.getId());
                if (StringUtils.isBlank(code)) {
                    accidentDo1.setAccidentcode(date + "-0001");
                } else {
                    String[] split = code.split("-");
                    int i = Integer.parseInt(split[split.length - 1]) + 1;
                    accidentDo1.setAccidentcode(date + "-" + String.format("%04d", i));
                }
                accidentService.updateById(accidentDo1);
            }
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.saveError();
        }
    }

    @Override
    public String getCodeMAx() {
        return accidentDao.getCodeMAx();
    }

    /**
     * 事故保存后生成隐患跟踪任务
     *
     * @param map
     */
    public void createHiddenTask(Map<String, Object> map) {
        if (map.get("ID") != null && !"".equals(map.get("ID"))) {
            String accidenId = map.get("ID").toString();
            AccidentDo accidentDo = this.getById(accidenId);
            // 跟踪计划获取
            Map<String, Object> parMap = new HashMap<>();
            parMap.put("ACCIDENTID", accidenId);
            List<AccidentTrackPlanDo> accidentTrackPlans = (List<AccidentTrackPlanDo>) accidentTrackPlanService.listByMap(parMap);
            if (accidentTrackPlans != null && accidentTrackPlans.size() > 0) {
                List<HiddenDangerTrackDo> hiddenDangerTracks = new ArrayList<>();
                accidentTrackPlans.forEach(v -> {
                    HiddenDangerTrackDo hiddenDangerTrack = new HiddenDangerTrackDo();
                    hiddenDangerTrack.setId(SecurityUtil.getUUID());
                    hiddenDangerTrack.setFindtime(accidentDo.getHappendate());
                    hiddenDangerTrack.setTrackuserid(v.getChargeuserid());
                    hiddenDangerTrack.setTrackusername(v.getChargeusername());
                    // 设置最初指派跟踪人
                    hiddenDangerTrack.setFirsttrackuserid(v.getChargeuserid());
                    hiddenDangerTrack.setFirsttrackusername(v.getChargeusername());
                    hiddenDangerTrack.setDangerworkpark(accidentDo.getAccidentdept());
                    hiddenDangerTrack.setDangerworkparkname(accidentDo.getAccidentdeptname());
                    hiddenDangerTrack.setHiddendangercontent(v.getCorrectivemeasures());
                    hiddenDangerTrack.setRectificationdate(v.getExpectedfinishdate());
                    hiddenDangerTrack.setFinishdate(v.getActualfinishdate());
                    hiddenDangerTrack.setRelationid(v.getId());
                    hiddenDangerTrack.setStatus(MlConstants.HIDDEN_DANGER_STATUS_NOTCLOSE);
                    hiddenDangerTrack.setDangersource(MlConstants.DANGER_SOURCE_ACCIDENT);
                    hiddenDangerTrack.setFinduser(accidentDo.getCreateuserid());
                    hiddenDangerTrack.setFindusername(accidentDo.getCreateusername());
                    // 隐患类型默认为一般
                    hiddenDangerTrack.setHiddendangertype(MlConstants.HIDDEN_DANGER_TYPE_NORMAL);
                    // 总编号生成
                    SysSerialVo sysSerialVo = new SysSerialVo();
                    sysSerialVo.setRuleTemplateStr("@{date:yyyyMMdd}@{serialNumber:#000000}");
                    sysSerialVo.setModulecode("asp_hidden_danger_track_CODE");
                    hiddenDangerTrack.setCode(sysSerialNumberService.getCodeByTemplate(sysSerialVo));
                    // 图片
                    hiddenDangerTrack.setCheckphoto(accidentDo.getAccidentphoto());

                    hiddenDangerTracks.add(hiddenDangerTrack);
                });
                // 隐患跟踪任务批量保存
                hiddenDangerTrackService.saveBatch(hiddenDangerTracks);
            }
        }
    }

    @Override
    public R word(Map<String, Object> map) {
        String fileName = "";
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        String accidentname = (String) map.get("ACCIDENTNAME");
        //主键
        String id = (String) map.get("ID");

        //发生日期
        String happendate = (String) map.get("HAPPENDATE");
        if (StringUtils.isNotEmpty(happendate)) {
            map.put("HAPPENDATE", happendate);
            happendate = happendate.substring(0, 10);
        }

        fileName = accidentname + happendate;
        //报告日期
        String reportdate = (String) map.get("REPORTDATE");
        if (StringUtils.isNotEmpty(reportdate)) {
            String format = reportdate.substring(0, 10);
            map.put("REPORTDATE", format);
        }
        //是否受伤人员
        String haspeopleinjured = (String) map.get("HASPEOPLEINJURED");
        if (StringUtils.isNotBlank(haspeopleinjured)) {
            haspeopleinjured = dictService.getDictNameByValue("YN", haspeopleinjured);
            map.put("HASPEOPLEINJURED", haspeopleinjured);
        }
        //申报工伤
        String declarejobinjured = (String) map.get("DECLAREJOBINJURED");
        if (StringUtils.isNotBlank(declarejobinjured)) {
            declarejobinjured = dictService.getDictNameByValue("declaration-industrial-injury", declarejobinjured);
            map.put("DECLAREJOBINJURED", declarejobinjured);
        }
        //财务受损
        String hasfinanceloss = (String) map.get("HASFINANCELOSS");
        if (StringUtils.isNotBlank(hasfinanceloss)) {
            hasfinanceloss = dictService.getDictNameByValue("YN", hasfinanceloss);
            map.put("HASFINANCELOSS", hasfinanceloss);
        }
        //是否厂内
        String isinfactory = (String) map.get("ISINFACTORY");
        if (StringUtils.isNotBlank(isinfactory)) {
            isinfactory = dictService.getDictNameByValue("YN", isinfactory);
            map.put("ISINFACTORY", isinfactory);
        }
        //事故类型
        String accidenttype = (String) map.get("ACCIDENTTYPE");
        if (StringUtils.isNotBlank(accidenttype)) {
            accidenttype = dictService.getDictNameByValue("accident-type", accidenttype);
            map.put("ACCIDENTTYPE", accidenttype);
        }


        //事故受伤员工表
        List<AccidentUserVo> UserList = accidentUserService.getByAccidentId(id);
        if (UserList.size() > 0) {
            for (int i = 0; i < UserList.size(); i++) {
                //出生日期
                Date birthdate = UserList.get(i).getBirthdate();
                if (birthdate != null) {
                    String format = format1.format(birthdate);
                    UserList.get(i).setBirthdatestring(format);
                }
                //	入职日期
                Date entrydate = UserList.get(i).getEntrydate();
                if (entrydate != null) {
                    String format = format1.format(entrydate);
                    UserList.get(i).setEntrydatestring(format);
                }
                //是否加班
                String hasworkovertime = UserList.get(i).getHasworkovertime();
                if (StringUtils.isNotBlank(hasworkovertime)) {
                    hasworkovertime = dictService.getDictNameByValue("YN", hasworkovertime);
                    UserList.get(i).setHasworkovertime(hasworkovertime);
                }

                //受伤部位 injuredsite
                String injuredsite = UserList.get(i).getInjuredsite();
                if (StringUtils.isNotBlank(injuredsite)) {
                    injuredsite = dictService.getDictNameByValue("Injured-site", injuredsite);
                    UserList.get(i).setInjuredsite(injuredsite);
                }
                //受伤程度 DEGREEOFINJURY
                String degreeofinjury = UserList.get(i).getDegreeofinjury();
                if (StringUtils.isNotBlank(degreeofinjury)) {
                    degreeofinjury = dictService.getDictNameByValue("injury-degree", degreeofinjury);
                    UserList.get(i).setDegreeofinjury(degreeofinjury);
                }
            }
        }

        //事故受损物体表
        List<AccidentObjectDo> accidentObjectDoList = accidentObjectService.getByAccidentId(id);

        //事故跟踪计划表
        List<AccidentTrackPlanVo> accidentTrackPlanVoList = accidentTrackPlanService.getByAccidentId(id);
        if (accidentTrackPlanVoList.size() > 0) {
            for (int i = 0; i < accidentTrackPlanVoList.size(); i++) {
                //开始日期
                Date startdate = accidentTrackPlanVoList.get(i).getStartdate();
                if (startdate != null) {
                    String format = format1.format(startdate);
                    accidentTrackPlanVoList.get(i).setStartdatestring(format);
                }
                //预计完成日期 expectedfinishdate
                Date expectedfinishdate = accidentTrackPlanVoList.get(i).getExpectedfinishdate();
                if (expectedfinishdate != null) {
                    String format = format1.format(expectedfinishdate);
                    accidentTrackPlanVoList.get(i).setExpectedfinishdatestring(format);
                }
                //实际完成日期
                Date finishdate = accidentTrackPlanVoList.get(i).getFinishdate();
                if (finishdate != null) {
                    String format = format1.format(finishdate);
                    accidentTrackPlanVoList.get(i).setFinishdatestring(format);
                }

            }
        }
        map.put("UserList", UserList);
        map.put("accidentObjectDoList", accidentObjectDoList);
        map.put("accidentTrackPlanVoList", accidentTrackPlanVoList);

        //图片处理
        List<AccidentVo> imageList = new ArrayList();
        String accidentphoto = (String) map.get("ACCIDENTPHOTO");
        if (StringUtils.isNotEmpty(accidentphoto)) {
            String[] split = accidentphoto.split("\\^_\\^");  //多个图片切割
            for (int i = 0; i < split.length; i++) {
                String[] split1 = split[i].split("-;-");  //单图片切割
                String fileId = split1[1];
                AccidentVo Path = accidentService.getFilePathById(fileId);
                String data = Path.getFullpath();
                if (StringUtils.isNotEmpty(data)) {
                    try {
                        String imageBase = wordExportUtil.getImageBase(data);
                        AccidentVo accidentVo = new AccidentVo();
                        accidentVo.setReplace(data);
                        accidentVo.setImageBase(imageBase);
                        imageList.add(i, accidentVo);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        map.put("imageList", imageList);
        List<AccidentVo> fileList = accidentService.getFileList();
        if (fileList.size() > 0) {
            String fullpath = fileList.get(0).getFullpath();
            if (StringUtils.isNotEmpty(fullpath)) {
                wordExportUtil.createDoc(map, fullpath, fileName + ".doc");
            }
        }
        return R.ok(fileName);
    }

    @Override
    public AccidentVo getFilePathById(String id) {
        return accidentDao.getFilePathById(id);
    }

    @Override
    public List<AccidentVo> getFileList() {
        return accidentDao.getFileList();
    }

    @Override
    @Transactional
    public R report() {
        //============查询工厂，按照工厂进行分类查询=================
        //获取上一个月的年份和月份
        LocalDateTime lastMonth = DateUtil.toLocalDateTime(DateUtil.offsetMonth(DateUtil.date(), -1));
        int year = lastMonth.getYear();
        int month = lastMonth.getMonthValue();
        //防止重复执行,生成报表前，删除原数据
        aspAccidentReportService.remove(new QueryWrapper<AspAccidentReport>().lambda().eq(AspAccidentReport::getYear, year).eq(AspAccidentReport::getMonth, month));

        //从组织机构中获取最上级的工厂，即pid=-1的
        List<Map<String, String>> factoryList = this.baseMapper.selectFactory();
        factoryList.forEach(item -> {
            //============根据工厂查询前一个月的事故数据，按部门/车间统计===================
            //获取工厂的ID
            String tenantId = item.get("ID");
            List<AspAccidentReport> list = handleData(year, month, tenantId);

            //============数据保存==============
            aspAccidentReportService.saveBatch(list);
        });

        return R.ok();
    }

    private List<AspAccidentReport> handleData(int year, int month, String tenantId) {
        //统计数据
        List<AspAccidentReport> list = this.baseMapper.selectReport(year, month, tenantId);
        //计算月合计
        Integer valueTotal = 0;
        Integer peopleTotal = 0;
        for (AspAccidentReport aspAccidentReport : list) {
            //设值基础数据
            aspAccidentReport.setDeleted("0");
            aspAccidentReport.setCreatedate(new Date());
            aspAccidentReport.setModifydate(new Date());
            aspAccidentReport.setId(SecurityUtil.getUUID());

            //设值年
            aspAccidentReport.setYear(year);
            //设值月
            aspAccidentReport.setMonth(month);
            //设值工厂ID
            aspAccidentReport.setTenantid(tenantId);
            //设值加上百分号
            aspAccidentReport.setValue(aspAccidentReport.getValue() + "%");
            //计算总工伤数
            valueTotal += aspAccidentReport.getCount();
            //计算总人数
            peopleTotal += aspAccidentReport.getDepartpeople();
        }
        //计算月合计
        double v = 0.00;
        if(peopleTotal != 0){
            v = (valueTotal * 1.0000 / peopleTotal * 1.0000) * 100.0000;
        }
        String monthTotal;
        if(v == 0.00){
            monthTotal = "0.00%";
        }else {
            monthTotal = BigDecimal.valueOf(v).setScale(2, RoundingMode.HALF_UP).toString() + "%";
        }
        for (AspAccidentReport aspAccidentReport : list) {
            //设值月合计
            aspAccidentReport.setMonthtotal(monthTotal);
        }
        return list;
    }

    @Override
    public List<AspAccidentReportVo> selectReport(Integer year, String tenantId) {
        List<AspAccidentReportVo> res = new LinkedList<>();
        //=============查询已生成报表的部分=============
        List<AspAccidentReport> list = this.baseMapper.selectReportByYear(year, tenantId);

        //=============查询未生成报表的部分=============
        //获取当前的年月
        LocalDateTime lastMonth = DateUtil.toLocalDateTime(DateUtil.date());
        int nowYear = lastMonth.getYear();
        int nowMonth = lastMonth.getMonthValue();
        //判断查询的是否是当前年，如果不是当前年，跳过此处数据处理
        if(year.equals(nowYear)){
            //===========判断当前月份，需要重新处理当前月份的统计数据
            //查询当前月份的报表数据（实时）
            //统计数据
            List<AspAccidentReport> aspAccidentReportList = handleData(nowYear, nowMonth, tenantId);
            //=================合并报表===========
            list.addAll(aspAccidentReportList);
        }

        //先初始化map
        //查询所有车间和车间的员工人数
        List<Map<String, Object>> orgMap = this.baseMapper.getOrgMap(tenantId);
        //组装map《部门ID，报表数据》
        Map<String, AspAccidentReportVo> map = new HashMap<>();
        for (Map<String, Object> stringStringMap : orgMap) {
            AspAccidentReportVo aspAccidentReportVo = new AspAccidentReportVo();
            aspAccidentReportVo.setDepartId(stringStringMap.get("departId").toString());
            aspAccidentReportVo.setDepartName(stringStringMap.get("departName").toString());
            aspAccidentReportVo.setDepartPeople(Integer.parseInt(stringStringMap.get("totalPeople").toString()));
            map.put(stringStringMap.get("departId").toString(), aspAccidentReportVo);
        }

        for (AspAccidentReport item : list) {
            Integer month = item.getMonth();
            if(month == 1){
                if(!map.containsKey(item.getDepartid())){
                    AspAccidentReportVo aspAccidentReportVo = new AspAccidentReportVo();
                    aspAccidentReportVo.setDepartId(item.getDepartid());
                    aspAccidentReportVo.setDepartName(item.getDepartname());
                    aspAccidentReportVo.setCount1(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }else {
                    AspAccidentReportVo aspAccidentReportVo = map.get(item.getDepartid());
                    aspAccidentReportVo.setCount1(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }
            }
            if(month == 2){
                if(!map.containsKey(item.getDepartid())){
                    AspAccidentReportVo aspAccidentReportVo = new AspAccidentReportVo();
                    aspAccidentReportVo.setDepartId(item.getDepartid());
                    aspAccidentReportVo.setDepartName(item.getDepartname());
                    aspAccidentReportVo.setCount2(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }else {
                    AspAccidentReportVo aspAccidentReportVo = map.get(item.getDepartid());
                    aspAccidentReportVo.setCount2(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }
            }
            if(month == 3){
                if(!map.containsKey(item.getDepartid())){
                    AspAccidentReportVo aspAccidentReportVo = new AspAccidentReportVo();
                    aspAccidentReportVo.setDepartId(item.getDepartid());
                    aspAccidentReportVo.setDepartName(item.getDepartname());
                    aspAccidentReportVo.setCount3(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }else {
                    AspAccidentReportVo aspAccidentReportVo = map.get(item.getDepartid());
                    aspAccidentReportVo.setCount3(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }
            }
            if(month == 4){
                if(!map.containsKey(item.getDepartid())){
                    AspAccidentReportVo aspAccidentReportVo = new AspAccidentReportVo();
                    aspAccidentReportVo.setDepartId(item.getDepartid());
                    aspAccidentReportVo.setDepartName(item.getDepartname());
                    aspAccidentReportVo.setCount4(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }else {
                    AspAccidentReportVo aspAccidentReportVo = map.get(item.getDepartid());
                    aspAccidentReportVo.setCount4(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }
            }
            if(month == 5){
                if(!map.containsKey(item.getDepartid())){
                    AspAccidentReportVo aspAccidentReportVo = new AspAccidentReportVo();
                    aspAccidentReportVo.setDepartId(item.getDepartid());
                    aspAccidentReportVo.setDepartName(item.getDepartname());
                    aspAccidentReportVo.setCount5(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }else {
                    AspAccidentReportVo aspAccidentReportVo = map.get(item.getDepartid());
                    aspAccidentReportVo.setCount5(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }
            }
            if(month == 6){
                if(!map.containsKey(item.getDepartid())){
                    AspAccidentReportVo aspAccidentReportVo = new AspAccidentReportVo();
                    aspAccidentReportVo.setDepartId(item.getDepartid());
                    aspAccidentReportVo.setDepartName(item.getDepartname());
                    aspAccidentReportVo.setCount6(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }else {
                    AspAccidentReportVo aspAccidentReportVo = map.get(item.getDepartid());
                    aspAccidentReportVo.setCount6(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }
            }
            if(month == 7){
                if(!map.containsKey(item.getDepartid())){
                    AspAccidentReportVo aspAccidentReportVo = new AspAccidentReportVo();
                    aspAccidentReportVo.setDepartId(item.getDepartid());
                    aspAccidentReportVo.setDepartName(item.getDepartname());
                    aspAccidentReportVo.setCount7(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }else {
                    AspAccidentReportVo aspAccidentReportVo = map.get(item.getDepartid());
                    aspAccidentReportVo.setCount7(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }
            }
            if(month == 8){
                if(!map.containsKey(item.getDepartid())){
                    AspAccidentReportVo aspAccidentReportVo = new AspAccidentReportVo();
                    aspAccidentReportVo.setDepartId(item.getDepartid());
                    aspAccidentReportVo.setDepartName(item.getDepartname());
                    aspAccidentReportVo.setCount8(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }else {
                    AspAccidentReportVo aspAccidentReportVo = map.get(item.getDepartid());
                    aspAccidentReportVo.setCount8(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }
            }
            if(month == 9){
                if(!map.containsKey(item.getDepartid())){
                    AspAccidentReportVo aspAccidentReportVo = new AspAccidentReportVo();
                    aspAccidentReportVo.setDepartId(item.getDepartid());
                    aspAccidentReportVo.setDepartName(item.getDepartname());
                    aspAccidentReportVo.setCount9(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }else {
                    AspAccidentReportVo aspAccidentReportVo = map.get(item.getDepartid());
                    aspAccidentReportVo.setCount9(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }
            }
            if(month == 10){
                if(!map.containsKey(item.getDepartid())){
                    AspAccidentReportVo aspAccidentReportVo = new AspAccidentReportVo();
                    aspAccidentReportVo.setDepartId(item.getDepartid());
                    aspAccidentReportVo.setDepartName(item.getDepartname());
                    aspAccidentReportVo.setCount10(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }else {
                    AspAccidentReportVo aspAccidentReportVo = map.get(item.getDepartid());
                    aspAccidentReportVo.setCount10(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }
            }
            if(month == 11){
                if(!map.containsKey(item.getDepartid())){
                    AspAccidentReportVo aspAccidentReportVo = new AspAccidentReportVo();
                    aspAccidentReportVo.setDepartId(item.getDepartid());
                    aspAccidentReportVo.setDepartName(item.getDepartname());
                    aspAccidentReportVo.setCount11(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }else {
                    AspAccidentReportVo aspAccidentReportVo = map.get(item.getDepartid());
                    aspAccidentReportVo.setCount11(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }
            }
            if(month == 12){
                if(!map.containsKey(item.getDepartid())){
                    AspAccidentReportVo aspAccidentReportVo = new AspAccidentReportVo();
                    aspAccidentReportVo.setDepartId(item.getDepartid());
                    aspAccidentReportVo.setDepartName(item.getDepartname());
                    aspAccidentReportVo.setCount12(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }else {
                    AspAccidentReportVo aspAccidentReportVo = map.get(item.getDepartid());
                    aspAccidentReportVo.setCount12(item.getValue());
                    map.put(item.getDepartid(), aspAccidentReportVo);
                }
            }
        }

        //=============根据最终结果的部门和计算合计====================
        //合计的合计
        String totalOfTotal = "";

        //传入月为-1表示查询整年的数据
        List<AspAccidentReport> aspAccidentReportAll = handleData(year, -1, tenantId);
        for (AspAccidentReport item : aspAccidentReportAll) {
            String departId = item.getDepartid();
            AspAccidentReportVo aspAccidentReportVo = map.get(departId);
            aspAccidentReportVo.setCountAll(item.getValue());
            totalOfTotal = item.getMonthtotal();
        }

        //=============结果赋值===============
        List<AspAccidentReportVo> resList = new LinkedList<>();
        for (Map.Entry<String, AspAccidentReportVo> entry : map.entrySet()) {
            resList.add(entry.getValue());
        }
        //下方的合计
        AspAccidentReportVo downTotal = new AspAccidentReportVo();
        downTotal.setDepartName("合计");
        downTotal.setCountAll(totalOfTotal);
        for (AspAccidentReport item : list) {
            Integer month = item.getMonth();
            String monthTotal = item.getMonthtotal();
            if(month == 1){
                downTotal.setCount1(monthTotal);
            }
            if(month == 2){
                downTotal.setCount2(monthTotal);
            }
            if(month ==3){
                downTotal.setCount3(monthTotal);
            }
            if(month == 4){
                downTotal.setCount4(monthTotal);
            }
            if(month == 5){
                downTotal.setCount5(monthTotal);
            }
            if(month == 6){
                downTotal.setCount6(monthTotal);
            }
            if(month == 7){
                downTotal.setCount7(monthTotal);
            }
            if(month == 8){
                downTotal.setCount8(monthTotal);
            }
            if(month == 9){
                downTotal.setCount9(monthTotal);
            }
            if(month == 10){
                downTotal.setCount10(monthTotal);
            }
            if(month == 11){
                downTotal.setCount11(monthTotal);
            }
            if(month == 12){
                downTotal.setCount12(monthTotal);
            }
        }
        resList.add(downTotal);
        return resList;
    }

}
