<?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.lz.modules.equipment.dao.EquipmentInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.lz.modules.equipment.entity.EquipmentInfo">
        <id column="id" property="id"/>
        <result column="is_delete" property="isDelete"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="brand_name" property="brandName"/>
        <result column="spec_type" property="specType"/>
        <result column="type" property="type"/>
        <result column="num" property="num"/>
        <result column="unit" property="unit"/>
        <result column="user" property="user"/>
        <result column="owner" property="owner"/>
        <result column="qr" property="qr"/>
        <result column="original_code" property="originalCode"/>
        <result column="code" property="code"/>
        <result column="mark" property="mark"/>
        <result column="gmt_outbound" property="gmtOutbound"/>
        <result column="gmt_inbound" property="gmtInbound"/>
        <result column="equipment_state" property="equipmentState"/>
        <result column="buy_amount" property="buyAmount"/>
        <result column="sale_amount" property="saleAmount"/>
        <result column="no" property="no"/>
        <result column="user_id" property="userId"/>
        <result column="sai_id" property="saiId"/>
        <result column="count" property="count"/>
        <result column="type_id" property="typeId"/>
        <result column="brand_id" property="brandId"/>
        <result column="specs_di" property="specsId"/>
    </resultMap>


    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id AS id, is_delete AS isDelete, gmt_create AS gmtCreate, gmt_modified AS gmtModified, brand_name AS brandName, spec_type AS specType, type AS type, num AS num, unit AS unit, user AS user, owner AS owner, qr AS qr, original_code AS originalCode, code AS code, mark AS mark, gmt_outbound AS gmtOutbound, gmt_inbound AS gmtInbound, equipment_state AS equipmentState, buy_amount AS buyAmount, sale_amount AS saleAmount, no AS no, user_id AS userId, sai_id AS saiId, count AS count, type_id AS typeId, brand_id AS brandId, specs_id AS specsId
    </sql>




    <select id="selectEquipmentInfoById" resultType="EquipmentInfo" >
        select * from equipment_info  where id=#{id} and is_delete = 0 limit 1
    </select>


    <insert id="insertEquipmentInfo" parameterType="EquipmentInfo" useGeneratedKeys="true" keyProperty="id" >
        insert into equipment_info(
        <if test="brandName != null">brand_name, </if>
        <if test="specType != null">spec_type, </if>
        <if test="type != null">type, </if>
        <if test="num != null">num, </if>
        <if test="unit != null">unit, </if>
        <if test="user != null">user, </if>
        <if test="owner != null">owner, </if>
        <if test="qr != null">qr, </if>
        <if test="originalCode != null">original_code, </if>
        <if test="code != null">code, </if>
        <if test="mark != null">mark, </if>
        <if test="gmtOutbound != null">gmt_outbound, </if>
        <if test="gmtInbound != null">gmt_inbound, </if>
        <if test="equipmentState != null">equipment_state, </if>
        <if test="buyAmount != null">buy_amount, </if>
        <if test="saleAmount != null">sale_amount, </if>
        <if test="no != null">no, </if>
        <if test="userId != null">user_id, </if>
        <if test="saiId != null">sai_id, </if>
        <if test="typeId != null">type_id, </if>
        <if test="brandId != null">brand_id, </if>
        <if test="specsId != null">specs_id, </if>
        <if test="count != null">count, </if>
        is_delete,
        gmt_create,
        gmt_modified
        )values(
        <if test="brandName != null">#{ brandName}, </if>
        <if test="specType != null">#{ specType}, </if>
        <if test="type != null">#{ type}, </if>
        <if test="num != null">#{ num}, </if>
        <if test="unit != null">#{ unit}, </if>
        <if test="user != null">#{ user}, </if>
        <if test="owner != null">#{ owner}, </if>
        <if test="qr != null">#{ qr}, </if>
        <if test="originalCode != null">#{ originalCode}, </if>
        <if test="code != null">#{ code}, </if>
        <if test="mark != null">#{ mark}, </if>
        <if test="gmtOutbound != null">#{ gmtOutbound}, </if>
        <if test="gmtInbound != null">#{ gmtInbound}, </if>
        <if test="equipmentState != null">#{ equipmentState}, </if>
        <if test="buyAmount != null">#{ buyAmount}, </if>
        <if test="saleAmount != null">#{ saleAmount}, </if>
        <if test="no != null">#{ no}, </if>
        <if test="userId != null">#{ userId}, </if>
        <if test="saiId != null">#{ saiId}, </if>
        <if test="typeId != null">#{typeId}, </if>
        <if test="brandId != null">#{brandId}, </if>
        <if test="specsId != null">#{specsId]}, </if>
        <if test="count != null">#{ count}, </if>
        0,
        now(),
        now()
        )
    </insert>


    <update id="updateEquipmentInfoById" parameterType="EquipmentInfo" >
        update
        equipment_info
        <trim prefix="set" suffixOverrides=",">
            <if test="isDelete != null">is_delete = #{isDelete},</if>
            <if test="gmtCreate != null">gmt_create = #{gmtCreate},</if>
            <if test="brandName != null">brand_name = #{brandName},</if>
            <if test="specType != null">spec_type = #{specType},</if>
            <if test="type != null">type = #{type},</if>
            <if test="num != null">num = #{num},</if>
            <if test="unit != null">unit = #{unit},</if>
            <if test="user != null">user = #{user},</if>
            <if test="owner != null">owner = #{owner},</if>
            <if test="qr != null">qr = #{qr},</if>
            <if test="originalCode != null">original_code = #{originalCode},</if>
            <if test="code != null">code = #{code},</if>
            <if test="mark != null">mark = #{mark},</if>
            <if test="gmtOutbound != null">gmt_outbound = #{gmtOutbound},</if>
            <if test="gmtInbound != null">gmt_inbound = #{gmtInbound},</if>
            <if test="equipmentState != null">equipment_state = #{equipmentState},</if>
            <if test="buyAmount != null">buy_amount = #{buyAmount},</if>
            <if test="saleAmount != null">sale_amount = #{saleAmount},</if>
            <if test="no != null">no = #{no},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="saiId != null">sai_id = #{saiId},</if>
            <if test="typeId != null">type_id=#{typeId}, </if>
            <if test="brandId != null">brand_id=#{brandId}, </if>
            <if test="specsId != null">specs_id=#{specsId]}, </if>
            <if test="count != null">count = #{count}</if>
        </trim>
        ,gmt_modified = now()
        where id = #{id}
    </update>


    <update id="updateCoverEquipmentInfoById" parameterType="EquipmentInfo" >
        update
            equipment_info
        set
            is_delete = #{isDelete},
            gmt_create = #{gmtCreate},
            brand_name = #{brandName},
            spec_type = #{specType},
            type = #{type},
            num = #{num},
            unit = #{unit},
            user = #{user},
            owner = #{owner},
            qr = #{qr},
            original_code = #{originalCode},
            code = #{code},
            mark = #{mark},
            gmt_outbound = #{gmtOutbound},
            gmt_inbound = #{gmtInbound},
            equipment_state = #{equipmentState},
            buy_amount = #{buyAmount},
            sale_amount = #{saleAmount},
            no = #{no},
            user_id = #{userId},
            sai_id = #{saiId},
            type_id=#{typeId},
            brand_id=#{brandId},
            specs_id=#{specsId},
            count = #{count}
        ,gmt_modified = now()
        where id = #{id}
    </update>


    <update id="deleteEquipmentInfoById" parameterType="java.lang.Long">
        update equipment_info set is_delete = 1 where id=#{id} limit 1
    </update>


    <select id="selectEquipmentInfoByCode" resultType="EquipmentInfo">
          select * from equipment_info  where code=#{code} and is_delete = 0 limit 1
    </select>

    <select id="selectByCondition" resultType="com.lz.modules.equipment.entity.EquipmentInfo">
        select * from equipment_info  where is_delete = 0
        <if test="req.type != null">and type = #{req.type}</if>
        <if test="req.brandName != null">and brand_name = #{req.brandName}</if>
        <if test="req.specType != null and req.specType != '' ">
            AND spec_type LIKE CONCAT('%',#{req.specType},'%')
        </if>
        <if test="req.equipmentState != null">and equipment_state = #{req.equipmentState}</if>
        <if test="req.user != null and req.user != '' ">
            AND user LIKE CONCAT('%',#{req.user},'%')
        </if>
        <if test="req.owner != null and req.owner != '' ">
            AND owner LIKE CONCAT('%',#{req.owner},'%')
        </if>
        <if test="req.gmtInStartTime != null and req.gmtInStartTime != '' ">
            AND DATE_FORMAT(gmt_inbound, '%Y-%m-%d %H:%i:%S')  <![CDATA[ >= ]]> DATE_FORMAT(#{req.gmtInStartTime}, '%Y-%m-%d %H:%i:%S')
        </if>
        <if test="req.gmtInEndTime != null and req.gmtInEndTime != '' ">
            AND DATE_FORMAT(gmt_inbound, '%Y-%m-%d %H:%i:%S')  <![CDATA[ <= ]]> DATE_FORMAT(#{req.gmtInEndTime}, '%Y-%m-%d %H:%i:%S')
        </if>
        <if test="req.gmtOutStartTime != null and req.gmtOutStartTime != '' ">
            AND DATE_FORMAT(gmt_outbound, '%Y-%m-%d %H:%i:%S')  <![CDATA[ >= ]]> DATE_FORMAT(#{req.gmtOutStartTime}, '%Y-%m-%d %H:%i:%S')
        </if>
        <if test="req.gmtOutEndTime != null and req.gmtOutEndTime != '' ">
            AND DATE_FORMAT(gmt_outbound, '%Y-%m-%d %H:%i:%S')  <![CDATA[ <= ]]> DATE_FORMAT(#{req.gmtOutEndTime}, '%Y-%m-%d %H:%i:%S')
        </if>
        <if test="req.mark != null and req.mark != '' ">
            AND mark LIKE CONCAT('%',#{req.mark},'%')
        </if>
        <if test="req.originalCode != null and req.originalCode != '' ">
            AND original_code LIKE CONCAT('%',#{req.originalCode},'%')
        </if>
        <if test="req.code != null and req.code != '' ">
            AND code LIKE CONCAT('%',#{req.code},'%')
        </if>
        <if test="req.qr != null and req.qr != '' ">
            AND qr LIKE CONCAT('%',#{req.qr},'%')
        </if>

        <choose>
            <when test="req.other != null and req.other == 2">
                AND count = #{req.count}-1
            </when>
            <otherwise>
                AND count = #{req.count}
            </otherwise>
        </choose>

        <if test="req.other != null and req.other != '' ">
            <if test="req.other==1">
                AND mark !=''  and mark is not null
            </if>
            <if test="req.other==2">
                and code not in (select code from equipment_info where is_delete = 0 AND count = #{req.count})
            </if>
            <if test="req.other==3">
                and equipment_state = 2
            </if>

        </if>
        order by id desc
    </select>

    <select id="selectByStaffIdOrDepId" resultType="EquipmentInfo">

          select * from equipment_info  where is_delete = 0
        <if test="nameModel.employee == true">
            and  user_id = #{nameModel.userId}
        </if>
        <if test="nameModel.employee == false">
            and  sai_id = #{nameModel.userId}
        </if>
    </select>

    <select id="selectEquipmentInfoByCount" resultType="EquipmentInfo">

        select * from equipment_info  where is_delete = 0 and count=#{count} limit 1

    </select>


    <select id="selectCountAllByState" resultType="java.lang.Integer">
        select ifnull(count(*),0) from equipment_info  where is_delete = 0  and count = #{num}
    </select>


    <select id="selectCountByState" resultType="java.lang.Integer">
         select ifnull(count(*),0) from equipment_info  where is_delete = 0 and equipment_state = #{state} and count = #{num}
    </select>


    <select id="selectCountByDepartmentIds" resultType="java.lang.Integer">
        select ifnull(count(*),0) from equipment_info  where is_delete = 0 and count = #{num}
        and  depart_id in
        <foreach collection="asList" item="item" index="index" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="selectCountByNotFind" resultType="java.lang.Integer">

          select ifnull(count(*),0) from equipment_info  where is_delete = 0 and count = #{num} -1
            and code not in ( select code from equipment_info where  is_delete = 0 and count = #{num} )

    </select>
    <select id="selectCountAllDepartment" resultType="java.lang.Integer">
          select ifnull(count(*),0) from equipment_info  where is_delete = 0 and  count =#{num}   and depart_id is not null and depart_id > 0
    </select>

    <update id="updateCoverEquipmentInfoByTypeId" >
        update equipment_info set type = #{type} where type_id=#{id} and is_delete=0
    </update>

    <update id="updateCoverEquipmentInfoByBrandId" >
        update equipment_info set brand_name = #{brand} where brand_id=#{id} and is_delete=0
    </update>

    <update id="updateCoverEquipmentInfoBySpecsId" >
        update equipment_info set spec_type = #{specs} where specs_id=#{id} and is_delete=0
    </update>

</mapper>

