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

import cn.estsh.i3plus.ext.mes.api.base.IFourMConfirmService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesFourMConfirm;
import cn.estsh.i3plus.ext.mes.pojo.model.FourMConfirmModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesFourMConfirmRepository;
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.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 org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yumingxing
 * @version 1.0
 * @date 2021/2/2 15:19
 **/
@Service
public class FourMConfirmService implements IFourMConfirmService {

    @Autowired
    private MesFourMConfirmRepository fourMConfirmRepository;

    /**
     * 新增
     *
     * @param fourMConfirm
     * @return
     */
    @Override
    public MesFourMConfirm insert(MesFourMConfirm fourMConfirm) {
        ConvertBean.modelInitialize(fourMConfirm, AuthUtil.getSessionUser());
        fourMConfirm.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());

        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(AuthUtil.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(fourMConfirm.getConfirmCode(), "confirmCode", ddlPackBean);
        MesFourMConfirm mesEquipmentCheck = fourMConfirmRepository.getByProperty(ddlPackBean);
        if (!ObjectUtils.isEmpty(mesEquipmentCheck)) {
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode(), "数据已存在");
        }

        return fourMConfirmRepository.insert(fourMConfirm);
    }

    /**
     * 修改
     *
     * @param fourMConfirm
     * @return
     */
    @Override
    public MesFourMConfirm update(MesFourMConfirm fourMConfirm) {
        //1、判断单据是否存在
        Long id = fourMConfirm.getId();
        MesFourMConfirm fourMConfirm2 = fourMConfirmRepository.getById(id);
        if (ObjectUtils.isEmpty(fourMConfirm2)) {
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(), "自动配置不存在");
        }

        if (!fourMConfirm.getConfirmCode().equals(fourMConfirm2.getConfirmCode())) {
            DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(AuthUtil.getOrganizeCode());
            DdlPreparedPack.getStringEqualPack(fourMConfirm.getConfirmCode(), "confirmCode", ddlPackBean);
            DdlPreparedPack.getNumEqualPack(fourMConfirm.getManageType(), "manageType", ddlPackBean);
            MesFourMConfirm mesEquipmentCheck = fourMConfirmRepository.getByProperty(ddlPackBean);
            if (!ObjectUtils.isEmpty(mesEquipmentCheck)) {
                throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode(), "数据已存在");
            }
        }
        fourMConfirm2.setManageType(fourMConfirm.getManageType());
        fourMConfirm2.setConfirmCode(fourMConfirm.getConfirmCode());
        fourMConfirm2.setConfirmContent(fourMConfirm.getConfirmContent());
        ConvertBean.serviceModelUpdate(fourMConfirm2, AuthUtil.getSessionUser().getUserName());
        return fourMConfirmRepository.save(fourMConfirm2);
    }

    /**
     * 查询
     *
     * @param fourMConfirm
     * @param pager
     * @return
     */
    @Override
    public ListPager<FourMConfirmModel> queryByPager(MesFourMConfirm fourMConfirm, Pager pager) {
        List<FourMConfirmModel> modelList = new ArrayList<>();
        DdlPackBean packBean = MesHqlPack.getMesAllData(AuthUtil.getOrganizeCode());
        DdlPreparedPack.getStringLikerPack(fourMConfirm.getConfirmCode(), "confirmCode", packBean);
        DdlPreparedPack.getNumEqualPack(fourMConfirm.getIsValid(), "isValid", packBean);
        DdlPreparedPack.getNumEqualPack(fourMConfirm.getManageType(), "manageType", packBean);
        pager = PagerHelper.getPager(pager, fourMConfirmRepository.findByHqlWhereCount(packBean));
        List<MesFourMConfirm> fourMConfirmList = fourMConfirmRepository.findByHqlWherePage(packBean, pager);
        if (!CollectionUtils.isEmpty(fourMConfirmList)) {
            for (MesFourMConfirm item : fourMConfirmList) {
                if (null == item) {
                    continue;
                }
                FourMConfirmModel model = new FourMConfirmModel();
                BeanUtils.copyProperties(item, model);
                model.setManageTypeName(MesExtEnumUtil.MANAGE_TYPE.valueOfDescription(model.getManageType()));
                modelList.add(model);
            }
        }
        return new ListPager(modelList, pager);
    }

    /**
     * 根据
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public int updateIsValidById(long id, Integer status) {
        //1、判断单据是否存在
        MesFourMConfirm fourMConfirm = fourMConfirmRepository.getById(id);
        if (ObjectUtils.isEmpty(fourMConfirm)) {
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(), "自动配置不存在");
        }

        return fourMConfirmRepository.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();
    }
}
