<?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.api.manage.dao.chargeManagement.SysChargesTemplateDetailDao">
    <select id="list" parameterType="com.api.model.chargeManagement.SearchChargesTemplateDetail" resultType="com.api.vo.chargeManagement.VoChargesTemplateDetail">
        select sctd.id,sctd.name,sctd.unit_price,sctd.type,sctd.marker,(
        case sctd.modify_date is null
            when true
            then
                sctd.create_date
            else
                sctd.modify_date
        end
        ) as modify_date,su.actual_name as create_name,sctd.status from sys_charges_template_detail as sctd,sys_user as su,sys_charges_template as sct
        <where>
            sct.id = #{chargesTemplateId}
            and sctd.charges_template_id = sct.id
            and sctd.create_id = su.id
            <if test="name != null and name != ''">
                and sctd.name like concat('%',#{name},'%')
            </if>
        </where>
    </select>

    <insert id="insert" parameterType="com.api.model.chargeManagement.SysChargesTemplateDetail">
        insert into
        <trim prefix="sys_charges_template_detail(" suffix=")" suffixOverrides=",">
            charges_template_id,
            name,
            status,
            <if test="unitPrice != null">
                unit_price,
            </if>
            <if test="type != null">
                type,
            </if>
            create_id,
            create_date,
            marker
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            #{chargesTemplateId},
            #{name},
            #{status},
            <if test="unitPrice != null">
                #{unitPrice},
            </if>
            <if test="type != null">
                #{type},
            </if>
            #{createId},
            #{createDate},
            #{marker}
        </trim>
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>

    <select id="findById" parameterType="java.lang.Integer" resultType="com.api.vo.chargeManagement.VoFindByIdChargesTemplateDetail">
        select * from sys_charges_template_detail where id = #{id}
    </select>

    <update id="update" parameterType="com.api.model.chargeManagement.SysChargesTemplateDetail">
        update sys_charges_template_detail
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="unitPrice != null">
                unit_price = #{unitPrice},
            </if>
            <if test="type != null">
                type = #{type},
            </if>
            modify_id = #{modifyId},
            modify_date = #{modifyDate},
        </set>
        where id = #{id} and charges_template_id = #{chargesTemplateId}
    </update>

    <delete id="delete" parameterType="java.lang.Integer">
        delete from sys_charges_template_detail where id = #{id}
    </delete>

    <select id="findTypeShowNameByShowValue" parameterType="java.lang.Integer" resultType="java.lang.String">
        select show_name from sys_data_dictionary
        where type_name = 'sys_charges_template_detail_type' and show_value = #{type}
    </select>

    <select id="findStatusShowNameByShowValue" parameterType="java.lang.Integer" resultType="java.lang.String">
        select show_name from sys_data_dictionary
        where type_name = 'sys_charges_template_status' and show_value = #{status}
    </select>

    <insert id="insertAdditionCost" parameterType="com.api.model.chargeManagement.SysChargesTemplateAdditionalCost">
        insert into sys_charges_template_additional_cost(
            charges_template_detail_id,
            name,
            cost
        )values (
            #{chargesTemplateDetailId},
            #{name},
            #{cost}
        )
    </insert>

    <select id="findAdditionalCostById" parameterType="java.lang.Integer" resultType="com.api.vo.chargeManagement.VoChargesTemplateAdditionalCost">
        select id,name,cost from sys_charges_template_additional_cost where charges_template_detail_id = #{id}
    </select>

    <delete id="deleteAdditionalCost" parameterType="java.lang.Integer">
        delete from sys_charges_template_additional_cost where charges_template_detail_id = #{id}
    </delete>

    <select id="findStatusByChargesTemplateDetailId" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select status from sys_charges_template_detail where id = #{id}
    </select>

    <update id="disable" parameterType="java.lang.Integer">
        update sys_charges_template_detail set status = 0 where id = #{id}
    </update>

    <update id="disableAll" parameterType="com.api.vo.chargeManagement.VoFindByIdChargesTemplateDetail">
        update sys_charges_template_detail set status = 0 where charges_template_id = #{chargesTemplateId} and marker = #{marker}
    </update>

    <update id="enable" parameterType="java.lang.Integer">
        update sys_charges_template_detail set status = 1 where id = #{id}
    </update>

    <select id="findCTDIdByCTId" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select id from sys_charges_template_detail where charges_template_id = #{id}
    </select>

    <select id="findDailyPaymentIsRelation" parameterType="java.util.List" resultType="java.lang.Integer">
        select count(*) from sys_daily_payment where charges_template_detail_id in
        <foreach collection="list" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </select>
    <select id="findDepositManagementIsRelation" parameterType="java.util.List" resultType="java.lang.Integer">
        select count(*) from sys_deposit_management where charges_template_detail_id in
        <foreach collection="list" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </select>
</mapper>