package com.jeeplus.modules.supply.stockOut.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.mapper.SupplyJudgementBillsMapper;
import com.jeeplus.modules.supply.stockOut.entity.SupplyStockOut;
import com.jeeplus.modules.supply.stockOut.mapper.SupplyStockOutMapper;
import com.jeeplus.modules.supply.utils.CommonUtil;
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-25 10:31
 **/
@Service
@Transactional(readOnly = true)
public class SupplyStockOutService  extends CrudService<SupplyStockOutMapper, SupplyStockOut> {

    @Autowired
    private SupplyJudgementBillsMapper supplyJudgementBillsMapper;

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











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

                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());
                //冲击力材料类型
                if(StringUtils.isNotBlank(info.getAssessType())){
                    if(info.getAssessType().contains("材料")){
                        info.setMaterialsType("材料");
                    } else if (info.getAssessType().contains("设备")){
                        info.setMaterialsType("设备");
                    } else {
                        info.setMaterialsType("");
                    }
                }
                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;
        }

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


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

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