<?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.microservices.pms.product.mapper.PmsProductMapper">

    <resultMap type="com.microservices.pms.product.domain.PmsProduct" id="PmsProductResult">
        <result property="id"    column="id"    />
        <result property="productName"    column="product_name"    />
        <result property="productDescription"    column="product_description"    />
        <result property="productAssigneeGitlinkIds" column="product_assignee_gitlink_ids"/>
        <result property="productIdentifier"    column="product_identifier"    />
        <result property="isTop"    column="is_top"    />
        <result property="sort"    column="sort"    />
        <result property="deptId"    column="dept_id"    />
        <result property="pmsEnterpriseId"    column="pms_enterprise_id"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="reservedField1"    column="reserved_field_1"    />
        <result property="reservedField2"    column="reserved_field_2"    />
    </resultMap>

    <sql id="selectPmsProductVo">
        select id,
               product_name,
               product_description,
               product_assignee_gitlink_ids,
               product_identifier,
               is_top,
               sort,
               dept_id,
               pms_enterprise_id,
               update_time,
               update_by,
               create_time,
               create_by,
               del_flag,
               reserved_field_1,
               reserved_field_2
        from pms_product
    </sql>

    <sql id="selectPmsProductAliasVo">
        select ppd.id,
               ppd.product_name,
               ppd.product_description,
               ppd.product_assignee_gitlink_ids,
               ppd.product_identifier,
               ppd.is_top,
               ppd.sort,
               ppd.dept_id,
               ppd.pms_enterprise_id,
               ppd.update_time,
               ppd.update_by,
               ppd.create_time,
               ppd.create_by,
               ppd.del_flag,
               ppd.reserved_field_1,
               ppd.reserved_field_2
        from pms_product ppd
                 left join pms_enterprise as pe on ppd.pms_enterprise_id = pe.id
    </sql>

    <select id="selectPmsProductList" parameterType="PmsProduct" resultMap="PmsProductResult">
        <include refid="selectPmsProductAliasVo"/>
        <include refid="selectWhere"/>
        order by ppd.is_top desc
        <choose>
            <when test="isDescByUpdateTime">
                , ppd.update_time desc,ppd.create_time desc
            </when>
            <otherwise>
                , ppd.update_time asc,ppd.create_time asc
            </otherwise>
        </choose>
    </select>

    <sql id="selectWhere">
        <where>
            <if test="productName != null  and productName != ''">and ppd.product_name like concat('%', #{productName},
                '%')
            </if>
            <if test="pmsEnterpriseIdentifier != null ">and pe.enterprise_identifier = #{pmsEnterpriseIdentifier}</if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
    </sql>

    <select id="selectPmsProductById" parameterType="Long" resultMap="PmsProductResult">
        <include refid="selectPmsProductVo"/>
        where id = #{id}
    </select>
    <select id="selectPmsProductByIdentifier" resultMap="PmsProductResult">
        <include refid="selectPmsProductVo"/>
        where product_identifier = #{identifier}
    </select>
    <select id="selectPmsProductByNameCount" resultType="java.lang.Integer">
        select count(0)
        from pms_product ppd
                 left join pms_enterprise as pe on ppd.pms_enterprise_id = pe.id
        where ppd.product_name = #{name}
          and pe.enterprise_identifier = #{pmsEnterpriseIdentifier}
    </select>
    <select id="selectPmsProductCount" resultType="java.lang.Integer">
        select count(0) from pms_product ppd
        left join pms_enterprise as pe on ppd.pms_enterprise_id=pe.id
        <include refid="selectWhere"/>
    </select>

    <insert id="insertPmsProduct" parameterType="PmsProduct" useGeneratedKeys="true" keyProperty="id">
        insert into pms_product
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="productName != null and productName != ''">product_name,</if>
            <if test="productDescription != null">product_description,</if>
            <if test="productAssigneeGitlinkIds != null and productAssigneeGitlinkIds != ''">
                product_assignee_gitlink_ids,
            </if>
            <if test="productIdentifier != null">product_identifier,</if>
            <if test="isTop != null">is_top,</if>
            <if test="sort != null">sort,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="pmsEnterpriseId != null">pms_enterprise_id,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="reservedField1 != null">reserved_field_1,</if>
            <if test="reservedField2 != null">reserved_field_2,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="productName != null and productName != ''">#{productName},</if>
            <if test="productDescription != null">#{productDescription},</if>
            <if test="productAssigneeGitlinkIds != null and productAssigneeGitlinkIds != ''">
                #{productAssigneeGitlinkIds},
            </if>
            <if test="productIdentifier != null">#{productIdentifier},</if>
            <if test="isTop != null">#{isTop},</if>
            <if test="sort != null">#{sort},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="pmsEnterpriseId != null">#{pmsEnterpriseId},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="reservedField1 != null">#{reservedField1},</if>
            <if test="reservedField2 != null">#{reservedField2},</if>
         </trim>
    </insert>

    <update id="updatePmsProduct" parameterType="PmsProduct">
        update pms_product
        <trim prefix="SET" suffixOverrides=",">
            <if test="productName != null and productName != ''">product_name = #{productName},</if>
            <if test="productDescription != null">product_description = #{productDescription},</if>
            <if test="productAssigneeGitlinkIds != null and productAssigneeGitlinkIds != ''">
                product_assignee_gitlink_ids = #{productAssigneeGitlinkIds},
            </if>
            <if test="productIdentifier != null">product_identifier = #{productIdentifier},</if>
            <if test="isTop != null">is_top = #{isTop},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="pmsEnterpriseId != null">pms_enterprise_id = #{pmsEnterpriseId},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="reservedField1 != null">reserved_field_1 = #{reservedField1},</if>
            <if test="reservedField2 != null">reserved_field_2 = #{reservedField2},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePmsProductById" parameterType="Long">
        delete from pms_product where id = #{id}
    </delete>

    <delete id="deletePmsProductByIds" parameterType="String">
        delete from pms_product where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>