<?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.info.collection.mapper.CcdiAssetInfoMapper">

    <resultMap id="CcdiAssetInfoResultMap" type="com.ruoyi.info.collection.domain.CcdiAssetInfo">
        <id property="assetId" column="asset_id"/>
        <result property="familyId" column="family_id"/>
        <result property="personId" column="person_id"/>
        <result property="assetMainType" column="asset_main_type"/>
        <result property="assetSubType" column="asset_sub_type"/>
        <result property="assetName" column="asset_name"/>
        <result property="ownershipRatio" column="ownership_ratio"/>
        <result property="purchaseEvalDate" column="purchase_eval_date"/>
        <result property="originalValue" column="original_value"/>
        <result property="currentValue" column="current_value"/>
        <result property="valuationDate" column="valuation_date"/>
        <result property="assetStatus" column="asset_status"/>
        <result property="remarks" column="remarks"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <select id="selectByFamilyId" resultMap="CcdiAssetInfoResultMap">
        SELECT
            asset_id, family_id, person_id, asset_main_type, asset_sub_type, asset_name,
            ownership_ratio, purchase_eval_date, original_value, current_value,
            valuation_date, asset_status, remarks, create_by, create_time, update_by, update_time
        FROM ccdi_asset_info
        WHERE family_id = #{familyId}
        ORDER BY create_time DESC, asset_id DESC
    </select>

    <select id="selectByFamilyIdAndPersonId" resultMap="CcdiAssetInfoResultMap">
        SELECT
            asset_id, family_id, person_id, asset_main_type, asset_sub_type, asset_name,
            ownership_ratio, purchase_eval_date, original_value, current_value,
            valuation_date, asset_status, remarks, create_by, create_time, update_by, update_time
        FROM ccdi_asset_info
        WHERE family_id = #{familyId}
          AND person_id = #{personId}
        ORDER BY create_time DESC, asset_id DESC
    </select>

    <delete id="deleteByFamilyId">
        DELETE FROM ccdi_asset_info
        WHERE family_id = #{familyId}
    </delete>

    <delete id="deleteByFamilyIdAndPersonId">
        DELETE FROM ccdi_asset_info
        WHERE family_id = #{familyId}
          AND person_id = #{personId}
    </delete>

    <delete id="deleteByFamilyIds">
        DELETE FROM ccdi_asset_info
        WHERE family_id IN
        <foreach collection="familyIds" item="familyId" open="(" separator="," close=")">
            #{familyId}
        </foreach>
    </delete>

    <insert id="insertBatch">
        INSERT INTO ccdi_asset_info
        (family_id, person_id, asset_main_type, asset_sub_type, asset_name,
         ownership_ratio, purchase_eval_date, original_value, current_value,
         valuation_date, asset_status, remarks, create_by, create_time, update_by, update_time)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.familyId}, #{item.personId}, #{item.assetMainType}, #{item.assetSubType}, #{item.assetName},
             #{item.ownershipRatio}, #{item.purchaseEvalDate}, #{item.originalValue}, #{item.currentValue},
             #{item.valuationDate}, #{item.assetStatus}, #{item.remarks}, #{item.createBy}, NOW(), #{item.updateBy}, NOW())
        </foreach>
    </insert>

    <select id="selectOwnerCandidatesByRelationCertNos" resultType="map">
        SELECT
            relation_cert_no AS personId,
            person_id AS familyId
        FROM ccdi_staff_fmy_relation
        WHERE is_emp_family = 1
          AND relation_cert_no IN
        <foreach collection="relationCertNos" item="relationCertNo" open="(" separator="," close=")">
            #{relationCertNo}
        </foreach>
    </select>

    <select id="selectOwnerCandidatesByBaseStaffIdCards" resultType="map">
        SELECT
            id_card AS personId,
            id_card AS familyId
        FROM ccdi_base_staff
        WHERE id_card IN
        <foreach collection="idCards" item="idCard" open="(" separator="," close=")">
            #{idCard}
        </foreach>
    </select>

</mapper>
