package org.linlinjava.jz.db.service;

import org.linlinjava.jz.db.dao.JzCampaignAssignedLogMapper;
import org.linlinjava.jz.db.domain.JzCampaignAssignedLog;
import org.linlinjava.jz.db.domain.JzCampaignAssignedLogExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 任务分配记录
 * 记录最后一个处理人
 */
@Service
public class JzCampaignAssignedLogService {

    @Resource
    private JzCampaignAssignedLogMapper campaignAssignedLogMapper;

    /**
     * 获取最后一个处理人
     * @param campaignId
     * @return
     */
    public Integer findByCampaignId(Integer campaignId) {
        JzCampaignAssignedLogExample example = new JzCampaignAssignedLogExample();
        example.or().andCampaignIdEqualTo(campaignId);
        example.orderBy("id desc");
        JzCampaignAssignedLog assignedLog= campaignAssignedLogMapper.selectOneByExample(example);
        if(assignedLog==null)
            return 0;
        else
            return  assignedLog.getUserId();

    }

    public Integer findByCampaignId(Integer campaignId,Integer assignedId) {
        JzCampaignAssignedLogExample example = new JzCampaignAssignedLogExample();
        example.or().andCampaignIdEqualTo(campaignId).andAssignedIdEqualTo(assignedId);
        example.orderBy("id desc");
        JzCampaignAssignedLog assignedLog= campaignAssignedLogMapper.selectOneByExample(example);
        if(assignedLog==null)
            return 0;
        else
            return  assignedLog.getUserId();

    }

    //记录一条新记录
    public void  add(Integer campaignId,Integer assigned,Integer userId,Integer place) {
        JzCampaignAssignedLog log = new JzCampaignAssignedLog();
        log.setCampaignId(campaignId);
        log.setAssignedId(assigned);
        log.setUserId(userId);
        log.setCreateTime(LocalDateTime.now());
        log.setPlace(place);
        campaignAssignedLogMapper.insertSelective(log);
    }


}
