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

import com.deer.wms.produce.manage.dao.MachiningProductMapper;
import com.deer.wms.produce.manage.dao.ProcessBomMapper;
import com.deer.wms.produce.manage.dao.ProcessMaterialsBomMapper;
import com.deer.wms.produce.manage.dao.ProductProcessBomMapper;
import com.deer.wms.produce.manage.model.*;
import com.deer.wms.produce.manage.service.MachiningProductService;
import com.deer.wms.produce.manage.service.ProcessMaterialsBomService;
import com.deer.wms.produce.manage.service.ProductProcessBomService;


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.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by guo on 2019/07/18.
 */
@Service
@Transactional
public class ProductProcessBomServiceImpl extends AbstractService<ProductProcessBom, Integer> implements ProductProcessBomService {

    @Autowired
    private ProductProcessBomMapper productProcessBomMapper;

    @Autowired
    private ProcessBomMapper processBomMapper;

    @Autowired
    private ProcessMaterialsBomMapper processMaterialsBomMapper;

    @Autowired
    private ProcessMaterialsBomService processMaterialsBomService;

    @Autowired
    private MachiningProductMapper machiningProductMapper;


    @Override
    public List<ProductProcessBomDto> findProBomAndProList(ProductProcessBomParams params) {
        return productProcessBomMapper.findProBomAndProList(params);
    }

    @Override
    public List<ProductProcessBomDto> findListByTerm(ProductProcessBomParams params) {
        return productProcessBomMapper.findListByTerm(params);
    }

    @Override
    public List<ProductProcessBom> findListById(ProductProcessBomParams params) {
        return productProcessBomMapper.findListById(params);
    }

    @Override
    public List<ProductProcessBomVO> findByProductNameAndProductNum(ProductProcessBomParams mtAloneProductProcessBomParams) {
        return productProcessBomMapper.findByProductNameAndProductNum(mtAloneProductProcessBomParams);
    }

    @Override
    public void addProductProcessBom(ProductProcessBomParams params) {
        ProductProcessBom productProcessBom = new ProductProcessBom();
        productProcessBom.setCreateTime(params.getCreateTime());
        productProcessBom.setCompanyId(params.getCompanyId());
        productProcessBom.setSpecification(params.getWorkSpecification());
        productProcessBom.setProductId(params.getProductId());
        productProcessBom.setProcessContent(params.getProcessContent());
        productProcessBom.setProductProcessName(params.getProductProcessName());

        productProcessBomMapper.insertSelective(productProcessBom);
        for (ProcessBomParams processBomParams:params.getProcessBomParamsList()) {
            ProcessBom processBom = new ProcessBom();
            processBom.setCreateTime(new Date());
            processBom.setProcessName(processBomParams.getProcessName());
            processBom.setProductProcessId(productProcessBom.getId());
            processBom.setSpecification(processBomParams.getSpecification());
            processBom.setProcessContent(processBomParams.getProcessContent());
            processBom.setProcessStep(processBomParams.getProcessStep());
            processBom.setMemo(processBomParams.getMemo());
            processBom.setCompanyId(params.getCompanyId());
            processBom.setProcessApartment(processBomParams.getProcessApartment());
            processBomMapper.insertSelective(processBom);
            for (ProcessMaterialsBomParams processMaterialsBomParams:processBomParams.getProcessMaterialsBomParamsList()) {
                processMaterialsBomParams.setProcessBomId(processBom.getId());
            }
        }
        List<ProcessMaterialsBom> processMaterialsBomList = new ArrayList<>();
        for (int i=0;i<params.getProcessBomParamsList().size();i++) {
            for (ProcessMaterialsBomParams processMaterialsBomParams:params.getProcessBomParamsList().get(i).getProcessMaterialsBomParamsList()){
                ProcessMaterialsBom processMaterialsBom = new ProcessMaterialsBom();
                processMaterialsBom.setCreateTime(new Date());
                processMaterialsBom.setProcessBomId(processMaterialsBomParams.getProcessBomId());
                processMaterialsBom.setMaterialsId(processMaterialsBomParams.getMaterialsId());
                processMaterialsBom.setCode(processMaterialsBomParams.getCode());
                processMaterialsBom.setSpecification(processMaterialsBomParams.getSpecification());
                processMaterialsBom.setDosage(processMaterialsBomParams.getDosage().floatValue());
                processMaterialsBom.setUnitId(processMaterialsBomParams.getUnitId());
                processMaterialsBom.setCompanyId(params.getCompanyId());

                processMaterialsBomList.add(processMaterialsBom);
            }
        }
        processMaterialsBomMapper.insertList(processMaterialsBomList);

    }

    @Override
    public void updateProductProcessBom(ProductProcessBomParams params) {
        System.out.println(params.toString());
        ProductProcessBom productProcessBom = new ProductProcessBom();
        productProcessBom.setId(params.getId());
        productProcessBom.setSpecification(params.getWorkSpecification());
        productProcessBom.setProcessContent(params.getProcessContent());
        productProcessBom.setProductProcessName(params.getProductProcessName());
        productProcessBomMapper.updateByPrimaryKeySelective(productProcessBom);

        List<ProcessBom> processBomList = new ArrayList<>();
        for (ProcessBomParams processBomParams:params.getProcessBomParamsList()) {
            ProcessBom processBom = new ProcessBom();
            processBom.setId(processBomParams.getProcessId());
            processBom.setUpdateTime(new Date());
            processBom.setProcessName(processBomParams.getProcessName());
            processBom.setProductProcessId(processBomParams.getProductProcessId());
            processBom.setSpecification(processBomParams.getSpecification());
            processBom.setProcessContent(processBomParams.getProcessContent());
            processBom.setProcessStep(processBomParams.getProcessStep());
            processBom.setProcessApartment(processBomParams.getProcessApartment());
            processBomMapper.saveOrUpdate(processBom);
            System.out.println("id="+ processBom.getId());
            for (ProcessMaterialsBomParams processMaterialsBomParams:processBomParams.getProcessMaterialsBomParamsList()) {
                processMaterialsBomParams.setProcessBomId(processBom.getId());
            }
//            processBomList.add(processBom);
        }
//        processBomMapper.insertOrUpdate(processBomList);

        List<ProcessMaterialsBom> processMaterialsBomList = new ArrayList<>();
        for (int i=0;i<params.getProcessBomParamsList().size();i++) {
            for (ProcessMaterialsBomParams processMaterialsBomParams:params.getProcessBomParamsList().get(i).getProcessMaterialsBomParamsList()){
                ProcessMaterialsBom processMaterialsBom = new ProcessMaterialsBom();
                processMaterialsBom.setId(processMaterialsBomParams.getProcessMaterialsBomId());
                processMaterialsBom.setUpdateTime(new Date());
                processMaterialsBom.setProcessBomId(processMaterialsBomParams.getProcessBomId());
                processMaterialsBom.setMaterialsId(processMaterialsBomParams.getMaterialsId());
                processMaterialsBom.setCode(processMaterialsBomParams.getCode());
                processMaterialsBom.setSpecification(processMaterialsBomParams.getSpecification());
                processMaterialsBom.setDosage(processMaterialsBomParams.getDosage().floatValue());
                processMaterialsBom.setUnitId(processMaterialsBomParams.getUnitId());
                processMaterialsBomList.add(processMaterialsBom);
            }
        }
        processMaterialsBomMapper.insertOrUpdate(processMaterialsBomList);
    }

    @Override
    public ProductProcessBomHo findAllBomByProdProcBom(ProductProcessBomParams params) {
        ProductProcessBomHo productProcessBomHo = new ProductProcessBomHo();
        //产品bom的id、名称获取
        ProductProcessBom productProcessBom = productProcessBomMapper.selectByProdProcBom(params);
        productProcessBomHo.setProdProcBomId(productProcessBom.getId());
        productProcessBomHo.setProdProcBomName(productProcessBom.getProductProcessName());
        //工序bom获取
        ProcessBomParams processBomParams = new ProcessBomParams();
        processBomParams.setProductProcessId(params.getProductBomId());
        processBomParams.setCompanyId(params.getCompanyId());
        List<ProcessBom> processBomList =  processBomMapper.findListByProductBom(processBomParams);
        List<ProcessBomHo> processBomHoList = null;
        if(null != processBomList){
            processBomHoList = new ArrayList<ProcessBomHo>(processBomList.size());
            for(ProcessBom processBom : processBomList){
                ProcessBomHo processBomHo = new ProcessBomHo();
                BeanUtils.copyProperties(processBom, processBomHo);
                processBomHoList.add(processBomHo);
                //物料bom获取
                ProcessMaterialsBomParams procMatBomParams = new ProcessMaterialsBomParams();
                procMatBomParams.setProcessBomId(processBom.getId());
                procMatBomParams.setCompanyId(processBomParams.getCompanyId());
                List<ProcessMaterialsBomVo> procMatBomVoList = processMaterialsBomService.findProcMatBomVoListByProcBom(procMatBomParams);
                processBomHo.setProcMatBomVoList(procMatBomVoList);
            }
        }
        productProcessBomHo.setProcessBomHoList(processBomHoList);

        return productProcessBomHo;
    }

    @Override
    public ProductProcessBom selectByProdProcBom(ProductProcessBomParams params) {
        return productProcessBomMapper.selectByProdProcBom(params);
    }

    @Override
    public ProductProcessBomDto findDataById(Integer id) {
        return productProcessBomMapper.findDataById(id);
    }
}
