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

import cn.estsh.i3plus.core.api.iservice.busi.ISysOrderNoRuleService;
import cn.estsh.i3plus.core.apiservice.util.OrderNoMakeUtil;
import cn.estsh.i3plus.platform.common.tool.StringTool;
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.HqlPack;
import cn.estsh.i3plus.pojo.platform.bean.SysOrderNoRule;
import cn.estsh.i3plus.pojo.platform.repository.SysOrderNoRuleRepository;
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 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 : 2018-11-21 20:04
 * @Modify:
 **/
@Service
public class SysOrderNoRuleService implements ISysOrderNoRuleService {

    public static final Logger LOGGER = LoggerFactory.getLogger(SysOrderNoRuleService.class);

    @Autowired
    private SysOrderNoRuleRepository sysOrderNoRuleRDao;

    @Override
    public SysOrderNoRule insertSysOrderNoRule(SysOrderNoRule sysOrderNoRule) {
        if(sysOrderNoRuleRDao.isExitByProperty("orderNoRuleCode",sysOrderNoRule.getOrderNoRuleCode())){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("规则代码已存在")
                    .setErrorSolution("请重新输入规则代码")
                    .build();
        }
        return sysOrderNoRuleRDao.insert(sysOrderNoRule);
    }

    @Override
    public void updateSysOrderNoRule(SysOrderNoRule sysOrderNoRule) {
        // 校验唯一
        String hqlPack = CoreHqlPack.packHqlSysOrderNoRuleCode(sysOrderNoRule);
        long  repeatCount = sysOrderNoRuleRDao.findByHqlWhereCount(hqlPack);
        if (repeatCount > 0){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("规则代码已存在")
                    .setErrorSolution("请重新输入规则代码")
                    .build();
        }
        sysOrderNoRuleRDao.update(sysOrderNoRule);
    }

    @Override
    public void deleteSysOrderNoRuleById(Long id) {
        sysOrderNoRuleRDao.deleteById(id);
    }

    @Override
    public SysOrderNoRule getSysOrderNoRuleById(Long id) {
        return sysOrderNoRuleRDao.getById(id);
    }

    @Override
    public ListPager querySysOrderNoRuleByPager(SysOrderNoRule sysOrderNoRule, Pager pager) {
        if(sysOrderNoRule == null) {
            pager = PagerHelper.getPager(pager, sysOrderNoRuleRDao.listCount());
            return new ListPager(sysOrderNoRuleRDao.listPager(pager),pager);
        }else {
            String hqlPack = CoreHqlPack.packHqlSysOrderNoRule(sysOrderNoRule);
            pager = PagerHelper.getPager(pager, sysOrderNoRuleRDao.findByHqlWhereCount(hqlPack));
            return new ListPager(sysOrderNoRuleRDao.findByHqlWherePage(hqlPack + sysOrderNoRule.orderBy(),pager),pager);
        }
    }

    @Override
    public synchronized SysOrderNoRule getSysOrderNoRuleCode(String code) {
        SysOrderNoRule sysOrderNoRule = sysOrderNoRuleRDao.getByProperty("orderNoRuleCode",code);

        if (sysOrderNoRule == null) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("规则代码不存在存在")
                    .setErrorSolution("请重新输入规则代码")
                    .build();
        }else if(sysOrderNoRule.getOrderNoRuleStatus() == CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue()){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("单号规则已禁用")
                    .setErrorSolution("请重新输入规则代码")
                    .build();
        }else {
            sysOrderNoRule = OrderNoMakeUtil.next(sysOrderNoRule);
            sysOrderNoRuleRDao.update(sysOrderNoRule);
            return sysOrderNoRule;
        }
    }

    @Override
    public void updateSysOrderNoRuleCodeStatusById(Long id, Integer status) {
        sysOrderNoRuleRDao.updateByProperties("id",id,"orderNoRuleStatus",status);
    }

    @Override
    public void deleteSysOrderNoRuleByIds(Long[] ids) {
        sysOrderNoRuleRDao.deleteByIds(ids);
    }

    @Override
    public void updateSysOrderNoRuleByIds(Long[] ids, Integer status) {
        StringBuffer where = new StringBuffer();
        HqlPack.getInPack(String.join(",", StringTool.getArrayString(ids)), "id", where);
        sysOrderNoRuleRDao.updateByHqlWhere(where.toString(), "orderNoRuleStatus", status);
    }

    @Override
    public void insertSysOrderNoRuleList(List sysOrderNoRuleList) {
        sysOrderNoRuleRDao.saveAll(sysOrderNoRuleList);
    }


}
