package shop.alien.mapper;

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.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("select dyna1.* from (with dynamice as( " +
            "select lud.id, lud.top_status, lud.top_time, lud.title, lud.phone_id phoneId, " +
            "lud.context, lud.image_path, lud.address, lud.address_name, lud.address_context, " +
            "lud.liulan_count, lud.dianzan_count, lud.type, lud.created_time, substring_index(lud.phone_id, '_', 1) flag, " +
            "substring_index(lud.phone_id, '_', -1) phone, lud.draft , lud.address_province, lud.transfer_count " +
            "from life_user_dynamics lud " +
            "where lud.delete_flag = 0 and lud.enable_status = 0 and lud.draft = 0 and " +
            "not exists (select 1 from life_user_violation luv where luv.delete_flag = 0 and luv.processing_status = 1 " +
            "AND luv.dynamics_id = lud.id) order by lud.created_time desc) " +
            "select dynamice.*, info.store_name userName, user.head_img userImage, info.id storeUserId, user.id storeOrUserId, 0 isExpert, info.score_avg scoreAvg, info.business_section businessSection, info.business_type_name businessTypeName " +
            "from dynamice " +
            "join store_user user on dynamice.phone = user.phone and user.delete_flag = 0 " +
            "and user.status = 0 and user.logout_flag = 0 " +
            "join store_info info on info.id = user.store_id and info.delete_flag = 0 " +
            "and info.store_status = 1 " +
            "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, 0 scoreAvg, 0 businessSection, 0 businessTypeName " +
            "from dynamice " +
            "join life_user user on dynamice.phone = user.user_phone and user.delete_flag = 0 " +
            "and user.logout_flag = 0 " +
            "left join life_user_expert  lue on lue.user_id = user.id and lue.delete_flag = 0 " +
            "where dynamice.flag = 'user') dyna1 order by dyna1.top_status desc, dyna1.top_time desc, created_time desc")
    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> getStoreDynamicslistWithWrapper(@Param("userId") String userId, @Param(Constants.WRAPPER) QueryWrapper<LifeUserDynamics> dynamicsWrapper);

    @Select("with middle_lud as (\n" +
            "  select distinct  \n" +
            "    lud.*,\n" +
            "    '1' as isLike  \n" +
            "  from life_user_dynamics lud\n" +
            "  inner join (\n" +
            "    select distinct huifu_id  \n" +
            "    from life_like_record llr\n" +
            "    where llr.dianzan_id = #{phoneId}\n" +
            "      and llr.delete_flag = 0\n" +
            "  ) llr_sub on lud.id = llr_sub.huifu_id\n" +
            "  where lud.delete_flag = 0\n" +
            "    and not exists (  -- 替代NOT IN，避免NULL问题\n" +
            "      select 1 from life_blacklist lb\n" +
            "      where lb.blocker_phone_id = #{phoneId}\n" +
            "        and lb.delete_flag = 0\n" +
            "        and lb.blocked_phone_id = lud.phone_id\n" +
            "    )\n" +
            ")\n" +
            "select middle_lud.*,\n" +
            "  CASE \n" +
            "    WHEN lf.id is not null THEN 1\n" +
            "    ELSE 0                        \n" +
            "  END AS isFollowThis,  \n" +
            "  CASE \n" +
            "    WHEN lf1.id is not null THEN 1\n" +
            "    ELSE 0                        \n" +
            "  END AS isFollowMe  \n" +
            "from middle_lud\n" +
            "left join life_fans lf on lf.fans_id = #{phoneId} \n" +
            "                      and lf.followed_id = middle_lud.phone_id\n" +
            "                      and lf.delete_flag = '0'" +
            "left join life_fans lf1 on lf1.fans_id = middle_lud.phone_id \n" +
            "                      and lf1.followed_id = #{phoneId}\n" +
            "                      and lf1.delete_flag = '0'")
    List<LifeUserDynamicsVo> selectDianZanList(String phoneId);

    List<LifeUserDynamicsVo> getDynamicsList(@Param("nickName") String nickName, @Param("userType") String userType, @Param("dynamicsType") Integer dynamicsType, @Param("releaseStartTime") String releaseStartTime, @Param("releaseEndTime") String releaseEndTime, @Param("storeName") String storeName);

    List<LifeUserDynamicsVo> getDynamicsDetail(@Param("id") Integer id);

    List<LifeUserDynamicsVo> getStoreDynamicslist(@Param("userId") String userId, @Param("phoneId") String phoneId);

    @Select("select * from life_user_dynamics ${ew.customSqlSegment}")
    LifeUserDynamics selectAll(@Param(Constants.WRAPPER) LambdaQueryWrapper<LifeUserDynamics> and);
}