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

import cn.estsh.i3plus.ext.mes.api.base.IFourMChangeService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesFourMChange;
import cn.estsh.i3plus.ext.mes.pojo.model.FourMChangeModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesFourMChangeRepository;
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.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 FourMChangeService implements IFourMChangeService {

    @Autowired
    private MesFourMChangeRepository fourMChangeRepository;

    /**
     * 新增
     *
     * @param fourMChange
     * @return
     */
    @Override
    public MesFourMChange insert(MesFourMChange fourMChange) {
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(AuthUtil.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(fourMChange.getWorkCellCode(), MesExtConstWords.WORK_CELL_CODE, ddlPackBean);
        DdlPreparedPack.getStringEqualPack(fourMChange.getWorkCenterCode(), MesExtConstWords.WORK_CENTER_CODE, ddlPackBean);
        DdlPreparedPack.getNumEqualPack(fourMChange.getManageType(), MesExtConstWords.MANAGE_TYPE, ddlPackBean);
        MesFourMChange mesEquipmentCheck = fourMChangeRepository.getByProperty(ddlPackBean);
        if (!ObjectUtils.isEmpty(mesEquipmentCheck)) {
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode(), "数据已存在");
        }

        fourMChange.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
        ConvertBean.serviceModelInitialize(fourMChange, AuthUtil.getSessionUser().getUserName());
        return fourMChangeRepository.insert(fourMChange);
    }

    /**
     * 修改
     *
     * @param fourMChange
     * @return
     */
    @Override
    public MesFourMChange update(MesFourMChange fourMChange) {
        MesFourMChange fourMChange2 = fourMChangeRepository.getById(fourMChange.getId());
        if (ObjectUtils.isEmpty(fourMChange2)) {
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(), "数据不存在");
        }

        fourMChange2.setBeforeContent(fourMChange.getBeforeContent());
        fourMChange2.setAfterContent(fourMChange.getAfterContent());
        ConvertBean.serviceModelUpdate(fourMChange, AuthUtil.getSessionUser().getUserName());
        return fourMChangeRepository.save(fourMChange2);
    }

    /**
     * 查询
     *
     * @param fourMChange
     * @param pager
     * @return
     */
    @Override
    public ListPager<FourMChangeModel> queryByPager(MesFourMChange fourMChange, Pager pager) {
        List<FourMChangeModel> modelList = new ArrayList<>();
        DdlPackBean packBean = MesHqlPack.getMesAllData(AuthUtil.getOrganizeCode());
        DdlPreparedPack.getNumEqualPack(fourMChange.getIsValid(), MesExtConstWords.IS_VALID, packBean);
        DdlPreparedPack.getStringLikerPack(fourMChange.getWorkCellCode(), MesExtConstWords.WORK_CELL_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(fourMChange.getWorkCenterCode(), MesExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getNumEqualPack(fourMChange.getManageType(), MesExtConstWords.MANAGE_TYPE, packBean);
        pager = PagerHelper.getPager(pager, fourMChangeRepository.findByHqlWhereCount(packBean));
        List<MesFourMChange> fourMChangeList = fourMChangeRepository.findByHqlWherePage(packBean, pager);
        if (!CollectionUtils.isEmpty(fourMChangeList)) {
            for (MesFourMChange item : fourMChangeList) {
                if (null == item) {
                    continue;
                }
                FourMChangeModel model = new FourMChangeModel();
                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、判断单据是否存在
        MesFourMChange fourMChange = fourMChangeRepository.getById(id);
        if (ObjectUtils.isEmpty(fourMChange)) {
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(), "自动配置不存在");
        }

        return fourMChangeRepository.updateByProperties(new String[]{MesExtConstWords.ID}, new Object[]{id},
                new String[]{MesExtConstWords.IS_VALID, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME},
                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();
    }
}
