<?xml version="1.0" encoding="UTF-8"?>

<!--
  ~
  ~      Copyright (c) 2018-2025, lengleng All rights reserved.
  ~
  ~  Redistribution and use in source and binary forms, with or without
  ~  modification, are permitted provided that the following conditions are met:
  ~
  ~ Redistributions of source code must retain the above copyright notice,
  ~  this list of conditions and the following disclaimer.
  ~  Redistributions in binary form must reproduce the above copyright
  ~  notice, this list of conditions and the following disclaimer in the
  ~  documentation and/or other materials provided with the distribution.
  ~  Neither the name of the yifu4cloud.com developer nor the names of its
  ~  contributors may be used to endorse or promote products derived from
  ~  this software without specific prior written permission.
  ~  Author: lengleng (wangiegie@gmail.com)
  ~
  -->

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.yifu.cloud.plus.v1.ekp.mapper.EkpAlertTableMapper">

  <resultMap id="budgetWarningMap" type="com.yifu.cloud.plus.v1.ekp.vo.BudgetWarningVo">
    <id property="km" column="km"/>
    <result property="at" column="at"/>
    <result property="ye" column="ye"/>
    <result property="bl" column="bl"/>
    <result property="cbzx" column="cbzx"/>
	<result property="txr" column="txr"/>

  </resultMap>
	<resultMap id="ekpAlertTableMap" type="com.yifu.cloud.plus.v1.ekp.entity.EkpAlertTable">
		<id property="fdId" column="fd_id"/>
		<result property="fdAlertSubject" column="fd_alert_subject"/>
		<result property="fdAlertSubjectText" column="fd_alert_subject_text"/>
		<result property="fdEffectiveStartDate" column="fd_effective_start_date"/>
		<result property="fdEffectiveEndDate" column="fd_effective_end_date"/>
		<result property="fdDetectionMethod" column="fd_detection_method"/>
		<result property="fdThresholdValue" column="fd_threshold_value"/>
		<result property="fdIsActive" column="fd_is_active"/>
		<result property="fdAlertMessage" column="fd_alert_message"/>
		<result property="fdEverydayTime" column="fd_everyday_time"/>
		<result property="fdChecktype" column="fd_checktype"/>
		<result property="fdMonthday" column="fd_monthday"/>
	</resultMap>
    <sql id="Base_Column_List">
                a.fd_id,
                a.fd_alert_subject,
                a.fd_alert_subject_text,
                a.fd_effective_start_date,
                a.fd_effective_end_date,
                a.fd_detection_method,
                a.fd_threshold_value,
                a.fd_is_active,
                a.fd_alert_message,
                a.fd_everyday_time,
                a.fd_checktype,
                a.fd_monthday
    </sql>
    <sql id="ekpAlertTable_where">
        <if test="ekpAlertTable != null">
                    <if test="ekpAlertTable.fdId != null and ekpAlertTable.fdId.trim() != ''">
                        AND a.fd_id = #{ekpAlertTable.fdId}
                    </if>
                    <if test="ekpAlertTable.fdAlertSubject != null and ekpAlertTable.fdAlertSubject.trim() != ''">
                        AND a.fd_alert_subject = #{ekpAlertTable.fdAlertSubject}
                    </if>
                    <if test="ekpAlertTable.fdAlertSubjectText != null and ekpAlertTable.fdAlertSubjectText.trim() != ''">
                        AND a.fd_alert_subject_text = #{ekpAlertTable.fdAlertSubjectText}
                    </if>
                    <if test="ekpAlertTable.fdEffectiveStartDate != null">
                        AND a.fd_effective_start_date = #{ekpAlertTable.fdEffectiveStartDate}
                    </if>
                    <if test="ekpAlertTable.fdEffectiveEndDate != null">
                        AND a.fd_effective_end_date = #{ekpAlertTable.fdEffectiveEndDate}
                    </if>
                    <if test="ekpAlertTable.fdDetectionMethod != null and ekpAlertTable.fdDetectionMethod.trim() != ''">
                        AND a.fd_detection_method = #{ekpAlertTable.fdDetectionMethod}
                    </if>
                    <if test="ekpAlertTable.fdThresholdValue != null">
                        AND a.fd_threshold_value = #{ekpAlertTable.fdThresholdValue}
                    </if>
                    <if test="ekpAlertTable.fdIsActive != null and ekpAlertTable.fdIsActive.trim() != ''">
                        AND a.fd_is_active = #{ekpAlertTable.fdIsActive}
                    </if>
                    <if test="ekpAlertTable.fdAlertMessage != null and ekpAlertTable.fdAlertMessage.trim() != ''">
                        AND a.fd_alert_message = #{ekpAlertTable.fdAlertMessage}
                    </if>
                    <if test="ekpAlertTable.fdEverydayTime != null">
                        AND a.fd_everyday_time = #{ekpAlertTable.fdEverydayTime}
                    </if>
                    <if test="ekpAlertTable.fdChecktype != null and ekpAlertTable.fdChecktype.trim() != ''">
                        AND a.fd_checktype = #{ekpAlertTable.fdChecktype}
                    </if>
                    <if test="ekpAlertTable.fdMonthday != null">
                        AND a.fd_monthday = #{ekpAlertTable.fdMonthday}
                    </if>
        </if>
    </sql>
    <!--ekpAlertTable简单分页查询-->
    <select id="getEkpAlertTablePage" resultMap="ekpAlertTableMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ekp_alert_table a
        <where>
            1=1
            <include refid="ekpAlertTable_where"/>
        </where>
    </select>

	<select id="getBudgetWarningVos" resultMap="budgetWarningMap">
		select
			a.fd_ke_mu as 'km',
				DATE_FORMAT(NOW(),"%Y-%m-%d %H:%i") as 'at',
				ROUND((sum(a.fd_money) - sum(ifnull(b.fd_money,0))),2) as 'ye'
				,ROUND(sum(ifnull(b.fd_money,0))/sum(a.fd_money)*100,2) as 'bl',
				a.fd_cheng_ben_two as 'cbzx',c.txr
		from (select sum(a.fd_money) fd_money,a.fd_cheng_ben_two,a.fd_ke_mu,a.fd_fee_type,a.fd_id from view_xing_zheng_yu_suan_all a where a.fd_date_month like concat(DATE_FORMAT(now(),'%Y-'),'%') GROUP BY a.fd_ke_mu,a.fd_cheng_ben_two) a
			left join (
			select
				ifnull(sum(a.fd_money),0) fd_money,fd_ke_mu,fd_cheng_ben_two
			from view_xing_zheng_yu_suan a where DATE_FORMAT(a.fd_date,'%Y') = DATE_FORMAT(now(),'%Y')
			GROUP BY fd_ke_mu,fd_cheng_ben_two
		) b on b.fd_ke_mu = a.fd_ke_mu and b.fd_cheng_ben_two = a.fd_cheng_ben_two
			LEFT JOIN (
			select  GROUP_CONCAT(DISTINCT CONCAT(d.fd_login_name,"_",d.fd_id)) as 'txr',cb.fd_3c290891cded14 from  ekp_3cf9b7112e544ba9fe18 cb
			LEFT JOIN ekporg_18ca06406b8e5d35104b t on t.fd_parent_id=cb.fd_id
			LEFT JOIN sys_org_person d on t.sys_org_person_id=d.fd_id
			GROUP BY cb.fd_id
		) c on c.fd_3c290891cded14 = a.fd_cheng_ben_two
		where a.fd_ke_mu = #{km}
		GROUP BY a.fd_ke_mu,a.fd_cheng_ben_two ORDER BY a.fd_id
	</select>

	<select id="getEkpAlertTableList" resultMap="ekpAlertTableMap">
		SELECT
		<include refid="Base_Column_List"/>
		FROM ekp_alert_table a
		<where>
			1=1
			AND a.fd_effective_start_date <![CDATA[ <= ]]>  NOW()
			AND a.fd_effective_end_date >= NOW()
			AND a.fd_is_active = '是'
			<if test="type ==0 ">
				AND fd_checktype = '每月'
			</if>
			<if test="type ==1 ">
				AND fd_checktype = '每日'
			</if>
		</where>
	</select>
</mapper>
