<?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.ruoyi.web.modules.goodsservice.mapper.BizGoodsServiceMapper">

    <resultMap id="BizGoodsServiceResult" type="com.ruoyi.web.modules.goodsservice.domain.BizGoodsService">
        <id property="serviceId" column="service_id"/>
        <result property="serviceName" column="service_name"/>
        <result property="serviceIntro" column="service_intro"/>
        <result property="serviceIcon" column="service_icon"/>
        <result property="defaultShowFlag" column="default_show_flag"/>
        <result property="sortNo" column="sort_no"/>
        <result property="delFlag" column="del_flag"/>
        <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"/>
    </resultMap>

    <sql id="selectVo">
        select service_id, service_name, service_intro, service_icon, default_show_flag, sort_no,
               del_flag, create_by, create_time, update_by, update_time, remark
        from biz_goods_service
    </sql>

    <select id="selectById" resultMap="BizGoodsServiceResult">
        <include refid="selectVo"/>
        where service_id = #{serviceId} and del_flag = '0'
    </select>

    <select id="selectList" resultMap="BizGoodsServiceResult">
        <include refid="selectVo"/>
        where del_flag = '0'
        <if test="serviceName != null and serviceName != ''">
            and service_name like concat('%', #{serviceName}, '%')
        </if>
        order by sort_no asc, create_time asc
    </select>

    <select id="selectAllEnabled" resultMap="BizGoodsServiceResult">
        <include refid="selectVo"/>
        where del_flag = '0'
        order by sort_no asc, create_time asc
    </select>

    <select id="selectDefaultShowEnabled" resultMap="BizGoodsServiceResult">
        <include refid="selectVo"/>
        where del_flag = '0' and default_show_flag = '1'
        order by sort_no asc, create_time asc
    </select>

    <select id="countByName" resultType="int">
        select count(1) from biz_goods_service
        where service_name = #{serviceName} and del_flag = '0'
        <if test="excludeId != null">
            and service_id != #{excludeId}
        </if>
    </select>

    <select id="existsGoodsRef" resultType="int">
        select count(1)
        from biz_goods_service_snapshot s
        inner join biz_goods g on g.goods_id = s.goods_id and g.del_flag = '0'
        where s.service_id = #{serviceId}
        limit 1
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="serviceId">
        insert into biz_goods_service (
            service_name, service_intro, service_icon, default_show_flag, sort_no,
            del_flag, create_by, create_time, remark
        ) values (
            #{serviceName}, #{serviceIntro}, #{serviceIcon}, #{defaultShowFlag}, #{sortNo},
            '0', #{createBy}, sysdate(), #{remark}
        )
    </insert>

    <update id="update">
        update biz_goods_service
        <set>
            <if test="serviceName != null">service_name = #{serviceName},</if>
            <if test="serviceIntro != null">service_intro = #{serviceIntro},</if>
            <if test="serviceIcon != null">service_icon = #{serviceIcon},</if>
            <if test="defaultShowFlag != null">default_show_flag = #{defaultShowFlag},</if>
            <if test="sortNo != null">sort_no = #{sortNo},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_by = #{updateBy},
            update_time = sysdate()
        </set>
        where service_id = #{serviceId} and del_flag = '0'
    </update>

    <update id="logicDeleteByIds">
        update biz_goods_service
        set del_flag = '2', update_by = #{updateBy}, update_time = sysdate()
        where del_flag = '0' and service_id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>
