package com.foreverwin.mesnac.anomaly.service.impl;

import com.foreverwin.mesnac.anomaly.dto.IntegrationLogDto;
import com.foreverwin.mesnac.anomaly.mapper.ReportMapper;
import com.foreverwin.mesnac.anomaly.service.ReportService;
import com.foreverwin.mesnac.meapi.mapper.DataFieldListMapper;
import com.foreverwin.mesnac.meapi.mapper.ItemMapper;
import com.foreverwin.mesnac.meapi.mapper.ShopOrderMapper;
import com.foreverwin.mesnac.meapi.model.*;
import com.foreverwin.mesnac.meapi.service.NcCodeService;
import com.foreverwin.mesnac.meapi.service.NwaUserService;
import com.foreverwin.mesnac.meapi.util.StringUtils;
import com.foreverwin.modular.core.util.CommonMethods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Author zhaojiawei
 * @Since 2021-08-23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ReportServiceImpl implements ReportService {

    @Autowired
    private NcCodeService ncCodeService;

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private NwaUserService nwaUserService;

    @Autowired
    private DataFieldListMapper dataFieldListMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private ShopOrderMapper shopOrderMapper;

    /**
     * 接口日志表
     * @return
     */
    @Override
    public List<IntegrationLogDto> findIntegrationLog(Map<String, Object> map) {
        String site = CommonMethods.getSite();
        return reportMapper.findIntegrationLog(site,map);
    }

    /**
     * 零件加工报表
     * @return
     */
    @Override
    public  List<Map<String, Object>> partsManufacturing(Map<String,Object> map) {
        String site = CommonMethods.getSite();
        String locale = LocaleContextHolder.getLocale().getLanguage();
        map.put("site",site);
        map.put("locale",locale);
        List<Map<String, Object>> maps = reportMapper.partsManufacturing(map);
//        for(Map<String,Object> m: maps){
//            //判断子件是否全部已经完工，如果有一个没有完工，则全部都没有完工，设置为否
//            String isComplete = (String)m.get("ZI_JIAN");
//            if("否".equals(isComplete)){
//                for(Map<String,Object> m2: maps){
//                    m2.put("ZI_JIAN","否");
//                }
//                break;
//            }
//        }
        return maps;
    }

    /**
     * 余料退回明细表
     * @param item
     * @param itemDescription
     * @return
     */
    @Override
    public List<Map<String, Object>> surplusReturnTable(String item, String itemDescription) {
        String locale = LocaleContextHolder.getLocale().getLanguage();
        String site = CommonMethods.getSite();
        return reportMapper.surplusReturnTable(site,locale,item,itemDescription);
    }

    /**
     * 资源维修报表
     * @param map
     * @return
     */
    @Override
    public List<Map<String, Object>> resourceRepair(Map<String, Object> map) {
        String locale = LocaleContextHolder.getLocale().getLanguage();
        String site = CommonMethods.getSite();

        Map<String, List<NcCode>> ncCodeMap = new HashMap<>();
        NcCode ncCode = new NcCode();
        ncCode.setSite(site);
        List<NcCode> ncCodeList = ncCodeService.selectList(ncCode);
        if (ncCodeList != null && ncCodeList.size() > 0) {
            ncCodeMap = ncCodeList.stream().collect(Collectors.groupingBy(NcCode::getNcCode));
        }

        Map<String, List<NwaUser>> userMap = new HashMap<>();
        NwaUser nwaUser = new NwaUser();
        nwaUser.setSite(site);
        List<NwaUser> nwaUserList = nwaUserService.selectList(nwaUser);
        if(nwaUserList != null && nwaUserList.size() > 0) {
            userMap  = nwaUserList.stream().collect(Collectors.groupingBy(NwaUser::getUserName));
        }

        List<Map<String, Object>> maps = reportMapper.resourceRepair(site, locale, map);


        for (int i = 0; i < maps.size(); i++) {
            Map<String, Object> temporaryMap = maps.get(i);
            String ncCodes = (String)temporaryMap.get("NC_CODE_NO");
            if(StringUtils.isBlank(ncCodes)){
                maps.get(i).put("NC_CODE_DESCRIPTION", "");
                continue;
            }
            String []ncCodeArray = ncCodes.split(",");
            String ncCodeDesc = "";
            for (int j = 0; j < ncCodeArray.length; j++) {
                if(ncCodeMap.get(ncCodeArray[j]) != null){
                    ncCodeDesc += ncCodeMap.get(ncCodeArray[j]).get(0).getDescription()+";";
                }
            }
            maps.get(i).put("NC_CODE_DESCRIPTION", ncCodeDesc);
        }


        for (int i = 0; i < maps.size(); i++) {
            Map<String, Object> temporaryMap = maps.get(i);
            String repairUser = (String)temporaryMap.get("REPAIR_USERS");
            if(StringUtils.isBlank(repairUser)){
                maps.get(i).put("REPAIR_USERS_DESCRIPTION", "");
                continue;
            }
            String []repairUserArray = repairUser.split(",");
            String repairUserDescription = "";
            for (int j = 0; j < repairUserArray.length; j++) {
                if(userMap.get(repairUserArray[j]) != null){
                    repairUserDescription += userMap.get(repairUserArray[j]).get(0).getFullName()+";";
                }
            }
            maps.get(i).put("REPAIR_USERS_DESCRIPTION", repairUserDescription);
        }
        return maps;
    }

    /**
     * 检验记录报表
     * @param paramMap
     * @return
     */
    @Override
    public List<Map<String, Object>> findInspectionTask(Map paramMap) {
        return reportMapper.findInspectionTask(paramMap);
    }

    /**
     * 查询质检问题统计表
     * @param paramMap
     * @return
     */
    @Override
    public List<Map<String, Object>> findQualityInspectionReport(Map paramMap) {
        String site = CommonMethods.getSite();
        paramMap.put("site",site);
        paramMap.put("locale",LocaleContextHolder.getLocale().getLanguage());
        List<Map<String, Object>> qualityInspectionReport = reportMapper.findQualityInspectionReport(paramMap);


        //原因分类
        Map<String, List<DataFieldList>> dataFieldListMap = new HashMap<>();
        DataField dataField = new DataField();
        dataField.setSite(site);
        dataField.setDataField("REASON_CATEGORY");
        List<DataFieldList> dataFieldList = dataFieldListMapper.findDataFieldList(dataField);
        if (dataFieldList != null && dataFieldList.size() > 0) {
            dataFieldListMap = dataFieldList.stream().collect(Collectors.groupingBy(DataFieldList::getDataValue));
        }
        for (int i = 0; i < qualityInspectionReport.size(); i++) {
            Map<String, Object> temporaryMap = qualityInspectionReport.get(i);
            String dutyCauseType = (String) temporaryMap.get("DUTY_CAUSE_TYPE");
            if (StringUtils.isBlank(dutyCauseType)) {
                qualityInspectionReport.get(i).put("DUTY_CAUSE_TYPE_DESCRIPTION", "");
                continue;
            }
            String[] dutyCauseTypeSplit = dutyCauseType.split(",");
            StringBuilder dutyCauseTypeDescription = new StringBuilder();
            for (int j = 0; j < dutyCauseTypeSplit.length; j++) {
                if(dataFieldListMap.get(dutyCauseTypeSplit[j]) != null){
                    dutyCauseTypeDescription.append(dataFieldListMap.get(dutyCauseTypeSplit[j]).get(0).getDescription()).append(";");
                }
            }
            qualityInspectionReport.get(i).put("DUTY_CAUSE_TYPE_DESCRIPTION", dutyCauseTypeDescription);
        }


        //责任分类
        dataField.setDataField("RESPONSIBILITY_TYPE");
        dataFieldList = dataFieldListMapper.findDataFieldList(dataField);
        if (dataFieldList != null && dataFieldList.size() > 0) {
            dataFieldListMap = dataFieldList.stream().collect(Collectors.groupingBy(DataFieldList::getDataValue));
        }
        for (int i = 0; i < qualityInspectionReport.size(); i++) {
            Map<String, Object> temporaryMap = qualityInspectionReport.get(i);
            String dutyType = (String) temporaryMap.get("DUTY_TYPE");
            if (StringUtils.isBlank(dutyType)) {
                qualityInspectionReport.get(i).put("DUTY_TYPE_DESCRIPTION", "");
                continue;
            }
            String[] dutyTypeSplit = dutyType.split(",");
            StringBuilder dutyTypeDescription = new StringBuilder();
            for (int j = 0; j < dutyTypeSplit.length; j++) {
                if(dataFieldListMap.get(dutyTypeSplit[j]) != null){
                    dutyTypeDescription.append(dataFieldListMap.get(dutyTypeSplit[j]).get(0).getDescription()).append(";");
                }
            }
            qualityInspectionReport.get(i).put("DUTY_TYPE_DESCRIPTION", dutyTypeDescription);
        }


        //责任部门
        dataField.setDataField("RESPONSIBILITY_DEPARTMENT");
        dataFieldList = dataFieldListMapper.findDataFieldList(dataField);
        if (dataFieldList != null && dataFieldList.size() > 0) {
            dataFieldListMap = dataFieldList.stream().collect(Collectors.groupingBy(DataFieldList::getDataValue));
        }
        for (int i = 0; i < qualityInspectionReport.size(); i++) {
            Map<String, Object> temporaryMap = qualityInspectionReport.get(i);
            String dutyDepart = (String) temporaryMap.get("DUTY_DEPART");
            if (StringUtils.isBlank(dutyDepart)) {
                qualityInspectionReport.get(i).put("DUTY_DEPART_DESCRIPTION", "");
                continue;
            }
            String[] dutyDepartSplit = dutyDepart.split(",");
            StringBuilder dutyDepartDescription = new StringBuilder();
            for (int j = 0; j < dutyDepartSplit.length; j++) {
                if(dataFieldListMap.get(dutyDepartSplit[j]) != null){
                    dutyDepartDescription.append(dataFieldListMap.get(dutyDepartSplit[j]).get(0).getDescription()).append(";");
                }
            }
            qualityInspectionReport.get(i).put("DUTY_DEPART_DESCRIPTION", dutyDepartDescription);
        }

        return qualityInspectionReport;
    }

    @Override
    public List<Map<String, Object>> findAbnormalRecord(Map paramMap) {
        String site = CommonMethods.getSite();
        paramMap.put("site",site);
        paramMap.put("locale",LocaleContextHolder.getLocale().getLanguage());
        List<Map<String, Object>> findAbnormalRecord = reportMapper.findAbnormalRecord(paramMap);



        Map<String, List<DataFieldList>> dataFieldListMap = new HashMap<>();
        DataField dataField = new DataField();
        dataField.setSite(site);
        //责任部门
        dataField.setDataField("RESPONSIBILITY_DEPARTMENT");
        List<DataFieldList> dataFieldList = dataFieldListMapper.findDataFieldList(dataField);
        if (dataFieldList != null && dataFieldList.size() > 0) {
            dataFieldListMap = dataFieldList.stream().collect(Collectors.groupingBy(DataFieldList::getDataValue));
        }
        for (int i = 0; i < findAbnormalRecord.size(); i++) {
            Map<String, Object> temporaryMap = findAbnormalRecord.get(i);
            String dutyDepart = (String) temporaryMap.get("DUTY_DEPART");
            if (StringUtils.isBlank(dutyDepart)) {
                findAbnormalRecord.get(i).put("DUTY_DEPART_DESCRIPTION", "");
                continue;
            }
            String[] dutyDepartSplit = dutyDepart.split(",");
            StringBuilder dutyDepartDescription = new StringBuilder();
            for (int j = 0; j < dutyDepartSplit.length; j++) {
                if(dataFieldListMap.get(dutyDepartSplit[j]) != null){
                    dutyDepartDescription.append(dataFieldListMap.get(dutyDepartSplit[j]).get(0).getDescription()).append(";");
                }
            }
            findAbnormalRecord.get(i).put("DUTY_DEPART_DESCRIPTION", dutyDepartDescription);
        }
        return findAbnormalRecord;
    }

    @Override
    public List<Map<String, Object>> shopOrderSchedule(Map paramMap) {
        String site = CommonMethods.getSite();
        paramMap.put("site",site);
        paramMap.put("locale",LocaleContextHolder.getLocale().getLanguage());
        return reportMapper.shopOrderSchedule(paramMap);
    }

    @Override
    public List<Map<String, Object>> findOperatorScheduleReport(Map paramMap) {
        String site = CommonMethods.getSite();
        paramMap.put("site",site);
        paramMap.put("locale",LocaleContextHolder.getLocale().getLanguage());
        return reportMapper.findOperatorScheduleReport(paramMap);
    }

    @Override
    public List<Map<String, Object>> findWorkOrderSchedule(Map paramMap) {
        String site = CommonMethods.getSite();
        paramMap.put("site",site);
        paramMap.put("locale",LocaleContextHolder.getLocale().getLanguage());
        return reportMapper.findWorkOrderSchedule(paramMap);
    }
}












