package shop.alien.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import shop.alien.entity.store.TagsMain;
import shop.alien.entity.store.vo.StoreCommentVo;
import shop.alien.entity.store.vo.TagsMainVo;

import java.util.List;

/**
 * qxy
 * AI 服务标签主表
 */
@Mapper
public interface TagsMainMapper extends BaseMapper<TagsMain> {

    @Insert({
            "<script>", // 开启动态 SQL 脚本
            "INSERT INTO tags_main (",
            "  id, tag_name, delete_flag, created_time, created_user_id,",
            "  updated_time, updated_user_id, store_id, tag_type",
            ")",
            "VALUES",
            "<foreach collection='list' item='item' separator=','>",
            "(",
            "  #{item.id},",
            "  #{item.tagName},",
            "  #{item.deleteFlag},",
            "  #{item.createdTime},",
            "  #{item.createdUserId},",
            "  #{item.updatedTime},",
            "  #{item.updatedUserId},",
            "  #{item.storeId},",
            "  #{item.tagType}",
            ")",
            "</foreach>",
            "</script>" // 关闭动态 SQL 脚本
    })
    int insertBatchTagsMain(List<TagsMain> tagsMainList);


    @Select("SELECT\n" +
            "  main.id,\n" +
            "  main.tag_name,\n" +
            "  main.store_id,\n" +
            "  main.tag_type,\n" +
            "  IFNULL(GROUP_CONCAT(DISTINCT synonym.comment_id ORDER BY synonym.comment_id SEPARATOR ','), '') AS evaluate_ids,\n" +
            "  IFNULL(COUNT(DISTINCT synonym.comment_id), 0) AS evaluateNumber\n" +
            "FROM\n" +
            "  tags_main main\n" +
            "LEFT JOIN tags_synonym synonym \n" +
            "  ON main.id = synonym.main_tag_id\n" +
            "  AND synonym.delete_flag = 0  \n" +
            "WHERE\n" +
            "  main.store_id = #{storeId}\n" +
            "  AND main.delete_flag = 0\n" +
            "GROUP BY\n" +
            "  main.id, main.tag_name, main.store_id, main.tag_type  \n" +
            "ORDER BY\n" +
            "  main.id;")
    List<TagsMainVo> getStoreEvaluateTags(Integer storeId);


    @Select("SELECT\n" +
            "  ts.comment_id,\n" +
            "  MIN(ts.id) AS tag_id,  \n" +
            "  ts.main_tag_id,\n" +
            "  GROUP_CONCAT(DISTINCT ts.synonym_tag SEPARATOR ',') AS synonym_tags,  \n" +
            "  MIN(ts.delete_flag) AS tag_delete_flag,\n" +
            "  MAX(ts.created_time) AS tag_created_time,\n" +
            "  sc.*\n" +
            "FROM\n" +
            "  tags_synonym ts\n" +
            "LEFT JOIN store_comment sc \n" +
            "  ON ts.comment_id = sc.id\n" +
            "WHERE\n" +
            "  ts.main_tag_id = #{mainId}  \n" +
            "GROUP BY\n" +
            "  ts.comment_id, ts.main_tag_id,  \n" +
            "  sc.id  \n" +
            "ORDER BY\n" +
            "  sc.created_time DESC; ")
    List<StoreCommentVo> getstoreCommentList(Integer mainId);
}
