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

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

import java.util.List;

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

    @Autowired
    private MesPrinterCfgRepository printerCfgRepository;

    @Override
    public ListPager<MesPrinterCfg> queryPrinterCfgByPager(MesPrinterCfg printerCfg, Pager pager) {
        DdlPackBean hqlPack = MesExtHqlPack.getPrinterCfg(printerCfg);
        pager = PagerHelper.getPager(pager, printerCfgRepository.findByHqlWhereCount(hqlPack));
        hqlPack.setOrderByStr(printerCfg.orderBy());
        List<MesPrinterCfg> printerCfgList = printerCfgRepository.findByHqlWherePage(hqlPack, pager);
        return new ListPager(printerCfgList, pager);
    }

    @Override
    public MesPrinterCfg insertPrinterCfg(MesPrinterCfg printerCfg) {
        DdlPackBean packBean = MesExtHqlPack.getAllBaseData(printerCfg.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(printerCfg.getPrinterNo(), MesExtConstWords.PRINTER_NO, packBean);
        MesPrinterCfg printerCfgDb = printerCfgRepository.getByProperty(packBean);
        if (null != printerCfgDb) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("打印机编号[%s]记录已存在", printerCfg.getPrinterNo())
                    .build();
        }
        ConvertBean.serviceModelInitialize(printerCfg, printerCfg.getCreateUser());
        return printerCfgRepository.insert(printerCfg);
    }

    @Override
    public MesPrinterCfg updatePrinterCfg(MesPrinterCfg printerCfg) {
        MesPrinterCfg printerCfgDb = printerCfgRepository.getById(printerCfg.getId());
        if (printerCfgDb == null) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("打印机编号[%s]记录不存在", printerCfg.getPrinterNo())
                    .build();
        }

        MesPrinterCfg printerCfgExist = printerCfgRepository.getByProperty(
                new String[]{MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID, MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.PRINTER_NO},
                new Object[]{CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), printerCfg.getOrganizeCode(), printerCfg.getPrinterNo()});

        if(printerCfgExist != null && printerCfgExist.getId().compareTo(printerCfg.getId()) != 0){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("打印机编号[%s]已存在", printerCfg.getPrinterNo())
                    .build();
        }
        printerCfgDb.setPrinterNo(printerCfg.getPrinterNo());
        printerCfgDb.setPrinterName(printerCfg.getPrinterName());
        printerCfgDb.setIp(printerCfg.getIp());
        printerCfgDb.setPort(printerCfg.getPort());
        printerCfgDb.setMemo(printerCfg.getMemo());
        printerCfgDb.setPosition(printerCfg.getPosition());
        ConvertBean.serviceModelUpdate(printerCfgDb, printerCfg.getModifyUser());
        return printerCfgRepository.save(printerCfgDb);
    }

    @Override
    public int updatePrinterCfgValidById(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();
        }
        MesPrinterCfg printerCfgDb = printerCfgRepository.getById(id);
        if (printerCfgDb == null || CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == printerCfgDb.getIsDeleted()) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("ID[%s]记录不存在", id)
                    .build();
        }
        return printerCfgRepository.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)});
    }

}
