<?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.aegis.resource.equipment.mapper.ResourceEquipmentMapper">

    <resultMap type="Equipment" id="EquipmentResult">
        <id     property="id"              column="id"               />
        <result property="equipmentCode"  column="equipment_code"   />
        <result property="equipmentName"  column="equipment_name"   />
        <result property="equipmentType"  column="equipment_type"   />
        <result property="brand"          column="brand"            />
        <result property="model"          column="model"            />
        <result property="teamId"         column="team_id"          />
        <result property="warehouseId"    column="warehouse_id"     />
        <result property="teamName"       column="team_name"         />
        <result property="warehouseName"  column="warehouse_name"    />
        <result property="status"         column="status"           />
        <result property="capabilities"   column="capabilities"     />
        <result property="purchaseDate"   column="purchase_date"    />
        <result property="warrantyDate"   column="warranty_date"    />
        <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"           />
    </resultMap>

    <sql id="selectEquipmentVo">
        select e.id, e.equipment_code, e.equipment_name, e.equipment_type, e.brand, e.model, 
               e.team_id, t.team_name, e.warehouse_id, w.warehouse_name, e.status, e.capabilities, 
               e.purchase_date, e.warranty_date, e.del_flag, e.create_by, e.create_time, 
               e.update_by, e.update_time, e.remark
        from resource_equipment e
        left join resource_team t on e.team_id = t.id and t.del_flag = '0'
        left join resource_warehouse w on e.warehouse_id = w.id and w.status = '0'
    </sql>

    <select id="selectEquipmentList" parameterType="Equipment" resultMap="EquipmentResult">
        <include refid="selectEquipmentVo"/>
        <where>
            e.del_flag = '0'
            <if test="equipmentCode != null and equipmentCode != ''">
                AND e.equipment_code like concat('%', #{equipmentCode}, '%')
            </if>
            <if test="equipmentName != null and equipmentName != ''">
                AND e.equipment_name like concat('%', #{equipmentName}, '%')
            </if>
            <if test="equipmentType != null and equipmentType != ''">
                AND e.equipment_type = #{equipmentType}
            </if>
            <if test="brand != null and brand != ''">
                AND e.brand like concat('%', #{brand}, '%')
            </if>
            <if test="teamId != null">
                AND e.team_id = #{teamId}
            </if>
            <if test="warehouseId != null">
                AND e.warehouse_id = #{warehouseId}
            </if>
            <if test="status != null and status != ''">
                AND e.status = #{status}
            </if>
            <if test="params.beginTime != null and params.beginTime != ''">
                AND date_format(e.create_time,'%Y%m%d') &gt;= date_format(#{params.beginTime},'%Y%m%d')
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                AND date_format(e.create_time,'%Y%m%d') &lt;= date_format(#{params.endTime},'%Y%m%d')
            </if>
        </where>
        order by e.create_time desc
    </select>

    <select id="selectEquipmentById" parameterType="Long" resultMap="EquipmentResult">
        <include refid="selectEquipmentVo"/>
        where e.id = #{id} and e.del_flag = '0'
    </select>

    <select id="selectEquipmentListByTeamId" parameterType="Long" resultMap="EquipmentResult">
        <include refid="selectEquipmentVo"/>
        where e.team_id = #{teamId} and e.del_flag = '0'
        order by e.create_time desc
    </select>

    <insert id="insertEquipment" parameterType="Equipment" useGeneratedKeys="true" keyProperty="id">
        insert into resource_equipment(
            <if test="equipmentCode != null and equipmentCode != ''">equipment_code,</if>
            <if test="equipmentName != null and equipmentName != ''">equipment_name,</if>
            <if test="equipmentType != null">equipment_type,</if>
            <if test="brand != null">brand,</if>
            <if test="model != null">model,</if>
            <if test="teamId != null">team_id,</if>
            <if test="warehouseId != null">warehouse_id,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="capabilities != null">capabilities,</if>
            <if test="purchaseDate != null">purchase_date,</if>
            <if test="warrantyDate != null">warranty_date,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            create_time
        )values(
            <if test="equipmentCode != null and equipmentCode != ''">#{equipmentCode},</if>
            <if test="equipmentName != null and equipmentName != ''">#{equipmentName},</if>
            <if test="equipmentType != null">#{equipmentType},</if>
            <if test="brand != null">#{brand},</if>
            <if test="model != null">#{model},</if>
            <if test="teamId != null">#{teamId},</if>
            <if test="warehouseId != null">#{warehouseId},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="capabilities != null">#{capabilities},</if>
            <if test="purchaseDate != null">#{purchaseDate},</if>
            <if test="warrantyDate != null">#{warrantyDate},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            sysdate()
        )
    </insert>

    <update id="updateEquipment" parameterType="Equipment">
        update resource_equipment
        <set>
            <if test="equipmentCode != null and equipmentCode != ''">equipment_code = #{equipmentCode},</if>
            <if test="equipmentName != null and equipmentName != ''">equipment_name = #{equipmentName},</if>
            <if test="equipmentType != null">equipment_type = #{equipmentType},</if>
            <if test="brand != null">brand = #{brand},</if>
            <if test="model != null">model = #{model},</if>
            <if test="teamId != null">team_id = #{teamId},</if>
            <if test="warehouseId != null">warehouse_id = #{warehouseId},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="capabilities != null">capabilities = #{capabilities},</if>
            <if test="purchaseDate != null">purchase_date = #{purchaseDate},</if>
            <if test="warrantyDate != null">warranty_date = #{warrantyDate},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate()
        </set>
        where id = #{id}
    </update>

    <update id="deleteEquipmentById" parameterType="Long">
        update resource_equipment set del_flag = '2' where id = #{id}
    </update>

    <update id="deleteEquipmentByIds" parameterType="Long">
        update resource_equipment set del_flag = '2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="checkEquipmentCodeUnique" resultMap="EquipmentResult">
        <include refid="selectEquipmentVo"/>
        where e.equipment_code = #{equipmentCode} and e.del_flag = '0'
        <if test="id != null">
            AND e.id != #{id}
        </if>
        limit 1
    </select>

    <select id="selectDeletedEquipmentByCode" parameterType="String" resultMap="EquipmentResult">
        <include refid="selectEquipmentVo"/>
        where e.equipment_code = #{equipmentCode} and e.del_flag = '2'
        limit 1
    </select>

    <update id="restoreEquipment" parameterType="Equipment">
        update resource_equipment
        <set>
            <if test="equipmentCode != null and equipmentCode != ''">equipment_code = #{equipmentCode},</if>
            <if test="equipmentName != null and equipmentName != ''">equipment_name = #{equipmentName},</if>
            <if test="equipmentType != null">equipment_type = #{equipmentType},</if>
            <if test="brand != null">brand = #{brand},</if>
            <if test="model != null">model = #{model},</if>
            <if test="teamId != null">team_id = #{teamId},</if>
            <if test="warehouseId != null">warehouse_id = #{warehouseId},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="capabilities != null">capabilities = #{capabilities},</if>
            <if test="purchaseDate != null">purchase_date = #{purchaseDate},</if>
            <if test="warrantyDate != null">warranty_date = #{warrantyDate},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            del_flag = '0',
            update_time = sysdate()
        </set>
        where id = #{id}
    </update>

</mapper>