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_user_type phoneUserType, lud.phone_ref_id phoneRefId, " +
            "case when lud.phone_user_type = 1 then concat('user_', (select user_phone from life_user where id = lud.phone_ref_id and delete_flag = 0 limit 1)) " +
            "when lud.phone_user_type = 2 then concat('store_', (select phone from store_user where id = lud.phone_ref_id and delete_flag = 0 limit 1)) " +
            "when lud.phone_user_type = 3 then concat('lawyer_', (select phone from lawyer_user where id = lud.phone_ref_id and delete_flag = 0 limit 1)) " +
            "end 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, " +
            "case when lud.phone_user_type = 1 then 'user' when lud.phone_user_type = 2 then 'store' when lud.phone_user_type = 3 then 'lawyer' end flag, " +
            "case when lud.phone_user_type = 1 then (select user_phone from life_user where id = lud.phone_ref_id and delete_flag = 0 limit 1) " +
            "when lud.phone_user_type = 2 then (select phone from store_user where id = lud.phone_ref_id and delete_flag = 0 limit 1) " +
            "when lud.phone_user_type = 3 then (select phone from lawyer_user where id = lud.phone_ref_id and delete_flag = 0 limit 1) end phone, " +
            "lud.draft , lud.address_province, lud.transfer_count, lud.cover_image " +
            "from life_user_dynamics lud " +
            "where lud.delete_flag = 0 and lud.enable_status = 0 and lud.draft = 0 " +
            "and lud.phone_user_type is not null and lud.phone_ref_id is not null 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.phoneUserType = 2 and user.id = dynamice.phoneRefId 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.phoneUserType = 2 " +
            "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.phoneUserType = 1 and user.id = dynamice.phoneRefId 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.phoneUserType = 1) 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, " +
            "case when lud.phone_user_type = 2 then su.head_img else lu.user_image end user_image, " +
            "case when lud.phone_user_type = 2 then su.nick_name else lu.user_name end user_name, " +
            "IF(llr.huifu_id IS NOT NULL, 1, 0) AS isLike\n" +
            "from life_user_dynamics lud\n" +
            "left join store_user su on lud.phone_user_type = 2 and su.id = lud.phone_ref_id and su.delete_flag = 0\n" +
            "left join life_user lu on lud.phone_user_type = 1 and lu.id = lud.phone_ref_id and lu.delete_flag = 0\n" +
            "left join life_like_record llr\n" +
            "on llr.huifu_id = lud.id\n" +
            "and llr.delete_flag = 0\n" +
            "and llr.dianzan_user_type = 1 and llr.dianzan_ref_id = #{userId}" +
            "${ew.customSqlSegment}")
    List<LifeUserDynamicsVo> getStoreDynamicslistWithWrapper(@Param("userId") String userId, @Param(Constants.WRAPPER) QueryWrapper<LifeUserDynamics> dynamicsWrapper);

    @Select("WITH middle_lud AS (\n" +
            "    SELECT \n" +
            "        lud.*,\n" +
            "        '1' AS isLike,\n" +
            "        llr_sub.like_created_time\n" +
            "    FROM life_user_dynamics lud\n" +
            "    INNER JOIN (\n" +
            "        SELECT \n" +
            "            huifu_id, \n" +
            "            MAX(created_time) AS like_created_time\n" +
            "        FROM life_like_record llr\n" +
            "        WHERE \n" +
            "        llr.dianzan_user_type = #{viewerUserType}\n" +
            "        and llr.dianzan_ref_id = #{viewerRefId}\n" +
            "            AND llr.delete_flag = 0\n" +
            "        GROUP BY huifu_id\n" +
            "    ) llr_sub ON lud.id = llr_sub.huifu_id\n" +
            "    WHERE lud.delete_flag = 0\n" +
            "        AND NOT EXISTS (\n" +
            "            SELECT 1 \n" +
            "            FROM life_blacklist lb\n" +
            "            WHERE lb.blocker_id = #{viewerRefId}\n" +
            "            and lb.blocked_type = #{viewerUserType}\n" +
            "                AND lb.delete_flag = 0\n" +
            "                AND  lb.blocker_id = lud.phone_ref_id \n" +
            "            and lb.blocked_type = lud.phone_user_type \n" +
            "        )\n" +
            ")\n" +
            "SELECT \n" +
            "    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 \n" +
            "    ON \n" +
            "    lf.fans_ref_id = #{viewerRefId} and\n" +
            "    lf.fans_user_type = #{viewerUserType} and\n" +
            "    lf.followed_ref_id = middle_lud.phone_ref_id and \n" +
            "    lf.followed_user_type = middle_lud.phone_user_type \n" +
            "    AND lf.delete_flag = '0'\n" +
            "LEFT JOIN life_fans lf1 \n" +
            "    ON \n" +
            "     lf1.fans_ref_id = middle_lud.phone_ref_id and\n" +
            "    lf1.fans_user_type = middle_lud.phone_user_type  and\n" +
            "     lf1.followed_ref_id = #{viewerRefId} and \n" +
            "    lf1.followed_user_type = #{viewerUserType}\n" +
            "    AND lf1.delete_flag = '0'\n" +
            "ORDER BY middle_lud.like_created_time DESC")
    List<LifeUserDynamicsVo> selectDianZanList(@Param("viewerUserType") Integer viewerUserType,
                                               @Param("viewerRefId") Integer viewerRefId);

    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("phoneUserType") Integer phoneUserType,
                                                  @Param("phoneRefId") Integer phoneRefId);

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