package com.jeeplus.modules.supply.materialsProvidedByAForReview.service;

import com.jeeplus.common.config.Global;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.supply.judgementBills.mapper.SupplyJudgementBillsMapper;
import com.jeeplus.modules.supply.materialLibrary.service.MaterialLibraryService;
import com.jeeplus.modules.supply.materialsProvidedByAForReview.entity.MaterialsProvidedByAForReview;
import com.jeeplus.modules.supply.materialsProvidedByAForReview.mapper.MaterialsProvidedByAForReviewMapper;
import com.jeeplus.modules.supply.utils.CommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 甲供材报审Service
 * @author: 黄俊杰
 * @create: 2022-02-28 16:00
 */
@Service
@Transactional(readOnly = true)
public class MaterialsProvidedByAForReviewService extends CrudService<MaterialsProvidedByAForReviewMapper, MaterialsProvidedByAForReview> {
    /** 主机 */
    private final static String host = Global.getConfig("remoteServer.host");
    private static Logger logger = LoggerFactory.getLogger(MaterialLibraryService.class);

    @Autowired
    private MaterialsProvidedByAForReviewMapper materialsProvidedByAForReviewMapper;

    @Autowired
    private SupplyJudgementBillsMapper supplyJudgementBillsMapper;


    /**
     * 页面显示
     * @param page 分页对象
     * @param materialsProvidedByAForReview
     * @return
     */
    @Override
    public Page<MaterialsProvidedByAForReview> findPage(Page<MaterialsProvidedByAForReview> page, MaterialsProvidedByAForReview materialsProvidedByAForReview){
        return super.findPage(page,materialsProvidedByAForReview);
    }

    /**
     * 批量删除
     * @param idList
     */
    @Transactional(readOnly = false)
    public void deleteByIdList(List<String> idList){
        materialsProvidedByAForReviewMapper.deleteByIdList(idList);
    }

    /**
     * 处理导入数据
     * @param list
     * @return
     */
    @Transactional(readOnly = false)
    public Map disposeMaterialsProvidedByAForReview(List<MaterialsProvidedByAForReview> list){
        Map map = new HashMap();
        Set<String> set = new HashSet<>();
        //判断必填字段不为空
        Iterator<MaterialsProvidedByAForReview> it = list.iterator();
        while(it.hasNext()){
            MaterialsProvidedByAForReview info = it.next();
            if(StringUtils.isBlank(info.getMaterialNumber())){   //物料编码不为空
                map.put("success",false);
                map.put("message","存在物料编码为空的数据，请确认后重新提交！");
                return map;
            }else if(StringUtils.isBlank(info.getActualQuantityIssued())){
                map.put("success",false);
                map.put("message","存在出库数量为空的数据，请确认后重新提交！");
                return map;
            }else if(StringUtils.isBlank(info.getProjectDefinition())){
                map.put("success",false);
                map.put("message","存在项目定义号为空的数据，请确认后重新提交！");
                return map;
            }else if(StringUtils.isBlank(info.getSubmittalsId())){
                map.put("success",false);
                map.put("message","存在送审id为空的数据，请确认后重新提交！");
                return map;
            }else {
                //将项目定义号放入set中去重
                set.add(info.getProjectDefinition());
                info.preInsert();
            }
        }

        //查询项目定义号对应的项目信息
        List<String> projectDefinitionList = new ArrayList(set);
        List<String> projectDefinitionInfoList = supplyJudgementBillsMapper.getProjectDefinitionList(projectDefinitionList);
        //判断项目信息和数据库中的项目信息数量是否一致，不一致则找到对应信息并进行提示
        if(projectDefinitionList.size() != projectDefinitionInfoList.size()){
            String nonentityProjectDefinitions = CommonUtil.comparisonProjectDefinition(projectDefinitionInfoList,projectDefinitionList);
            map.put("success",false);
            map.put("message","查询不到项目定义号为：" + nonentityProjectDefinitions + " 的项目信息。请先上传对应项目信息！");
            return map;
        }

        //提前保存读取的list的长度
        int size = list.size();
        map.put("size",size);

        //批量处理
        disposeMaterialsProvidedByAForReviewList(list);
        map.put("success",true);
        return map;
    }

    /**
     * 批量新增
     * @param projectBasicsList
     * @return
     */
    @Transactional(readOnly = false)
    public String disposeMaterialsProvidedByAForReviewList(List<MaterialsProvidedByAForReview> projectBasicsList){
        //分批处理
        if(null!=projectBasicsList && projectBasicsList.size()>0){
            int pointsDataLimit = 5000;//限制条数
            Integer size = projectBasicsList.size();
            //判断是否有必要分批
            if(pointsDataLimit<size){
                int part = size/pointsDataLimit;//分批数
                //
                for (int i = 0; i < part; i++) {
                    //5000条
                    List<MaterialsProvidedByAForReview> listPage = projectBasicsList.subList(0, pointsDataLimit);
                    materialsProvidedByAForReviewMapper.insertList(listPage);
                    //剔除
                    projectBasicsList.subList(0, pointsDataLimit).clear();
                }
                if(!projectBasicsList.isEmpty()){
                    materialsProvidedByAForReviewMapper.insertList(projectBasicsList);
                }
            }else{
                materialsProvidedByAForReviewMapper.insertList(projectBasicsList);
            }
        }
        return null;
    }

}
