package cn.axzo.msg.center.dal.mapper;

import cn.axzo.msg.center.domain.dto.PendingCalendarCodeDTO;
import cn.axzo.msg.center.domain.entity.PendingMessageRecord;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
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 java.util.Date;
import java.util.List;

/**
 * @description
 *   待办消息记录Mapper
 * @author cold_blade
 * @date 2023/9/20
 * @version 1.0
 */
public interface PendingMessageRecordMapper extends BaseMapper<PendingMessageRecord> {
    List<PendingMessageRecord> queryByTemplateCodes(@Param("pendingCalendarCodeDTO") PendingCalendarCodeDTO pendingCalendarCodeDTO, @Param("workspaceId") Long workspaceId,
                                                    @Param("selectDate") Date selectDate, @Param("personId") Long personId,@Param("isNowDay")  Boolean isNowDay);

    @Select("select pmr.*,GROUP_CONCAT(pmr.executor_name) AS executorName,CASE WHEN biz_category='FLOW' THEN biz_code WHEN `request_no` !='' THEN request_no ELSE id END AS biz_group from (select * from pending_message_record ${ew.customSqlSegment})pmr group by biz_group ORDER BY CASE WHEN pmr.`state` ='COMPLETED' THEN pmr.update_at ELSE pmr.`create_at` END DESC")
    List<PendingMessageRecord> selectPageWithCustomSql(@Param(Constants.WRAPPER) Wrapper<PendingMessageRecord> wrapper, IPage<PendingMessageRecord> page);

    @Select("select count(1) from (SELECT CASE WHEN biz_category='FLOW' THEN biz_code WHEN `request_no` !='' THEN request_no ELSE id END AS biz_group FROM pending_message_record ${ew.customSqlSegment} group by biz_group ) pmr")
    Integer selecCustomSqlCount(@Param(Constants.WRAPPER) Wrapper<PendingMessageRecord> wrapper);
}

