package com.njcn.gather.aireport.reportapproval.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.gather.aireport.reportapproval.pojo.param.ReportApprovalParam;
import com.njcn.gather.aireport.reportapproval.pojo.po.ReportApprovalLogPO;
import com.njcn.gather.aireport.reportapproval.pojo.vo.ReportApprovalLogVO;
import com.njcn.gather.aireport.reportapproval.pojo.vo.ReportApprovalPendingVO;
import com.njcn.gather.aireport.reportmodel.pojo.vo.ReportModelUserNameVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Collection;
import java.util.List;

@Mapper
public interface ReportApprovalMapper extends BaseMapper<ReportApprovalLogPO> {

    @Select({
            "<script>",
            "select id, name from sys_user where id in",
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    List<ReportModelUserNameVO> selectUserNamesByIds(@Param("ids") Collection<String> ids);

    @Select({
            "<script>",
            "select count(1) from (",
            "<if test='param.reportType == null or param.reportType == \"\" or param.reportType == \"REPORT_MODEL\"'>",
            "select model.id",
            "from ai_reportmodel model",
            "where model.status = 1 and model.state = '02'",
            "</if>",
            "<if test='param.reportType == null or param.reportType == \"\"'>",
            "union all",
            "</if>",
            "<if test='param.reportType == null or param.reportType == \"\" or param.reportType == \"TEST_REPORT\"'>",
            "select report.id",
            "from ai_testreport report",
            "where report.status = 1 and report.state = '02'",
            "</if>",
            ") pending",
            "</script>"
    })
    long countPendingReports(@Param("param") ReportApprovalParam.PendingQueryParam param);

    @Select({
            "<script>",
            "select * from (",
            "<if test='param.reportType == null or param.reportType == \"\" or param.reportType == \"REPORT_MODEL\"'>",
            "select 'REPORT_MODEL' as reportType, model.id as reportId, model.name as reportName, model.state,",
            "model.update_by as submitterId, user.name as submitterName, model.update_time as submitTime",
            "from ai_reportmodel model",
            "left join sys_user user on user.id = model.update_by",
            "where model.status = 1 and model.state = '02'",
            "</if>",
            "<if test='param.reportType == null or param.reportType == \"\"'>",
            "union all",
            "</if>",
            "<if test='param.reportType == null or param.reportType == \"\" or param.reportType == \"TEST_REPORT\"'>",
            "select 'TEST_REPORT' as reportType, report.id as reportId, report.no as reportName, report.state,",
            "report.update_by as submitterId, user.name as submitterName, report.update_time as submitTime",
            "from ai_testreport report",
            "left join sys_user user on user.id = report.update_by",
            "where report.status = 1 and report.state = '02'",
            "</if>",
            ") pending",
            "order by pending.submitTime desc",
            "limit #{offset}, #{size}",
            "</script>"
    })
    List<ReportApprovalPendingVO> selectPendingPage(@Param("offset") long offset,
                                                    @Param("size") long size,
                                                    @Param("param") ReportApprovalParam.PendingQueryParam param);

    @Select({
            "<script>",
            "select id, report_type as reportType, report_id as reportId, report_name as reportName,",
            "approval_result as approvalResult, approval_reason as approvalReason, before_state as beforeState,",
            "after_state as afterState, approver_name as approverName, approval_time as approvalTime",
            "from ai_report_approval_log",
            "where status = 1",
            "<if test='param.reportType != null and param.reportType != \"\"'>",
            "and report_type = #{param.reportType}",
            "</if>",
            "order by approval_time desc, create_time desc",
            "</script>"
    })
    Page<ReportApprovalLogVO> selectLogPage(Page<ReportApprovalLogVO> page,
                                            @Param("param") ReportApprovalParam.LogQueryParam param);
}
