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

import cn.estsh.i3plus.ext.mes.api.base.IWeighService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWeigh;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesWeighRepository;
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.platform.common.util.MesConstWords;
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.i3plus.pojo.mes.sqlpack.MesHqlPack;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @author yumingxing
 * @version 1.0
 * @date 2021/2/7 14:20
 **/
@Service
@Slf4j
public class WeighService implements IWeighService {

    @Autowired
    private MesWeighRepository weighRepository;

    /**
     * 新增
     *
     * @param weigh
     * @return
     */
    @Override
    public MesWeigh insert(MesWeigh weigh) {
        String partNo = weigh.getPartNo();
        MesWeigh byProperty = weighRepository.getByProperty("partNo", partNo);
        if (!ObjectUtils.isEmpty(byProperty)) {
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode(), "数据已存在");
        }
        ConvertBean.modelInitialize(weigh, AuthUtil.getSessionUser());
        weigh.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
        return weighRepository.insert(weigh);
    }

    /**
     * 修改
     *
     * @param weigh
     * @return
     */
    @Override
    public MesWeigh update(MesWeigh weigh) {
        //1、判断称重物料是否存在
        String partNo = weigh.getPartNo();

        Long id = weigh.getId();
        MesWeigh weigh2 = weighRepository.getById(id);
        if (ObjectUtils.isEmpty(weigh2)) {
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(), "称重物料配置不存在");
        }
        if (!weigh2.getPartNo().equals(partNo)) {
            MesWeigh byProperty = weighRepository.getByProperty("partNo", partNo);
            if (!ObjectUtils.isEmpty(byProperty)) {
                throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode(), "数据已存在");
            }
        }

        //2、修改
        BeanUtils.copyProperties(weigh, weigh2);
        ConvertBean.modelUpdate(weigh2, AuthUtil.getSessionUser());
        return weighRepository.save(weigh2);
    }

    /**
     * 查询
     *
     * @param weigh
     * @param pager
     * @return
     */
    @Override
    public ListPager<MesWeigh> queryByPager(MesWeigh weigh, Pager pager) {
        DdlPackBean packBean = MesHqlPack.getMesAllData(AuthUtil.getOrganizeCode());
        DdlPreparedPack.getNumEqualPack(weigh.getIsValid(), "isValid", packBean);
        DdlPreparedPack.getStringEqualPack(weigh.getPartNo(), "partNo", packBean);
        DdlPreparedPack.getStringLikerPack(weigh.getPartName(), "partName", packBean);
        pager = PagerHelper.getPager(pager, weighRepository.findByHqlWhereCount(packBean));
        return new ListPager(weighRepository.findByHqlWherePage(packBean, pager), pager);
    }

    /**
     * 禁用/启用
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public int updateIsValidById(long id, Integer status) {
        //1、判断单据是否存在
        MesWeigh weigh = weighRepository.getById(id);
        if (ObjectUtils.isEmpty(weigh)) {
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(), "称重物料配置不存在");
        }

        return weighRepository.updateByProperties(new String[]{"id"}, new Object[]{id},
                new String[]{MesConstWords.IS_VALID, "modifyUser", "modifyDatetime"},
                new Object[]{status, AuthUtil.getSessionUser().getUserName(), TimeTool.getNowTime(true)});
    }



    /**
     * 异常
     *
     * @param softTypeCode          softTypeCode
     * @param errorDetailString     错误消息字符串
     * @param imppExceptionEnumCode 异常代码
     * @param args                  参数
     */
    private void throwImppException(String softTypeCode, String errorDetailString, String imppExceptionEnumCode, Object... args) {
        throw ImppExceptionBuilder.newInstance()
                .setSystemID(softTypeCode)
                .setErrorCode(imppExceptionEnumCode)
                .setErrorDetail(errorDetailString, args)
                .build();
    }
}
