<?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.bonus.sgzb.base.mapper.MaPropInfoMapper">
    <resultMap type="com.bonus.sgzb.base.domain.MaPropInfo" id="MaPropInfoResult">
        <id     property="propId"      column="prop_id"      />
        <result property="propName"    column="prop_name"    />
        <result property="code"     column="code"     />
        <result property="status"     column="status"     />
        <result property="deptId"   column="dept_id"   />
        <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="companyId"    column="company_id"    />
        <result property="deptName"    column="dept_name"    />
    </resultMap>
    <resultMap type="com.bonus.sgzb.base.domain.MaPropSet" id="MaPropSetResult">
        <result property="id"    column="id"    />
        <result property="propId"    column="prop_id"    />
        <result property="typeId"    column="type_id"    />
        <result property="status"    column="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="companyId"    column="company_id"    />
    </resultMap>
    <resultMap type="com.bonus.sgzb.base.api.domain.MaType" id="MaTypeResult">
        <result property="typeId"    column="type_id"    />
        <result property="typeName"    column="type_name"    />
        <result property="parentId"    column="parent_id"    />
        <result property="status"    column="status"    />
        <result property="num"    column="num"    />
        <result property="unitId"    column="unit_id"    />
        <result property="manageType"    column="manage_type"    />
        <result property="leasePrice"    column="lease_price"    />
        <result property="rentPrice"    column="rent_price"    />
        <result property="buyPrice"    column="buy_price"    />
        <result property="payPrice"    column="pay_price"    />
        <result property="level"    column="level"    />
        <result property="ratedLoad"    column="rated_load"    />
        <result property="testLoad"    column="test_load"    />
        <result property="holdingTime"    column="holding_time"    />
        <result property="warnNum"    column="warn_num"    />
        <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="isPlan"    column="is_plan"    />
        <result property="isAncuo"    column="is_ancuo"    />
        <result property="remark"    column="remark"    />
        <result property="companyId"    column="company_id"    />
        <result property="propName"    column="prop_name"    />
        <result property="id"    column="id"    />
    </resultMap>
    <sql id="selectMaPropInfoVo">
        select prop_id, prop_name, code, status, dept_id, del_flag, create_by, create_time, remark, company_id
        from ma_prop_info
    </sql>
    <select id="selectMaType" resultMap="MaTypeResult" parameterType="com.bonus.sgzb.base.api.domain.MaType">
        SELECT
        m.type_id,
        m.type_name,
        m.unit_name,
        m.lease_price,
        m.rent_price,
        m.buy_price,
        m.pay_price,
        m.remark,
        e.prop_name,
        us.nick_name as keeperUserName,
        mtf.file_name photoName,
        mtf.file_url photoUrl,
        mtf2.file_name documentName,
        mtf2.file_url documentUrl
        FROM
        ma_type m
        LEFT JOIN ma_prop_set l ON m.type_id = l.type_id
        LEFT JOIN ma_prop_info e ON e.prop_id = l.prop_id
        LEFT JOIN ma_type_keeper mtk on mtk.type_id=m.type_id
        LEFT JOIN sys_user us on us.user_id=mtk.user_id
        LEFT JOIN ( SELECT * FROM ma_type_file WHERE file_type = '1' ) mtf ON m.type_id = mtf.type_id
        LEFT JOIN ( SELECT * FROM ma_type_file WHERE file_type = '2' ) mtf2 ON m.type_id = mtf2.type_id
        <where>
            level = 4 and m.status = '0'
            <if test="typeName != null and typeName !=''">
                AND type_name like concat('%',#{typeName},'%')
            </if>
            <if test="dataCondition != null and dataCondition.size()>0">
                AND m.type_id in
                <foreach collection="dataCondition" item="typeId" index="index" open="(" separator="," close=")">
                    #{typeId}
                </foreach>
            </if>
        </where>
    </select>

    <insert id="insertMaPropSet" parameterType="com.bonus.sgzb.base.domain.MaPropSet">
        insert into ma_prop_set
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="propId != null">prop_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="status != null">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="companyId != null">company_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="propId != null">#{propId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="status != null">#{status},</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="companyId != null">#{companyId},</if>
        </trim>
    </insert>
    <update id="updateMaPropSet">
        update ma_prop_set
        <set>
            <if test="propId != null and propId != ''" >prop_id = #{propId},</if>
            <if test="typeId != null and typeId != ''">type_id = #{typeId},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>
            <if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
            <if test="createTime != null and createTime != ''">create_time = #{createTime},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
            <if test="companyId != null and companyId != ''">company_id = #{companyId},</if>
            update_time = sysdate()
        </set>
        where type_id = #{typeId}
    </update>
    <insert id="insertProp" parameterType="com.bonus.sgzb.base.mapper.MaPropInfoMapper" useGeneratedKeys="true" keyProperty="propId">
        insert into ma_prop_info(
        <if test="propId != null and propId != 0">prop_id,</if>
        <if test="propName != null and propName != ''">prop_name,</if>
        <if test="code != null and code != ''">code,</if>
        <if test="status != null and status != 0">status,</if>
        <if test="deptId != null and deptId != ''">dept_id,</if>
        <if test="delFlag != null and delFlag != 0">del_flag,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        <if test="remark != null and remark != ''">remark,</if>
        <if test="companyId != null and companyId != ''">company_id,</if>
        create_time
        )values(
        <if test="propId != null and propId != 0">#{propId},</if>
        <if test="propName != null and propName != ''">#{propName},</if>
        <if test="code != null and code != ''">#{code},</if>
        <if test="status != null and status != 0">#{status},</if>
        <if test="deptId != null and deptId != ''">#{deptId},</if>
        <if test="delFlag != null and delFlag != ''">#{delFlag},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        <if test="remark != null and remark != ''">#{remark},</if>
        <if test="companyId != null and companyId != ''">#{companyId},</if>
        sysdate()
        )
    </insert>
    <insert id="add">
        insert into ma_prop_set(type_id, prop_id,company_id,create_by,create_time) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.typeIds},#{item.propId},#{item.companyId},#{item.createBy},NOW())
        </foreach>
    </insert>

    <update id="updateMaProp">
        update ma_prop_info
        <set>
            <if test="propName != null and propName != ''">prop_name = #{propName},</if>
            <if test="code != null and code != ''">code = #{code},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="deptId != null and deptId != ''">dept_id = #{deptId},</if>
            <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="companyId != null and companyId != ''">company_id = #{companyId},</if>
            update_time = sysdate()
        </set>
        where prop_id = #{propId}
    </update>

    <delete id="deleteMaProp">
        update ma_prop_info set del_flag = '2' where prop_id = #{propId}
    </delete>
    <delete id="deleteMaPropSetByPropIds">
        delete from ma_prop_set where type_id =#{typeId}
    </delete>
    <delete id="deleteByIdsAll">
        delete from ma_prop_set where type_id in
        <foreach item="typeIds" collection="array" open="(" separator="," close=")">
            #{typeIds}
        </foreach>
    </delete>
    <select id="selectMaPropSet" resultType="com.bonus.sgzb.base.domain.MaPropInfo" resultMap="MaPropInfoResult">
        select a.prop_id, a.prop_name, a.code, a.status, a.dept_id, a.del_flag, a.create_by,
         a.create_time, a.remark, a.company_id ,b.dept_name
        from ma_prop_info a
        left join sys_dept b on a.dept_id = b.dept_id
        <where> a.del_flag = '0'
            <if test="propName != null and propName != ''">
                AND a.prop_name like concat('%', #{propName}, '%')
            </if>
            <if test="propId != null and propId != ''">
                AND a.prop_id = #{propId}
            </if>
            <if test="dataCondition != null and dataCondition.size()>0">
                AND a.prop_id in
                <foreach collection="dataCondition" item="propId" index="index" open="(" separator="," close=")">
                    #{propId}
                </foreach>
            </if>
        </where>
    </select>
    <select id="selectMaPropSets" resultType="com.bonus.sgzb.base.domain.MaPropInfo" resultMap="MaPropInfoResult">
        select prop_id, prop_name
        from ma_prop_info
        <where> del_flag = '0'
            <if test="bindingName != null and bindingName != ''">
                AND prop_name like concat('%', #{bindingName}, '%')
            </if>
        </where>
    </select>
    <select id="checkPropNameUnique" resultType="com.bonus.sgzb.base.domain.MaPropInfo" resultMap="MaPropInfoResult">
        <include refid="selectMaPropInfoVo"/>
        where prop_name = #{propName} and del_flag = '0' limit 1
    </select>
    <select id="checkPropIdUnique" resultType="com.bonus.sgzb.base.domain.MaPropSet" resultMap="MaPropSetResult">
		select 	id,prop_id,type_id,status,del_flag,create_by,create_time,update_by,update_time,remark,company_id
		from ma_prop_set
        where type_id = #{typeId} and prop_id = #{propId}
    </select>
</mapper>