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();
        return selectOne(new LambdaQueryWrapperX<CustomerInfoDO>()
                .eq(CustomerInfoDO::getCompanyName, companyName)
                .eqIfPresent(CustomerInfoDO::getCompanyId, companyId)
        );
    }

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

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

}