package com.liquidnet.service.adam.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.liquidnet.service.adam.dto.AdamCaomeiBadgeApplyAuditDetailDto;
import com.liquidnet.service.adam.dto.AdamCaomeiBadgeApplyAuditDto;
import com.liquidnet.service.adam.entity.AdamCaomeiBadgeApplyRecord;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface AdamCaomeiBadgeApplyRecordMapper extends BaseMapper<AdamCaomeiBadgeApplyRecord> {
    @Select({
            "<script>",
            "select ",
            "ar.apply_record_id as applyRecordId,",
            "ifnull(u.mobile, '') as userId,",
            "ifnull(ui.nickname, '') as nickname,",
            "ifnull(arn.name, '') as realName,",
            "ifnull(arn.id_card, '') as idCard,",
            "ifnull(b.name, '') as badgeName,",
            "ifnull(kp.title, ar.performance_id) as performanceName,",
            "ar.proof_image_url as proofImageUrl,",
            "ar.audit_status as auditStatus,",
            "ar.created_at as createdAt ",
            "from adam_caomei_badge_apply_record ar ",
            "left join adam_user u on ar.user_id = u.uid ",
            "left join adam_user_info ui on ar.user_id = ui.uid ",
            "left join (",
            "  select r.uid, r.name, r.id_card ",
            "  from adam_real_name r ",
            "  inner join (",
            "    select uid, max(mid) as max_mid from adam_real_name where state = 1 group by uid",
            "  ) rm on r.uid = rm.uid and r.mid = rm.max_mid and r.state = 1",
            ") arn on ar.user_id = arn.uid ",
            "left join adam_caomei_badge b on ar.badge_id = b.badge_id ",
            "left join kylin_performances kp on ar.performance_id = kp.performances_id ",
            "where 1 = 1 ",
            "<if test='userName != null and userName != \"\"'>",
            "and (ui.nickname like concat('%', #{userName}, '%') or arn.name like concat('%', #{userName}, '%')) ",
            "</if>",
            "<if test='auditStatus != null'>",
            "and ar.audit_status = #{auditStatus} ",
            "</if>",
            "order by case when ar.audit_status = 0 then 0 else 1 end asc, ar.created_at desc",
            "</script>"
    })
    List<AdamCaomeiBadgeApplyAuditDto> selectAuditList(@Param("userName") String userName,
                                                       @Param("auditStatus") Integer auditStatus);

    @Select({
            "select ",
            "ar.apply_record_id as applyRecordId,",
            "ifnull(u.mobile, '') as userId,",
            "ifnull(ui.nickname, '') as nickname,",
            "ifnull(arn.name, '') as realName,",
            "ifnull(arn.id_card, '') as idCard,",
            "ar.badge_id as badgeId,",
            "ifnull(b.name, '') as badgeName,",
            "ar.performance_id as performanceId,",
            "ifnull(kp.title, ar.performance_id) as performanceName,",
            "ar.proof_image_url as proofImageUrl,",
            "ar.audit_status as auditStatus,",
            "ifnull(ar.reject_reason, '') as rejectReason,",
            "ar.created_at as createdAt,",
            "ar.updated_at as updatedAt ",
            "from adam_caomei_badge_apply_record ar ",
            "left join adam_user u on ar.user_id = u.uid ",
            "left join adam_user_info ui on ar.user_id = ui.uid ",
            "left join (",
            "  select r.uid, r.name, r.id_card ",
            "  from adam_real_name r ",
            "  inner join (select uid, max(mid) as max_mid from adam_real_name where state = 1 group by uid) rm ",
            "    on r.uid = rm.uid and r.mid = rm.max_mid and r.state = 1",
            ") arn on ar.user_id = arn.uid ",
            "left join adam_caomei_badge b on ar.badge_id = b.badge_id ",
            "left join kylin_performances kp on ar.performance_id = kp.performances_id ",
            "where ar.apply_record_id = #{applyRecordId} ",
            "limit 1"
    })
    AdamCaomeiBadgeApplyAuditDetailDto selectAuditDetailByApplyRecordId(@Param("applyRecordId") String applyRecordId);

    @Update({
            "update adam_caomei_badge_apply_record ",
            "set audit_status = 1, reject_reason = null, updated_at = now() ",
            "where apply_record_id = #{applyRecordId} and audit_status = 0"
    })
    int passAudit(@Param("applyRecordId") String applyRecordId);

    @Update({
            "update adam_caomei_badge_apply_record ",
            "set audit_status = 2, reject_reason = #{rejectReason}, updated_at = now() ",
            "where apply_record_id = #{applyRecordId} and audit_status = 0"
    })
    int rejectAudit(@Param("applyRecordId") String applyRecordId, @Param("rejectReason") String rejectReason);

    @Insert({
            "insert ignore into adam_caomei_user_badge (user_id, badge_id, source, created_at) ",
            "values (#{userId}, #{badgeId}, 3, now())"
    })
    int issueBadgeIfAbsent(@Param("userId") String userId, @Param("badgeId") String badgeId);
}
