/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.system.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springblade.modules.system.excel.UserExcel;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.vo.UserLvVo;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Mapper 接口
 *
 * @author Chill
 */
public interface UserMapper extends BaseMapper<User> {

	/**
	 * 自定义分页
	 *
	 * @param page
	 * @param user
	 * @param deptIdList
	 * @param tenantId
	 * @return
	 */
	List<User> selectUserPage(IPage<User> page, @Param("user") User user, @Param("deptIdList") List<Long> deptIdList, @Param("tenantId") String tenantId);

	/**
	 * 获取用户
	 *
	 * @param tenantId
	 * @param account
	 * @param password
	 * @return
	 */
	User getUser(String tenantId, String account, String password);

	/**
	 * 获取导出用户数据
	 *
	 * @param queryWrapper
	 * @return
	 */
	List<UserExcel> exportUser(@Param("ew") Wrapper<User> queryWrapper);

	/**
	 * 获取子节点进行计算
	 * @param userId
	 * @return
	 */
    List<UserLvVo> getUserTree(Long userId);

	/**
	 * 获取所有直推节点大于15的用户
	 * @return
	 */
	List<Long> getParentIdList();

	/**
	 * 找出符合直推创客人数条件的用户
	 * @param userIds 用户查询用户id列表
	 * @param num 创客数量
	 * @return
	 */
	List<Long> getMakerList(List<Long> userIds, Integer num);

	/**
	 * 找出符合团队人数条件的用户
	 * @param userIds 用户查询用户id列表
	 * @param num 创客数量
	 * @return
	 */
	List<Long> getTeamList(List<Long> userIds, Integer num);

	/**
	 * 找出符合轰动条件条件的用户
	 * @param userIds 用户查询用户id列表
	 * @return
	 */
	List<Long> getNewList(List<Long> userIds);

	/**
	 * 找出符合团队购买数量达标的客户id
	 * @param userIds 用户查询用户id列表
	 * @param num 创客数量
	 * @param consignDate 购买时间
	 * @return
	 */
	List<Long> getTeamConsignList(List<Long> userIds, Integer num, Date consignDate);

	/**
	 * 找出符合直推购买数量达标的客户id
	 * @param userIds 用户查询用户id列表
	 * @param num 创客数量
	 * @param consignDate 购买时间
	 * @return
	 */
	List<Long> getRecomConsignList(List<Long> userIds, Integer num, Date consignDate);


	/**
	 * 查询当天用户的团队和直推人员购买数量
	 * @param account 用户查询用户账号
	 * @param consignDate 购买时间
	 * @return
	 */
	List<Map<String,String>> countRecomAndTeamConsign(Long userId, Date consignDate, Long consignConfigId);

	@Select("SELECT u.* FROM blade_user u JOIN (SELECT DISTINCT user_id FROM t_order WHERE order_type = 3 and create_time >= #{date}) t on u.id = t.user_id")
	List<User> snapUpPeopleToday(@Param("date") Date date);

	@Select("SELECT u.* FROM blade_user u JOIN (SELECT DISTINCT user_id FROM t_order WHERE order_type = 3 and create_time >= #{startTime} and create_time <= #{endTime}) t on u.id = t.user_id")
	List<User> snapUpPeopleYesterday(@Param("startTime") Date startTime, @Param("endTime") Date endTime);
}
