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

import cn.estsh.i3plus.core.api.iservice.busi.ISysTaskCycleService;
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.SysRefTaskCyclePlan;
import cn.estsh.i3plus.pojo.platform.bean.SysTaskCycle;
import cn.estsh.i3plus.pojo.platform.repository.SysRefTaskCyclePlanRepository;
import cn.estsh.i3plus.pojo.platform.repository.SysTaskCycleRepository;
import cn.estsh.i3plus.pojo.platform.repository.SysTaskPlanRepository;
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 : wei.peng
 * @Date : 2018-10-24 20:40
 * @Modify :
 **/
@Service
public class SysTaskCycleService implements ISysTaskCycleService {

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

    @Autowired
    private SysTaskCycleRepository sysTaskCycleRDao;

    @Autowired
    private SysTaskPlanRepository sysTaskPlanRDao;

    @Autowired
    private SysRefTaskCyclePlanRepository sysRefTaskCyclePlanRDao;

    @Override
    @ApiOperation(value = "修改任务周期",notes = "修改任务周期")
    public void updateSysTaskCycle(SysTaskCycle sysTaskCycle) {
        sysTaskCycleRDao.update(sysTaskCycle);
    }

    @Override
    @ApiOperation(value = "删除任务周期",notes = "根据id删除任务周期")
    public void deleteSysTaskCycleById(Long id) {
        List<SysRefTaskCyclePlan> refExpressionTriggerList = sysRefTaskCyclePlanRDao.findByProperty("taskCycleId",id);
        if(refExpressionTriggerList.size() != 0){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("存在关联任务计划【" + refExpressionTriggerList.get(0).getTaskPlanName()
                                             + refExpressionTriggerList.get(0).getTaskPlanGroupName() + "】，无法删除")
                    .setErrorSolution("请重新选择")
                    .build();
        }

        sysTaskCycleRDao.deleteById(id);
    }

    @Override
    @ApiOperation(value = "新增任务周期",notes = "新增任务周期")
    public SysTaskCycle insertSysTaskCycle(SysTaskCycle sysTaskCycle) {
        return sysTaskCycleRDao.insert(sysTaskCycle);
    }

    @Override
    @ApiOperation(value = "查询所有任务周期信息",notes = "查询所有任务周期信息")
    public List<SysTaskCycle> listSysTaskCycle() {
        return sysTaskCycleRDao.list();
    }

    @Override
    @ApiOperation(value = "根据ID 查询任务周期",notes = "根据ID 查询任务周期")
    public SysTaskCycle getSysTaskCycleById(Long id) {
        return sysTaskCycleRDao.getById(id);
    }

    @Override
    @ApiOperation(value = "任务周期查询，分页，排序",notes = "任务周期查询，分页，排序")
    public ListPager querySysTaskCycleByPager(SysTaskCycle sysTaskCycle, Pager pager) {
        if(sysTaskCycle == null) {
            pager = PagerHelper.getPager(pager, sysTaskCycleRDao.listCount());
            return new ListPager(sysTaskCycleRDao.listPager(pager),pager);
        }else {
            String hqlPack = CoreHqlPack.packHqlSysTaskCycle(sysTaskCycle);
            pager = PagerHelper.getPager(pager, sysTaskCycleRDao.findByHqlWhereCount(hqlPack));
            return new ListPager(sysTaskCycleRDao.findByHqlWherePage(hqlPack + sysTaskCycle.orderBy(),pager),pager);
        }
    }

    @Override
    @ApiOperation(value = "根据id批量删除任务周期",notes = "根据id批量删除任务周期")
    public void deleteSysTaskCycleByIds(Long[] ids) {
        List<SysRefTaskCyclePlan> refExpressionTriggerList = sysRefTaskCyclePlanRDao.findByHqlWhere(CoreHqlPack.packHqlIds("taskCycleId",ids));
        if(refExpressionTriggerList.size() != 0){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("存在关联任务计划【" + refExpressionTriggerList.get(0).getTaskPlanName()
                            + refExpressionTriggerList.get(0).getTaskPlanGroupName() + "】，无法删除")
                    .setErrorSolution("请重新选择")
                    .build();
        }

        sysTaskCycleRDao.deleteByIds(ids);
    }

    @Override
    @ApiOperation(value = "根据任务周期id查询相关任务计划信息",notes = "根据任务周期id查询相关任务计划信息")
    public List<SysRefTaskCyclePlan> findRefTaskCyclePlanByCycleId(Long cycleId) {
        return sysRefTaskCyclePlanRDao.findByProperty("taskCycleId",cycleId);
    }
}
