<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
这个文件是自动生成的。
不要修改此文件。所有改动将在下次重新自动生成时丢失。
-->
<mapper namespace="com.ym.mec.biz.dal.dao.FinancialExpenditureDao">

    <resultMap type="com.ym.mec.biz.dal.entity.FinancialExpenditure" id="FinancialExpenditure">
        <result column="id_" property="id"/>
        <result column="batch_no_" property="batchNo"/>
        <result column="financial_process_no_" property="financialProcessNo"/>
        <result column="dingtalk_process_no_" property="dingtalkProcessNo"/>
        <result column="organ_id_" property="organId"/>
        <result column="cooperation_organ_id_" property="cooperationOrganId"/>
        <result column="apply_user_" property="applyUser"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="fee_project_" property="feeProject" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="amount_" property="amount"/>
        <result column="item_detail_" property="itemDetail"/>
        <result column="payment_time_" property="paymentTime"/>
        <result column="del_flag_" property="delFlag"/>
        <result column="cause_" property="cause"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
		<result column="tenant_id_" property="tenantId" />
    </resultMap>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="FinancialExpenditure">
        SELECT *
        FROM financial_expenditure
        WHERE id_ = #{id}
    </select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="FinancialExpenditure">
        SELECT *
        FROM financial_expenditure
        WHERE del_flag_ = 0 and tenant_id_ = #{tenantId}
        ORDER BY id_
    </select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.FinancialExpenditure" useGeneratedKeys="true"
            keyColumn="id"
            keyProperty="id">
        INSERT INTO financial_expenditure (batch_no_, financial_process_no_, dingtalk_process_no_,
                                           process_id_,organ_id_, cooperation_organ_id_, apply_user_, apply_user_id_, amount_, item_detail_,
                                           payment_time_, cause_, create_time_, update_time_, fee_project_, type_, tenant_id_)
        VALUES (#{batchNo}, #{financialProcessNo}, #{dingtalkProcessNo},#{processId}, #{organId}, #{cooperationOrganId},
                #{applyUser}, #{applyUserId}, #{amount},
                #{itemDetail}, #{paymentTime}, #{cause}, now(), now(),
                #{feeProject,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{tenantId})
    </insert>

    <insert id="batchInsert" parameterType="com.ym.mec.biz.dal.entity.FinancialExpenditure" useGeneratedKeys="true"
            keyColumn="id"
            keyProperty="id">
        INSERT INTO financial_expenditure (batch_no_,financial_process_no_,dingtalk_process_no_,
        process_id_,organ_id_,cooperation_organ_id_,apply_user_,amount_,item_detail_,payment_time_,cause_,create_time_,update_time_,fee_project_,type_,tenant_id_)
        VALUES
        <foreach collection="financialExpenditures" item="item" separator=",">
            (#{item.batchNo},#{item.financialProcessNo},#{item.dingtalkProcessNo},#{item.organId},#{item.processId},
            #{item.cooperationOrganId},#{item.applyUser},#{item.amount},
            #{item.itemDetail},#{item.paymentTime},#{item.cause},now(),now(),
            #{item.feeProject,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            #{item.type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{item.tenantId})
        </foreach>
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.FinancialExpenditure">
        UPDATE financial_expenditure
        <set>
            <if test="feeProject != null">
                fee_project_ = #{feeProject,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="type != null">
                type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="batchNo != null">
                batch_no_ = #{batchNo},
            </if>
            <if test="financialProcessNo != null">
                financial_process_no_ = #{financialProcessNo},
            </if>
            <if test="dingtalkProcessNo != null">
                dingtalk_process_no_ = #{dingtalkProcessNo},
            </if>
            <if test="organId != null">
                organ_id_ = #{organId},
            </if>
            <if test="cooperationOrganId != null">
                cooperation_organ_id_ = #{cooperationOrganId},
            </if>
            <if test="applyUser != null">
                apply_user_ = #{applyUser},
            </if>
            <if test="amount != null">
                amount_ = #{amount},
            </if>
            <if test="itemDetail != null">
                item_detail_ = #{itemDetail},
            </if>
            <if test="paymentTime != null">
                payment_time_ = #{paymentTime},
            </if>
            <if test="cause != null">
                cause_ = #{cause},
            </if>
            update_time_ = NOW()
        </set>
        WHERE id_ = #{id} and tenant_id_ = #{tenantId}
    </update>

    <!-- 根据主键删除一条记录 -->
    <update id="delete">
        UPDATE financial_expenditure
        SET del_flag_ = 1
        WHERE id_ = #{id}
    </update>
    <update id="batchDel">
        UPDATE financial_expenditure
        SET del_flag_ = 1
        WHERE FIND_IN_SET(id_, #{ids})
    </update>

    <sql id="queryPageSql">
        <where>
            fe.del_flag_ = 0 and fe.tenant_id_ = #{tenantId}
            <if test="batchNo != null and batchNo != ''">
                AND fe.batch_no_ = #{batchNo}
            </if>
            <if test="feeProject != null">
                AND fe.fee_project_ = #{feeProject}
            </if>
            <if test="type != null">
                AND fe.type_ = #{type}
            </if>
            <if test="financialProcessNo != null and financialProcessNo != ''">
                AND fe.financial_process_no_ = #{financialProcessNo}
            </if>
            <if test="dingtalkProcessNo != null and dingtalkProcessNo != ''">
                AND fe.dingtalk_process_no_ = #{dingtalkProcessNo}
            </if>
            <if test="search != null and search != ''">
                AND (fe.id_ LIKE CONCAT('%',#{search},'%') OR fe.financial_process_no_ LIKE CONCAT('%',#{search},'%')
                OR fe.dingtalk_process_no_ LIKE CONCAT('%',#{search},'%') OR fe.apply_user_ LIKE
                CONCAT('%',#{search},'%'))
            </if>
            <if test="organId != null and organId != ''">
                AND FIND_IN_SET(fe.organ_id_,#{organId})
            </if>
            <if test="cooperationOrganId != null">
                AND fe.cooperation_organ_id_ = #{cooperationOrganId}
            </if>
            <if test="startTime != null and startTime != ''">
                AND DATE_FORMAT(fe.payment_time_,'%Y-%m-%d') &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                AND DATE_FORMAT(fe.payment_time_,'%Y-%m-%d') &lt;= #{endTime}
            </if>
        </where>
    </sql>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="FinancialExpenditure" parameterType="map">
        SELECT fe.*,o.name_ organ_name_,co.name_ cooperation_name_,su.real_name_
        FROM financial_expenditure fe
        LEFT JOIN organization o ON o.id_ = fe.organ_id_
        LEFT JOIN cooperation_organ co ON co.id_ = fe.cooperation_organ_id_
        <include refid="queryPageSql"/>
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(DISTINCT fe.id_) FROM financial_expenditure fe
        <include refid="queryPageSql"/>
    </select>

    <resultMap type="com.ym.mec.biz.dal.dto.FinancialExpenditureDto" id="FinancialExpenditureDto"
               extends="FinancialExpenditure">
        <result column="organ_name_" property="organName"/>
        <result column="cooperation_name_" property="cooperationName"/>
    </resultMap>

    <select id="findFinancialExpenditureCount" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT fe.id_) FROM financial_expenditure fe
        <include refid="queryPageSql"/>
    </select>
    <select id="queryFinancialExpenditurePage" resultMap="FinancialExpenditureDto">
        SELECT fe.*,o.name_ organ_name_,co.name_ cooperation_name_
        FROM financial_expenditure fe
        LEFT JOIN organization o ON o.id_ = fe.organ_id_
        LEFT JOIN cooperation_organ co ON co.id_ = fe.cooperation_organ_id_
        <include refid="queryPageSql"/>
        ORDER BY fe.id_ DESC
        <include refid="global.limit"/>
    </select>

    <!-- 统计合作单位的支出 -->
    <select id="getCooperationExpenditure" resultMap="FinancialExpenditure"><![CDATA[
        SELECT organ_id_, cooperation_organ_id_, type_, SUM(amount_) amount_
        FROM financial_expenditure
        WHERE payment_time_ >= #{startTime}
          AND payment_time_ <= #{endTime}
          AND del_flag_ = 0 and tenant_id_ = #{tenantId}
        GROUP BY organ_id_, cooperation_organ_id_, type_
        ]]></select>
        
    <select id="countBydingTalk" resultType="java.lang.String">
        SELECT dingtalk_process_no_ FROM financial_expenditure WHERE del_flag_ = 0 AND dingtalk_process_no_ IN
        <foreach collection="collect" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
        GROUP BY dingtalk_process_no_
    </select>
    <resultMap id="PWorkOrderInfo" type="com.ym.mec.biz.dal.dto.PWorkOrderInfo">
        <result property="id" column="id"/>
        <result property="classify" column="classify"/>
        <result property="isEnd" column="is_end"/>
        <result property="isDenied" column="is_denied"/>
        <result property="isCancel" column="is_cancel"/>
        <result property="creator" column="mec_user_id"/>
        <result property="process" column="process"/>
        <result property="title" column="title"/>
        <result property="deptId" column="dept_id"/>
    </resultMap>
    <select id="getWorkOrderInfo" resultMap="PWorkOrderInfo">
        SELECT woi.*,su.mec_user_id FROM oa_pro.p_work_order_info woi
        LEFT JOIN oa_pro.p_work_order_circulation_history woch ON woi.id = woch.work_order
        LEFT JOIN oa_pro.p_process_info pi ON pi.id = woi.classify
        LEFT JOIN oa_pro.sys_user su ON su.user_id = woi.creator
        WHERE woi.is_end = 1  AND woi.is_denied = 0  AND woi.is_cancel = 0
        AND woch.`status` != 0 AND woi.id = #{workOrderId} AND pi.fee_type = 1 LIMIT 1
    </select>
    <select id="getFormStructure" resultType="java.lang.String">
        SELECT form_structure FROM oa_pro.p_work_order_tpl_data WHERE work_order = #{workOrderId}
    </select>
    <select id="getFormData" resultType="java.lang.String">
        SELECT form_data FROM oa_pro.p_work_order_tpl_data WHERE work_order = #{workOrderId}
    </select>
    <select id="getTplInfo" resultType="java.lang.String">
        SELECT form_structure FROM oa_pro.p_tpl_info WHERE id = #{tplInfoId}
    </select>
    <select id="findByBatchNoAndProcessNo" resultType="integer">
        SELECT id_ FROM financial_expenditure WHERE batch_no_ = #{workOrderId} AND financial_process_no_ = #{workOrderId} LIMIT 1
    </select>
    <select id="getDeptId" resultType="java.lang.Integer">
        SELECT organ_id FROM oa_pro.sys_dept WHERE dept_id = #{deptId}
    </select>
</mapper>
