<?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" />
    </resultMap>

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

    <select id="selectByConditionMaterial" parameterType="com.jsh.erp.datasource.entities.MaterialExample" resultMap="ResultMapList">
        select m.*,u.uname unitName, mc.name categoryName
        FROM jsh_material m
        left JOIN jsh_unit u on m.UnitId = u.id
        left JOIN jsh_materialcategory mc on m.CategoryId = mc.id
        where 1=1
        <if test="name != null">
            and m.name like '%${name}%'
        </if>
        <if test="model != null">
            and m.model like '%${model}%'
        </if>
        <if test="categoryId != 1">
            and m.CategoryId in (${categoryIds})
        </if>
        order by m.id asc
        <if test="offset != null and rows != null">
            limit #{offset},#{rows}
        </if>
    </select>

    <select id="countsByMaterial" resultType="java.lang.Integer">
        SELECT
        COUNT(m.id)
        FROM jsh_material m
        left JOIN jsh_unit u on m.UnitId = u.id
        left JOIN jsh_materialcategory mc on m.CategoryId = mc.id
        WHERE 1=1
        <if test="name != null">
            and m.name like '%${name}%'
        </if>
        <if test="model != null">
            and m.model like '%${model}%'
        </if>
        <if test="categoryId != 1">
            and m.CategoryId in (${categoryIds})
        </if>
    </select>

    <select id="findUnitName" resultType="java.lang.String">
        select jsh_unit.UName  from jsh_unit left join jsh_material on UnitId=jsh_unit.id where jsh_material.id = ${mId}
    </select>

    <select id="findById" parameterType="com.jsh.erp.datasource.entities.MaterialExample" resultMap="ResultAndUnitMap">
        select m.*,u.UName from jsh_material m left join jsh_unit u on m.UnitId=u.id where m.id = ${id}
    </select>

    <select id="findBySelect" parameterType="com.jsh.erp.datasource.entities.MaterialExample" resultMap="ResultAndUnitMap">
        select m.*,u.UName from jsh_material m left join jsh_unit u on m.UnitId=u.id where m.enabled=1 ORDER BY Id asc
    </select>

    <update id="updatePriceNullByPrimaryKey" parameterType="java.lang.Long">
        update jsh_material
        set
        RetailPrice = null,
        LowPrice = null,
        PresetPriceOne = null,
        PresetPriceTwo = null
        where Id = #{id,jdbcType=BIGINT}
    </update>

    <update id="updateUnitIdNullByPrimaryKey" parameterType="java.lang.Long">
        update jsh_material
        set
        UnitId = null
        where Id = #{id,jdbcType=BIGINT}
    </update>

    <select id="findByAll" parameterType="com.jsh.erp.datasource.entities.MaterialExample" resultMap="ResultMapList">
        select m.*,u.uname unitName, mc.name categoryName
        FROM jsh_material m
        left JOIN jsh_unit u on m.UnitId = u.id
        left JOIN jsh_materialcategory mc on m.CategoryId = mc.id
        where 1=1
        <if test="name != null">
            and m.name like '%${name}%'
        </if>
        <if test="model != null">
            and m.model like '%${model}%'
        </if>
        <if test="categoryId != 1">
            and m.CategoryId in (${categoryIds})
        </if>
        order by m.id asc
    </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>

    </select>


</mapper>