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

import cn.estsh.i3plus.ext.mes.api.base.jx.IJxBadnessClassifyDetailService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesBadnessClassifyDetail;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesBadnessClassifyDetailRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesBadnessClassifyRepository;
import cn.estsh.i3plus.ext.mes.pojo.sqlpack.MesExtHqlPack;
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.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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @PROJECT_NAME: i3plus-qms-panasonic
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2022-06-07 10:45
 */
@Service
public class JxBadnessClassifyDetailService implements IJxBadnessClassifyDetailService {
    @Autowired
    private MesBadnessClassifyDetailRepository badnessClassifyDetailDao;

    @Autowired
    private MesBadnessClassifyRepository badnessClassifyDao;

    @Override
    public ListPager<MesBadnessClassifyDetail> queryBadnessClassifyDetailPager(MesBadnessClassifyDetail badnessClassifyDetail, Pager pager) {
        DdlPackBean packBean = MesExtHqlPack.getSxBadnessClassifyDetail(badnessClassifyDetail);
        pager = PagerHelper.getPager(pager, badnessClassifyDetailDao.findByHqlWhereCount(packBean));
        packBean.setOrderByStr(" order by modifyDatetime desc ");
        List<MesBadnessClassifyDetail> sxQmsBadnessClassifyDetails = badnessClassifyDetailDao.findByHqlWherePage(packBean, pager);
        return new ListPager(sxQmsBadnessClassifyDetails, pager);
    }

    @Override
    public void insertBadnessClassifyDetail(MesBadnessClassifyDetail sxQmsBadnessClassifyDetail, String userName) {
        MesBadnessClassifyDetail sxQmsBadnessClassifyDetailDb = badnessClassifyDetailDao.getByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.BADNESS_CLASSIFY_CODE, MesExtConstWords.BADNESS_CLASSIFY_DETAIL_CODE},
                new Object[]{sxQmsBadnessClassifyDetail.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), sxQmsBadnessClassifyDetail.getBadnessClassifyCode(), sxQmsBadnessClassifyDetail.getBadnessClassifyDetailCode()});

        if (null != sxQmsBadnessClassifyDetailDb) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.QMS.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("根据[不良分类编码,不良分类明细编码]查询已经存在此条数据,请勿重复新增")
                    .build();
        }

        ConvertBean.serviceModelInitialize(sxQmsBadnessClassifyDetail, userName);
        badnessClassifyDetailDao.insert(sxQmsBadnessClassifyDetail);
    }

    @Override
    public void updateBadnessClassifyDetail(MesBadnessClassifyDetail sxQmsBadnessClassifyDetail, String userName) {
        MesBadnessClassifyDetail badnessClassifyDb = badnessClassifyDetailDao.getById(sxQmsBadnessClassifyDetail.getId());
        if (null == badnessClassifyDb || CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == badnessClassifyDb.getIsDeleted()) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.QMS.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("ID[%s]数据不存在", sxQmsBadnessClassifyDetail.getId())
                    .build();
        }

        MesBadnessClassifyDetail badnessClassifyUpdateDb = badnessClassifyDetailDao.getByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.BADNESS_CLASSIFY_CODE, MesExtConstWords.BADNESS_CLASSIFY_DETAIL_CODE},
                new Object[]{sxQmsBadnessClassifyDetail.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), sxQmsBadnessClassifyDetail.getBadnessClassifyCode(),sxQmsBadnessClassifyDetail.getBadnessClassifyDetailCode()});

        if (null != badnessClassifyUpdateDb && badnessClassifyUpdateDb.getId().compareTo(sxQmsBadnessClassifyDetail.getId()) != 0) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.QMS.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("根据[不良分类编码,不良分类明细编码]查询已经存在此条数据,修改失败")
                    .build();
        }

        badnessClassifyDb.setBadnessClassifyCode(sxQmsBadnessClassifyDetail.getBadnessClassifyCode());
        badnessClassifyDb.setBadnessClassifyName(sxQmsBadnessClassifyDetail.getBadnessClassifyName());
        badnessClassifyDb.setBadnessClassifyDetailCode(sxQmsBadnessClassifyDetail.getBadnessClassifyDetailCode());
        badnessClassifyDb.setBadnessClassifyDetailName(sxQmsBadnessClassifyDetail.getBadnessClassifyDetailName());
        badnessClassifyDb.setOrganizeCode(sxQmsBadnessClassifyDetail.getOrganizeCode());
        ConvertBean.serviceModelUpdate(badnessClassifyDb, userName);
        badnessClassifyDetailDao.save(badnessClassifyDb);
    }

    @Override
    public void deleteBadnessClassifyDetail(Long ids[], String userName) {
        badnessClassifyDetailDao.deleteWeaklyByIds(ids, userName);
    }
}
