package cn.iocoder.yudao.module.visit.dal.mysql.customerinfo;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.visit.dal.dataobject.customerinfo.CustomerInfoDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.visit.controller.admin.customerinfo.vo.*;

/**
 * 客户信息 Mapper
 *
 * @author 超级管理员
 */
@Mapper
public interface CustomerInfoMapper extends BaseMapperX<CustomerInfoDO> {

    default PageResult<CustomerInfoDO> selectPage(CustomerInfoPageReqVO reqVO) {
        Long companyId = SecurityFrameworkUtils.getLoginUserDeptId();
        return selectPage(reqVO, new LambdaQueryWrapperX<CustomerInfoDO>()
                .likeIfPresent(CustomerInfoDO::getCustomerName, reqVO.getCustomerName())
                .eqIfPresent(CustomerInfoDO::getContact, reqVO.getContact())
                .likeIfPresent(CustomerInfoDO::getCompanyName, reqVO.getCompanyName())
                .eqIfPresent(CustomerInfoDO::getCustomerType, reqVO.getCustomerType())
                .eqIfPresent(CustomerInfoDO::getCityCode, reqVO.getCityCode())
                .eqIfPresent(CustomerInfoDO::getCompanyId,companyId)
                .betweenIfPresent(CustomerInfoDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CustomerInfoDO::getId));
    }
    default PageResult<CustomerInfoDO> selectHomePage(CustomerInfoPageReqVO reqVO) {
        Long companyId = SecurityFrameworkUtils.getLoginUserDeptId();
        return selectPage(reqVO, new LambdaQueryWrapperX<CustomerInfoDO>()
                .eqIfPresent(CustomerInfoDO::getCustomerName, reqVO.getCustomerName())
                .eqIfPresent(CustomerInfoDO::getContact, reqVO.getContact())
                .eqIfPresent(CustomerInfoDO::getCompanyName, reqVO.getCompanyName())
                .eqIfPresent(CustomerInfoDO::getCustomerType, reqVO.getCustomerType())
                .eqIfPresent(CustomerInfoDO::getCityCode, reqVO.getCityCode())
                .eqIfPresent(CustomerInfoDO::getCompanyId,companyId)
                .betweenIfPresent(CustomerInfoDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CustomerInfoDO::getId));
    }

    //根据公司名称查询客户信息（验证是否唯一使用）
    default CustomerInfoDO selectByCompanyName(String companyName){
        Long companyId = SecurityFrameworkUtils.getLoginUserDeptId();
        LambdaQueryWrapperX<CustomerInfoDO> eq = new LambdaQueryWrapperX<CustomerInfoDO>()
                .eq(CustomerInfoDO::getCompanyName, companyName);
        if (companyId == null) {
            eq.isNull(CustomerInfoDO::getCompanyId);
        }else{
            eq.eq(CustomerInfoDO::getCompanyId, companyId);
        }
        return selectOne(eq);
    }

    //根据公司名称查询客户名称和所属分公司id（打印用）
    default CustomerInfoDO selectByCompanyName(String companyName,Long companyId){
        LambdaQueryWrapperX<CustomerInfoDO> eq = new LambdaQueryWrapperX<CustomerInfoDO>()
                .eq(CustomerInfoDO::getCompanyName, companyName);
        if (companyId == null) {
            eq.isNull(CustomerInfoDO::getCompanyId);
        }else{
            eq.eq(CustomerInfoDO::getCompanyId, companyId);
        }
        return selectOne(eq);
    }

    // 根据公司名称或手机号查询（根据任一条件带出符合条件的列表）
    default CustomerInfoDO selectListByCompanyName(String companyName) {
        Long companyId = SecurityFrameworkUtils.getLoginUserDeptId();
        return selectOne(new LambdaQueryWrapperX<CustomerInfoDO>()
                .eqIfPresent(CustomerInfoDO::getCompanyId, companyId)
                .eqIfPresent(CustomerInfoDO::getCompanyName, companyName)
                .last("LIMIT 1"));
    }
    default List<CustomerInfoDO> selectListByContact(String contact) {
        Long companyId = SecurityFrameworkUtils.getLoginUserDeptId();
        return selectList(new LambdaQueryWrapperX<CustomerInfoDO>()
                .eqIfPresent(CustomerInfoDO::getCompanyId, companyId)
                .eqIfPresent(CustomerInfoDO::getContact, contact));
    }

    default List<CustomerInfoDO> selectList(Long companyId){
        return selectList(new LambdaQueryWrapperX<CustomerInfoDO>()
                .eqIfPresent(CustomerInfoDO::getCompanyId, companyId));
    }

}