package cn.estsh.i3plus.core.apiservice.serviceimpl.busi;

import cn.estsh.i3plus.core.api.iservice.busi.ISysBarcodeRuleService;
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.platform.bean.SysBarcodeRule;
import cn.estsh.i3plus.pojo.platform.repository.SysBarcodeRuleRepository;
import cn.estsh.i3plus.pojo.platform.sqlpack.CoreHqlPack;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.exception.ImppExceptionEnum;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description : 条码规则服务
 * @Reference :
 * @Author : yunhao
 * @CreateDate : 2019-03-12 16:13
 * @Modify:
 **/
@Service
public class SysBarcodeRuleService implements ISysBarcodeRuleService {
    public static final Logger LOGGER = LoggerFactory.getLogger(SysBarcodeRuleService.class);

    @Autowired
    private SysBarcodeRuleRepository sysBarcodeRuleRDao;

    @Override
    @ApiOperation(value = "新增条码规则信息")
    public SysBarcodeRule insertSysBarcodeRule(SysBarcodeRule barcodeRule) {
        // 数据校验
        if (sysBarcodeRuleRDao.isExitByProperty("barcodeRuleCode",barcodeRule.getBarcodeRuleCode())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("规则代码已存在")
                    .setErrorSolution("请重新输入规则代码")
                    .build();
        }

        return sysBarcodeRuleRDao.insert(barcodeRule);
    }

    @Override
    @ApiOperation(value = "更新条码规则信息")
    public void updateSysBarcodeRule(SysBarcodeRule barcodeRule) {
        List list = sysBarcodeRuleRDao.findByHqlWhere(CoreHqlPack.packHqlSysBarcodeRuleCode(barcodeRule));
        if (list.size() > 0) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("规则代码已存在")
                    .setErrorSolution("请重新输入规则代码")
                    .build();
        }

        sysBarcodeRuleRDao.update(barcodeRule);
    }

    @Override
    @ApiOperation(value = "查询所有条码规则信息",notes = "查询所有条码规则信息")
    public List<SysBarcodeRule> listSysBarcodeRule() {
        return sysBarcodeRuleRDao.list();
    }

    @Override
    @ApiOperation(value = "查询条码规则信息",notes = "根据ID查询条码规则信息")
    public SysBarcodeRule getSysBarcodeRuleById(Long id) {
        return sysBarcodeRuleRDao.getById(id);
    }

    @Override
    @ApiOperation(value = "根据id删除条码规则",notes = "根据id删除条码规则")
    public void deleteSysBarcodeRuleById(Long id) {
        sysBarcodeRuleRDao.deleteById(id);
    }

    @Override
    @ApiOperation(value = "根据规则代码查询条码规则",notes = "根据规则代码查询条码规则")
    public SysBarcodeRule getSysBarcodeRuleByCode(String code) {
        return sysBarcodeRuleRDao.getByProperty("barcodeRuleCode",code);
    }

    @Override
    @ApiOperation(value = "根据规则代码查询条码规则",notes = "根据规则代码查询条码规则")
    public ListPager<SysBarcodeRule> querySysBarcodeRuleByPager(SysBarcodeRule barcodeRule, Pager pager) {
        if(barcodeRule == null) {
            pager = PagerHelper.getPager(pager, sysBarcodeRuleRDao.listCount());
            return new ListPager(sysBarcodeRuleRDao.listPager(pager),pager);
        }else {
            String hqlPack = CoreHqlPack.packHqlSysBarcodeRule(barcodeRule);
            pager = PagerHelper.getPager(pager, sysBarcodeRuleRDao.findByHqlWhereCount(hqlPack));
            return new ListPager(sysBarcodeRuleRDao.findByHqlWherePage(hqlPack + barcodeRule.orderBy(),pager),pager);
        }
    }
}
