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

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.supply.judgementBills.entity.SupplyJudgementBills;
import com.jeeplus.modules.supply.judgementBills.mapper.SupplyJudgementBillsMapper;
import com.jeeplus.modules.supply.utils.CommonUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 审定单service
 * @author: 徐滕
 * @create: 2022-02-28 15:59
 **/
@Service
@Transactional(readOnly = true)
public class SupplyJudgementBillsService extends CrudService<SupplyJudgementBillsMapper, SupplyJudgementBills> {

    @Override
    public Page<SupplyJudgementBills> findPage(Page<SupplyJudgementBills> page, SupplyJudgementBills supplyJudgementBills) {
        return super.findPage(page, supplyJudgementBills);
    }


    /**
     * 导入物料库Excel数据
     * @param judgementBillsList
     * @return
     */
    @Transactional(readOnly = false)
    public Map disposeJudgementBills(List<SupplyJudgementBills> judgementBillsList){
        Map map = new HashMap();
        Set set = new HashSet<>();
        Iterator<SupplyJudgementBills> it = judgementBillsList.iterator();
        //删除项目编号为空的数据信息
        while(it.hasNext()){
            SupplyJudgementBills info = it.next();
            if(StringUtils.isBlank(info.getMaterialNumber())){//判定物料编码是否为空

                map.put("success",false);
                map.put("message","存在物料编码为空的数据，请确认后重新提交");
                return map;
            } else if(StringUtils.isBlank(info.getExamineCount())){//判定审定数量是否为空

                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 {
                //将项目定义号添加到set中
                set.add(info.getProjectDefinition());
                info.preInsert();
            }
        }
        //查询项目定义号对应的项目信息
        List<String> projectDefinitionList = new ArrayList(set);
        List<String> projectDefinitionInfoList = mapper.getProjectDefinitionList(projectDefinitionList);
        //判断项目信息和数据库中的项目信息数量是否一致，不一致则找到对应信息并进行提示
        if(projectDefinitionList.size() != projectDefinitionInfoList.size()){
            String nonentityProjectDefinitions = CommonUtil.comparisonProjectDefinition(projectDefinitionInfoList,projectDefinitionList);
            map.put("success",false);
            map.put("message","查询不到项目定义号为：" + nonentityProjectDefinitions + " 的项目信息。请先上传对应项目信息");
            return map;
        }
        //批量处理
        disposeStockOutList(judgementBillsList);
        map.put("success",true);
        return map;
    }


    /**
     * 批量处理物料库数据信息
     * @param judgementBillsList
     * @return
     */
    @Transactional(readOnly = false)
    public String disposeStockOutList(List<SupplyJudgementBills> judgementBillsList){
        //分批处理
        if(null!=judgementBillsList && judgementBillsList.size()>0){
            int pointsDataLimit = 5000;//限制条数
            Integer size = judgementBillsList.size();
            //判断是否有必要分批
            if(pointsDataLimit<size){
                int part = size/pointsDataLimit;//分批数
                //
                for (int i = 0; i < part; i++) {
                    //5000条
                    List<SupplyJudgementBills> listPage = judgementBillsList.subList(0, pointsDataLimit);
                    mapper.insertList(listPage);
                    //剔除
                    judgementBillsList.subList(0, pointsDataLimit).clear();
                }
                if(!judgementBillsList.isEmpty()){
                    mapper.insertList(judgementBillsList);
                }
            }else{
                mapper.insertList(judgementBillsList);
            }
        }
        return null;
    }

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