package cn.pluss.platform.mapper;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import cn.pluss.platform.dto.MerchSearchDTO;
import cn.pluss.platform.leshua.ExpandDto;
import cn.pluss.platform.vo.MerchSummaryVO;
import cn.pluss.platform.vo.UserNumVO;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.val;
import org.apache.ibatis.annotations.Mapper;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.vo.UserExpandVO;
import cn.pluss.platform.vo.UserInfoVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserAppMapper extends BaseMapper<UserApp> {

    @Select("SELECT ua.*,ui.phone FROM tb_pluss_user_app ua left join tb_pluss_user_info ui on ua.userId = ui.id WHERE ua.userId = #{userId}")
    UserApp selectInfoJoinByUserId(@Param("userId") Serializable userId);

    @Select("SELECT * FROM tb_pluss_user_app WHERE userId = #{userId}")
    UserApp selectByUserId(@Param("userId") Serializable userId);

    @Select("SELECT * FROM tb_pluss_user_app WHERE merchantCode = #{merchantCode} AND userType = 'promoter'")
    UserApp selectByMerchantCode(@Param("merchantCode") String merchantCode);

    default List<UserApp> queryUserAppList(UserApp userApp) {
        return selectList(new QueryWrapper<>(userApp));
    }

    Page<MerchSummaryVO> selectMerchantSummaryPage(Page<MerchSummaryVO> page, @Param("condition") MerchSearchDTO condition);

    MerchSummaryVO selectBillCountAndTotalFee(@Param("condition") MerchSearchDTO condition);

    Integer queryUserAppPageCount(Map<String, Object> map);

    List<List<?>> queryUserInfoVOPage(Map<String, Object> map);

    List<UserInfoVO> queryUserInfoVONoPage(Map<String, Object> map);

    Integer queryUserInfoVOPageCount(Map<String, Object> map);

    Integer queryUserInfoVONoPageCount(Map<String, Object> map);
    /**
     * 未实名信息
     *
     * @return
     */
    List<UserExpandVO> queryUserAppNoRealNamePage(Map<String, Object> map);

    /**
     * 未实名统计数量
     *
     * @param map
     * @return
     */
    Integer queryUserAppNoRealNamePageCount(Map<String, Object> map);

    /**
     * queryUserAppRealNamePage:(已实名认证信息). <br/>
     *
     * @param map
     * @return
     * 
     * @author Administrator
     * @since JDK 1.8
     */
    List<UserExpandVO> queryUserAppRealNamePage(Map<String, Object> map);

    /**
     * queryUserAppRealNamePage:(已实名认证条数). <br/>
     *
     * @param map
     * @return
     * 
     * @author Administrator
     * @since JDK 1.8
     */
    Integer queryUserAppRealNamePageCount(Map<String, Object> map);

    /**
     * 是商户的信息
     *
     * @param map
     * @return
     */
    List<UserExpandVO> queryUserAppIsMerchantPage(Map<String, Object> map);

    /**
     * 是商户信息统计数量
     *
     * @param map
     * @return
     */
    Integer queryUserAppIsMerchantPageCount(Map<String, Object> map);


    List<UserApp> queryUserAppListByPhone(UserApp userApp);

    List<UserApp> queryUserAppPageByPhone(Map<String, Object> map);

    Integer queryUserAppPageCountByPhone(Map<String, Object> map);

    UserApp queryUserAppByInviteNum(String inviteNum);

    /**
     * 分页查询拓展数据
     *
     * @param page      分页信息
     * @param expandDto 查询条件
     * @return
     */
    IPage<UserApp> getExPageUserApp(IPage<UserApp> page, @Param("expandDto") ExpandDto expandDto, @Param("parentIdList") List<String> parentIdList);

    IPage<UserApp> selectCustomList(IPage<UserApp> page, @Param(Constants.WRAPPER) Wrapper<UserApp> queryWrapper);

    /**
     * 根据推广员id关联查询商户信息，结算信息
     * @return
     */
    UserApp queryUserAppJoinById(Long userId);

    UserApp queryUserAppJoinByUserId(Long userId);

    default void updateUserAppUserId(UserApp userApp) {
        val uWrapper = new LambdaQueryWrapper<UserApp>()
                .eq(UserApp::getUserId, userApp.getUserId());
        update(userApp, uWrapper);
    }

    /**
     * 获取指定进件状态的商户
     */
    Integer selectUserAppWithChannelStatusCount(@Param(Constants.WRAPPER) Wrapper<UserApp> updateWrapper);

    /**
     * 获取指定进件状态的商户
     */
    List<UserApp> selectListWithChannelStatus(@Param(Constants.WRAPPER) Wrapper<UserApp> updateWrapper);

    /**
     * 根据用户父级id获取用户信息
     * @param userId
     * @return
     */
    List<UserApp> getUserAppInfoByParentId(Long userId);

    /**
     * 关联查询分页
     * @param page
     * @param userApp
     * @return
     */
    Page<UserApp> pageData(@Param("page") Page<UserApp> page, @Param("userApp") UserApp userApp);

    /**
     * 获取下级商户数量
     * @param userId
     * @return
     */
    Integer getSubUserCount(@Param("userId") Long userId,@Param("startDate")String startDate,@Param("endDate")String endDate);

    /**
     * 分页查询活动商户详情列表
     * @param
     * @return
     */
    Page<UserInfoVO> queryActPage(@Param("page")Page<UserInfoVO> page,@Param("userInfoVO") UserInfoVO userInfoVO);

    int getNotificationMenuId(String link);

    Integer getParentUserId(Integer parentUserId);

    Integer getUserId(String merchantCode);

    /**
     * 分页查询员工信息
     * @param page              分页以及排序
     * @param queryWrapper      查询条件
     * @return                  查询结果
     */
    Page<UserApp> selectUserApp(Page<UserApp> page, @Param(Constants.WRAPPER) Wrapper<Object> queryWrapper);

    UserApp selectUserApp(@Param(Constants.WRAPPER) Wrapper<Object> queryWrapper);

    /**
     * @description:根据userId获取用户的所有上级
     * @date: 2021/8/18 10:46
     * @param userId:
     * @return cn.pluss.platform.entity.UserApp
     */
    Map<String,Object> getUserAppParentsByUserId(String userId);


    /**
     * 获取实名认证审核中的商户
     * @return
     */
    @Select("SELECT * FROM tb_pluss_user_app WHERE `status` = 1 AND userType IN ('promoter', 'agent') " +
            "AND updateDt < DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 HOUR) ORDER BY id DESC LIMIT 200")
    List<UserApp> selectUserInCert();

    /**
     * @description:首页统计数据
     * @date: 2021/10/19 17:24
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    Map<String, Object> getPlatformData();

    Integer getMaxSort();

    @Select("SELECT ua.*,ul.rate FROM tb_pluss_user_app ua left join tb_pluss_user_level ul on ua.levelCode = ul.levelCode WHERE ua.roleCode = #{roleCode} and ua.levelCode = #{levelCode} limit 1 ")
    UserApp getRoleCodeAndLevelCode(@Param("roleCode") String roleCode, @Param("levelCode")String levelCode);

    /**
     * 获取直推用户的新增数量和总数
     * @date: 2022/1/24 23:26
     * @param userId:
     * @param date:
     * @param dateType:
     * @return cn.pluss.platform.vo.UserNumVO
     */
    UserNumVO getUserNumInfo(@Param("userId")Long userId, @Param("date")String date, @Param("dateType")Integer dateType);

    /**
     * 根据手机号查询用户信息
     * @param phone
     * @return
     */
    List<UserApp> getUserInfoByPhone(String phone);

}