<?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.msg.mapper.MsgInfoMapper">

  <resultMap id="msgInfoMap" type="com.yifu.cloud.plus.v1.msg.entity.MsgInfo">
    <id property="id" column="ID"/>
    <result property="orderId" column="ORDER_ID"/>
    <result property="url" column="URL"/>
    <result property="orderNo" column="ORDER_NO"/>
    <result property="alertType" column="ALERT_TYPE"/>
    <result property="createTime" column="CREATE_TIME"/>
    <result property="readStatus" column="READ_STATUS"/>
    <result property="alertUser" column="ALERT_USER"/>
	<result property="domainName" column="DOMAIN_NAME"/>
  </resultMap>
    <sql id="Base_Column_List">
                a.ID,
                a.ORDER_ID,
                a.URL,
                a.ORDER_NO,
                a.ALERT_TYPE,
                a.CREATE_TIME,
                a.READ_STATUS,
                a.ALERT_USER,
				a.DOMAIN_NAME
    </sql>
    <sql id="msgInfo_where">
        <if test="msgInfo != null">
                    <if test="msgInfo.id != null and msgInfo.id.trim() != ''">
                        AND a.ID = #{msgInfo.id}
                    </if>
                    <if test="msgInfo.orderId != null and msgInfo.orderId.trim() != ''">
                        AND a.ORDER_ID = #{msgInfo.orderId}
                    </if>
                    <if test="msgInfo.url != null and msgInfo.url.trim() != ''">
                        AND a.URL = #{msgInfo.url}
                    </if>
                    <if test="msgInfo.orderNo != null and msgInfo.orderNo.trim() != ''">
                        AND a.ORDER_NO = #{msgInfo.orderNo}
                    </if>
                    <if test="msgInfo.alertType != null and msgInfo.alertType.trim() != ''">
                        AND a.ALERT_TYPE = #{msgInfo.alertType}
                    </if>
                    <if test="msgInfo.createTime != null">
                        AND a.CREATE_TIME = #{msgInfo.createTime}
                    </if>
                    <if test="msgInfo.readStatus != null and msgInfo.readStatus.trim() != ''">
                        AND a.READ_STATUS = #{msgInfo.readStatus}
                    </if>
                    <if test="msgInfo.alertUser != null and msgInfo.alertUser.trim() != ''">
                        AND a.ALERT_USER = #{msgInfo.alertUser}
                    </if>
        </if>
    </sql>
    <!--msgInfo简单分页查询-->
    <select id="getMsgInfoPage" resultMap="msgInfoMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM msg_info a
        <where>
            1=1
            <include refid="msgInfo_where"/>
        </where>
    </select>
	<select id="getMsgInfo" resultMap="msgInfoMap">
		SELECT
		<include refid="Base_Column_List"/>
		FROM msg_info a
		<where>
			1=1
			<include refid="msgInfo_where"/>
		</where>
	</select>

	<select id="getMsgByOrderId" resultMap="msgInfoMap">
		SELECT
			a.fd_3ac901ba3b9d3e domain_name,
			p.fd_name ALERT_USER,
			a.fd_3ac901c5184274  ORDER_NO,
			a.fd_id  ORDER_ID,
			'0' as ALERT_TYPE
		FROM
			ekp_ea33075576149bb8a4f5 a
				LEFT JOIN sys_org_element p on a.fd_3bb0dd2400b154 = p.fd_id
		WHERE
			a.fd_id = #{orderId} limit 1
	</select>
	<!--  加急(2-3小时)|1  紧急(3-5小时)|4  常规(5-8小时)|7  -->
	<select id="getMsgByTask"  resultMap="msgInfoMap">
		SELECT * from (
		SELECT
			b1.fd_3ac901ba3b9d3e domain_name,
			o.fd_no ALERT_USER,
			b1.fd_3ac901c5184274 ORDER_NO,
			b1.fd_id ORDER_ID,
			'1' AS ALERT_TYPE,
			DATE_ADD(NOW(), INTERVAL 30 MINUTE)  as  'date1',
			IF(b1.fd_3b779ba2168eea = 1,
				DATE_ADD(a.fd_start_date,INTERVAL 3 HOUR),
				IF(b1.fd_3b779ba2168eea = 4,
				   DATE_ADD(a.fd_start_date,INTERVAL 5 HOUR),
				   IF(b1.fd_3b779ba2168eea = 7,
					  DATE_ADD(a.fd_start_date,INTERVAL 8 HOUR),
					  DATE_ADD(a.fd_start_date,INTERVAL 2 MONTH)))) as 'date2'
		FROM
			lbpm_workitem a
		LEFT JOIN lbpm_node e ON e.fd_id = a.fd_node_id
		LEFT JOIN lbpm_process p ON p.fd_id = e.fd_process_id
		LEFT JOIN ekp_ea33075576149bb8a4f5 b1 ON p.fd_model_id = b1.fd_id
		LEFT JOIN sys_org_element o ON o.fd_id = fd_expected_id
		WHERE
			e.fd_fact_node_id = 'N5'
		AND b1.fd_id IS NOT NULL
		) x
		where x.date1 >= date2 GROUP BY x.ORDER_NO
	</select>

	<select id="getMsgByTaskOne"  resultMap="msgInfoMap">
		SELECT
			b1.fd_3ac901ba3b9d3e domain_name,
			o.fd_no ALERT_USER,
			b1.fd_3ac901c5184274 ORDER_NO,
			b1.fd_id ORDER_ID,
			'3' AS ALERT_TYPE,
			b1.fd_3bddc3d6c8a734
		FROM
			ekp_ea33075576149bb8a4f5 b1
				LEFT JOIN sys_org_element o ON o.fd_id = b1.fd_3bf1511f1d618e
		WHERE
			b1.fd_3bddc3d6c8a734 = '待支出'
		  AND b1.fd_id IS NOT NULL and o.fd_id is not null
	</select>

	<delete id="deleteHandledMsg">
		DELETE FROM msg_info where ALERT_TYPE ='0'
		<if test="nos != null and nos.size > 0">
			and ORDER_NO not in
			<foreach item="items" index="index" collection="nos" open="(" separator="," close=")">
				#{items}
			</foreach>
		</if>
	</delete>
	<select id="getOrderNos" resultType="java.lang.String">
		SELECT
			b1.fd_3ac901c5184274 ORDER_NO
		FROM
			lbpm_workitem a
				LEFT JOIN lbpm_node e ON e.fd_id = a.fd_node_id
				LEFT JOIN lbpm_process p ON p.fd_id = e.fd_process_id
				LEFT JOIN ekp_ea33075576149bb8a4f5 b1 ON p.fd_model_id = b1.fd_id
		WHERE
			e.fd_fact_node_id = 'N5'
		  AND b1.fd_id IS NOT NULL
	</select>

	<insert id="insertMsg">
		insert into msg_info (ID,URL,ORDER_ID,ALERT_USER,DOMAIN_NAME,CREATE_TIME,ALERT_TYPE,ORDER_NO)
			value (REPLACE(UUID(),"-",""),#{vo.urlInfo},#{vo.orderId},#{vo.userName},#{vo.domainName},now(),'0',#{vo.dingdanNo})
	</insert>
</mapper>