package shop.alien.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import shop.alien.entity.store.StoreCommentAppeal;
import shop.alien.entity.store.vo.StoreCommentAppealVo;

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

/**
 * 评论申诉表 Mapper 接口
 *
 * @author ssk
 * @since 2025-01-02
 */
public interface StoreCommentAppealMapper extends BaseMapper<StoreCommentAppeal> {

    /**
     * 申诉历史-分页查询
     *
     * @param page         分页参数
     * @param queryWrapper 查询条件
     * @return IPage<StoreCommentAppealVo>
     */
    @Select("select a.*, b.dict_detail appeal_status_str, c.comment_content, c.comment_star, c.score score, c.other_score,c.is_anonymous,c.img_id commentImgId, d.user_name, d.user_image, e.store_name, f.phone store_phone, f.name store_contact, e.store_type " +
            "from store_comment_appeal a " +
            "left join store_dictionary b on a.appeal_status = b.dict_id and b.type_name = 'appealStatus' and b.delete_flag = 0 " +
            "left join store_comment c on a.comment_id = c.id " +
            "left join life_user d on c.user_id = d.id and d.delete_flag = 0 " +
            "left join store_info e on a.store_id = e.id and e.delete_flag = 0 " +
            "Left join store_user f on e.id = f.store_id and f.delete_flag = 0 ${ew.customSqlSegment}")
    IPage<StoreCommentAppealVo> getStoreCommentAppealPage(IPage<StoreCommentAppealVo> page, @Param(Constants.WRAPPER) QueryWrapper<StoreCommentAppealVo> queryWrapper);

    /**
     * 申诉历史-全部查询
     *
     * @param queryWrapper 查询条件
     * @return IPage<StoreCommentAppealVo>
     */
    @Select("select a.*, b.dict_detail appeal_status_str, c.comment_content, c.comment_star, d.user_name, d.user_image, e.store_name, f.phone store_phone, f.name store_contact, e.store_type from store_comment_appeal a " +
            "left join store_dictionary b on a.appeal_status = b.dict_id and b.type_name = 'appealStatus' " +
            "left join store_comment c on a.comment_id = c.id " +
            "left join life_user d on c.user_id = d.id  and d.delete_flag = 0 " +
            "left join store_info e on a.store_id = e.id " +
            "Left join store_user f on e.id = f.store_id and f.delete_flag = 0 ${ew.customSqlSegment}")
    List<StoreCommentAppealVo> getStoreCommentAppealPage(@Param(Constants.WRAPPER) QueryWrapper<StoreCommentAppealVo> queryWrapper);

    /**
     * 申诉详情
     *
     * @param queryWrapper 查询条件
     * @return StoreCommentAppealVo
     */
    @Select("select a.*, b.dict_detail appeal_status_str, c.comment_content, c.score, c.other_score, a.created_time comment_time, c.comment_star,c.is_anonymous,c.img_id commentImgId, d.user_name, e.phone store_phone, d.user_name userName, d.user_image userImage " +
            "from store_comment_appeal a " +
            "left join store_dictionary b on a.appeal_status = b.dict_id and b.type_name = 'appealStatus' " +
            "left join store_comment c on a.comment_id = c.id  " +
            "left join life_user d on c.user_id = d.id and d.delete_flag = 0 " +
            "left join store_user e on a.store_id = e.store_id and e.delete_flag = 0  ${ew.customSqlSegment}")
    StoreCommentAppealVo getCommentDetail(@Param(Constants.WRAPPER) QueryWrapper<StoreCommentAppealVo> queryWrapper);


    /**
     * 申诉列表
     *
     * @return List<Map < String, Object>>
     */
    @Select("SELECT sca.id, sca.appeal_reason, sca.appeal_status, sc.comment_content, si.img_url, sci.user_img_url, sc.id comment_id, sc.business_id " +
            "FROM store_comment_appeal sca " +
            "LEFT JOIN store_comment sc ON sca.comment_id = sc.id " +
            "LEFT JOIN store_img si ON sca.img_id = si.id " +
            "LEFT JOIN (SELECT sc.id id, si.img_url user_img_url FROM store_comment sc LEFT JOIN store_img si ON sc.img_id = si.id AND sc.delete_flag = 0) sci ON sci.id = sca.comment_id " +
            "WHERE sca.appeal_status = 0 and record_id is null AND sca.delete_flag = 0")
    List<Map<String, Object>> getAppealList();


    @Select("SELECT sca.id, sca.appeal_status, sca.final_result, sca.comment_id, sca.record_id " +
            "FROM store_comment_appeal sca " +
            "WHERE sca.appeal_status = 3")
    List<StoreCommentAppeal> getPendingAppeals();

    @Update("UPDATE store_comment_appeal " +
            "SET appeal_status = #{appealStatus}, final_result = #{finalResult} " +
            "WHERE record_id = #{recordId}")
    void updateByRecordId(@Param("recordId") Integer recordId,
                          @Param("appealStatus") Integer appealStatus,
                          @Param("finalResult") String finalResult);
}
