<?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="cn.iocoder.yudao.module.property.mapper.PropertyDealItemMapper">

    <resultMap id="BaseResultMap" type="cn.iocoder.yudao.module.property.vo.PropertyDealItemRespVO">
        <id property="id" column="cpdi_id" jdbcType="VARCHAR"/>
        <result property="dealId" column="cpdi_deal_id" jdbcType="VARCHAR"/>
        <result property="propertyId" column="cpdi_property_id" jdbcType="VARCHAR"/>
        <result property="dealWay" column="cpdi_deal_way" jdbcType="VARCHAR"/>
        <result property="oldCorpId" column="cpdi_old_corp_id" jdbcType="VARCHAR"/>
        <result property="corpId" column="cpdi_corp_id" jdbcType="VARCHAR"/>
        <result property="oldDeptId" column="cpdi_old_dept_id" jdbcType="BIGINT"/>
        <result property="deptId" column="cpdi_dept_id" jdbcType="BIGINT"/>
        <result property="oldPosId" column="cpdi_old_pos_id" jdbcType="VARCHAR"/>
        <result property="posId" column="cpdi_pos_id" jdbcType="VARCHAR"/>
        <result property="oldUserId" column="cpdi_old_user_id" jdbcType="BIGINT"/>
        <result property="userId" column="cpdi_user_id" jdbcType="BIGINT"/>
        <result property="remark" column="cpdi_remark" jdbcType="VARCHAR"/>

        <association property="property" javaType="cn.iocoder.yudao.module.property.entity.Property">
            <id property="id" column="cp_id" jdbcType="VARCHAR"/>
            <result property="corpId" column="cp_corp_id" jdbcType="VARCHAR"/>
            <result property="deptId" column="cp_dept_id" jdbcType="BIGINT"/>
            <result property="posId" column="cp_pos_id" jdbcType="VARCHAR"/>
            <result property="userId" column="cp_user_id" jdbcType="VARCHAR"/>
            <result property="propNo" column="cp_prop_no" jdbcType="VARCHAR"/>
            <result property="propName" column="cp_prop_name" jdbcType="VARCHAR"/>
            <result property="propCatg" column="cp_prop_catg" jdbcType="VARCHAR"/>
            <result property="useYear" column="cp_use_year" jdbcType="INTEGER"/>
            <result property="costType" column="cp_cost_type" jdbcType="VARCHAR"/>
            <result property="propNum" column="cp_prop_num" jdbcType="INTEGER"/>
            <result property="costTotal" column="cp_cost_total" jdbcType="DECIMAL"/>
            <result property="propStatus" column="cp_prop_status" jdbcType="VARCHAR"/>
            <result property="brand" column="cp_brand" jdbcType="VARCHAR"/>
            <result property="spec" column="cp_spec" jdbcType="VARCHAR"/>
            <result property="factory" column="cp_factory" jdbcType="VARCHAR"/>
            <result property="serialNo" column="cp_serial_no" jdbcType="VARCHAR"/>
            <result property="unit" column="cp_unit" jdbcType="VARCHAR"/>
            <result property="getDate" column="cp_get_date" jdbcType="DATE"/>
            <result property="prodDate" column="cp_prod_date" jdbcType="DATE"/>
            <result property="supplier" column="cp_supplier" jdbcType="VARCHAR"/>
            <result property="openDate" column="cp_open_date" jdbcType="DATE"/>
            <result property="netValue" column="cp_net_value" jdbcType="DECIMAL"/>
            <result property="voucherNo" column="cp_voucher_no" jdbcType="VARCHAR"/>
            <result property="keepCycleType" column="cp_keep_cycle_type" jdbcType="VARCHAR"/>
            <result property="keepCycle" column="cp_keep_cycle" jdbcType="INTEGER"/>
            <result property="lastKeepDate" column="cp_last_keep_date" jdbcType="DATE"/>
            <result property="nextKeepDate" column="cp_next_keep_date" jdbcType="DATE"/>
            <result property="fileUrls" column="cp_file_urls" jdbcType="VARCHAR"/>
            <result property="staffName" column="staff_name" jdbcType="VARCHAR"/>
            <result property="posName" column="pos_name" jdbcType="VARCHAR"/>
            <result property="remark" column="cp_remark" jdbcType="VARCHAR" />
        </association>

        <association property="propertyDeal" javaType="cn.iocoder.yudao.module.property.entity.PropertyDeal">
            <id property="id" column="cpd_id" jdbcType="VARCHAR"/>
            <result property="corpId" column="cpd_corp_id" jdbcType="VARCHAR"/>
            <result property="deptId" column="cpd_dept_id" jdbcType="BIGINT"/>
            <result property="dataType" column="cpd_data_type" jdbcType="VARCHAR"/>
            <result property="dealNo" column="cpd_deal_no" jdbcType="VARCHAR"/>
            <result property="dealDate" column="cpd_deal_date" jdbcType="DATE"/>
        </association>
    </resultMap>

    <sql id="Base_SQL">
        SELECT cpdi.id            AS cpdi_id,
               cpdi.deal_id       AS cpdi_deal_id,
               cpdi.property_id   AS cpdi_property_id,
               cpdi.deal_way      AS cpdi_deal_way,
               cpdi.old_corp_id   AS cpdi_old_corp_id,
               cpdi.corp_id       AS cpdi_corp_id,
               cpdi.old_dept_id   AS cpdi_old_dept_id,
               cpdi.dept_id       AS cpdi_dept_id,
               cpdi.old_pos_id    AS cpdi_old_pos_id,
               cpdi.pos_id        AS cpdi_pos_id,
               cpdi.old_user_id   AS cpdi_old_user_id,
               cpdi.user_id       AS cpdi_user_id,
               cpdi.remark        AS cpdi_remark,
               cp.id              AS cp_id,
               cp.corp_id         AS cp_corp_id,
               cp.dept_id         AS cp_dept_id,
               cp.pos_id          AS cp_pos_id,
               cp.user_id         AS cp_user_id,
               cp.prop_no         AS cp_prop_no,
               cp.prop_name       AS cp_prop_name,
               cp.prop_catg       AS cp_prop_catg,
               cp.use_year        AS cp_use_year,
               cp.cost_type       AS cp_cost_type,
               cp.prop_num        AS cp_prop_num,
               cp.cost_total      AS cp_cost_total,
               cp.prop_status     AS cp_prop_status,
               cp.brand           AS cp_brand,
               cp.spec            AS cp_spec,
               cp.factory         AS cp_factory,
               cp.serial_no       AS cp_serial_no,
               cp.unit            AS cp_unit,
               cp.get_date        AS cp_get_date,
               cp.prod_date       AS cp_prod_date,
               cp.supplier        AS cp_supplier,
               cp.open_date       AS cp_open_date,
               cp.net_value       AS cp_net_value,
               cp.voucher_no      AS cp_voucher_no,
               cp.keep_cycle_type AS cp_keep_cycle_type,
               cp.keep_cycle      AS cp_keep_cycle,
               cp.last_keep_date  AS cp_last_keep_date,
               cp.next_keep_date  AS cp_next_keep_date,
               cp.file_urls       AS cp_file_urls,
               cs.name            AS staff_name,
               cpp.pos_name       AS pos_name,
               cpd.id             AS cpd_id,
               cpd.corp_id        AS cpd_corp_id,
               cpd.dept_id        AS cpd_dept_id,
               cpd.data_type      AS cpd_data_type,
               cpd.deal_no        AS cpd_deal_no,
               cpd.deal_date      AS cpd_deal_date,
               cpdi.remark        AS cp_remark
        FROM company_property_deal_item cpdi
                 INNER JOIN company_property cp ON cpdi.property_id = cp.id
                 INNER JOIN company_property_deal cpd ON cpdi.deal_id = cpd.id
                 LEFT JOIN company_staff cs ON cp.user_id = cs.user_id
                 LEFT JOIN company_property_pos cpp ON cp.pos_id = cpp.id
        WHERE cpdi.deleted = '0'
    </sql>

    <select id="getPropertyDealItemPage" resultMap="BaseResultMap">
        <include refid="Base_SQL"/>
        AND cpd.data_type = #{map.dataType}
        <if test="map.dealNo != null and map.dealNo != ''">
            AND (cpd.deal_no like concat('%', #{map.dealNo}, '%'))
        </if>
        <if test="map.dealWay != null and map.dealWay != ''">
            AND (cpdi.deal_way = #{map.dealWay})
        </if>
        <if test="map.dealDateArray != null and map.dealDateArray.length > 0">
            AND (cpd.deal_date BETWEEN #{map.dealDateArray[0]} AND #{map.dealDateArray[1]})
        </if>
        order by cpdi.create_time desc
    </select>

    <select id="getPropertyDealItem" resultMap="BaseResultMap">
        <include refid="Base_SQL"/>
        AND cpdi.id = #{id}
    </select>
</mapper>