package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.busi;

import cn.estsh.i3plus.ext.mes.api.busi.IWorkOrderStartService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWeigh;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWeighManager;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWorkOrderExt;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesWeighManagerRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesWeighRepository;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author yumingxing
 * @version 1.0
 * @date 2021/2/19 16:06
 **/
@Service
public class WeighCheckService implements IWorkOrderStartService {

    @Autowired
    private MesWeighRepository weighRepository;
    @Autowired
    private MesWeighManagerRepository weighManagerRepository;

    /**
     * 工单启动后方法执行
     *
     * @param mesWorkOrderExt 工单
     * @return {@link ResultBean}
     */
    @Override
    public ResultBean execute(MesWorkOrderExt mesWorkOrderExt) {
        String partNo = mesWorkOrderExt.getPartNo();
        String workCenterCode = mesWorkOrderExt.getWorkCenterCode();
        String partNameRdd = mesWorkOrderExt.getPartNameRdd();
        String orderNo = mesWorkOrderExt.getOrderNo();
        String organizeCode = mesWorkOrderExt.getOrganizeCode();

        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(partNo, "partNo", ddlPackBean);
        List<MesWeigh> mesWeighs = weighRepository.findByHqlWhere(ddlPackBean);
        if (!CollectionUtils.isEmpty(mesWeighs)) {
            MesWeigh mesWeigh = mesWeighs.get(0);
            Double standardWeight = mesWeigh.getStandardWeight();
            MesWeighManager mesWeighManager = new MesWeighManager();
            mesWeighManager.setOrderNo(orderNo);
            mesWeighManager.setPartNo(partNo);
            mesWeighManager.setPartName(partNameRdd);
            mesWeighManager.setStandardWeight(standardWeight);
            mesWeighManager.setOrganizeCode(organizeCode);
            mesWeighManager.setWorkCenterCode(workCenterCode);
            mesWeighManager.setWeighNum(Double.valueOf(10));
            ConvertBean.modelInitialize(mesWeighManager, AuthUtil.getSessionUser());
            weighManagerRepository.save(mesWeighManager);
        }
        return ResultBean.success("添加成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
    }
}
