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

import cn.estsh.i3plus.ext.mes.api.base.IWorkCenterContainerLocationService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWorkCenterContainerLocation;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesWorkCenterContainerLocationRepository;
import cn.estsh.i3plus.ext.mes.pojo.sqlpack.MesExtHqlPack;
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.impp.framework.boot.exception.ImppExceptionBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Slf4j
@Service
public class WorkCenterContainerLocationService implements IWorkCenterContainerLocationService {

    @Autowired
    private MesWorkCenterContainerLocationRepository workCenterContainerLocationRepository;

    @Override
    public ListPager<MesWorkCenterContainerLocation> queryWorkCenterContainerLocationByPager(MesWorkCenterContainerLocation workCenterContainerLocation, Pager pager) {
        DdlPackBean hqlPack = MesExtHqlPack.getWorkCenterContainerLocation(workCenterContainerLocation);
        pager = PagerHelper.getPager(pager, workCenterContainerLocationRepository.findByHqlWhereCount(hqlPack));
        hqlPack.setOrderByStr(workCenterContainerLocation.orderBy());
        return new ListPager(workCenterContainerLocationRepository.findByHqlWherePage(hqlPack, pager), pager);
    }

    @Override
    public MesWorkCenterContainerLocation insertWorkCenterContainerLocation(MesWorkCenterContainerLocation workCenterContainerLocation) {
        MesWorkCenterContainerLocation workCenterContainerLocationDb1 = workCenterContainerLocationRepository.getByProperty(
                new String[]{MesExtConstWords.IS_DELETED, MesExtConstWords.CT_LOCATION_NO},
                new Object[]{CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), workCenterContainerLocation.getCtLocationNo()});
        if (null != workCenterContainerLocationDb1) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("容器位置编号[%s]记录已存在", workCenterContainerLocation.getCtLocationNo())
                    .build();
        }
        MesWorkCenterContainerLocation workCenterContainerLocationDb2 = workCenterContainerLocationRepository.getByProperty(
                new String[]{MesExtConstWords.IS_DELETED, MesExtConstWords.WORK_CENTER_CODE, MesExtConstWords.LOCATION_TYPE},
                new Object[]{CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), workCenterContainerLocation.getWorkCenterCode(), workCenterContainerLocation.getLocationType()});
        if (null != workCenterContainerLocationDb2) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("生产线[%s][%s]记录已存在",
                            workCenterContainerLocation.getWorkCenterCode(), MesExtEnumUtil.WORK_CENTER_CT_LOCATION_TYPE.valueOfDescription(workCenterContainerLocation.getLocationType()))
                    .build();
        }
        ConvertBean.serviceModelInitialize(workCenterContainerLocation, workCenterContainerLocation.getCreateUser());
        return workCenterContainerLocationRepository.insert(workCenterContainerLocation);
    }

    @Override
    public MesWorkCenterContainerLocation updateWorkCenterContainerLocation(MesWorkCenterContainerLocation workCenterContainerLocation) {
        MesWorkCenterContainerLocation workCenterContainerLocationDb = workCenterContainerLocationRepository.getByProperty(
                new String[]{MesExtConstWords.IS_DELETED, MesExtConstWords.CT_LOCATION_NO},
                new Object[]{CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), workCenterContainerLocation.getCtLocationNo()});
        if (null != workCenterContainerLocationDb && workCenterContainerLocationDb.getId().compareTo(workCenterContainerLocation.getId()) != 0) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("容器位置编号[%s]记录已存在", workCenterContainerLocation.getCtLocationNo())
                    .build();
        }
        workCenterContainerLocationDb = workCenterContainerLocationRepository.getById(workCenterContainerLocation.getId());
        if (null == workCenterContainerLocationDb) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("ID[%s]记录不存在", workCenterContainerLocation.getId())
                    .build();
        }
        workCenterContainerLocationDb.setCtLocationNo(workCenterContainerLocation.getCtLocationNo());
        workCenterContainerLocationDb.setLocationDesc(workCenterContainerLocation.getLocationDesc());
        ConvertBean.serviceModelUpdate(workCenterContainerLocationDb, workCenterContainerLocation.getModifyUser());
        return workCenterContainerLocationRepository.save(workCenterContainerLocationDb);
    }

    @Override
    public int updateWorkCenterContainerLocationValidById(Long id, Integer status, String organizeCode, String userName) {
        if (status != CommonEnumUtil.IS_VAILD.INVAILD.getValue() && status != CommonEnumUtil.IS_VAILD.VAILD.getValue()) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("启用,禁用枚举状态值[%s]未被定义", status)
                    .build();
        }
        return workCenterContainerLocationRepository.updateByProperties(new String[]{MesExtConstWords.ID}, new Object[]{id},
                new String[]{MesExtConstWords.IS_VALID, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME},
                new Object[]{status, userName, TimeTool.getNowTime(true)});
    }
}
