<?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.ruoyi.basicData.mapper.TFeesMapper">

    <resultMap type="TFees" id="TFeesResult">
        <result property="fId" column="f_id"/>
        <result property="fNo" column="f_no"/>
        <result property="fName" column="f_name"/>
        <result property="fFeetype" column="f_feetype"/>
        <result property="fFeeunitid" column="f_feeunitid"/>
        <result property="fCurrency" column="f_currency"/>
        <result property="fStatus" column="f_status"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="fDc" column="f_dc"/>
        <result property="isModify" column="is_modify"/>
    </resultMap>

    <sql id="selectTFeesVo">
        select f_id, f_no, f_name, f_feetype, f_feeunitid, f_currency, f_status, del_flag, create_by, create_time, update_by, update_time, remark, f_dc, is_modify from t_fees
    </sql>

    <select id="selectTFeesList" parameterType="TFees" resultMap="TFeesResult">
        <include refid="selectTFeesVo"/>
        <where>
            <if test="fNo != null  and fNo != ''">and f_no like concat('%', #{fNo}, '%')</if>
            <if test="fName != null  and fName != ''">and f_name like concat('%', #{fName}, '%')</if>
            <if test="fFeetype != null  and fFeetype != ''">and f_feetype = #{fFeetype}</if>
            <if test="fFeeunitid != null ">and f_feeunitid = #{fFeeunitid}</if>
            <if test="isModify != null  and isModify != ''"> and is_modify = #{isModify}</if>
            <if test="fCurrency != null  and fCurrency != ''">and f_currency like concat('%', #{fCurrency}, '%')</if>
            <if test="fStatus != null  and fStatus != ''">and f_status = #{fStatus}</if>
            <if test="fDc != null  and fDc != ''">and f_dc like concat('%', #{fDc}, '%') </if>
        </where>
        ORDER BY CONVERT(f_name USING gbk) asc
    </select>

    <select id="selectTFeesById" parameterType="Long" resultMap="TFeesResult">
        <include refid="selectTFeesVo"/>
        where f_id = #{fId}
    </select>

    <select id="selectTFeesModify" parameterType="TFees" resultType="String">
        select is_modify from t_fees where f_id = #{fId}
    </select>

    <insert id="insertTFees" parameterType="TFees" useGeneratedKeys="true" keyProperty="fId">
        insert into t_fees
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="fNo != null and fNo != ''">f_no,</if>
            <if test="fName != null and fName != ''">f_name,</if>
            <if test="fFeetype != null">f_feetype,</if>
            <if test="fFeeunitid != null">f_feeunitid,</if>
            <if test="fCurrency != null">f_currency,</if>
            <if test="fStatus != null">f_status,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="fDc != null">f_dc,</if>
            <if test="isModify != null">is_modify,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="fNo != null and fNo != ''">#{fNo},</if>
            <if test="fName != null and fName != ''">#{fName},</if>
            <if test="fFeetype != null">#{fFeetype},</if>
            <if test="fFeeunitid != null">#{fFeeunitid},</if>
            <if test="fCurrency != null">#{fCurrency},</if>
            <if test="fStatus != null">#{fStatus},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="fDc != null">#{fDc},</if>
            <if test="isModify != null">#{isModify},</if>
        </trim>
    </insert>

    <update id="updateTFees" parameterType="TFees">
        update t_fees
        <trim prefix="SET" suffixOverrides=",">
            <if test="fNo != null and fNo != ''">f_no = #{fNo},</if>
            <if test="fName != null and fName != ''">f_name = #{fName},</if>
            <if test="fFeetype != null">f_feetype = #{fFeetype},</if>
            <if test="fFeeunitid != null">f_feeunitid = #{fFeeunitid},</if>
            <if test="fCurrency != null">f_currency = #{fCurrency},</if>
            <if test="fStatus != null">f_status = #{fStatus},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="fDc != null">f_dc = #{fDc},</if>
            <if test="isModify != null">is_modify = #{isModify},</if>
        </trim>
        where f_id = #{fId}
    </update>

    <delete id="deleteTFeesById" parameterType="Long">
        delete from t_fees where f_id = #{fId}
    </delete>

    <delete id="deleteTFeesByIds" parameterType="String">
        delete from t_fees where f_id in
        <foreach item="fId" collection="array" open="(" separator="," close=")">
            #{fId}
        </foreach>
    </delete>

    <select id="checkFNoUnique" parameterType="String" resultMap="TFeesResult">
	    select f_id,f_no from t_fees where f_no = #{fNo} limit 1
    </select>

    <select id="checkUFNnameUnique" parameterType="String" resultMap="TFeesResult">
        select f_id,f_name from t_fees where f_name = #{fAame} limit 1
    </select>
    <select id="selectTFeesByFName" parameterType="string" resultMap="TFeesResult">
        select f_id,f_name from t_fees where f_name = #{fAame}
    </select>

    <select id="seletFeesByCCF" resultMap="TFeesResult">
        <include refid="selectTFeesVo"/>
        where f_no = 'CCF' and del_flag = 0
    </select>

    <select id="selectTFeesNameList" resultType="string">
        select f_name from t_fees where del_flag = 0 and f_dc like '%D%'
    </select>

    <select id="getFeeStatistics" resultType="map">
        select
            ifnull(round(sum(if(wf.f_dc = 'D', wf.f_amount, 0)) / 10000, 2), 0) as receivable,
            ifnull(round(sum(if(wf.f_dc = 'D', wf.f_stlamount, 0)) / 10000, 2), 0) as received,
            ifnull(round((sum(if(wf.f_dc = 'D', wf.f_amount, 0)) - sum(if(wf.f_dc = 'C', wf.f_amount, 0))) / 10000, 2), 0) as profit
        from t_warehousebillsfees wf
            left join t_warehousebills tw on wf.f_pid = tw.f_id
            left join sys_user u on tw.create_by = u.user_name
            left join sys_dept d on tw.f_bsdeptid = d.dept_id
        where wf.f_review_date between #{params.beginDate} and #{params.endDate}
            ${params.dataScope}
    </select>

    <select id="getArrearsStatistics" resultType="map">
        select
            ifnull(tc.f_cname, tc.f_name) as name,
            ifnull(round(sum(wf.f_amount) / 10000, 2), 0) as receivable
        from t_warehousebillsfees wf
            left join t_warehousebills tw on wf.f_pid = tw.f_id
            left join sys_user u on tw.create_by = u.user_name
            left join sys_dept d on tw.f_bsdeptid = d.dept_id
            left join t_corps tc on wf.f_corpid = tc.f_id
        where wf.f_dc = 'D'
            and wf.f_review_date is not null
            ${params.dataScope}
        group by wf.f_corpid
        order by receivable desc
        limit 12
    </select>

</mapper>
