package org.linlinjava.jz.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.jz.db.dao.CustomerClientMapper;
import org.linlinjava.jz.db.domain.CustomerClient;
import org.linlinjava.jz.db.domain.CustomerClientExample;
import org.linlinjava.jz.db.domain.UserVo;
import org.linlinjava.jz.db.util.RandUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author leo
 * 会员信息管理
 */
@Service
public class CustomerClientService {
    @Resource
    private CustomerClientMapper userMapper;

    private final CustomerClient.Column[] result = new CustomerClient.Column[]{CustomerClient.Column.id, CustomerClient.Column.customerName, CustomerClient.Column.tel};

    private final CustomerClient.Column[] result1 = new CustomerClient.Column[]{CustomerClient.Column.id, CustomerClient.Column.customerName, CustomerClient.Column.tel, CustomerClient.Column.customerNo, CustomerClient.Column.customerAddr, CustomerClient.Column.channelId, CustomerClient.Column.source, CustomerClient.Column.city, CustomerClient.Column.customerType, CustomerClient.Column.createTime};

    public CustomerClient findById(Integer userId) {
        return userMapper.selectByPrimaryKeySelective(userId.longValue(), result1);
    }

    public UserVo findUserVoById(Integer userId) {
        CustomerClient user = findById(userId);
        UserVo userVo = new UserVo();
        userVo.setNickname(user.getNickname());
        userVo.setAvatar(user.getAvatar());
        return userVo;
    }

    public CustomerClient queryByOid(String openId) {
        CustomerClientExample example = new CustomerClientExample();
        example.or().andWeixinOpenidEqualTo(openId).andStateEqualTo(0);
        return userMapper.selectOneByExample(example);
    }

    public CustomerClient queryByOpenIdOrTel(String openId, String tel) {
        CustomerClientExample example = new CustomerClientExample();
        CustomerClientExample.Criteria Criteria1 = example.createCriteria();
        CustomerClientExample.Criteria Criteria2 = example.createCriteria();
        Criteria1.andWeixinOpenidEqualTo(openId);
        Criteria2.andTelEqualTo(tel);
        return userMapper.selectOneByExample(example);
    }

    public int add(CustomerClient user) {
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        return userMapper.insertSelective(user);
    }

    public int countByCustomerNo(String no) {
        CustomerClientExample example = new CustomerClientExample();
        example.or().andCustomerNoEqualTo(no);
        return (int) userMapper.countByExample(example);
    }

    public CustomerClient findUserByMobile(String mobile) {
        CustomerClientExample example = new CustomerClientExample();
        example.or().andTelEqualTo(mobile);
        return userMapper.selectOneByExample(example);
    }

    public String getRandNum() {
        String customerNo = RandUtil.getRandomNum(8);
        while (countByCustomerNo(customerNo) != 0) {
            customerNo = RandUtil.getRandomNum(8);
        }
        return "M" + customerNo;
    }


    public List<CustomerClient> findUserIdByMobile(String mobile) {
        CustomerClientExample example = new CustomerClientExample();
        example.or().andTelEqualTo(mobile);
        return userMapper.selectByExampleSelective(example, result);
    }

    public int updateById(CustomerClient user) {
        user.setUpdateTime(LocalDateTime.now());
        return userMapper.updateByPrimaryKeySelective(user);
    }

    //行星数量
    public int starCount() {
        CustomerClientExample example = new CustomerClientExample();
        example.or().andIsDisEqualTo(true);
        return (int) userMapper.countByExample(example);
    }


    /*将用户更新为分销员*/
    public Integer updateByIdIsDis(Integer id) {
        CustomerClient user = findById(id);
        if (user == null) {
            return 0;
        }

        user.setIsDis(true);
        userMapper.updateByPrimaryKey(user);
        return user.getId().intValue();
    }

    public List<CustomerClient> querySelective(String username, String mobile, Integer type, Integer page, Integer size, String sort, String order) {
        CustomerClientExample example = new CustomerClientExample();
        CustomerClientExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(username)) {
            criteria.andCustomerNameLike("%" + username + "%");
        }
        if (!StringUtils.isEmpty(mobile)) {
            criteria.andTelLike("%" + mobile + "%");
            //criteria.andMobileEqualTo(mobile);
        }
        if (type != null && type > 0) {
            criteria.andCustomerTypeEqualTo(type.shortValue());
        }

        criteria.andStateEqualTo(0);//.andDeletedEqualTo(false);

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

        PageHelper.startPage(page, size);
        return userMapper.selectByExampleSelective(example, result1);
    }

    public int count() {
        CustomerClientExample example = new CustomerClientExample();
        example.or().andStateEqualTo(0);

        return (int) userMapper.countByExample(example);
    }

    public List<CustomerClient> queryByUsername(String username) {
        CustomerClientExample example = new CustomerClientExample();
        example.or().andCustomerNameEqualTo(username).andStateEqualTo(0);//.andDeletedEqualTo(false);
        return userMapper.selectByExample(example);
    }

    public boolean checkByUsername(String username) {
        CustomerClientExample example = new CustomerClientExample();
        example.or().andCustomerNameEqualTo(username).andStateEqualTo(0);//.andDeletedEqualTo(false);
        return userMapper.countByExample(example) != 0;
    }

    public List<CustomerClient> queryByMobile(String mobile) {
        CustomerClientExample example = new CustomerClientExample();
        example.or().andTelEqualTo(mobile).andStateEqualTo(0);//.andDeletedEqualTo(false);
        return userMapper.selectByExample(example);
    }

    public List<CustomerClient> queryByOpenid(String openid) {
        CustomerClientExample example = new CustomerClientExample();
        example.or().andWeixinOpenidEqualTo(openid).andStateEqualTo(0);//.andDeletedEqualTo(false);
        return userMapper.selectByExample(example);
    }

    public void deleteById(Integer id) {
        CustomerClient cuseotmerClient = findById(id);
        cuseotmerClient.setUpdateTime(LocalDateTime.now());
        cuseotmerClient.setState(1);
        userMapper.updateByPrimaryKeySelective(cuseotmerClient);
    }
}
