package cn.superdesk.uniorder.service.scheduler.impl;

import cn.superdesk.uniorder.common.exception.utils.AssertUtils;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.scheduler.SysQuartzJob;
import cn.superdesk.uniorder.dao.entity.scheduler.SysQuartzJobExample;
import cn.superdesk.uniorder.dao.mapper.scheduler.SysQuartzJobMapper;
import cn.superdesk.uniorder.service.scheduler.SysQuartzJobService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description: 定时任务
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2020-12-17 16:35:26
 * @UpdateUser: linjinyu
 * @UpdateDate: 2020-12-17 16:35:26
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Service
public class SysQuartzJobServiceImpl implements SysQuartzJobService {

    @Autowired
    private SysQuartzJobMapper sysQuartzJobMapper;


    /****************************************** extends BaseService 部分 start ******************************************/

    @Override
    public int insertSelective(SysQuartzJob entity) {
        return sysQuartzJobMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(SysQuartzJob entity) {
        return sysQuartzJobMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public SysQuartzJob findById(Long id) {
        return sysQuartzJobMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<SysQuartzJob> findByPaging(Map<String, Object> conditions, Integer pageNun, Integer pageSize) {
        return null;
    }

    @Override
    public List<SysQuartzJob> findAll(Map<String, Object> queryMap) {
        return null;
    }

    /****************************************** extends BaseService 部分   end ******************************************/


    public void addSysQuartzJob(SysQuartzJob entity) {
        sysQuartzJobMapper.insertSelective(entity);
    }

    public void updateSysQuartzJob(SysQuartzJob entity) {

        SysQuartzJob originEntity = sysQuartzJobMapper.selectByPrimaryKey(entity.getId());
        AssertUtils.notNull(originEntity);
        AnBeanUtils.copy(entity, originEntity);
        sysQuartzJobMapper.updateByPrimaryKey(originEntity);
    }


    public void deleteSysQuartzJob(Long id) {
        sysQuartzJobMapper.deleteByPrimaryKey(id);
    }

    public SysQuartzJob findSysQuartzJobById(Long id) {
        SysQuartzJob entity = sysQuartzJobMapper.selectByPrimaryKey(id);
        AssertUtils.notNull(entity);
        return entity;
    }

    /**
     * 获取 启用 Job
     *
     * @return
     */
    public List<SysQuartzJob> findSysQuartzJobByIsPauseEq0() {

        List<SysQuartzJob> result = null;

        SysQuartzJobExample example = new SysQuartzJobExample();
        example.createCriteria()
                .andIsPauseEqualTo(0);

        result = sysQuartzJobMapper.selectByExample(example);

        return result;
    }

    public R<List<SysQuartzJob>> pageQuery(Map<String, Object> conditions, int pageNun, int pageSize) {

        // 样例 保留
//		SysQuartzJob sysQuartzJobConditions = BeanUtils.mapToBean(conditions,SysQuartzJob.class);

        SysQuartzJobExample          example  = new SysQuartzJobExample();
        SysQuartzJobExample.Criteria criteria = example.createCriteria();

        PageHelper.startPage(pageNun, pageSize);
        List<SysQuartzJob>     resultList = sysQuartzJobMapper.selectByExample(example);
        PageInfo<SysQuartzJob> page       = new PageInfo<SysQuartzJob>(resultList);
        PageHelper.clearPage();

        return new R<List<SysQuartzJob>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), resultList);

    }

}