package org.linlinjava.jz.db.service;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import org.linlinjava.jz.db.dao.JzCampaignMapper;
import org.linlinjava.jz.db.domain.JzCampaign;
import org.linlinjava.jz.db.domain.JzCampaignExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Set;

/**
 * @author leo
 * 营销任务
 */
@Service
public class JzCampaignService {
    @Resource
    private JzCampaignMapper campaignMapper;


    public List<JzCampaign> querySelective(String name, Integer page, Integer size, String sort, String order) {
        JzCampaignExample example = new JzCampaignExample();
        JzCampaignExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return campaignMapper.selectByExample(example);
    }

    public JzCampaign findById(int id){
        return campaignMapper.selectByPrimaryKey(id);
    }

    public void  add(JzCampaign campaign){
        campaign.setCreateTime(LocalDateTime.now());
        campaign.setUpdateTime(LocalDateTime.now());
        campaignMapper.insertSelective(campaign);
    }

    public void update(JzCampaign campaign){
        campaign.setUpdateTime(LocalDateTime.now());
        campaignMapper.updateByPrimaryKeySelective(campaign);
    }

    /**
     * 获取所有可以执行的任务列表
     * @return
     */
    public List<JzCampaign> queryEnable(){
        JzCampaignExample example = new JzCampaignExample();
        JzCampaignExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andIsAutoAssignedEqualTo(true);//是否自动分配
        //分配时间
        String date=LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm"));
        criteria.andAssignedStartLessThan(date);
        criteria.andAssignedEndGreaterThanOrEqualTo(date);

        return campaignMapper.selectByExample(example);
    }

    /**
     * 获取每日运行一次的任务
     * @return
     */
    public List<JzCampaign> queryNightRun(){
        JzCampaignExample example = new JzCampaignExample();
        JzCampaignExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andIsAutoAssignedEqualTo(true);//是否自动分配
        criteria.andIsNightRunEqualTo(true);


        return campaignMapper.selectByExample(example);
    }

    public List<JzCampaign> queryAll(){
        return campaignMapper.selectByExample(new JzCampaignExample());
    }

    public List<JzCampaign> queryAllNoDeleted(){
        JzCampaignExample example=new JzCampaignExample();
        example.or().andDeletedEqualTo(false);
        return campaignMapper.selectByExample(example);
    }
}
