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.dao.MtAloneProcessMapper;
import com.deer.wms.produce.manage.model.*;
import com.deer.wms.produce.manage.service.MtAloneProcessService;
import com.deer.wms.produce.manage.service.ProcessBomService;
import com.deer.wms.produce.manage.service.ProcessMaterialsBomService;
import com.deer.wms.produce.manage.service.ProcessMaterialsService;
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 hy on 2019/07/19.
 */
@Service
@Transactional
public class MtAloneProcessServiceImpl extends AbstractService<MtAloneProcess, Integer> implements MtAloneProcessService {

    @Autowired
    private MtAloneProcessMapper mtAloneProcessMapper;

    @Autowired
    private ProcessBomService processBomService;

    @Autowired
    private ProcessMaterialsService processMaterialsService;

    @Autowired
    private ProcessMaterialsBomService processMaterialsBomService;

    @Override
    public List<MtAloneProcess> findProcessListByProductBom(ProcessBomParams params) {
        //通过产品bom获取工序bom
        List<ProcessBom> processBomList = processBomService.findListByProductBom(params);
        List<MtAloneProcess> processList = new ArrayList<MtAloneProcess>();

        if (processBomList != null && processBomList.size() != 0) {
            //循环工序bom给工序初步赋值，先不保存
            for (ProcessBom processBom : processBomList) {
                MtAloneProcess process = new MtAloneProcess();
                BeanUtils.copyProperties(processBom, process);
                processList.add(process);
            }
        }

        return processList;
    }

    @Override
    public List<MtAloneProcessVo> findProcessVoListByProductProcess(ProductProcessParams params) {
        return mtAloneProcessMapper.findProcessVoListByProductProcess(params);
    }

    @Override
    public ProcessHo queryProcessDetail(MtAloneProcessParams params) {
        //根据工序id查询工序详细信息
        MtAloneProcessVo processVo = (MtAloneProcessVo) mtAloneProcessMapper.selectProcessVoByProcessId(params);
        //根据工序id查询物料信息
        List<ProcessMaterialsVo> procMatVoList = processMaterialsService.findProcMatVoListByProcess(params);
        ProcessHo processHo = new ProcessHo();
        if (null != processVo) {
            BeanUtils.copyProperties(processVo, processHo);
            processHo.setProcessMaterialsVoList(procMatVoList);
        }
        return processHo;
    }

    @Override
    public void saveProcAndProcMat(ProcessAndProcMat processAndProcMat, CurrentUser currentUser) {
        //前端对工序赋值，更新工序
        MtAloneProcessVo mtAloneProcessVo = processAndProcMat.getMtAloneProcessVo();
        MtAloneProcess mtAloneProcess = new MtAloneProcess();
        BeanUtils.copyProperties(mtAloneProcessVo, mtAloneProcess);
        mtAloneProcessMapper.updateByPrimaryKey(mtAloneProcess);

        //通过工序bom的id获取物料bom
        ProcessMaterialsBomParams procMatBomParams = new ProcessMaterialsBomParams();
        procMatBomParams.setProcessBomId(processAndProcMat.getMtAloneProcessVo().getProcessBomId());
        procMatBomParams.setCompanyId(currentUser.getCompanyId());
        List<ProcessMaterialsBomVo> procMatBomVoList = processMaterialsBomService.findProcMatBomVoListByProcBom(procMatBomParams);
    }

    @Override
    public void updateProcess(ProcessHo processHo) {
        Date date = new Date();
        MtAloneProcess process = new MtAloneProcess();
        processHo.setUpdateTime(date);
        processHo.setHandleStatus(ProduceManageConstant.HANDLE_STATUS_FINISHED);
        BeanUtils.copyProperties(processHo, process);
        this.update(process);
        setNextProcessBeforeHandleStatus(process);//设置下道工序的前道工序处理状态为已处理

        List<ProcessMaterialsVo> procMatVoList = processHo.getProcessMaterialsVoList();
        if (null != procMatVoList && procMatVoList.size() != 0) {
            List<ProcessMaterials> procMatList = new ArrayList<ProcessMaterials>(procMatVoList.size());
            for (ProcessMaterialsVo processMaterialsVo : procMatVoList) {
                ProcessMaterials processMaterials = new ProcessMaterials();
                processMaterialsVo.setUpdateTime(date);
                BeanUtils.copyProperties(processMaterialsVo, processMaterials);
                procMatList.add(processMaterials);
            }
            processMaterialsService.updateList(procMatList);
        }
    }

    @Override
    public MtAloneProcessVo findProcessById(MtAloneProcessParams processParams) {
        return mtAloneProcessMapper.findProcessById(processParams);
    }

    //@Override
    //public List<Integer> processStepListByProdprocid(ProductProcessParams params) {
    //    return mtAloneProcessMapper.processStepListByProdprocid(params);
    //}

    //设置下一道工序的前道工序处理状态
    public void setNextProcessBeforeHandleStatus(MtAloneProcess currentProcess) {
        mtAloneProcessMapper.setNextProcessBeforeHandleStatus(currentProcess);
    }

}
