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

import cn.estsh.i3plus.ext.mes.api.base.IAntiStaticCheckService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesAntiStaticCheck;
import cn.estsh.i3plus.ext.mes.pojo.model.AntiStaticCheckModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesAntiStaticCheckRepository;
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.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.repository.MesWorkCellRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesWorkCenterRepository;
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/5 15:42
 **/
@Service
public class AntiStaticCheckService implements IAntiStaticCheckService {
    @Autowired
    private MesAntiStaticCheckRepository antiStaticCheckRepository;
    @Autowired
    private MesWorkCenterRepository workCenterRepository;
    @Autowired
    private MesWorkCellRepository workCellRepository;

    /**
     * 新增
     *
     * @param equipmentCheck
     * @return
     */
    @Override
    public MesAntiStaticCheck insert(MesAntiStaticCheck equipmentCheck) {

        ConvertBean.modelInitialize(equipmentCheck, AuthUtil.getSessionUser());
        equipmentCheck.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());

        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(AuthUtil.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getWorkCellCode(), "workCellCode", ddlPackBean);
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getWorkCenterCode(), "workCenterCode", ddlPackBean);
        MesAntiStaticCheck antiStaticCheck = antiStaticCheckRepository.getByProperty(ddlPackBean);
        if (!ObjectUtils.isEmpty(antiStaticCheck)) {
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode(), "数据已存在");
        }

        return antiStaticCheckRepository.insert(equipmentCheck);
    }

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

        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(AuthUtil.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getWorkCellCode(), MesExtConstWords.WORK_CELL_CODE, ddlPackBean);
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getWorkCenterCode(), MesExtConstWords.WORK_CENTER_CODE, ddlPackBean);
        MesAntiStaticCheck antiStaticCheck = antiStaticCheckRepository.getByProperty(ddlPackBean);

        if (!ObjectUtils.isEmpty(antiStaticCheck) && antiStaticCheck.getId().compareTo(id) != 0) {
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode(), "数据已存在");
        }
        equipmentCheckDb.setWorkCenterCode(equipmentCheck.getWorkCenterCode());
        equipmentCheckDb.setWorkCellCode(equipmentCheck.getWorkCellCode());
        equipmentCheckDb.setWorkCellName(equipmentCheck.getWorkCellName());
        ConvertBean.serviceModelUpdate(equipmentCheckDb, AuthUtil.getSessionUser().getUserName());
        return antiStaticCheckRepository.save(equipmentCheckDb);
    }

    /**
     * 查询
     * @param equipmentCheck
     * @param pager
     * @return
     */
    @Override
    public ListPager<AntiStaticCheckModel> queryByPager(MesAntiStaticCheck equipmentCheck, Pager pager) {
        List<AntiStaticCheckModel> resultList = new ArrayList<>();
        DdlPackBean packBean = MesHqlPack.getMesAllData(AuthUtil.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getWorkCellCode(), MesExtConstWords.WORK_CELL_CODE, packBean);
        DdlPreparedPack.getNumEqualPack(equipmentCheck.getIsValid(), MesExtConstWords.IS_VALID, packBean);
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getWorkCenterCode(), MesExtConstWords.WORK_CENTER_CODE, packBean);
        pager = PagerHelper.getPager(pager, antiStaticCheckRepository.findByHqlWhereCount(packBean));
        List<MesAntiStaticCheck> antiStaticCheckList = antiStaticCheckRepository.findByHqlWherePage(packBean, pager);
        if (!CollectionUtils.isEmpty(antiStaticCheckList)) {
            for (MesAntiStaticCheck item : antiStaticCheckList) {
                if (null != item) {
                    AntiStaticCheckModel result = new AntiStaticCheckModel();
                    BeanUtils.copyProperties(item, result);
                    resultList.add(result);
                }
            }
        }
        return new ListPager(resultList, pager);
    }

    /**
     * 根据
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public int updateIsValidById(long id, Integer status) {
        //1、判断单据是否存在
        MesAntiStaticCheck equipmentCheck = antiStaticCheckRepository.getById(id);
        if (ObjectUtils.isEmpty(equipmentCheck)) {
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(), "自动配置不存在");
        }
        if (CommonEnumUtil.IS_VAILD.VAILD.getValue() == status) {
            DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(equipmentCheck.getOrganizeCode());
            DdlPreparedPack.getStringEqualPack(equipmentCheck.getWorkCellCode(), MesExtConstWords.WORK_CELL_CODE, ddlPackBean);
            DdlPreparedPack.getStringEqualPack(equipmentCheck.getWorkCenterCode(), MesExtConstWords.WORK_CENTER_CODE, ddlPackBean);
            MesAntiStaticCheck antiStaticCheckDb = antiStaticCheckRepository.getByProperty(ddlPackBean);
            if (null != antiStaticCheckDb && antiStaticCheckDb.getId().compareTo(id) != 0) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                        .setErrorDetail("生产线[%s]工位[%s]已经存在有效的数据, 启用失败", equipmentCheck.getWorkCenterCode(), equipmentCheck.getWorkCellCode())
                        .build();
            }
        }
        return antiStaticCheckRepository.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();
    }
}
