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

import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.produce.manage.dao.ProductBatchDetectMapper;
import com.deer.wms.produce.manage.model.*;
import com.deer.wms.produce.manage.service.ProductBatchDetectDetService;
import com.deer.wms.produce.manage.service.ProductBatchDetectService;

import com.deer.wms.project.seed.core.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by  on 2019/09/09.
 */
@Service
@Transactional
public class ProductBatchDetectServiceImpl extends AbstractService<ProductBatchDetect, Integer> implements ProductBatchDetectService {

    @Autowired
    private ProductBatchDetectMapper productBatchDetectMapper;

    @Autowired
    private ProductBatchDetectDetService productBatchDetectDetService;

    @Override
    public List<ProductBatchDetect> findList(ProductBatchDetectParams  params) {
        return productBatchDetectMapper.findList(params);
    }

    @Override
    public void saveDetectRusult(ProductBatchDetectInfo productBatchDetectInfo, CurrentUser currentUser) {
        Date date = new Date();
        ProductProcessBatchDetInfo productProcessBatchDetInfo = productBatchDetectInfo.getProductProcessBatchDetInfo();
        MtAloneProcessVo processVo = productProcessBatchDetInfo.getProcessVo();//获取工序
        ProductProcessBatchDet productProcessBatchDet = productProcessBatchDetInfo.getProductProcessBatchDet();//获取批次明细
        /**检测等级赋值**/
        ProductBatchDetect productBatchDetect = productBatchDetectInfo.getProductBatchDetect();
        productBatchDetect.setOperatorId(currentUser.getUserId());
        productBatchDetect.setCreateTime(date);
        productBatchDetect.setUpdateTime(date);
        productBatchDetect.setVersion("1.1");
        productBatchDetect.setStatus(0);
        productBatchDetect.setProcessId(processVo.getId());
        productBatchDetect.setBatchDetId(productProcessBatchDet.getId());
        productBatchDetect.setBatchDetCode(productProcessBatchDet.getBatchDetCode());
        productBatchDetect.setCompanyId(currentUser.getCompanyId());
        productBatchDetectMapper.insert(productBatchDetect);
        /**瑕疵名细赋值**/
        List<ProductBatchDetectDet> productBatchDetectDetList = productBatchDetectInfo.getProductBatchDetectDetList();
        if(null!=productBatchDetectDetList && productBatchDetectDetList.size()!=0) {
            for (ProductBatchDetectDet productBatchDetectDet : productBatchDetectDetList) {
                productBatchDetectDet.setOperatorId(currentUser.getUserId());
                productBatchDetectDet.setCreateTime(date);
                productBatchDetectDet.setUpdateTime(date);
                productBatchDetectDet.setVersion("1.1");
                productBatchDetectDet.setStatus(0);
                productBatchDetectDet.setProcessId(processVo.getId());
                productBatchDetectDet.setBatchDetId(productProcessBatchDet.getId());
                productBatchDetectDet.setCompanyId(currentUser.getCompanyId());
            }
            productBatchDetectDetService.save(productBatchDetectDetList);
        }

    }
}
