package shop.alien.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
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.LifeUserDynamics;
import shop.alien.entity.store.vo.LifeUserDynamicsVo;

import java.util.List;

@Mapper
public interface LifeUserDynamicsMapper extends BaseMapper<LifeUserDynamics> {

    @Select("with dynamice as( " +
            "    select id, title, phone_id phoneId, context, image_path, address,address_name,address_context, liulan_count, dianzan_count, type, created_time, substring_index(phone_id, '_', 1) flag, substring_index(phone_id, '_', -1) phone, draft , address_province" +
            "    from life_user_dynamics " +
            "    where delete_flag = 0 and draft = 0 order by created_time desc" +
            ") " +
            "select dynamice.*, user.name userName, user.head_img userImage, info.id storeUserId, user.id storeOrUserId, 0 isExpert " +
            "from dynamice " +
            "join store_user user on dynamice.phone = user.phone and user.delete_flag = 0 " +
            "join store_info info on info.id = user.store_id and info.delete_flag = 0 " +
            "left join store_img img on img.store_id = user.store_id and img.img_type = '10' and img.delete_flag = 0  " +
            "where dynamice.flag = 'store' " +
            "union " +
            "select dynamice.*, user.user_name userName, user.user_image userImage, user.id storeUserId, user.id storeOrUserId, IF(lue.expert_code IS NOT NULL , 1, 0) AS isExpert " +
            "from dynamice " +
            "join life_user user on dynamice.phone = user.user_phone and user.delete_flag = 0 " +
            "left join life_user_expert  lue on lue.user_id = user.id and lue.delete_flag = 0 " +
            "where dynamice.flag = 'user'")
    List<LifeUserDynamicsVo> getLifeUserDynamicsList();

    @Select("select lud.id ,lud.image_path,lud.context,lu.user_image,lu.user_name,IF(llr.huifu_id IS NOT NULL, 1, 0) AS isLike\n" +
            "from life_user_dynamics lud\n" +
            "left join life_user lu \n" +
            "on lud.phone_id = CONCAT('store_', lu.user_phone )\n" +
            "left join life_like_record llr\n" +
            "on llr.huifu_id = lud.id\n" +
            "and lu.user_phone = (select CONCAT('user_',lu1.user_phone) from life_user lu1 where lu1.id = #{userId})" +
            "${ew.customSqlSegment}")
    List<LifeUserDynamicsVo> getStoreDynamicslist(@Param("userId") String userId, @Param(Constants.WRAPPER) QueryWrapper<LifeUserDynamics> dynamicsWrapper);

    @Select("select lud.*,'1' isLike\n" +
            "from life_user_dynamics lud \n" +
            "where lud.delete_flag = 0 \n" +
            "and lud.id in (select llr.huifu_id from life_like_record llr where llr.dianzan_id = #{phoneId} and llr.delete_flag = 0)")
    List<LifeUserDynamicsVo> selectDianZanList(String phoneId);
}