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

import cn.estsh.i3plus.ext.mes.api.base.IPrintPartParameterExtService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPrintPartParameterExt;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesPrintPartParameterExtRepository;
import cn.estsh.i3plus.ext.mes.pojo.sqlpack.MesExtHqlPack;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
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.exception.ImppExceptionBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-bu
 * @DESCRIPTION:
 * @USER: Xinwang.Yi
 * @DATE: 2022-01-11 17:57
 */
@Slf4j
@Service
public class PrintPartParameterExtService implements IPrintPartParameterExtService {

    @Autowired
    MesPrintPartParameterExtRepository printPartParameterExtRepository;

    @Override
    public ListPager<MesPrintPartParameterExt> queryPrintPartParameterExtByPager(MesPrintPartParameterExt printPartParameterExt, Pager pager) {
        DdlPackBean hqlPack = MesExtHqlPack.getPrintPartParameterExt(printPartParameterExt);
        pager = PagerHelper.getPager(pager, printPartParameterExtRepository.findByHqlWhereCount(hqlPack));
        hqlPack.setOrderByStr(printPartParameterExt.orderBy());
        return new ListPager(printPartParameterExtRepository.findByHqlWherePage(hqlPack, pager), pager);
    }

    @Override
    public MesPrintPartParameterExt insertPrintPartParameterExt(MesPrintPartParameterExt printPartParameterExt) {
        DdlPackBean packBean = MesExtHqlPack.getAllBaseData(printPartParameterExt.getOrganizeCode());
        DdlPreparedPack.getStringLikerPack(printPartParameterExt.getPartNo(), MesExtConstWords.PART_NO, packBean);
        DdlPreparedPack.getStringLikerPack(printPartParameterExt.getPartName(), MesExtConstWords.PART_NAME, packBean);
        MesPrintPartParameterExt printPartParameterExtDb = printPartParameterExtRepository.getByProperty(packBean);
        if (null != printPartParameterExtDb) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("物料代码[%s]物料名称[%s]记录已存在",
                            printPartParameterExtDb.getPartNo(), printPartParameterExtDb.getPartName())
                    .build();
        }

        ConvertBean.serviceModelInitialize(printPartParameterExt, printPartParameterExt.getCreateUser());
        printPartParameterExt.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        return printPartParameterExtRepository.insert(printPartParameterExt);
    }

    @Override
    public MesPrintPartParameterExt updatePrintPartParameterExt(MesPrintPartParameterExt printPartParameterExt) {
        //对id做有效性校验
        MesPrintPartParameterExt printPartParameterExtDb = printPartParameterExtRepository.getById(printPartParameterExt.getId());
        if (printPartParameterExtDb == null) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("物料编码[%s]物料名称[%s]记录不存在",
                            printPartParameterExt.getPartNo(), printPartParameterExt.getPartName())
                    .build();
        }

        printPartParameterExtDb.setPackageSize(printPartParameterExt.getPackageSize());
        printPartParameterExtDb.setNetWeight(printPartParameterExt.getNetWeight());
        printPartParameterExtDb.setRoughWeight(printPartParameterExt.getRoughWeight());
        printPartParameterExtDb.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        ConvertBean.serviceModelUpdate(printPartParameterExtDb, printPartParameterExt.getModifyUser());
        return printPartParameterExtRepository.save(printPartParameterExtDb);
    }

    @Override
    public int updatePrintPartParameterExtValidById(Long id, Integer 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(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("启用，禁用枚举状态值[%s]未被定义", status)
                    .build();
        }
        return printPartParameterExtRepository.updateByProperties(new String[]{MesExtConstWords.ID}, new Object[]{id},
                new String[]{MesExtConstWords.IS_VALID, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME, MesExtConstWords.SYSTEM_SYNC_STATUS},
                new Object[]{status, userName, TimeTool.getNowTime(true), MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue()});
    }
}
