package thyyxxk.webserver.dao.his.highreport;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.highreport.HighReportDto;
import thyyxxk.webserver.entity.highreport.ReportBaseInfo;
import thyyxxk.webserver.entity.highreport.TableDisplayInfo;
import thyyxxk.webserver.entity.highreport.TableGroupInfo;
import thyyxxk.webserver.entity.highreport.XYBean;
import thyyxxk.webserver.entity.highreport.ZbReportResult;
import thyyxxk.webserver.entity.highreport.ZdReportType;

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

/**
 * @description: 报表
 * @author: hsh
 * @date: 2022/11/2 10:32
 **/
@Mapper
public interface HighReportDao {

    @Select("<script> " +
            " select * from report_base_info where flag = 'Y' " +
            " <if test=\"reportType != null and reportType != '' \"> " +
            " and report_type = #{reportType} " +
            " </if> " +
            " <if test=\"menuId != null and menuId != '' \"> " +
            " and menu_id = #{menuId} " +
            " </if> " +
            " order by sort " +
            "</script> ")
    List<ReportBaseInfo> selectReportPortal(@Param("reportType")String reportType, @Param("menuId")String menuId);

    @Select("<script> " +
            " select * from report_base_info where flag = 'Y' " +
            " and report_id = #{reportId} " +
            " <if test=\"reportType != null and reportType != '' \"> " +
            " and report_type = #{reportType} " +
            " </if> " +
            " <if test=\"menuId != null and menuId != '' \"> " +
            " and menu_id = #{menuId} " +
            " </if> " +
            " order by sort " +
            "</script> ")
    ReportBaseInfo selectReportPortalMenu(@Param("reportId") String reportId, @Param("menuId")String menuId, @Param("reportType")String reportType);

    @Select(" select * from table_display_info where flag = 'Y' " +
            " and report_id = #{reportId} " +
            " order by sort ")
    List<TableDisplayInfo> selectTableDisplayInfoByReportId(@Param("reportId") String reportId);

    @Select(" select top 1 * from table_group_info where flag = 'Y' " +
            " and report_id = #{reportId} " +
            " and level_id = #{level} ")
    TableGroupInfo selectTableGroupInfoByLevel(@Param("reportId") String reportId, @Param("level") String level);

    @Select("<script> ${sql} </script>")
    List<Map<String, Object>> selectReportPortalData(@Param("reportId") String reportId, @Param("menuId")String menuId,
                                                     @Param("reportType")String reportType, @Param("sql")String sql);

    @Select("<script> ${sql} </script>")
    List<XYBean> selectReportPortalXYBean(@Param("reportId") String reportId, @Param("menuId")String menuId,
                                          @Param("reportType")String reportType, @Param("sql")String sql);

    @Select("<script> ${sql} </script>")
    String selectReportPortalOverview(@Param("reportId") String reportId, @Param("menuId")String menuId,
                               @Param("reportType")String reportType, @Param("sql")String sql);

    @Select(" select * from report_base_info where flag = 'Y' order by sort ")
    List<ReportBaseInfo> selectReportDictTree();

    @Update(" update report_base_info set flag = 'N' where report_id = #{reportId} ")
    int updateReportDictById(@Param("reportId") String reportId);

    @Select("<script> " +
            " select * from report_base_info where flag = 'Y' " +
            " <if test=\"reportId != null and reportId != '' \"> " +
            " and report_id = #{reportId} " +
            " </if> " +
            " order by sort " +
            "</script> ")
    List<ReportBaseInfo> selectReportDict(HighReportDto dto);

    @Select(" select * from zd_report_type where flag = 'Y' order by sort ")
    List<ZdReportType> selectReportType();

    @Select(" select report_id as code, report_label as name from zd_report_type where flag = 'Y' order by sort ")
    List<ZdReportType> selectReportTypeZd();

    @Select("<script>" +
            " select * from zb_report_result zb " +
            " where zb.id = #{id} " +
            " and zb.pid = #{pid} " +
            " and zb.year = #{year} " +
            "</script>")
    ZbReportResult selectReportResultByYear(@Param("id") String id, @Param("pid") String pid, @Param("year") String year);

    @Select("<script>" +
            " select * from zb_report_result zb " +
            " where zb.id = #{id} " +
            " and zb.pid = #{pid} " +
            " <if test=\"startTime != null and startTime != '' \"> " +
            " and zb.year &gt;= #{startTime} " +
            " </if> " +
            " <if test=\"endTime != null and endTime != '' \"> " +
            " and zb.year &lt;= #{endTime} " +
            " </if> " +
            "</script>")
    List<ZbReportResult> selectReportResultById(@Param("id") String id, @Param("pid") String pid, @Param("startTime") String startTime, @Param("endTime") String endTime);

    @Delete("<script>" +
            " delete zb from zb_report_result zb " +
            " where zb.id = #{id} " +
            " and zb.pid = #{pid} " +
            " and zb.year = #{year} " +
            "</script>")
    int deleteReportResultById(@Param("id") String id, @Param("pid") String pid, @Param("year") String year);

    @Insert("<script>" +
    " insert into zb_report_result " +
    " (id, pid, year, data_type, child_result, mom_result, calc_result, op, op_id, op_time) values " +
    " (#{id},#{pid},#{year},#{dataType},#{childResult}, " +
    " #{momResult},#{calcResult},#{op},#{opId},#{opTime} " +
    " ) " +
    "</script>")
    int saveTargetReportResult(ZbReportResult result);

    @Delete("<script>" +
            " delete from report_base_info " +
            " where report_id = #{reportId} " +
            "</script>")
    int deleteReportBaseInfoById(@Param("reportId") String reportId);

    @Insert("<script>" +
            " insert into report_base_info " +
            " (report_id, report_name, report_type, unit, level, gather_sql, base_sql, display_type, flag, " +
            "  menu_id, sort, report_sort, is_cw, is_yj, calc_child, calc_mom) values " +
            " (#{reportId},#{reportName},#{reportType},#{unit},#{level}, " +
            " #{gatherSql},#{baseSql},#{displayType},#{flag},#{menuId} " +
            " #{sort},#{reportSort},#{isCw},#{isYj},#{calcChild},#{calcMom} " +
            " ) " +
            "</script>")
    int saveReportBaseInfo(ReportBaseInfo info);

    @Select("<script>" +
            " select * from table_group_info where flag = 'Y' " +
            " and report_id = #{reportId} " +
            " <if test=\"level != null and level != '' \"> " +
            " and level_id = #{level} " +
            " </if> " +
            "</script>")
    TableGroupInfo selectTableGroupInfo(@Param("reportId") String reportId, @Param("level") String level);

    @Delete("<script>" +
            " delete from table_group_info " +
            " where report_id = #{reportId} " +
            "</script>")
    void deleteTableGroupInfoById(@Param("reportId") String reportId);

}
