package com.testor.module.hazard.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.testor.biz.sys.org.model.domain.SysOrg;
import com.testor.biz.sys.org.service.SysOrgService;
import com.testor.module.emergency.model.vo.TContingencyPlanVO;
import com.testor.module.hazard.dao.THazardWorkPlanCheckDao;
import com.testor.module.hazard.model.domain.THazardWorkPlan;
import com.testor.module.hazard.model.domain.THazardWorkPlanCheck;
import com.testor.module.hazard.model.dto.THazardWorkPlanCheckParam;
import com.testor.module.hazard.model.vo.HazardWorkPlanCheckComplianceStatisticsVO;
import com.testor.module.hazard.model.vo.HazardWorkPlanCheckProblemCountStatisticsVO;
import com.testor.module.hazard.model.vo.HazardWorkPlanCheckProblemTypeStatisticsVO;
import com.testor.module.hazard.model.vo.HazardWorkPlanCheckVO;
import com.testor.module.hazard.service.THazardWorkPlanCheckService;
import com.testor.module.notice.dao.TSysOrgDao;
import com.testor.module.safe.model.domain.TSafeHazardOrg;
import com.testor.module.safe.model.vo.TSafeHazardExportVO;
import com.testor.module.safe.model.vo.TSafeHazardOrgStatisticsVO;
import com.testor.module.safe.service.impl.TSafeHazardOrgServiceImpl;
import com.tongtech.tfw.backend.common.biz.constants.BizConstants;
import com.tongtech.tfw.backend.common.context.ContextUtils;
import com.tongtech.tfw.backend.core.helper.IdHelper;
import com.tongtech.tfw.backend.core.helper.ObjectHelper;
import com.tongtech.tfw.backend.core.helper.StringHelper;
import com.tongtech.tfw.backend.core.helper.bean.BeanHelper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.tongtech.tfw.backend.common.models.supers.SuperServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 危险作业计划监督检查Service业务层处理
 * 
 * @author testor-framework
 * @date 2025-04-16 10:09:22
 */
@Service
public class THazardWorkPlanCheckServiceImpl extends SuperServiceImpl<THazardWorkPlanCheckDao, THazardWorkPlanCheck> implements THazardWorkPlanCheckService
{

    private static final Logger logger = LoggerFactory.getLogger(TSafeHazardOrgServiceImpl.class);

    @Autowired(required = false)
    private THazardWorkPlanCheckDao tHazardWorkPlanCheckDao;

    @Autowired
    private TSysOrgDao orgDao;

    @Autowired
    private SysOrgService sysOrgService;

    @Override
    public Page<THazardWorkPlanCheck> selectTHazardWorkPlanCheckList(THazardWorkPlanCheckParam tHazardWorkPlanCheckParam) {
        IPage<THazardWorkPlanCheck> page = new Page<>(Long.valueOf(tHazardWorkPlanCheckParam.getPage()), Long.valueOf(tHazardWorkPlanCheckParam.getLimit()));
        return this.baseMapper.selectTHazardWorkPlanCheckList(page,tHazardWorkPlanCheckParam);
    }

    @Override
    public Page<THazardWorkPlanCheck> selectAppTHazardWorkPlanCheckList(THazardWorkPlanCheckParam tHazardWorkPlanCheckParam) {
        IPage<THazardWorkPlanCheck> page = new Page<>(Long.valueOf(tHazardWorkPlanCheckParam.getPage()), Long.valueOf(tHazardWorkPlanCheckParam.getLimit()));
        return this.baseMapper.selectAppTHazardWorkPlanCheckList(page,tHazardWorkPlanCheckParam);
    }

    @Override
    public Page<THazardWorkPlanCheck> selectTHazardWorkPlanCheckManageList(THazardWorkPlanCheckParam tHazardWorkPlanCheckParam) {
        /*tHazardWorkPlanCheckParam.setStartTime(montageStartTime(tHazardWorkPlanCheckParam.getStartTime()));
        tHazardWorkPlanCheckParam.setEndTime(montageEndTime(tHazardWorkPlanCheckParam.getEndTime()));*/
        IPage<THazardWorkPlanCheck> page = new Page<>(Long.valueOf(tHazardWorkPlanCheckParam.getPage()), Long.valueOf(tHazardWorkPlanCheckParam.getLimit()));
        List<String> orgIdList = new ArrayList<>();
        if(null != tHazardWorkPlanCheckParam.getOrgIdList() && tHazardWorkPlanCheckParam.getOrgIdList().size()>0){
            tHazardWorkPlanCheckParam.setOrgIdList(orgDao.selectAllSubId(tHazardWorkPlanCheckParam.getOrgIdList()));
            orgIdList.addAll(tHazardWorkPlanCheckParam.getOrgIdList());
        }else{
            orgIdList.addAll(orgDao.selectOrgIdAllSubId(ContextUtils.getLoginUser().getOrgId()));
        }
        tHazardWorkPlanCheckParam.setOrgIdList(orgIdList);
        return this.baseMapper.selectTHazardWorkPlanCheckManageList(page,tHazardWorkPlanCheckParam);
    }

    @Override
    public THazardWorkPlanCheck addEntity(THazardWorkPlanCheck addRequest) {
        if(ObjectHelper.isNotEmpty(addRequest)){
            // TODO 按需求添加数据去重，特殊值设定，业务异常
            String id = IdHelper.getId32bit();
            THazardWorkPlanCheck data= BeanHelper.beanToBean(addRequest,  THazardWorkPlanCheck.class);
            data.setId(id);
            boolean result= this.save(data);
            if(result){
                THazardWorkPlanCheck newEntity = new THazardWorkPlanCheck();
                newEntity.setId(id);
                return newEntity;
            }
		}
        return null;
    }

    @Override
    public THazardWorkPlanCheck detail(String id) {
        return this.baseMapper.detail(id);
    }

    @Override
    public void exportEntity(THazardWorkPlanCheckParam param, HttpServletResponse response) {
        try{
            List<HazardWorkPlanCheckVO> hazardWorkPlanCheckVOList = new ArrayList<>();

            param.setStartTime(montageStartTime(param.getStartTime()));
            param.setEndTime(montageEndTime(param.getEndTime()));
            List<THazardWorkPlanCheck> tHazardWorkPlanCheckPage = this.baseMapper.selectTHazardWorkPlanCheckManageExportList(param);

            int i = 1;
            for (THazardWorkPlanCheck entity : tHazardWorkPlanCheckPage) {
                HazardWorkPlanCheckVO vo = new HazardWorkPlanCheckVO();
                BeanUtils.copyProperties(entity, vo);  // 复制属性
                vo.setNumber(i);
                vo.setIsCompliant(vo.getIsCompliant().equals("1")?"是":"否");
                i =i+1;
                hazardWorkPlanCheckVOList.add(vo);
            }

            logger.info("**************************************写入模板*****************************");

            String fileName = "监督检查导出明细";
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");


            WriteSheet dealerSheet = EasyExcel.writerSheet(0, "监督检查导出明细")
                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).head(HazardWorkPlanCheckVO.class).build();
            ExcelWriter build = EasyExcelFactory.write(response.getOutputStream()).build();
            build.write(hazardWorkPlanCheckVOList,dealerSheet);
            build.finish();
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


    @Override
    public List<HazardWorkPlanCheckComplianceStatisticsVO> selectCheckComplianceStatistics(THazardWorkPlanCheckParam tHazardWorkPlanCheckParam) {

        tHazardWorkPlanCheckParam.setStartTime(montageStartTime(tHazardWorkPlanCheckParam.getStartTime()));
        tHazardWorkPlanCheckParam.setEndTime(montageEndTime(tHazardWorkPlanCheckParam.getEndTime()));

        String orgId = tHazardWorkPlanCheckParam.getOrgId();
        if (StringHelper.isEmpty(orgId)) {
            // 获取登录企业
            orgId = ContextUtils.getLoginUser().getOrgId();
        }
        String finalOrgId = orgId;
        List<String> orgIds = new ArrayList<>();
        if (tHazardWorkPlanCheckParam.getOrgIdList() != null && tHazardWorkPlanCheckParam.getOrgIdList().size() > 0) {
            //orgIds = tHazardWorkPlanCheckParam.getOrgIdList();
            //如果只查当前机构数据
            if (tHazardWorkPlanCheckParam.isThisOrg()) {
                orgIds = tHazardWorkPlanCheckParam.getOrgIdList();
            } else {
                List<String> orgIdList = tHazardWorkPlanCheckParam.getOrgIdList();
                for(String id : orgIdList){
                    List<String> ids = sysOrgService.list(new QueryWrapper<SysOrg>().eq(SysOrg.PARENT_ID, id).ne(SysOrg.STATUS, BizConstants.STATUS_DELETE).eq(SysOrg.IS_DEPT, 0).orderByAsc("tree_sort")).stream().map(SysOrg::getOrgId).collect(Collectors.toList());
                    //如果下级机构为空
                    if (ids == null || ids.size() == 0) {
                        //存入当前机构
                        ids.add(finalOrgId);
                    }
                    orgIds.addAll(ids);
                }
            }
        }else {
            //如果只查当前机构数据
            if (tHazardWorkPlanCheckParam.isThisOrg()) {
                orgIds.add(orgId);
            } else {
                List<String> ids = sysOrgService.list(new QueryWrapper<SysOrg>().eq(SysOrg.PARENT_ID, finalOrgId).ne(SysOrg.STATUS, BizConstants.STATUS_DELETE).eq(SysOrg.IS_DEPT, 0).orderByAsc("tree_sort")).stream().map(SysOrg::getOrgId).collect(Collectors.toList());
                //如果下级机构为空
                if (ids == null || ids.size() == 0) {
                    //存入当前机构
                    ids.add(finalOrgId);
                }
                orgIds.addAll(ids);
            }
        }

        List<HazardWorkPlanCheckComplianceStatisticsVO> hazardWorkPlanCheckComplianceStatisticsVOS = new ArrayList<>();
        orgIds.stream().forEach(item -> {
            HazardWorkPlanCheckComplianceStatisticsVO hazardWorkPlanCheckComplianceStatisticsVO = new HazardWorkPlanCheckComplianceStatisticsVO();
            List<String> ids = new ArrayList<>();
            if (tHazardWorkPlanCheckParam.isThisOrg()){
                ids.add(finalOrgId);
            }else {
                //如果下级机构为空
                ids = sysOrgService.list(new QueryWrapper<SysOrg>().and(sysOrgQueryWrapper -> sysOrgQueryWrapper.like(SysOrg.PARENT_IDS, item).or().eq(SysOrg.ORG_ID,item))
                        .ne(SysOrg.STATUS, BizConstants.STATUS_DELETE).eq(SysOrg.IS_DEPT, 0).orderByAsc("tree_sort")).stream().map(SysOrg::getOrgId).collect(Collectors.toList());
                if (ids == null || ids.size() == 0) {
                    //存入当前机构
                    ids.add(finalOrgId);

                }
            }
            //tHazardWorkPlanCheckParam.setOrgIdList(ids);
            tHazardWorkPlanCheckParam.setOrgIdList(ids);
            hazardWorkPlanCheckComplianceStatisticsVO = this.baseMapper.selectCheckComplianceStatistics(tHazardWorkPlanCheckParam).get(0);
            hazardWorkPlanCheckComplianceStatisticsVO.setOrgId(item);
            hazardWorkPlanCheckComplianceStatisticsVO.setOrgName(sysOrgService.getById(item).getOrgName());
            hazardWorkPlanCheckComplianceStatisticsVOS.add(hazardWorkPlanCheckComplianceStatisticsVO);
        });


        return hazardWorkPlanCheckComplianceStatisticsVOS;
    }

    @Override
    public List<HazardWorkPlanCheckProblemTypeStatisticsVO> selectCheckProblemTypeStatistics(THazardWorkPlanCheckParam tHazardWorkPlanCheckParam) {
        if (CollectionUtils.isEmpty(tHazardWorkPlanCheckParam.getOrgIdList())) {
            List<String> orgIdList = new ArrayList<>();
            orgIdList.add(ContextUtils.getLoginUser().getOrgId());
            tHazardWorkPlanCheckParam.setOrgIdList(orgIdList);
        }
        tHazardWorkPlanCheckParam.setStartTime(montageStartTime(tHazardWorkPlanCheckParam.getStartTime()));
        tHazardWorkPlanCheckParam.setEndTime(montageEndTime(tHazardWorkPlanCheckParam.getEndTime()));
        return this.baseMapper.selectCheckProblemTypeStatistics(tHazardWorkPlanCheckParam);
    }

    @Override
    public List<HazardWorkPlanCheckProblemCountStatisticsVO> selectCheckProblemCountStatistics(THazardWorkPlanCheckParam tHazardWorkPlanCheckParam) {
        if (CollectionUtils.isEmpty(tHazardWorkPlanCheckParam.getOrgIdList())) {
            List<String> orgIdList = new ArrayList<>();
            orgIdList.add(ContextUtils.getLoginUser().getOrgId());
            tHazardWorkPlanCheckParam.setOrgIdList(orgIdList);
        }
        tHazardWorkPlanCheckParam.setStartTime(montageStartTime(tHazardWorkPlanCheckParam.getStartTime()));
        tHazardWorkPlanCheckParam.setEndTime(montageEndTime(tHazardWorkPlanCheckParam.getEndTime()));
        return this.baseMapper.selectCheckProblemCountStatistics(tHazardWorkPlanCheckParam);
    }

    public String montageStartTime(String startTime){
        if(startTime !=null && !"".equals(startTime) ){
            startTime = startTime+" 00:00:00";
        }
        return startTime;
    }

    public String montageEndTime(String endTime){
        if(endTime !=null && !"".equals(endTime) ){
            endTime = endTime+" 23:59:59";
        }
        return endTime;
    }

}
