<?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">
<!-- 农村三资数据填报 biz_rural_three_assets -->
<mapper namespace="com.ruoyi.web.modules.industryservice.mapper.BizRuralThreeAssetsMapper">

    <resultMap type="com.ruoyi.web.modules.industryservice.domain.BizRuralThreeAssets"
               id="BizRuralThreeAssetsResult">
        <id     property="id"                         column="id"/>
        <result property="townDeptId"                 column="town_dept_id"/>
        <result property="townName"                   column="town_name"/>
        <result property="villageDeptId"              column="village_dept_id"/>
        <result property="villageName"                column="village_name"/>
        <result property="operatingFixedAssets"       column="operating_fixed_assets"/>
        <result property="nonOperatingFixedAssets"    column="non_operating_fixed_assets"/>
        <result property="fixedAssets"                column="fixed_assets"/>
        <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"/>
    </resultMap>

    <sql id="selectVo">
        select r.id, r.town_dept_id, r.town_name, r.village_dept_id, r.village_name,
               r.operating_fixed_assets, r.non_operating_fixed_assets, r.fixed_assets,
               r.create_by, r.create_time, r.update_by, r.update_time, r.remark
        from biz_rural_three_assets r
    </sql>

    <select id="selectBizRuralThreeAssetsById" parameterType="long" resultMap="BizRuralThreeAssetsResult">
        <include refid="selectVo"/>
        where r.id = #{id}
    </select>

    <select id="countByVillageExcludeId" resultType="int">
        select count(1)
        from biz_rural_three_assets
        where village_dept_id = #{villageDeptId}
        <if test="excludeId != null">
            and id != #{excludeId}
        </if>
    </select>

    <select id="selectBizRuralThreeAssetsList"
            parameterType="com.ruoyi.web.modules.industryservice.domain.BizRuralThreeAssets"
            resultMap="BizRuralThreeAssetsResult">
        <include refid="selectVo"/>
        left join sys_dept d on r.town_dept_id = d.dept_id
        <where>
            <if test="townDeptId != null">
                and r.town_dept_id = #{townDeptId}
            </if>
            <if test="villageDeptId != null">
                and r.village_dept_id = #{villageDeptId}
            </if>
        </where>
        order by d.order_num asc, r.village_dept_id asc, r.id desc
    </select>

    <insert id="insertBizRuralThreeAssets"
            parameterType="com.ruoyi.web.modules.industryservice.domain.BizRuralThreeAssets"
            useGeneratedKeys="true" keyProperty="id">
        insert into biz_rural_three_assets (
            town_dept_id, town_name, village_dept_id, village_name,
            operating_fixed_assets, non_operating_fixed_assets, fixed_assets,
            create_by, create_time, remark
        ) values (
            #{townDeptId}, #{townName}, #{villageDeptId}, #{villageName},
            #{operatingFixedAssets}, #{nonOperatingFixedAssets}, #{fixedAssets},
            #{createBy}, sysdate(), #{remark}
        )
    </insert>

    <update id="updateBizRuralThreeAssets"
            parameterType="com.ruoyi.web.modules.industryservice.domain.BizRuralThreeAssets">
        update biz_rural_three_assets set
            town_dept_id = #{townDeptId},
            town_name = #{townName},
            village_dept_id = #{villageDeptId},
            village_name = #{villageName},
            operating_fixed_assets = #{operatingFixedAssets},
            non_operating_fixed_assets = #{nonOperatingFixedAssets},
            fixed_assets = #{fixedAssets},
            update_by = #{updateBy},
            update_time = sysdate(),
            remark = #{remark}
        where id = #{id}
    </update>

    <delete id="deleteBizRuralThreeAssetsByIds">
        delete from biz_rural_three_assets where id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
