package com.deer.wms.produce.manage.service.impl;

import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.produce.manage.constant.ProduceManageConstant;
import com.deer.wms.produce.manage.constant.ProduceManagePublicMethod;
import com.deer.wms.produce.manage.dao.ProductProcessMapper;
import com.deer.wms.produce.manage.model.*;
import com.deer.wms.produce.manage.service.*;
import com.deer.wms.project.seed.core.service.AbstractService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by hy on 2019/07/19.
 */
@Service
@Transactional
public class ProductProcessServiceImpl extends AbstractService<ProductProcess, Integer> implements ProductProcessService {

    @Autowired
    private ProductProcessMapper productProcessMapper;

    @Autowired
    private ProductProcessService productProcessService;

    @Autowired
    private ProcessBomService processBomService;

    @Autowired
    private MtAloneProcessService processService;

    @Autowired
    private ProductProcessBomService productProcessBomService;

    @Autowired
    private ProcessMaterialsBomService processMaterialsBomService;

    @Autowired
    private ProcessMaterialsService processMaterialsService;

    @Autowired
    private ProductProcessBatchDetService productProcessBatchDetService;

    @Override
    public List<ProductProcessVo> findList(ProductProcessParams params) {
        return productProcessMapper.findList(params);
    }

    @Override
    public void save(ProductProcess productProcess, CurrentUser currentUser) {
        Date date = new Date();

        /**批次生产加工单赋值**/
        productProcess.setOperatorId(currentUser.getUserId());
        productProcess.setCreateTime(date);
        productProcess.setUpdateTime(date);
        productProcess.setVersion("1.1");
        productProcess.setStatus(ProduceManageConstant.STATUS_AVAILABLE);
        productProcess.setWorkOrderCode(ProduceManagePublicMethod.creatUniqueCode("PCJGD"));
        productProcess.setShrinkage((productProcess.getDetectionLen() / productProcess.getLen()) * 100 + "%");
        productProcess.setReviewStatus(ProduceManageConstant.REVIEW_STATUS_FORREVIEW);
        productProcess.setCompanyId(currentUser.getCompanyId());
        productProcessMapper.insert(productProcess);
    }

    @Override
    public void saveProdAndProc(ProductAndProcess productAndProcess, CurrentUser currentUser) {
        //保存批次产品加工信息
        ProductProcess productProcess = productAndProcess.getProductProcess();
        Date date = new Date();
        productProcess.setOperatorId(currentUser.getUserId());
        productProcess.setCreateTime(date);
        productProcess.setUpdateTime(date);
        productProcess.setVersion("1.1");
        productProcess.setStatus(ProduceManageConstant.STATUS_AVAILABLE);
        productProcess.setWorkOrderCode(ProduceManagePublicMethod.creatUniqueCode("PCJGD"));
        productProcess.setShrinkage((productProcess.getDetectionLen() / productProcess.getLen()) * 100 + "%");
        productProcess.setReviewStatus(ProduceManageConstant.REVIEW_STATUS_FORREVIEW);
        productProcess.setCompanyId(currentUser.getCompanyId());
        productProcessMapper.insert(productProcess);

        //保存由前端获取的工序信息
        List<MtAloneProcessVo> processVoList = productAndProcess.getProcessVoList();
        if (null != processVoList) {
            List<MtAloneProcess> processList = new ArrayList<MtAloneProcess>(processVoList.size());
            for (MtAloneProcess processVo : processVoList) {
                MtAloneProcess process = new MtAloneProcess();
                BeanUtils.copyProperties(processVo, process);
                process.setProductProcessId(productProcess.getId());
                processList.add(process);
            }
            processService.save(processList);
        }
    }

    @Override
    public ProductProcessHo queryProductProcessDetail(ProductProcessParams params) {
        //根据批次生产加工id查询批次产品加工单信息
        ProductProcess productProcess = productProcessMapper.selectByPrimaryKey(params.getProductProcessId());
        //根据批次产品加工id查询工序信息
        List<MtAloneProcessVo> tempProcessVoList = processService.findProcessVoListByProductProcess(params);

        ProductProcessHo productProcessHo = new ProductProcessHo();
        if (null != productProcess) {
            BeanUtils.copyProperties(productProcess, productProcessHo);
            if (null != tempProcessVoList) {
                List<ProcessHo> processHoList = new ArrayList<ProcessHo>(tempProcessVoList.size());
                for (MtAloneProcessVo processVo : tempProcessVoList) {
                    ProcessHo processHo = new ProcessHo();
                    BeanUtils.copyProperties(processVo, processHo);
                    processHoList.add(processHo);
                }
                productProcessHo.setProcessHoList(processHoList);
            }
        }

        return productProcessHo;
    }


    @Override
    public ProductProcessHo initialProdProcByProdProcBom(ProductProcessBomParams params) {
        ProductProcessHo productProcessHo = new ProductProcessHo();

        //批次产品加工单初步赋值，关联bom
        ProductProcessBom productProcessBom = productProcessBomService.selectByProdProcBom(params);
        if (null != productProcessBom) {
            productProcessHo.setProductBomId(productProcessBom.getId());
            productProcessHo.setProductProcessBomName(productProcessBom.getProductProcessName());
            //工序初步赋值，关联bom
            ProcessBomParams processBomParams = new ProcessBomParams();
            processBomParams.setProductProcessId(params.getProductBomId());
            processBomParams.setCompanyId(params.getCompanyId());
            List<ProcessBom> processBomList = processBomService.findListByProductBom(processBomParams);
            List<ProcessHo> processHoList;
            if (null != processBomList) {
                processHoList = new ArrayList<ProcessHo>(processBomList.size());
                for (ProcessBom processBom : processBomList) {
                    ProcessHo processHo = new ProcessHo();
                    processHo.setProcessBomId(processBom.getId());
                    processHo.setProcessBomName(processBom.getProcessName());
                    processHo.setProcessStep(processBom.getProcessStep());
                    processHo.setProcessName(processBom.getProcessName());
                    processHo.setProcessContent(processBom.getProcessContent());
                    processHo.setSpecification(processBom.getSpecification());

                    //物料初步赋值，关联bom
                    ProcessMaterialsBomParams procMatBomParams = new ProcessMaterialsBomParams();
                    procMatBomParams.setProcessBomId(processBom.getId());
                    procMatBomParams.setCompanyId(processBomParams.getCompanyId());
                    List<ProcessMaterialsBomVo> procMatBomVoList = processMaterialsBomService.findProcMatBomVoListByProcBom(procMatBomParams);
                    List<ProcessMaterialsVo> procMatVoList;
                    if (null != procMatBomVoList) {
                        procMatVoList = new ArrayList<ProcessMaterialsVo>(procMatBomVoList.size());
                        for (ProcessMaterialsBomVo procMatBomVo : procMatBomVoList) {
                            ProcessMaterialsVo procMatVo = new ProcessMaterialsVo();
                            procMatVo.setMaterialsBomId(procMatBomVo.getId());
                            procMatVo.setMaterialsName(procMatBomVo.getMaterialsName());
                            procMatVo.setSpecification(procMatBomVo.getSpecification());
                            procMatVo.setUnitId(procMatBomVo.getUnitId());
                            procMatVo.setUnitName0(procMatBomVo.getUnitName());
                            procMatVo.setPlanDosage(procMatBomVo.getDosage());//先按照模板对计划用量赋值
                            procMatVoList.add(procMatVo);
                        }
                        processHo.setProcessMaterialsVoList(procMatVoList);
                    }
                    processHoList.add(processHo);
                }
                productProcessHo.setProcessHoList(processHoList);
            }
        }

        return productProcessHo;
    }

    @Override
    public void addProduceProcess(ProductProcessHo productProcessHo, CurrentUser currentUser) {
        //保存批次产品加工信息
        Date date = new Date();
        ProductProcess productProcess = new ProductProcess();
        BeanUtils.copyProperties(productProcessHo, productProcess);
        productProcess.setOperatorId(currentUser.getUserId());
        productProcess.setCreateTime(date);
        productProcess.setUpdateTime(date);
        productProcess.setVersion("1.1");
        productProcess.setStatus(ProduceManageConstant.STATUS_AVAILABLE);
        productProcess.setWorkOrderCode(ProduceManagePublicMethod.creatUniqueCode("PCJGD"));
        productProcess.setShrinkage((productProcess.getDetectionLen() / productProcess.getLen()) * 100 + "%");
        productProcess.setReviewStatus(ProduceManageConstant.REVIEW_STATUS_FORREVIEW);
        productProcess.setCompanyId(currentUser.getCompanyId());
        productProcessMapper.insert(productProcess);

        //保存工序和物料信息，避免循环操作数据库
        List<ProcessHo> processHoList = productProcessHo.getProcessHoList();
        if (null != processHoList && processHoList.size() != 0) {
            List<MtAloneProcess> processList = new ArrayList<MtAloneProcess>(processHoList.size());
            for (ProcessHo processHo : processHoList) {
                MtAloneProcess process = new MtAloneProcess();
                BeanUtils.copyProperties(processHo, process);
                process.setOperatorId(currentUser.getUserId());
                process.setCreateTime(date);
                process.setUpdateTime(date);
                process.setVersion("1.1");
                process.setStatus(ProduceManageConstant.STATUS_AVAILABLE);
                process.setProductProcessId(productProcess.getId());
                process.setCode(ProduceManagePublicMethod.creatUniqueCode("JGGX"));
                process.setCompanyId(currentUser.getCompanyId());
                process.setHandleStatus(ProduceManageConstant.HANDLE_STATUS_FORHANDLE);
                process.setBeforeHandleStatus(ProduceManageConstant.HANDLE_STATUS_FORHANDLE);
                processList.add(process);
            }
            processService.save(processList);

            List<ProcessMaterials> processMaterialsList = new ArrayList<ProcessMaterials>();
            for (int i = 0; i < processHoList.size(); i++) {
                ProcessHo processHo = processHoList.get(i);
                MtAloneProcess process = processList.get(i);
                List<ProcessMaterialsVo> processMaterialsVoList = processHo.getProcessMaterialsVoList();
                if (null != processMaterialsVoList && processMaterialsVoList.size() != 0) {
                    for (ProcessMaterialsVo processMaterialsVo : processMaterialsVoList) {
                        ProcessMaterials processMaterials = new ProcessMaterials();
                        BeanUtils.copyProperties(processMaterialsVo, processMaterials);
                        processMaterials.setOperatorId(currentUser.getUserId());
                        processMaterials.setCreateTime(date);
                        processMaterials.setUpdateTime(date);
                        processMaterials.setVersion("1.1");
                        processMaterials.setStatus(ProduceManageConstant.STATUS_AVAILABLE);
                        processMaterials.setProcessId(process.getId());
                        processMaterials.setCode(ProduceManagePublicMethod.creatUniqueCode("JGWL"));
                        processMaterials.setPlanDosage(productProcess.getPiNum() * processMaterials.getPlanDosage());
                        processMaterials.setCompanyId(currentUser.getCompanyId());
                        processMaterialsList.add(processMaterials);
                    }
                }
            }

            if (processMaterialsList.size() != 0)
                processMaterialsService.save(processMaterialsList);
        }

        //保存批次明细
        List<ProductProcessBatchDet> productProcessBatchDetList = new ArrayList<ProductProcessBatchDet>();
        for (int i = 0; i < productProcess.getPiNum(); i++) {
            ProductProcessBatchDet productProcessBatchDet = new ProductProcessBatchDet();
            productProcessBatchDet.setOperatorId(currentUser.getUserId());
            productProcessBatchDet.setCreateTime(date);
            productProcessBatchDet.setUpdateTime(date);
            productProcessBatchDet.setVersion("1.1");
            productProcessBatchDet.setStatus(0);
            productProcessBatchDet.setBatchCode(productProcess.getBatchCode());
            productProcessBatchDet.setDeliveryCode(productProcess.getDeliveryCode());
            productProcessBatchDet.setProductProcessId(productProcess.getId());
            productProcessBatchDet.setVolumeNum(i+1+"");
            productProcessBatchDet.setBatchDetCode(ProduceManagePublicMethod.creatBatchDetCode(productProcessBatchDet.getCreateTime(), productProcessBatchDet.getProductProcessId(), productProcessBatchDet.getBatchCode(), productProcess.getPiNum(), productProcessBatchDet.getVolumeNum()));
            productProcessBatchDet.setCompanyId(currentUser.getCompanyId());

            productProcessBatchDetList.add(productProcessBatchDet);
        }

        if (productProcessBatchDetList.size() != 0)
            productProcessBatchDetService.save(productProcessBatchDetList);
    }

    @Override
    public ProductProcessVo findProductProcessById(ProductProcessParams productProcessParams) {
        return productProcessMapper.findProductProcessById(productProcessParams);
    }

    //@Override
    //public ProcessAndDet stepAndNumByProdprocid(ProductProcessParams params) {
    //    ProcessAndDet processAndDet = new ProcessAndDet();
    //    List<Integer> processStepList = processService.processStepListByProdprocid(params);
    //    List<String> volumeStepList = productProcessBatchDetService.volumeStepListByProdprocid(params);
    //    processAndDet.setProcessStepList(processStepList);
    //    processAndDet.setVolumeStepList(volumeStepList);
    //    return processAndDet;
    //}


    //@Override
    //public void deleteRelevantById(Integer produceProcessId, CurrentUser currentUser) {
    //    productProcessMapper.deleteByPrimaryKey(produceProcessId);
    //    HashMap produceProcessMap = new HashMap();
    //    produceProcessMap.put("companyId", currentUser.getCompanyId());
    //    produceProcessMap.put("produceProcessId", produceProcessId);
    //    processService.deleteByproduceProcessId(produceProcessId);
    //
    //}


}
