package org.linlinjava.jz.db.service;

import org.linlinjava.jz.db.dao.CustomerFnMapper;
import org.linlinjava.jz.db.domain.CustomerFn;
import org.linlinjava.jz.db.domain.CustomerFnExample;
import org.linlinjava.jz.db.domain.JzCampaign;
import org.linlinjava.jz.db.domain.JzOrder;
import org.springframework.stereotype.Service;

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

/**
 * 客户亲请号码表
 */
@Service
public class JzCustomerFnService {
    private final CustomerFn.Column[] result = new CustomerFn.Column[]{CustomerFn.Column.id, CustomerFn.Column.customerId, CustomerFn.Column.mobile};

    @Resource
    private CustomerFnMapper fnMapper;

    public void add(CustomerFn fn) {
        fn.setCreateTime(LocalDateTime.now());
        fn.setUpdateTime(LocalDateTime.now());
        fn.setDeleted(false);
        CustomerFn cust = findByMobile(fn.getCustomerId(), fn.getMobile());
        if(null!=cust) {
        	return;//已存在就不加了
        }
        fnMapper.insertSelective(fn);
    }

    public List<CustomerFn> queryByCustomerId(int customer_id) {
        CustomerFnExample example = new CustomerFnExample();
        example.or().andCustomerIdEqualTo(customer_id);

        return fnMapper.selectByExampleSelective(example, result);
    }

    public void update(CustomerFn fn) {
        fn.setUpdateTime(LocalDateTime.now());
        fnMapper.updateByPrimaryKeySelective(fn);
    }

    public CustomerFn findById(int id) {
        return fnMapper.selectByPrimaryKey(id);
    }

    public List<CustomerFn> queryByCustomerId(Integer customer_id) {
        CustomerFnExample example = new CustomerFnExample();
        example.or().andCustomerIdEqualTo(customer_id).andDeletedEqualTo(false);
        return fnMapper.selectByExampleSelective(example, result);
    }

    public CustomerFn findByMobile(int customer_id, String mobile) {
        CustomerFnExample example = new CustomerFnExample();
        example.or().andMobileEqualTo(mobile).andCustomerIdEqualTo(customer_id).andDeletedEqualTo(false);
        return fnMapper.selectOneByExampleSelective(example, result);
    }

    public CustomerFn findByMobile(String mobile) {
        CustomerFnExample example = new CustomerFnExample();
        example.or().andMobileEqualTo(mobile).andDeletedEqualTo(false);
        return fnMapper.selectOneByExampleSelective(example, result);
    }


}
