<?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.MaterialMapperEx">
    <resultMap extends="com.jsh.erp.datasource.mappers.MaterialMapper.BaseResultMap" id="ResultMapList" type="com.jsh.erp.datasource.entities.MaterialVo4Unit">
        <result column="unitName" jdbcType="VARCHAR" property="unitName" />
        <result column="categoryName" jdbcType="VARCHAR" property="categoryName" />
        <result column="bar_code" jdbcType="VARCHAR" property="mBarCode" />
        <result column="commodity_unit" jdbcType="VARCHAR" property="commodityUnit" />
        <result column="purchase_decimal" jdbcType="VARCHAR" property="purchaseDecimal" />
        <result column="commodity_decimal" jdbcType="VARCHAR" property="commodityDecimal" />
        <result column="wholesale_decimal" jdbcType="VARCHAR" property="wholesaleDecimal" />
        <result column="low_decimal" jdbcType="VARCHAR" property="lowDecimal" />
    </resultMap>

    <resultMap extends="com.jsh.erp.datasource.mappers.MaterialMapper.BaseResultMap" id="ResultAndUnitMap" type="com.jsh.erp.datasource.entities.MaterialVo4Unit">
        <result column="unit_name" jdbcType="VARCHAR" property="unitName" />
    </resultMap>

    <select id="selectByConditionMaterial" parameterType="com.jsh.erp.datasource.entities.MaterialExample" resultMap="ResultMapList">
        select m.*,u.name unitName, mc.name categoryName, me.bar_code,
        me.purchase_decimal, me.commodity_decimal, me.wholesale_decimal,me.low_decimal
        FROM jsh_material m
        left JOIN jsh_material_extend me on m.id = me.material_id and ifnull(me.delete_Flag,'0') !='1'
        left JOIN jsh_unit u on m.unit_id = u.id and ifnull(u.delete_Flag,'0') !='1'
        left JOIN jsh_material_category mc on m.category_id = mc.id and ifnull(mc.status,'0') !='2'
        where 1=1
        and me.default_flag=1
        <if test="barCode != null">
            and me.bar_code like '%${barCode}%'
        </if>
        <if test="name != null">
            and m.name like '%${name}%'
        </if>
        <if test="standard != null">
            and m.standard like '%${standard}%'
        </if>
        <if test="model != null">
            and m.model like '%${model}%'
        </if>
        <if test="categoryIds != null">
            and m.category_id in (${categoryIds})
        </if>
        and ifnull(m.delete_flag,'0') !='1'
        order by m.id desc
        <if test="offset != null and rows != null">
            limit #{offset},#{rows}
        </if>
    </select>

    <select id="countsByMaterial" resultType="java.lang.Long">
        SELECT
        COUNT(m.id)
        FROM jsh_material m
        left JOIN jsh_material_extend me on m.id = me.material_id and ifnull(me.delete_Flag,'0') !='1'
        left JOIN jsh_unit u on m.unit_id = u.id and ifnull(u.delete_Flag,'0') !='1'
        left JOIN jsh_material_category mc on m.category_id = mc.id and ifnull(mc.status,'0') !='2'
        WHERE 1=1
        and me.default_flag=1
        <if test="barCode != null">
            and me.bar_code like '%${barCode}%'
        </if>
        <if test="name != null">
            and m.name like '%${name}%'
        </if>
        <if test="standard != null">
            and m.standard like '%${standard}%'
        </if>
        <if test="model != null">
            and m.model like '%${model}%'
        </if>
        <if test="categoryIds != null">
            and m.category_id in (${categoryIds})
        </if>
        and ifnull(m.delete_flag,'0') !='1'
    </select>

    <select id="findUnitName" resultType="java.lang.String">
        select u.name  from jsh_unit u
        left join jsh_material m on m.unit_id=u.id and ifnull(m.delete_flag,'0') !='1'
        where m.id = ${mId}
        and ifnull(u.delete_flag,'0') !='1'
    </select>

    <select id="findById" parameterType="com.jsh.erp.datasource.entities.MaterialExample" resultMap="ResultAndUnitMap">
        select m.*,u.name unit_name from jsh_material m
        left join jsh_unit u on m.unit_id=u.id and ifnull(u.delete_flag,'0') !='1'
        where m.id = ${id}
        and ifnull(m.delete_flag,'0') !='1'
    </select>

    <select id="findByIdWithBarCode" parameterType="com.jsh.erp.datasource.entities.MaterialExample" resultMap="ResultAndUnitMap">
        select m.*,u.name unit_name,me.bar_code m_bar_code, me.commodity_unit, me.purchase_decimal, me.commodity_decimal,
        me.wholesale_decimal, me.low_decimal
        from jsh_material m
        left join jsh_material_extend me on m.id=me.material_id and ifnull(me.delete_Flag,'0') !='1'
        left join jsh_unit u on m.unit_id=u.id and ifnull(u.delete_Flag,'0') !='1'
        where me.id = ${meId}
        and ifnull(m.delete_flag,'0') !='1'
    </select>

    <select id="findBySelectWithBarCode" parameterType="com.jsh.erp.datasource.entities.MaterialExample" resultMap="ResultAndUnitMap">
        select m.*,u.name unit_name,me.bar_code m_bar_code,me.id meId,me.commodity_unit from jsh_material m
        left join jsh_material_extend me on m.id=me.material_id and ifnull(me.delete_Flag,'0') !='1'
        left join jsh_unit u on m.unit_id=u.id and ifnull(u.delete_Flag,'0') !='1'
        where m.enabled=1 and me.id is not null
        <if test="q != null">
            and (m.name like '%${q}%' or me.bar_code like '%${q}%')
        </if>
        and ifnull(m.delete_flag,'0') !='1'
        ORDER BY id desc
        <if test="offset != null and rows != null">
            limit #{offset},#{rows}
        </if>
    </select>

    <select id="findBySelectWithBarCodeCount" resultType="java.lang.Integer">
        select count(1) from jsh_material m
        left join jsh_material_extend me on m.id=me.material_id and ifnull(me.delete_Flag,'0') !='1'
        left join jsh_unit u on m.unit_id=u.id and ifnull(u.delete_Flag,'0') !='1'
        where m.enabled=1 and me.id is not null
        <if test="q != null">
            and (m.name like '%${q}%' or me.bar_code like '%${q}%')
        </if>
        and ifnull(m.delete_flag,'0') !='1'
    </select>

    <select id="findByAll" parameterType="com.jsh.erp.datasource.entities.MaterialExample" resultMap="ResultMapList">
        select m.*,u.name unitName, mc.name categoryName,me.commodity_unit,me.purchase_decimal, me.commodity_decimal,
        me.wholesale_decimal, me.low_decimal
        FROM jsh_material m
        left join jsh_material_extend me on m.id=me.material_id and ifnull(me.delete_Flag,'0') !='1'
        left JOIN jsh_unit u on m.unit_id = u.id and ifnull(u.delete_Flag,'0') !='1'
        left JOIN jsh_material_category mc on m.category_id = mc.id and ifnull(mc.status,'0') !='2'
        where 1=1
        and me.default_flag=1
        <if test="name != null">
            and m.name like '%${name}%'
        </if>
        <if test="model != null">
            and m.model like '%${model}%'
        </if>
        <if test="categoryIds != null">
            and m.category_id in (${categoryIds})
        </if>
        and ifnull(m.delete_flag,'0') !='1'
        order by m.id desc
    </select>
    <select id="findByMaterialName"  resultType="com.jsh.erp.datasource.entities.Material">
        select m.*
        FROM jsh_material m
        where 1=1
        <if test="name != null">
            and m.name =#{name}
        </if>
        and ifnull(m.delete_flag,'0') !='1'
    </select>

    <select id="getMaterialEnableSerialNumberList" parameterType="java.util.Map" resultMap="com.jsh.erp.datasource.mappers.MaterialMapper.BaseResultMap">
        select
        id, category_id, name, mfrs, safety_stock, model, standard, color, unit, remark,
        unit_id, enabled,enable_serial_number
        FROM jsh_material
        where 1=1
        and enabled ='1'
        and enable_serial_number ='1'
        <if test="name != null and name != ''">
            <bind name="name" value="'%' + _parameter.name + '%'" />
            and name like #{name}
        </if>
        <if test="model != null and model != ''">
            <bind name="model" value="'%' + _parameter.model + '%'" />
            and model like #{model}
        </if>
        and ifnull(delete_flag,'0') !='1'
        order by id desc
    </select>

    <select id="getMaterialEnableSerialNumberCount" resultType="java.lang.Long">
        select count(1)
        FROM jsh_material
        where 1=1
        and enabled ='1'
        and enable_serial_number ='1'
        <if test="name != null and name != ''">
            <bind name="name" value="'%' + _parameter.name + '%'" />
            and name like #{name}
        </if>
        <if test="model != null and model != ''">
            <bind name="model" value="'%' + _parameter.model + '%'" />
            and model like #{model}
        </if>
        and ifnull(delete_flag,'0') !='1'
    </select>

    <update id="batchDeleteMaterialByIds">
        update jsh_material
        set delete_flag='1'
        where 1=1
        and ifnull(delete_flag,'0') !='1'
        and id in (
        <foreach collection="ids" item="id" separator=",">
            #{id}
        </foreach>
        )
    </update>
    <select id="getMaterialListByCategoryIds"  resultMap="com.jsh.erp.datasource.mappers.MaterialMapper.BaseResultMap">
        select
        <include refid="com.jsh.erp.datasource.mappers.MaterialMapper.Base_Column_List" />
        from jsh_material
        where 1=1
        and category_id in (
        <foreach collection="categoryIds" item="categoryId" separator=",">
            #{categoryId}
        </foreach>
        )
        and ifnull(delete_flag,'0') !='1'
    </select>
    <select id="getMaterialListByUnitIds"  resultMap="com.jsh.erp.datasource.mappers.MaterialMapper.BaseResultMap">
        select
        <include refid="com.jsh.erp.datasource.mappers.MaterialMapper.Base_Column_List" />
        from jsh_material
        where 1=1
        and unit_id in (
        <foreach collection="unitIds" item="unitId" separator=",">
            #{unitId}
        </foreach>
        )
        and ifnull(delete_flag,'0') !='1'
    </select>

    <select id="getMaxBarCode" resultType="java.lang.String">
        select max(CAST(l.bar_code AS SIGNED)) bar_code from jsh_material_extend l
        where 1=1
        and ifnull(l.delete_Flag,'0') !='1'
    </select>

    <select id="getMaterialByMeId" parameterType="com.jsh.erp.datasource.entities.MaterialExample" resultMap="ResultMapList">
        select m.*,me.bar_code,u.name unitName, mc.name categoryName
        FROM jsh_material m
        left join jsh_material_extend me on m.id=me.material_id and ifnull(me.delete_Flag,'0') !='1'
        left JOIN jsh_unit u on m.unit_id = u.id and ifnull(u.delete_flag,'0') !='1'
        left JOIN jsh_material_category mc on m.category_id = mc.id and ifnull(mc.status,'0') !='2'
        where 1=1
        <if test="meId != null">
            and me.id = ${meId}
        </if>
        and ifnull(m.delete_flag,'0') !='1'
    </select>

    <select id="getMaterialNameList" resultType="java.lang.String">
        select m.name from jsh_material m
        where m.name is not null and m.name !='' and ifnull(m.delete_flag,'0') !='1'
        group by m.name
        order by m.name asc
    </select>
</mapper>