<?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.ghy.goods.mapper.GoodsMapper">
    <resultMap id="GoodsResult" type="com.ghy.goods.domain.Goods">
        <id     property="goodsId"             column="goods_id"       />
        <result property="goodsCode"           column="goods_code"     />
        <result property="deptId"              column="dept_id"     />
        <result property="goodsName"           column="goods_name"     />
        <result property="goodsDesc"           column="goods_desc"     />
        <result property="goodsUnit"           column="goods_unit"     />
        <result property="warrantyPeriod"           column="warranty_period"     />
        <result property="servActivity"           column="serv_activity"     />
        <result property="expectDuration"           column="expect_duration"     />
        <result property="goodsSort"           column="goods_sort"/>
        <result property="workerId"           column="worker_id"/>
        <result property="deptGoodsCategoryId" column="dept_goods_category_id"/>
        <result property="goodsImgUrl"         column="goods_img_url"/>
        <result property="goodsVideoUrl"       column="goods_video_url"/>
        <result property="status"              column="status"/>
        <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"        />
        <result property="areaDesc"              column="area_desc"        />
        <result property="keyword"             column="keyword"        />
    </resultMap>

    <sql id="selectGoods">
        SELECT goods_id, goods_code, dept_id, goods_name, goods_desc, warranty_period, serv_activity, goods_unit, expect_duration, goods_sort, worker_id,
               dept_goods_category_id, goods_img_url, goods_video_url, status, create_by, create_time, remark, area_desc, keyword
        FROM goods
    </sql>

    <sql id="selectGoodsWithArea">
        SELECT DISTINCT g.goods_id, goods_code, dept_id, goods_name, goods_desc, goods_sort, worker_id, goods_unit, warranty_period, serv_activity, expect_duration,
               dept_goods_category_id, goods_img_url, goods_video_url, status, g.create_by, g.create_time, g.remark, g.area_desc, g.keyword
        FROM goods g
        LEFT JOIN goods_area ga ON g.goods_id = ga.goods_id
    </sql>

    <update id="updateGoods" parameterType="com.ghy.goods.domain.Goods">
        UPDATE goods
        <set>
            <if test="goodsName != null and goodsName != ''">goods_name = #{goodsName},</if>
            <if test="goodsDesc != null and goodsDesc != ''">goods_desc = #{goodsDesc},</if>
            <if test="goodsSort != null and goodsSort != ''">goods_sort = #{goodsSort},</if>
            <if test="deptGoodsCategoryId != null and deptGoodsCategoryId != ''">dept_goods_category_id = #{deptGoodsCategoryId},</if>
            <if test="goodsImgUrl != null and goodsImgUrl != ''">goods_img_url = #{goodsImgUrl},</if>
            <if test="goodsVideoUrl != null and goodsVideoUrl != ''">goods_video_url = #{goodsVideoUrl},</if>
            <if test="status != null">`status` = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="goodsUnit != null and goodsUnit != ''">goods_unit = #{goodsUnit},</if>
            <if test="warrantyPeriod != null and warrantyPeriod != ''">warranty_period = #{warrantyPeriod},</if>
            <if test="servActivity != null and servActivity!=''">serv_activity = #{servActivity},</if>
            <if test="expectDuration != null and expectDuration!=''">expect_duration = #{expectDuration},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="areaDesc != null and areaDesc != ''">area_desc = #{areaDesc},</if>
            update_time = sysdate()
        </set>
        WHERE goods_id = #{goodsId}
    </update>

    <update id="setCode">
        UPDATE goods
        SET goods_code = #{goodsCode}, update_time = sysdate()
        WHERE goods_id = #{goodsId}
    </update>

    <insert id="insertGoods" parameterType="com.ghy.goods.domain.Goods" useGeneratedKeys="true" keyProperty="goodsId">
        insert into goods(
        <if test="goodsCode != null and goodsCode != ''">goods_code,</if>
        <if test="deptId != null and deptId != ''">dept_id,</if>
        <if test="goodsName != null and goodsName != ''">goods_name,</if>
        <if test="goodsDesc != null and goodsDesc != ''">goods_desc,</if>
        <if test="goodsUnit != null and goodsUnit != ''">goods_unit,</if>
        <if test="warrantyPeriod != null and warrantyPeriod != ''">warranty_period,</if>
        <if test="servActivity != null and servActivity != ''">serv_activity,</if>
        <if test="expectDuration != null and expectDuration != ''">expect_duration,</if>
        <if test="goodsSort != null and goodsSort != ''">goods_sort,</if>
        <if test="workerId != null and workerId != ''">worker_id,</if>
        <if test="deptGoodsCategoryId != null and deptGoodsCategoryId != 0">dept_goods_category_id,</if>
        <if test="goodsImgUrl != null and goodsImgUrl != ''">goods_img_url,</if>
        <if test="goodsVideoUrl != null and goodsVideoUrl != ''">goods_video_url,</if>
        <if test="status != null and status != ''">status,</if>
        <if test="remark != null and remark != ''">remark,</if>
        <if test="areaDesc != null and areaDesc != ''">area_desc,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        create_time
        )values(
        <if test="goodsCode != null and goodsCode != ''">#{goodsCode},</if>
        <if test="deptId != null and deptId != ''">#{deptId},</if>
        <if test="goodsName != null and goodsName != ''">#{goodsName},</if>
        <if test="goodsDesc != null and goodsDesc != ''">#{goodsDesc},</if>
        <if test="goodsUnit != null and goodsUnit != ''">#{goodsUnit},</if>
        <if test="warrantyPeriod != null and warrantyPeriod != ''">#{warrantyPeriod},</if>
        <if test="servActivity != null and servActivity != ''">#{servActivity},</if>
        <if test="expectDuration != null and expectDuration != ''">#{expectDuration},</if>
        <if test="goodsSort != null and goodsSort != ''">#{goodsSort},</if>
        <if test="workerId != null and workerId != ''">#{workerId},</if>
        <if test="deptGoodsCategoryId != null and deptGoodsCategoryId != 0">#{deptGoodsCategoryId},</if>
        <if test="goodsImgUrl != null and goodsImgUrl != ''">#{goodsImgUrl},</if>
        <if test="goodsVideoUrl != null and goodsVideoUrl != ''">#{goodsVideoUrl},</if>
        <if test="status != null and status != ''">#{status},</if>
        <if test="remark != null and remark != ''">#{remark},</if>
        <if test="areaDesc != null and areaDesc != ''">#{areaDesc},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        sysdate()
        )
    </insert>

    <select id="selectGoodsList" parameterType="com.ghy.goods.domain.Goods" resultMap="GoodsResult">
        <include refid="selectGoodsWithArea" />
        LEFT JOIN sys_area sa ON sa.area_id = ga.country_area_id
        <where>
            <if test="goodsCode != null and goodsCode != ''">
                AND goods_code like concat('%', #{goodsCode}, '%')
            </if>
            <if test="deptGoodsCategoryId != null and deptGoodsCategoryId != ''">
                AND dept_goods_category_id = #{deptGoodsCategoryId}
            </if>
            <if test="deptGoodsCategoryIds != null and deptGoodsCategoryIds.size > 0">
                AND dept_goods_category_id in
                <foreach collection="deptGoodsCategoryIds" item="deptGoodsCategoryId" open="(" separator="," close=")">
                    #{deptGoodsCategoryId}
                </foreach>
            </if>
            <if test="areaId != null and areaId != ''">
                AND country_area_id = #{areaId}
            </if>
            <if test="areaIds != null and areaIds.size > 0">
                AND country_area_id in
                <foreach collection="areaIds" item="areaId" open="(" separator="," close=")">
                    #{areaId}
                </foreach>
            </if>
            <if test="exceptParentAreaId != null">
                AND sa.parent_code != #{exceptParentAreaId}
            </if>
            <if test="goodsName != null and goodsName != ''">
                AND goods_name like concat('%', #{goodsName}, '%')
            </if>
            <if test="status != null">
                AND status = #{status}
            </if>
            <if test="workerId != null">
                AND worker_id = #{workerId}
            </if>
            <if test="goodsId != null">
                AND g.goods_id = #{goodsId}
            </if>
            <if test="keyword != null and keyword != ''">
                AND g.keyword LIKE concat('%', #{keyword}, '%')
            </if>
        </where>
        /* 默认生成时间排序 */
        order by create_time
        <trim suffixOverrides=",">
            <choose>
                <when test="params.createTimeSort != null and params.createTimeSort != ''">
                    ${params.createTimeSort}
                </when>
                <otherwise>
                    desc
                </otherwise>
            </choose>
        </trim>
    </select>

    <select id="selectById" parameterType="long" resultMap="GoodsResult">
        <include refid="selectGoods"/>
        <where>
            <if test="goodsId != null and goodsId != 0">
                AND goods_id = #{goodsId}
            </if>
        </where>
    </select>

    <delete id="deleteGoodsByIds" parameterType="Long">
        delete from goods where goods_id in
        <foreach collection="array" item="goodsId" open="(" separator="," close=")">
            #{goodsId}
        </foreach>
    </delete>

    <select id="checkGoodsNameUnique" parameterType="String" resultMap="GoodsResult">
        <include refid="selectGoods"/>
        where goods_name=#{goodsName} limit 1
    </select>

    <select id="checkGoodsCodeUnique" parameterType="String" resultMap="GoodsResult">
        <include refid="selectGoods"/>
        where goods_code=#{goodsCode} limit 1
    </select>

    <select id="selectOneByGoodsCategoryId" parameterType="String" resultMap="GoodsResult">
        <include refid="selectGoods"/>
        WHERE dept_goods_category_id = #{deptGoodsCategoryId} LIMIT 1
    </select>

    <select id="selectByIds" resultMap="GoodsResult">
        <include refid="selectGoods"/>
        where goods_id IN
        <foreach collection="collection" item="ids" open="(" separator="," close=")">
            #{ids}
        </foreach>
    </select>

    <select id="selectByCategoryIds" resultMap="GoodsResult">
        <include refid="selectGoods"/>
        where dept_goods_category_id IN
        <foreach collection="collection" item="ids" open="(" separator="," close=")">
            #{ids}
        </foreach>
    </select>
</mapper>
