package org.linlinjava.jz.db.service;

import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.jz.db.dao.JzCalllogMapper;
import org.linlinjava.jz.db.domain.JzCalllog;
import org.linlinjava.jz.db.domain.JzCalllogExample;
import org.springframework.stereotype.Service;

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

/**
 * 进电 外拨电访结果处理类
 */
@Service
public class JzCalllogService {

    @Resource
    private JzCalllogMapper calllogMapper;

    public List<JzCalllog> querySelective(String mobile, String callType, List<Short> callResultArray,Integer campaignId, Integer page, Integer limit, String sort, String order) {
        JzCalllogExample example = new JzCalllogExample();
        JzCalllogExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(mobile)) {
            criteria.andMobileLike("%" + mobile + "%");
        }
        if (!StringUtils.isEmpty(callType)) {
            criteria.andCallTypeEqualTo(callType);
        }
        if (callResultArray != null && callResultArray.size() > 0) {
            criteria.andCallResultIn(callResultArray);
        }
        if(campaignId!=null&&campaignId>0){
            criteria.andCampaignIdEqualTo(campaignId);
        }

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

        PageHelper.startPage(page, limit);
        List<JzCalllog> calllogs = calllogMapper.selectByExample(example);

        for(JzCalllog calllog:calllogs){

        }
        return calllogs;
    }

    public int add(JzCalllog calllog) {
        calllog.setEndTime(LocalDateTime.now());
        calllogMapper.insertSelective(calllog);
        return calllog.getId();
    }

    public int deleteById(JzCalllog calllog) {
        return calllogMapper.updateByPrimaryKeySelective(calllog);
    }

    public int updateById(JzCalllog calllog) {
        return calllogMapper.updateByPrimaryKeySelective(calllog);
    }

    public JzCalllog findById(Integer id) {
        return calllogMapper.selectByPrimaryKey(id);
    }


    public List<JzCalllog> queryByCustomerId(Integer customerId) {
        JzCalllogExample example = new JzCalllogExample();
        JzCalllogExample.Criteria criteria = example.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        return calllogMapper.selectByExample(example);
    }

}
