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

import cn.estsh.i3plus.ext.mes.api.base.jx.IManageCodeService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesManageCode;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesManageCodeRepository;
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.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.base.tool.HqlPack;
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 java.util.List;
/**
 * @Author: xinwang.yi
 * @CreateDate: 2023/10/09 11:41 上午
 * @Description:
 **/
@Service
public class ManageCodeService implements IManageCodeService {

    @Autowired
    private MesManageCodeRepository mesManageCodeRDao;

    @Override
    public MesManageCode insertMesManageCode(MesManageCode mesManageCode) {
        //数据唯一性校验
        StringBuffer whereStr = new StringBuffer();

        // 封装唯一校验查询条件
        HqlPack.getStringEqualPack(mesManageCode.getOrganizeCode(), MesExtConstWords.ORGANIZE_CODE, whereStr);
        HqlPack.getStringEqualPack(mesManageCode.getManageCode(), MesExtConstWords.MANAGE_CODE, whereStr);

        if (mesManageCodeRDao.isExitByHqlWhere(whereStr.toString())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("管理码【%s】在管理码信息中已存在"
                            , mesManageCode.getManageCode())
                    .setErrorSolution("请更换新增参数")
                    .build();
        }

        ConvertBean.serviceModelInitialize(mesManageCode, AuthUtil.getSessionUser().getUserName());
        return mesManageCodeRDao.insert(mesManageCode);
    }

    @Override
    public void updateMesManageCode(MesManageCode mesManageCode, String userName) {
        MesManageCode manageCodeDb = mesManageCodeRDao.getById(mesManageCode.getId());

        if (null == manageCodeDb)
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("ID[%s]记录不存在", manageCodeDb.getId())
                    .build();

        // 封装唯一校验查询条件
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(mesManageCode.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(mesManageCode.getOrganizeCode(), MesExtConstWords.ORGANIZE_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(mesManageCode.getManageCode(), MesExtConstWords.MANAGE_CODE, packBean);
        MesManageCode manageCodeDbIsExist = mesManageCodeRDao.getByProperty(packBean);

        if (null != manageCodeDbIsExist && manageCodeDbIsExist.getId().compareTo(manageCodeDbIsExist.getId()) != 0) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("管理码【%s】在管理码信息启用或禁用数据中已存在"
                            , mesManageCode.getManageCode())
                    .setErrorSolution("请更换新增参数")
                    .build();
        }

        manageCodeDb.setManageCode(mesManageCode.getManageCode());
        manageCodeDb.setWorkCenterCode(mesManageCode.getWorkCenterCode());
        manageCodeDb.setWorkCenterName(mesManageCode.getWorkCenterName());
        manageCodeDb.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());

        ConvertBean.serviceModelUpdate(manageCodeDb, userName);
        mesManageCodeRDao.save(manageCodeDb);
    }

    @Override
    public ListPager<MesManageCode> queryMesManageCodeByPager(MesManageCode mesManageCode, Pager pager) {
        if (mesManageCode == null) {
            pager = PagerHelper.getPager(pager, mesManageCodeRDao.listCount());
            return new ListPager(mesManageCodeRDao.listPager(pager), pager);
        } else {

            DdlPackBean hqlPack = MesExtHqlPack.getMesManageCode(mesManageCode);
            hqlPack.setOrderByStr(mesManageCode.orderBy());
            pager = PagerHelper.getPager(pager, mesManageCodeRDao.findByHqlWhereCount(hqlPack));
            List<MesManageCode> byHqlWherePage = mesManageCodeRDao.findByHqlWherePage(hqlPack, pager);
            return new ListPager(byHqlWherePage, pager);
        }
    }

    @Override
    public int deleteWeaklyMesManageCodeByIds(String[] ids, String userName) {

        StringBuffer wheres = new StringBuffer();
        HqlPack.getInPack(String.join(",", ids), MesExtConstWords.ID, wheres);

        int num = mesManageCodeRDao.updateByHqlWhere(
                wheres.toString(),
                new String[]{MesExtConstWords.SYSTEM_SYNC_STATUS, MesExtConstWords.IS_VALID, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME},
                new Object[]{MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue(), CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), userName, TimeTool.getNowTime(true)}
        );
        return num;
    }

    @Override
    public int updateMesManageCodeStatus(String[] ids, int status, String userName) {
        StringBuffer wheres = new StringBuffer();
        HqlPack.getInPack(String.join(",", ids), MesExtConstWords.ID, wheres);

        int num = mesManageCodeRDao.updateByHqlWhere(
                wheres.toString(),
                new String[]{MesExtConstWords.SYSTEM_SYNC_STATUS, MesExtConstWords.IS_VALID, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME},
                new Object[]{MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue(), status, userName, TimeTool.getNowTime(true)}
        );
        return num;
    }
}
