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.ZbReportMonthScore;
import thyyxxk.webserver.entity.highreport.ZbReportResult;
import thyyxxk.webserver.entity.highreport.ZdReportType;
import thyyxxk.webserver.entity.targetmanagement.TargetManagement;

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, report_cycle) values " +
    " (#{id},#{pid},#{year},#{dataType},#{childResult}, " +
    " #{momResult},#{calcResult},#{op},#{opId},#{opTime},#{reportCycle}  " +
    " ) " +
    "</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);

    @Select("<script>" +
            " select * from zb_report_result zb " +
            " where 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> selectReportResultByPid(@Param("pid") String pid, @Param("startTime") String startTime, @Param("endTime") String endTime);

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

    @Select("<script>" +
            " select * from zb_report_month_score zb " +
            " where zb.id = #{id} " +
            " and zb.pid = #{pid} " +
            " and zb.month = #{month} " +
            "</script>")
    ZbReportMonthScore selectReportResultByMonth(@Param("id") String id, @Param("pid") String pid, @Param("month") String month);

    @Delete("<script>" +
            " delete zb from zb_report_month_score zb " +
            " where zb.id = #{id} " +
            " and zb.pid = #{pid} " +
            " and zb.month = #{month} " +
            "</script>")
    void deleteReportResultMonthById(@Param("id") String id, @Param("pid") String pid, @Param("month") String month);

    @Insert("<script>" +
            " insert into zb_report_month_score " +
            " (id, pid, month, name, child_result, mom_result, calc_result, data_type, child_id, mom_id, " +
            "  dept_id, dept_name, author_id, author_name, score, create_time) values " +
            " (#{id},#{pid},#{month},#{name},#{childResult},#{momResult},#{calcResult},#{dataType},#{childId},#{momId}, " +
            " #{deptId},#{deptName},#{authorId},#{authorName},#{score},#{createTime} " +
            " ) " +
            "</script>")
    int saveTargetReportMonthResult(ZbReportMonthScore score);

    @Select("<script>" +
            "select info.id, info.pid, info.name, info.dept, info.dept_code, info.op, info.op_id, info.op, info.op_id as opId, " +
            " f.report_cycle as reportCycle, f.report_score as reportScore, f. criterion_score as criterionScore, " +
            " f.year_range as yearRange, f.remark " +
            "from zb_zd_base_info info " +
            "left join (select score.*, cycle.year_range, cycle.remark " +
            " from zb_report_cycle_score score, zb_report_cycle cycle " +
            " where score.report_cycle = cycle.report_cycle and cycle.report_cycle = #{reportCycle}) f on info.id = f.id " +
            "where info.state = 'Y' and info.is_leaf = '1' " +
            " <if test=\"dept != null and dept != '' \"> " +
            " and info.dept_code = #{dept} " +
            " </if> " +
            " <if test=\"id != null and id != '' \"> " +
            " and info.id = #{id} " +
            " </if> " +
            " order by len(case when charindex('-', info.sort) = 0 then info.sort else substring(info.sort,1,charindex('-',info.sort)-1) end) " +
            "</script>")
    List<Map<String, Object>> selectTargetCycleScore(TargetManagement dto);

    @Select("<script>" +
            "select r.* from zb_report_result r " +
            " where r.report_cycle = #{reportCycle} " +
            " <if test=\"id != null and id != '' \"> " +
            " and r.id = #{id} " +
            " </if> " +
            " <if test=\"pid != null and pid != '' \"> " +
            " and r.pid = #{id} " +
            " </if> " +
            "</script>")
    List<ZbReportResult> selectTargetReportResultByCycle(@Param("reportCycle") String reportCycle, @Param("id") String id, @Param("pid") String pid);

    @Select("<script>" +
            " select zb.* from zb_report_month_score zb " +
            " where 1 = 1" +
            " <if test=\"id != null and id != '' \"> " +
            " and zb.id = #{id} " +
            " </if> " +
            " <if test=\"pid != null and pid != '' \"> " +
            " and zb.pid = #{pid} " +
            " </if> " +
            " <if test=\"dept != null and dept != '' \"> " +
            " and zb.dept_id = #{dept} " +
            " </if> " +
            " <if test=\"startTime != null and startTime != '' \"> " +
            " and zb.month &gt;= #{startTime} " +
            " </if> " +
            " <if test=\"endTime != null and endTime != '' \"> " +
            " and zb.month &lt;= #{endTime} " +
            " </if> " +
            "</script>")
    List<ZbReportMonthScore> selectTargetMonthScoreSummary(TargetManagement dto);

    @Select("<script> " +
            " select * from report_base_info where flag = 'Y' and report_type = 'sg' " +
            " <if test=\"id != null and id != '' \"> " +
            " and report_id = #{id} " +
            " </if> " +
            " <if test=\"text != null and text != '' \"> " +
            "    and (isnull(report_id,'') like 'sg_${text}%' or isnull(report_name,'') like '${text}%' ) " +
            " </if> " +
            " order by sort " +
            "</script> ")
    List<ReportBaseInfo> selectTargetDictConstantData(TargetManagement dto);

    @Update(" update zb_report_result set file_id = #{fileId} " +
            "  where id = #{id} and pid = #{pid} and year = #{year} ")
    void updateZbReportResultFileIdById(ZbReportResult result);
}
