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

import com.foreverwin.mesnac.anomaly.dto.PartsManufacturing;
import com.foreverwin.mesnac.anomaly.mapper.ReportMapper;
import com.foreverwin.mesnac.anomaly.service.ReportService;
import com.foreverwin.mesnac.integration.model.IntegrationLog;
import com.foreverwin.mesnac.meapi.model.NcCode;
import com.foreverwin.mesnac.meapi.model.NwaUser;
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;

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

    /**
     * 零件加工报表
     * @param partsManufacturing
     * @return
     */
    @Override
    public  List<Map<String, Object>> partsManufacturing(PartsManufacturing partsManufacturing) {
        String site = CommonMethods.getSite();
        String locale = LocaleContextHolder.getLocale().getLanguage();
        return reportMapper.partsManufacturing(site,locale,partsManufacturing);
    }

    @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);
    }

    @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++) {
                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++) {
                repairUserDescription += userMap.get(repairUserArray[j]).get(0).getFullName()+";";
            }
            maps.get(i).put("REPAIR_USERS_DESCRIPTION", repairUserDescription);
        }




        return maps;
    }
}
