<?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.jsh.erp.datasource.mappers.SerialNumberMapperEx">
    <resultMap extends="com.jsh.erp.datasource.mappers.SerialNumberMapper.BaseResultMap"  id="SerialNumberExBaseResultMap" type="com.jsh.erp.datasource.entities.SerialNumberEx">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="materialCode" jdbcType="VARCHAR" property="materialCode" />
        <result column="materialName" jdbcType="VARCHAR" property="materialName" />
        <result column="creatorName" jdbcType="VARCHAR" property="creatorName" />
        <result column="updaterName" jdbcType="VARCHAR" property="updaterName" />
        <result column="depotHeadNumber" jdbcType="VARCHAR" property="depotHeadNumber" />
        <result column="depotHeadType" jdbcType="VARCHAR" property="depotHeadType" />
    </resultMap>
    <select id="selectByConditionSerialNumber" resultMap="SerialNumberExBaseResultMap">
        select
        ser.id, ser.material_id, ser.serial_number, ser.is_sell, ser.remark, ser.delete_flag, ser.create_time,
        ser.update_time,me.bar_code as materialCode,mat.name as materialName,null as creator,null as updater,null as creatorName,
        null as updaterName,ser.depot_head_id, dh.number as depotHeadNumber,concat(dh.sub_type,dh.type) as depotHeadType
        FROM jsh_serial_number ser
        left join jsh_material mat on mat.id = ser.material_Id and ifnull(mat.delete_flag,'0') !='1'
        left join jsh_material_extend me on mat.id=me.material_id and ifnull(me.delete_Flag,'0') !='1'
        left join jsh_depot_head dh on dh.id= ser.depot_head_id and ifnull(dh.delete_flag,'0') !='1'
        where 1=1
        <if test="serialNumber != null">
            <bind name="serialNumber" value="'%' + _parameter.serialNumber + '%'" />
            and ser.serial_number like  #{serialNumber}
        </if>
        <if test="materialName != null">
            <bind name="materialName" value="'%' + _parameter.materialName + '%'" />
            and mat.name like  #{materialName}
        </if>
        and ifnull(ser.delete_flag,'0') !='1'
        order by ser.id desc
        <if test="offset != null and rows != null">
            limit #{offset},#{rows}
        </if>
    </select>
    <select id="countSerialNumber"  resultType="java.lang.Long">
        SELECT
        COUNT(ser.id)
        FROM jsh_serial_number ser
        left JOIN jsh_material mat on mat.id = ser.material_Id and ifnull(mat.delete_flag,'0') !='1'
        where 1=1
        <if test="serialNumber != null">
            <bind name="serialNumber" value="'%' + _parameter.serialNumber + '%'" />
            and ser.serial_Number like #{serialNumber}
        </if>
        <if test="materialName != null">
            <bind name="materialName" value="'%' + _parameter.materialName + '%'" />
            and mat.name like  #{materialName}
        </if>
        and ifnull(ser.delete_flag,'0') !='1'
        order by ser.id desc
    </select>
    <select id="findById" resultMap="SerialNumberExBaseResultMap">
        SELECT
        ser.id, ser.material_id, ser.serial_number, ser.is_sell, ser.remark, ser.delete_flag, ser.create_time,
        ser.update_time,ser.creator,ser.updater,mat.name as materialName,cr.username as creatorName,
        ur.username as updaterName,ser.depot_head_id
        FROM jsh_serial_number ser
        left JOIN jsh_material mat on mat.id = ser.material_Id and ifnull(mat.delete_Flag,'0') !='1'
        left join jsh_user cr on ser.creator=cr.id and ifnull(cr.status,'0') not in('1','2')
        left join jsh_user ur on ser.updater=ur.id and ifnull(ur.status,'0') not in('1','2')
        where 1=1
        and ser.id=#{id}
        and ifnull(ser.delete_flag,'0') !='1'
        order by ser.id desc
    </select>
    <select id="findBySerialNumber" resultMap="SerialNumberExBaseResultMap">
        SELECT
        ser.id, ser.material_id, ser.serial_number, ser.is_sell, ser.remark, ser.delete_flag, ser.create_time,
        ser.update_time,ser.creator,ser.updater,ser.depot_head_id
        FROM jsh_serial_number ser
        where 1=1
        <if test="serialNumber != null">
            and ser.serial_number=#{serialNumber}
        </if>
        and ifnull(ser.delete_flag,'0') !='1'
        order by ser.id desc
    </select>
    <insert id="addSerialNumber" parameterType="com.jsh.erp.datasource.entities.SerialNumberEx"
            useGeneratedKeys="true" keyProperty="id" keyColumn="id">
       insert into jsh_serial_number
        (material_id, serial_number, is_sell, remark,delete_flag,
        create_time, creator,update_time, updater,depot_head_id)
        values
        (#{materialId},#{serialNumber},#{isSell},#{remark},#{deleteFlag},
        #{createTime},#{creator},#{updateTime},#{updater},#{depotHeadId}
        )
    </insert>
    <update id="updateSerialNumber" parameterType="com.jsh.erp.datasource.entities.SerialNumberEx">
        update jsh_serial_number
        <set>
            <if test="materialId != null">
                material_id = #{materialId,jdbcType=BIGINT},
            </if>
            <if test="serialNumber != null">
                serial_number = #{serialNumber,jdbcType=VARCHAR},
            </if>
            <if test="isSell != null">
                is_sell = #{isSell,jdbcType=VARCHAR},
            </if>
            <if test="remark != null">
                remark = #{remark,jdbcType=VARCHAR},
            </if>
            <if test="deleteFlag != null">
                delete_flag = #{deleteFlag,jdbcType=VARCHAR},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updater != null">
                updater = #{updater,jdbcType=BIGINT},
            </if>
            <if test="depotHeadId != null">
                depot_head_id = #{depotHeadId,jdbcType=BIGINT},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <select id="findSerialNumberByMaterialId" resultType="java.lang.Integer">
        SELECT
        count(ser.id)
        FROM jsh_serial_number ser
        where 1=1
        <if test="materialId != null">
            and ser.material_Id=#{materialId}
        </if>
        and ifnull(ser.delete_flag,'0') !='1'
        and ser.is_sell !='1'
    </select>
    <select id="countSerialNumberByMaterialIdAndDepotheadId" resultType="java.lang.Integer">
        SELECT
        count(ser.id)
        FROM jsh_serial_number ser
        where 1=1
        <if test="materialId != null">
            and ser.material_id=#{materialId}
        </if>
        <if test="depotHeadId != null">
            and ser.depot_head_id=#{depotHeadId}
        </if>
        and ser.is_sell =#{isSell,jdbcType=VARCHAR}
        and ifnull(ser.delete_flag,'0') !='1'
    </select>
    <update id="sellSerialNumber">
        update jsh_serial_number
        <set>
            is_sell = '1',
            <if test="depotHeadId !=null ">
                depot_head_id = #{depotHeadId},
            </if>
            <if test="updateTime !=null ">
                update_time = #{updateTime},
            </if>
            <if test="updater != null">
                updater = #{updater},
            </if>
        </set>
        where 1=1
        <if test="materialId != null">
            and material_id = #{materialId}
        </if>
            and is_sell != '1'
            and ifnull(delete_flag,'0') !='1'
        <if test="count != null">
        and id in
            ( select batchSN.id from
                ( select selFrom.id from jsh_serial_number selFrom
                    where 1=1
                    <if test="materialId != null">
                        and selFrom.material_Id = #{materialId}
                    </if>
                        and selFrom.is_sell != '1'
                        and ifnull(selFrom.delete_flag,'0') !='1'
                    limit 0,#{count}
                ) batchSN
            )
        </if>
    </update>

    <update id="cancelSerialNumber">
        update jsh_serial_number
        <set>
                is_sell = '0', depot_head_id=null,
            <if test="updateTime !=null ">
                update_time = #{updateTime},
            </if>
            <if test="updater != null">
                updater = #{updater},
            </if>
        </set>
        where 1=1
        <if test="materialId != null">
            and material_id = #{materialId}
        </if>
        <if test="depotHeadId !=null ">
            and depot_head_id = #{depotHeadId,jdbcType=BIGINT}
        </if>
            and is_sell != '0'
            and ifnull(delete_flag,'0') !='1'
        <if test="count != null">
            and id in
            ( select batchSN.id from
            ( select selFrom.id from jsh_serial_number selFrom
            where 1=1
            <if test="materialId != null">
                and selFrom.material_id = #{materialId}
            </if>
            <if test="depotHeadId !=null ">
                and selFrom.depot_head_id = #{depotHeadId,jdbcType=BIGINT}
            </if>
                and selFrom.is_sell !='0'
                and ifnull(selFrom.delete_flag,'0') !='1'
            limit 0,#{count}
            ) batchSN
            )
        </if>
    </update>
    <insert id="batAddSerialNumber"
            useGeneratedKeys="true" keyProperty="id" keyColumn="id">
       insert into jsh_serial_number
        (material_id, serial_number, is_sell, remark,delete_flag,
        create_time, creator,update_time, updater)
        values
        <foreach collection='list' item='each' separator=','>
        (#{each.materialId},#{each.serialNumber},'0',#{each.remark},'0',
        #{each.createTime},#{each.creator},#{each.updateTime},#{each.updater}
        )
        </foreach>
    </insert>
    <update id="batchDeleteSerialNumberByIds">
        update jsh_serial_number
        set update_time=#{updateTime},updater=#{updater},delete_flag='1'
        where 1=1
        and id in (
        <foreach collection="ids" item="id" separator=",">
            #{id}
        </foreach>
        )
    </update>





</mapper>