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

import cn.estsh.i3plus.ext.mes.api.base.ep.IEpEquipmentService;
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.enumutil.MesEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesEquipment;
import cn.estsh.i3plus.pojo.mes.repository.MesEquipmentRepository;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2021-12-13 15:27
 */
@Service
public class EpEquipmentService implements IEpEquipmentService {
    @Autowired
    private MesEquipmentRepository equipmentRepository;

    @Override
    public MesEquipment insert(MesEquipment mesEquipment) {
        MesEquipment equipment = equipmentRepository.getByProperty(
                new String[]{MesExtConstWords.EQUIPMENT_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID},
                new Object[]{mesEquipment.getEquipmentCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue()});
        if(equipment != null){
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(), "已存在相同的设备CODE");
        }

        ConvertBean.modelInitialize(mesEquipment, AuthUtil.getSessionUser());
        mesEquipment.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
        return equipmentRepository.insert(mesEquipment);
    }

    @Override
    public MesEquipment update(MesEquipment epEquipmentCheck) {
        //1、判断设备点检项目是否存在
        Long id = epEquipmentCheck.getId();
        MesEquipment equipmentMeta = equipmentRepository.getById(id);
        if(ObjectUtils.isEmpty(equipmentMeta)){
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(), "自动配置不存在");
        }

        //2、复制Bean
        //BeanUtils.copyProperties(epEquipmentCheck,equipment);
        equipmentMeta.setMemo(epEquipmentCheck.getMemo());
        equipmentMeta.setEquipmentName(epEquipmentCheck.getEquipmentName());
        ConvertBean.modelUpdate(equipmentMeta,AuthUtil.getSessionUser());
        return equipmentRepository.save(equipmentMeta);
    }

    @Override
    public ListPager queryByPager(MesEquipment equipment, Pager pager) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(AuthUtil.getOrganizeCode());
        DdlPreparedPack.getStringRightLikerPack(equipment.getEquipmentCode(), MesExtConstWords.EQUIPMENT_CODE,packBean);
        DdlPreparedPack.getStringRightLikerPack(equipment.getEquipmentName(), MesExtConstWords.EQUIPMENT_NAME,packBean);
        DdlPreparedPack.getNumEqualPack(equipment.getIsValid(),MesExtConstWords.IS_VALID,packBean);

        pager = PagerHelper.getPager(pager, equipmentRepository.findByHqlWhereCount(packBean));
        return new ListPager(equipmentRepository.findByHqlWherePage(packBean, pager), pager);
    }

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

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