package com.fs.store.mapper;

import java.util.List;

import com.fs.store.domain.FsUser;
import com.fs.store.vo.FsUserListVO;
import com.fs.store.vo.FsUserVO;

import com.fs.store.vo.FsCompanyUserListQueryVO;
import com.fs.store.vo.FsUserShareVO;
import com.fs.store.vo.FsUserTuiVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 用户Mapper接口
 *
 * @author fs
 * @date 2022-03-15
 */
public interface FsUserMapper
{
    /**
     * 查询用户
     *
     * @param userId 用户ID
     * @return 用户
     */
    public FsUser selectFsUserById(Long userId);

    /**
     * 查询用户列表
     *
     * @param fsUser 用户
     * @return 用户集合
     */
    public List<FsUser> selectFsUserList(FsUser fsUser);

    /**
     * 查询用户列表
     *
     * @param fsUser 用户
     * @return 用户集合
     */
    @Select({"<script> " +
            "select  b.total_amount,b.last_buy_time,p.pay_money as number,p.payment_id,p.pay_time," +
            " u.*,u2.phone spread_phone FROM fs_user u LEFT JOIN  (" +
            " SELECT max(payment_id) as payment_id, max(pay_time) as last_buy_time,SUM(pay_money) as total_amount,user_id " +
            " FROM fs_store_payment" +
            " WHERE  status = 1 and user_id is not null group by user_id " +
            ") b ON u.user_id=b.user_id " +
            " LEFT JOIN fs_store_payment p ON u.user_id=p.user_id AND b.last_buy_time=p.pay_time AND b.payment_id=p.payment_id"+
            " LEFT JOIN fs_user u2 on u2.user_id=u.spread_user_id "+
            " WHERE 1=1 " +
            "<if test = 'maps.nickname != null and  maps.nickname !=\"\"    '> " +
            "and u.nickname like CONCAT('%',#{maps.nickname},'%') " +
            "</if>" +
            "<if test = 'maps.phone != null   and  maps.phone !=\"\"   '> " +
            "and u.phone=#{maps.phone} " +
            "</if>" +
            "<if test = 'maps.registerDate != null and  maps.registerDate !=\"\"'> " +
            "and  DATE_FORMAT(u.register_date, '%Y-%m-%d')  = DATE_FORMAT(#{maps.registerDate}, '%Y-%m-%d') " +
            "</if>" +
            "<if test = 'maps.registerCode != null  and  maps.registerCode !=\"\"   '> " +
            "and u.register_code=#{maps.registerCode} " +
            "</if>" +
            "<if test='maps.status != null  and  maps.status !=\"\"'> and u.status = #{maps.status}</if>"+
            "<if test='maps.level != null  and  maps.level !=\"\"'> and u.level = #{maps.level}</if>"+
            "<if test='maps.isPromoter != null  and  maps.isPromoter !=\"\"'> and u.is_promoter = #{maps.isPromoter}</if>" +
            " order by u.user_id desc "+
            "</script>"}
            )
    public List<FsUserListVO> selectFsUserVOList(@Param("maps") FsUser fsUser);

    /**
     * 新增用户
     *
     * @param fsUser 用户
     * @return 结果
     */
    public int insertFsUser(FsUser fsUser);

    /**
     * 修改用户
     *
     * @param fsUser 用户
     * @return 结果
     */
    public int updateFsUser(FsUser fsUser);

    /**
     * 删除用户
     *
     * @param userId 用户ID
     * @return 结果
     */
    public int deleteFsUserById(Long userId);

    /**
     * 批量删除用户
     *
     * @param userIds 需要删除的数据ID
     * @return 结果
     */
    public int deleteFsUserByIds(Long[] userIds);

    @Update("update fs_user set integral=integral-#{integral}" +
            " where user_id=#{uid}")
    int decIntegral(@Param("uid") Long uid, @Param("integral")double integral);
    @Update("update fs_user set pay_count=pay_count+1" +
            " where user_id=#{userId}")
    int incPayCount(Long userId);
    @Select("select * from fs_user where phone=#{phone}")
    FsUser selectFsUserByPhone(String phone);


    @Select("select  b.total_amount,b.last_buy_time,p.pay_money as number,p.payment_id,p.pay_time," +
            " u.* FROM fs_user u LEFT JOIN  (" +
            " SELECT max(payment_id) as payment_id, max(pay_time) as last_buy_time,SUM(pay_money) as total_amount,user_id " +
            " FROM fs_store_payment" +
            " WHERE  status = 1 and user_id is not null group by user_id " +
            ") b ON u.user_id=b.user_id " +
            " LEFT JOIN fs_store_payment p ON u.user_id=p.user_id AND b.last_buy_time=p.pay_time AND b.payment_id=p.payment_id"+
            " WHERE u.user_id=#{userId}")
    FsUserVO selectFsUserByUserId(Long userId);


    @Select({"<script> " +
            "select u.*,cu.nick_name as company_user_nick_name  from fs_user u left join company_user cu on u.company_user_id=cu.user_id " +
            "where u.is_del=0 " +
            "<if test = 'maps.nickname != null and  maps.nickname !=\"\"    '> " +
            "and u.nickname like CONCAT('%',#{maps.nickname},'%') " +
            "</if>" +
            "<if test = 'maps.phone != null   and  maps.phone !=\"\"   '> " +
            "and u.phone=#{maps.phone} " +
            "</if>" +
            "<if test = 'maps.companyId != null    '> " +
            "and u.company_id=#{maps.companyId} " +
            "</if>" +
            "<if test = 'maps.status != null   and  maps.status !=\"\"   '> " +
            "and u.status=#{maps.status} " +
            "</if>" +
            "<if test = 'maps.companyUserId != null    '> " +
            "and u.company_user_id=#{maps.companyUserId} " +
            "</if>" +
            " order by u.create_time desc "+
            "</script>"})
    List<FsCompanyUserListQueryVO> selectFsCompanyUserListQuery(@Param("maps")FsUser fsUser);
    @Select({"<script> " +
            "select u.user_id,u.phone,u.avatar,u.nickname,ifnull((select sum(b.number) from fs_user_bill b where b.category='brokerage_price' and b.bill_type=1  and b.tui_user_id=u.user_id and b.user_id=#{userId} ),0)-ifnull((select sum(b.number) from fs_user_bill b where b.category='brokerage_price' and b.bill_type=0 and b.tui_user_id=u.user_id and b.user_id=#{userId}  ),0) as tui_money ,u.create_time from fs_user u " +
            "where u.spread_user_id=#{userId} " +
            "order by tui_money desc " +
            "</script>"})
    List<FsUserTuiVO> selectFsUserTuiList(String userId);
    @Select({"<script> " +
            "select count(1) from fs_user  " +
            "where 1=1 " +
            "<if test = 'type != null and  type ==1  '> " +
            "and  DATE_FORMAT(create_time, '%Y-%m-%d')  = DATE_FORMAT(NOW(), '%Y-%m-%d') " +
            "</if>" +
            "<if test = 'companyId != null  '> " +
            "and  company_id=#{companyId} " +
            "</if>" +
            "</script>"})
    Long selectFsUserCount(@Param("type") int type,@Param("companyId") Long companyId);

    @Select("select * from fs_user where ma_open_id=#{openid}")
    FsUser selectFsUserByMaOpenId(String openid);
    @Select("select * from fs_user where mp_open_id=#{openid}")
    FsUser selectFsUserByMpOpenId(String openid);
    @Select("select * from fs_user where union_id=#{unionId}")
    FsUser selectFsUserByUnionId(String unionId);

    Long selectCrmCustomerCount(int type, Long companyId);

    @Select("select * from fs_user where user_code=#{userCode}")
    FsUser selectFsUserByUserCode(String userCode);
    @Select("select * from fs_user where user_id=#{userId} for update")
    FsUser selectFsUserByIdForUpdate(Long userId);
    @Select({"<script> " +
             "SELECT SUM(CASE WHEN b.bill_type = 1 THEN b.number ELSE 0 END)-SUM(CASE WHEN b.bill_type = 0 THEN b.number ELSE 0 END) AS tui_money, u.user_id,u.phone,u.avatar,u.nickname FROM fs_user_bill b LEFT JOIN fs_user u ON u.user_id= b.user_id WHERE b.category = 'brokerage_price' " +
            "<if test = 'type != null   and type==2   '> " +
            "and  b.create_time &gt;= DATE_FORMAT(NOW() ,'%Y-%m-01') " +
            "</if>" +
            "<if test = 'type != null   and type==1   '> " +
            "and  YEARWEEK(b.create_time, 1) = YEARWEEK(CURDATE(), 1) " +
            "</if>" +
            " GROUP BY b.user_id ORDER BY   \n" +
            "tui_money DESC "+
            "</script>"})
    List<FsUserShareVO> selectFsUserShareList(@Param("userId") String userId,@Param("type")Integer type);
}
