package shop.alien.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import shop.alien.entity.store.LifeBrowseRecord;

import java.util.List;
import java.util.Map;

@Mapper
public interface LifeBrowseRecordMapper extends BaseMapper<LifeBrowseRecord> {

    @Select("SELECT a.id,a.liulan_date liulanDate, b.store_type storeType, b.store_position storePosition, a.liulan_time liulanTime, " +
            "c.img_url image, b.store_name storeName, b.store_address storeDetailAddress, b.id storeId, a.created_time, d.img_url entranceImage, dict.dict_detail storeTypeStr,ROUND(b.score_avg, 2) scoreAvg,b.business_types_name businessTypesName,b.business_section businessSection " +
            "FROM life_browse_record a " +
            "LEFT JOIN store_info b ON a.store_id = b.id " +
            "LEFT JOIN store_img c on b.id = c.store_id and c.img_type = 11 and c.delete_flag = 0 " +
            "LEFT JOIN store_img d on b.id = d.store_id and d.img_type = 1 and d.delete_flag = 0 " +
            "left join store_dictionary dict on dict.type_name = 'storeType' and dict.dict_id = b.store_type and dict.delete_flag = 0 " +
            "WHERE a.user_id = #{userId} AND a.delete_flag = 0 AND b.delete_flag = 0 ORDER BY a.liulan_time desc")
    List<Map<String, Object>> getBrowseRecordByUserId(String userId);
    @Select("select lbr.id,lgbm.group_name groupName,lgbm.id couponId,si.id storeId,ROUND(b.score_avg, 2) scoreAvg, original_price originalPrice,preferential_price preferentialPrice,lbr.liulan_date liulanDate,si.store_name storeName,si2.img_url\n" +
            "from life_browse_record lbr \n" +
            "inner join life_group_buy_main lgbm on lgbm.id = lbr.coupon_id \n" +
            "and lgbm.delete_flag = 0 and lbr.delete_flag = 0\n" +
            "left join store_info si on si.id = lgbm.store_id and si.delete_flag = 0\n" +
            "left join store_img si2 on si2.id = SUBSTRING_INDEX(lgbm.image_id, ',', 1) and si2.delete_flag = 0\n" +
            "where lbr.user_id = #{userId} and lbr.coupon_id is not null\n" +
            "Order by lbr.liulan_time desc")
    List<Map<String, Object>> getGroupBuyBrowseRecordByUserId(String userId);

    @Select("SELECT " +
            "lbr.id, " +
            "lud.id dynamicsId, " +
            "IFNULL(lud.cover_image, '') coverImage, " +
            "lbr.liulan_date liulanDate, " +
            "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.phone_user_type phoneUserType, " +
            "lud.phone_ref_id phoneRefId, " +
            "lud.title, " +
            "lud.context, " +
            "lud.image_path imagePath, " +
            "lud.address, " +
            "lud.address_context addressContext, " +
            "lud.address_name addressName, " +
            "lud.liulan_count liulanCount, " +
            "lud.dianzan_count dianzanCount, " +
            "lud.type, " +
            "lud.draft, " +
            "lud.created_time createdTime, " +
            "lud.updated_time updatedTime, " +
            "lud.address_province addressProvince, " +
            "lud.top_status topStatus, " +
            "lud.top_time topTime, " +
            "lud.enable_status enableStatus, " +
            "lud.expert_id expertId, " +
            "lud.business_id businessId, " +
            "lud.transfer_count transferCount, " +
            "lud.reality_count realityCount, " +
            "lud.check_flag checkFlag, " +
            "lud.ai_task_id aiTaskId, " +
            "lud.reason, " +
            "lud.delete_flag deleteFlag, " +
            "si1.id storeId, " +
            "si1.store_name storeName, " +
            "si1.score_avg scoreAvg, " +
            "si1.business_section businessSection, " +
            "si1.business_section_name businessSectionName, " +
            "ROUND(IFNULL(si1.score_one, 0), 2) scoreOne, " +
            "ROUND(IFNULL(si1.score_two, 0), 2) scoreTwo, " +
            "ROUND(IFNULL(si1.score_three, 0), 2) scoreThree, " +
            "IFNULL(cr.ratingCount, 0) ratingCount " +
            "FROM " +
            "life_browse_record lbr " +
            "INNER JOIN life_user_dynamics lud ON lud.id = lbr.dynamics_id " +
            "AND lud.delete_flag = 0 " +
            "AND lbr.delete_flag = 0 " +
            "LEFT JOIN store_user su ON su.delete_flag = 0 " +
            "AND lud.phone_user_type = 2 AND su.id = lud.phone_ref_id " +
            "LEFT JOIN store_info si1 ON si1.id = su.store_id " +
            "AND si1.delete_flag = 0 " +
            "LEFT JOIN ( " +
            "SELECT " +
            "business_id, " +
            "count(*) AS ratingCount " +
            "FROM " +
            "common_rating " +
            "WHERE " +
            "business_type = 1 " +
            "AND delete_flag = 0 " +
            "AND audit_status = 1 " +
            "AND is_show = 1 " +
            "GROUP BY " +
            "business_id " +
            ") cr ON cr.business_id = si1.id " +
            "WHERE " +
            "lbr.user_id = #{userId} " +
            "AND lbr.dynamics_id IS NOT NULL " +
            "ORDER BY " +
            "lbr.liulan_time DESC")
    List<Map<String, Object>> getDynamicsBrowseRecordByUserId(String userId);
}