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

import cn.estsh.i3plus.ext.mes.api.base.IProdPackExtService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProdPackExt;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesProdPackExtRepository;
import cn.estsh.i3plus.ext.mes.pojo.sqlpack.MesExtHqlPack;
import cn.estsh.i3plus.ext.mes.pojo.util.ConvertBeanExt;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Description:
 * @Reference:
 * @Author: wangjie
 * @CreateDate: 2019\11\4 10:39
 * @Modify:
 **/
@Service
public class ProdPackExtService implements IProdPackExtService {

    @Autowired
    private MesProdPackExtRepository prodPackExtRepository;

    @Override
    public ListPager<MesProdPackExt> queryProdPackByPager(MesProdPackExt prodPackExt, Pager pager) {
        DdlPackBean hqlPack = MesExtHqlPack.getMesProdPackExt(prodPackExt, AuthUtil.getOrganize().getOrganizeCode());
        pager = PagerHelper.getPager(pager, prodPackExtRepository.findByHqlWhereCount(hqlPack));
        hqlPack.setOrderByStr(prodPackExt.orderBy());
        return new ListPager(prodPackExtRepository.findByHqlWherePage(hqlPack, pager), pager);
    }

    @Override
    public MesProdPackExt insertProdPack(MesProdPackExt prodPackExt) {
        if (StringUtils.isEmpty(prodPackExt.getPartNo()) && StringUtils.isEmpty(prodPackExt.getPptCode())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("入参缺少物料编号或者产品生产类型")
                    .build();
        }
        if (!StringUtils.isEmpty(prodPackExt.getPartNo()) && !StringUtils.isEmpty(prodPackExt.getPptCode())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("入参物料编号跟产品生产类型不能同时维护")
                    .build();
        }

        DdlPackBean packBean = new DdlPackBean();
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), MesExtConstWords.IS_DELETED, packBean);
        DdlPreparedPack.getStringEqualPack(prodPackExt.getOrganizeCode(), MesExtConstWords.ORGANIZE_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(prodPackExt.getPartNo(), MesExtConstWords.PART_NO, packBean);
        DdlPreparedPack.getStringEqualPack(prodPackExt.getPptCode(), MesExtConstWords.PPT_CODE, packBean);
        List<MesProdPackExt> prodPackExtDbList = prodPackExtRepository.findByHqlWhere(packBean);
        if (!CollectionUtils.isEmpty(prodPackExtDbList)) {
            for (MesProdPackExt prodPackExtDb : prodPackExtDbList) {
                if (null != prodPackExtDb) {
                    if ((StringUtils.isEmpty(prodPackExt.getCustomerCode()) && StringUtils.isEmpty(prodPackExtDb.getCustomerCode())) ||
                            (!StringUtils.isEmpty(prodPackExt.getCustomerCode()) && !StringUtils.isEmpty(prodPackExtDb.getCustomerCode()) &&
                                    prodPackExt.getCustomerCode().equals(prodPackExtDb.getCustomerCode()))) {
                        throw ImppExceptionBuilder.newInstance()
                                .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                                .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                                .setErrorDetail("物料号为[%s]" + (StringUtils.isEmpty(prodPackExt.getCustomerCode()) ? "%s" :
                                        "客户代码为[%s]") + "的记录已存在", prodPackExt.getPartNo(), StringUtils.isEmpty(prodPackExt.getCustomerCode()) ? "" :prodPackExt.getCustomerCode())
                                .build();
                    }
                }
            }
        }
        ConvertBean.serviceModelInitialize(prodPackExt, prodPackExt.getCreateUser());
        new ConvertBeanExt(prodPackExt).convertBean(prodPackExt);
        return prodPackExtRepository.insert(prodPackExt);
    }

    @Override
    public MesProdPackExt updateProdPack(MesProdPackExt prodPackExt) {
        MesProdPackExt prodPackExtDb = prodPackExtRepository.getById(prodPackExt.getId());
        if (null == prodPackExtDb) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("物料号为[%s]的记录不存在", prodPackExt.getPartNo())
                    .build();
        }
        prodPackExtDb.setPackLabelTemplate(prodPackExt.getPackLabelTemplate());
        prodPackExtDb.setPackLabelTemplate2(prodPackExt.getPackLabelTemplate2());
        prodPackExtDb.setPackLabelTemplate3(prodPackExt.getPackLabelTemplate3());
        prodPackExtDb.setPackLabelTemplate4(prodPackExt.getPackLabelTemplate4());
        prodPackExtDb.setCustLabelTemplate(prodPackExt.getCustLabelTemplate());
        prodPackExtDb.setPackSpec(prodPackExt.getPackSpec());
        prodPackExtDb.setProdCfgType(prodPackExt.getProdCfgType());
        prodPackExtDb.setProdLabelTemplate(prodPackExt.getProdLabelTemplate());
        prodPackExtDb.setProcessLabelTemplate(prodPackExt.getProcessLabelTemplate());
        prodPackExtDb.setQualityAssureTemplate(prodPackExt.getQualityAssureTemplate());
        prodPackExtDb.setBigProcessLabelTemplate(prodPackExt.getBigProcessLabelTemplate());
        prodPackExtDb.setBigPackageLabelTemplate(prodPackExt.getBigPackageLabelTemplate());
        ConvertBean.serviceModelUpdate(prodPackExtDb, prodPackExt.getModifyUser());
        new ConvertBeanExt(prodPackExtDb).convertBean(prodPackExtDb);
        return prodPackExtRepository.save(prodPackExtDb);
    }

    @Override
    public void updateProdPackValidById(Long id, int status, String userName) {
        if (status != CommonEnumUtil.IS_VAILD.INVAILD.getValue() && status != CommonEnumUtil.IS_VAILD.VAILD.getValue()) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("启用，禁用枚举状态值[%s]未被定义", status)
                    .build();
        }

        MesProdPackExt prodPackExt = prodPackExtRepository.getById(id);
        if (null == prodPackExt || prodPackExt.getIsValid() == status) return;
        prodPackExt.setIsValid(status);
        ConvertBean.serviceModelUpdate(prodPackExt, userName);
        new ConvertBeanExt(prodPackExt).convertBean(prodPackExt);
        prodPackExtRepository.save(prodPackExt);
    }

    @Override
    public MesProdPackExt getProdPackExt(String organizeCode, String partNo, String pptCode) {
        MesProdPackExt prodPackExt = prodPackExtRepository.getByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID, MesExtConstWords.PART_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), partNo});
        if (null == prodPackExt && !StringUtils.isEmpty(pptCode)) {
            prodPackExt = prodPackExtRepository.getByProperty(
                    new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID, MesExtConstWords.PPT_CODE},
                    new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), pptCode});
        }
        return prodPackExt;
    }

}
