package shop.alien.mapper;

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 org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import shop.alien.entity.store.LifeUser;
import shop.alien.entity.store.vo.LifeFansVo;
import shop.alien.entity.store.vo.LifeUserVo;

import java.util.List;

/**
 * 用户
 */
@Mapper
public interface LifeUserMapper extends BaseMapper<LifeUser> {

    @Select("select info.store_name, user.phone, img.img_url " +
            "from store_info info " +
            "join store_user user on user.store_id = info.id " +
            "left join store_img img on img.store_id = info.id and img.img_type = '10' and img.delete_flag = 0 " +
            "where info.delete_flag = 0 and user.delete_flag = 0 " +
            "and substring_index(#{phoneId}, '_', 1) = 'store' " +
            "and substring_index(#{phoneId}, '_', -1) = user.phone " +
            "union " +
            "select user_name name, user_phone phone, user_image img_url " +
            "from life_user " +
            "where delete_flag = 0 and substring_index(#{phoneId}, '_', 1) = 'user' and substring_index(#{phoneId}, '_', -1) = user_phone")
    LifeFansVo getUserInfoByPhoneId(@Param("phoneId") String phoneId);

    LifeFansVo getUserInfoByPhoneIdList(@Param("phoneId") String phoneId);

    @Select("SELECT * FROM ( " +
            " SELECT USER.id, concat( 'store_', USER.phone ) phoneId, USER.head_img imgUrl, " +
            " store.store_blurb blurb, " +
            " store.store_name storeUserName, " +
            " USER.nick_name accountName " +
            " FROM store_user USER " +
            " INNER JOIN store_info store ON USER.store_id = store.id " +
            " WHERE USER.delete_flag = 0 AND store.delete_flag = 0 AND USER.logout_flag  in (0,2) " +
            " UNION ALL SELECT id, concat( 'user_', user_phone ) phoneId, user_image imgUrl, jianjie blurb, user_name NAME, user_name accountName " +
            " FROM life_user WHERE delete_flag = 0 ) a " +
            " ${ew.customSqlSegment}")
    IPage<LifeUserVo> getStoreAndUserByName(IPage<LifeUserVo> iPage, @Param(Constants.WRAPPER) QueryWrapper<LifeUserVo> wrapper);

    LifeUserVo getRemoveUser(@Param("id") String id);

    /**
     * 根据id集合查询用户信息
     *
     * @param queryWrapper 查询条件
     * @return 用户信息
     */
    @Select("SELECT * " +
            "FROM life_user ${ew.customSqlSegment}")
    List<LifeUserVo> getUserByIds(@Param(Constants.WRAPPER) QueryWrapper<LifeUserVo> queryWrapper);

    /**
     * 根据id集合查询用户信息
     *
     * @param queryWrapper 查询条件
     * @return 用户信息
     */
    @Select("SELECT * " +
            "FROM life_user ${ew.customSqlSegment}")
    LifeUserVo getUserById(@Param(Constants.WRAPPER) QueryWrapper<LifeUserVo> queryWrapper);

    /**
     * 统计用户设备总数：device_id 为逗号拼接，按逗号分段计数后求和
     */
    @Select("SELECT IFNULL(SUM(CASE " +
            "WHEN device_id IS NULL OR TRIM(device_id) = '' THEN 0 " +
            "ELSE LENGTH(device_id) - LENGTH(REPLACE(device_id, ',', '')) + 1 END), 0) " +
            "FROM life_user WHERE delete_flag = 0")
    Long countTotalDeviceNum();
}