package org.linlinjava.jz.db.service;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import org.linlinjava.jz.db.dao.JzCalllogRecordMapper;
import org.linlinjava.jz.db.domain.*;
import org.linlinjava.jz.db.util.TicketStatusConstant;
import org.springframework.stereotype.Service;

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

@Service
public class JzCalllogRecordService {

    @Resource
    private JzCalllogRecordMapper recordMapper;
    @Resource
    private CustomerClientService customerClientService;

    public List<JzCalllogRecord> querySelective(String mobile, String callType, Integer page, Integer size, String sort, String order) {
        JzCalllogRecordExample example = new JzCalllogRecordExample();
        JzCalllogRecordExample.Criteria criteria1 = example.createCriteria();

        if (!StringUtils.isEmpty(callType)) {
            criteria1.andCallTypeEqualTo(callType);
        }
        if (!StringUtils.isEmpty(mobile)) {
            CustomerClient customerClient = customerClientService.findUserByMobile(mobile);
            if (null != customerClient) {
                criteria1.andCustomerIdEqualTo(customerClient.getId().intValue());
            } else {
                criteria1.andCustomerIdEqualTo(0);
            }
        }

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

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

    public int add(JzCalllogRecord record) {
        record.setCreateTime(LocalDateTime.now());
        recordMapper.insertSelective(record);
        return record.getId();
    }

    public void update(List<Integer> reocrds, int callid) {
        for (int id : reocrds) {
            JzCalllogRecord record = recordMapper.selectByPrimaryKey(id);
            record.setCallId(callid);
            recordMapper.updateByPrimaryKeySelective(record);
        }
    }

    public JzCalllogRecord findById(int id) {
        return recordMapper.selectByPrimaryKey(id);
    }

    public int update(JzCalllogRecord record) {
        return recordMapper.updateByPrimaryKeySelective(record);
    }


    public List<JzCalllogRecord> queryCustomerId(Integer customerId) {
        JzCalllogRecordExample example = new JzCalllogRecordExample();
        JzCalllogRecordExample.Criteria criteria1 = example.createCriteria();
        criteria1.andCustomerIdEqualTo(customerId);
        return recordMapper.selectByExample(example);
    }

    public JzCalllogRecord findByRecordId(String recordId) {
        JzCalllogRecordExample example = new JzCalllogRecordExample();
        JzCalllogRecordExample.Criteria criteria1 = example.createCriteria();
        criteria1.andRecordIdEqualTo(recordId);
        return recordMapper.selectOneByExample(example);
    }
}
