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

import cn.estsh.i3plus.core.api.iservice.busi.ISysTaskService;
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.SysTask;
import cn.estsh.i3plus.pojo.platform.repository.SysTaskPlanRepository;
import cn.estsh.i3plus.pojo.platform.repository.SysTaskRepository;
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 : 2018-12-26 21:42
 * @Modify:
 **/
@Service
public class SysTaskService implements ISysTaskService {

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

    @Autowired
    private SysTaskRepository sysTaskRDao;

    @Autowired
    private SysTaskPlanRepository sysTaskPlanRDao;

    @Override
    @ApiOperation(value = "添加任务",notes = "添加任务")
    public SysTask insertSysTask(SysTask sysTask) {
        if(sysTaskRDao.findByPropertyCount("name",sysTask.getName()) > 0){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("任务名称不能相同")
                    .setErrorSolution("请重新输入")
                    .build();
        }

        return sysTaskRDao.insert(sysTask);
    }

    @Override
    @ApiOperation(value = "修改任务信息",notes = "修改任务信息")
    public void updateSysTask(SysTask sysTask) {
        // 业务校验，不能重名
        StringBuffer sb = new StringBuffer();
        HqlPack.getStringEqualPack(sysTask.getName(),"name",sb);
        HqlPack.getNumNOEqualPack(sysTask.getId(),"id",sb);

        if(sysTaskRDao.findByHqlWhereCount(sb.toString()) > 0){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("任务名称不能相同")
                    .setErrorSolution("请重新输入")
                    .build();
        }

        SysTask newSysTask = sysTaskRDao.getById(sysTask.getId());
        newSysTask.setName(sysTask.getName());
        newSysTask.setTaskPackage(sysTask.getTaskPackage());
        newSysTask.setTaskClass(sysTask.getTaskClass());
        newSysTask.setTaskDescription(sysTask.getTaskDescription());
        newSysTask.setModifyDatetime(sysTask.getModifyDatetime());
        newSysTask.setModifyUser(sysTask.getModifyUser());

        sysTaskRDao.update(newSysTask);
        // 修改冗余信息
        sysTaskPlanRDao.updateByProperties("taskId",sysTask.getId(),
                "taskNameRdd",sysTask.getName());
    }

    @Override
    @ApiOperation(value = "查询所有任务",notes = "查询所有任务")
    public List<SysTask> listSysTask() {
        return sysTaskRDao.list();
    }

    @Override
    @ApiOperation(value = "根据id查询任务信息",notes = "根据id查询任务信息")
    public SysTask getSysTaskById(Long id) {
        return sysTaskRDao.getById(id);
    }

    @Override
    @ApiOperation(value = "删除任务",notes = "根据id删除任务")
    public void deleteSysTaskById(Long id) {
        if(sysTaskPlanRDao.isExitByProperty("taskId",id)){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("存在关联任务计划，无法删除")
                    .setErrorSolution("请重新选择")
                    .build();
        }

        sysTaskRDao.deleteById(id);
    }

    @Override
    @ApiOperation(value = "删除任务",notes = "根据id批量删除任务")
    public void deleteSysTaskByIds(Long[] ids) {
        sysTaskRDao.deleteByIds(ids);
    }

    @Override
    @ApiOperation(value = "任务管理复杂查询，分页，排序",notes = "任务管理复杂查询，分页，排序")
    public ListPager<SysTask> querySysTaskByPager(SysTask sysTask, Pager pager) {
        if(sysTask == null) {
            pager = PagerHelper.getPager(pager, sysTaskRDao.listCount());
            return new ListPager(sysTaskRDao.listPager(pager),pager);
        }else {
            String hqlPack = CoreHqlPack.packHqlSysTask(sysTask);
            pager = PagerHelper.getPager(pager, sysTaskRDao.findByHqlWhereCount(hqlPack));
            return new ListPager(sysTaskRDao.findByHqlWherePage(hqlPack + sysTask.orderBy(),pager),pager);
        }
    }
}
