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

import cn.estsh.i3plus.ext.mes.api.base.IWorkCenterNotificationBarService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWorkCenterNotificationBar;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWorkCenterNotificationBar;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesWorkCenterNotificationBarRepository;
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.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.sqlpack.MesHqlPack;
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;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/01/17 17:59 下午
 * @Description:
 **/
@Service
public class WorkCenterNotificationBarService implements IWorkCenterNotificationBarService {

    @Autowired
    private MesWorkCenterNotificationBarRepository mesWorkCenterNotificationBarRDao;

    @Override
    public MesWorkCenterNotificationBar insertWorkCenterNotificationBar(MesWorkCenterNotificationBar workCenterNotificationBar) {
        ConvertBean.saveOrUpdate(workCenterNotificationBar, AuthUtil.getSessionUser().getUserName());
        workCenterNotificationBar.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
        MesWorkCenterNotificationBar workCenterNotificationBarDb = mesWorkCenterNotificationBarRDao.getByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.AREA_CODE, MesExtConstWords.WORK_CENTER_CODE},
                new Object[]{workCenterNotificationBar.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), workCenterNotificationBar.getAreaCode(), workCenterNotificationBar.getWorkCenterCode()});
        if (null != workCenterNotificationBarDb) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("已存在区域【%s】、产线【%s】的公告记录",
                            workCenterNotificationBar.getAreaCode(), workCenterNotificationBar.getWorkCenterCode())
                    .build();
        }
        return mesWorkCenterNotificationBarRDao.insert(workCenterNotificationBar);
    }

    @Override
    public MesWorkCenterNotificationBar updateWorkCenterNotificationBar(MesWorkCenterNotificationBar workCenterNotificationBar) {
        MesWorkCenterNotificationBar workCenterNotificationBarDb2 = mesWorkCenterNotificationBarRDao.getByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.AREA_CODE, MesExtConstWords.WORK_CENTER_CODE},
                new Object[]{workCenterNotificationBar.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), workCenterNotificationBar.getAreaCode(), workCenterNotificationBar.getWorkCenterCode()});
        if (null != workCenterNotificationBarDb2 && workCenterNotificationBarDb2.getId().compareTo(workCenterNotificationBar.getId()) != 0) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("已存在区域【%s】、产线【%s】的公告记录",
                            workCenterNotificationBar.getAreaCode(), workCenterNotificationBar.getWorkCenterCode())
                    .build();
        }
        MesWorkCenterNotificationBar workCenterNotificationBarDb = mesWorkCenterNotificationBarRDao.getById(workCenterNotificationBar.getId());
        if (null == workCenterNotificationBarDb) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("ID【%s】数据不存在", workCenterNotificationBar.getId())
                    .build();
        }
        workCenterNotificationBarDb.setAreaCode(workCenterNotificationBar.getAreaCode());
        workCenterNotificationBarDb.setWorkCenterCode(workCenterNotificationBar.getWorkCenterCode());
        workCenterNotificationBarDb.setContent(workCenterNotificationBar.getContent());
        ConvertBean.serviceModelUpdate(workCenterNotificationBarDb, AuthUtil.getSessionUser().getUserName());
        return mesWorkCenterNotificationBarRDao.save(workCenterNotificationBarDb);
    }


    @Override
    public ListPager<MesWorkCenterNotificationBar> queryWorkCenterNotificationBarByPager(MesWorkCenterNotificationBar workCenterNotificationBar, Pager pager) {
        DdlPackBean packBean = MesHqlPack.getMesAllData(AuthUtil.getOrganize().getOrganizeCode());
        DdlPreparedPack.getNumEqualPack(workCenterNotificationBar.getIsValid(), MesExtConstWords.IS_VALID, packBean);
        DdlPreparedPack.getStringEqualPack(workCenterNotificationBar.getAreaCode(), MesExtConstWords.AREA_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(workCenterNotificationBar.getWorkCenterCode(), MesExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(workCenterNotificationBar.getContent(), MesExtConstWords.CONTENT, packBean);
        pager = PagerHelper.getPager(pager, mesWorkCenterNotificationBarRDao.findByHqlWhereCount(packBean));
        packBean.setOrderByStr(workCenterNotificationBar.orderBy());
        return new ListPager(mesWorkCenterNotificationBarRDao.findByHqlWherePage(packBean, pager), pager);
    }

    @Override
    public int updateStatusWorkCenterNotificationBar(long id, Integer status) {
        MesWorkCenterNotificationBar workCenterNotificationBarDb = mesWorkCenterNotificationBarRDao.getById(id);
        if(null == workCenterNotificationBarDb){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("该产线公告不存在")
                    .build();
        }
        return mesWorkCenterNotificationBarRDao.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)});
    }
}
