<?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="cn.iocoder.yudao.module.inspection.mapper.ProjectRoyaltyMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <insert id="insertBatch">
        INSERT INTO inspection_project_royalty (project_id, goods_id, royalty_amount, deleted, create_time) VALUES
        <foreach collection="createReq" item="item" separator=",">
            (#{item.projectId},#{item.goodsId},#{item.royaltyAmount},0, NOW())
        </foreach>
    </insert>
    <update id="updtaBatch">
        UPDATE inspection_project_royalty
        SET royalty_amount =
        CASE
        <foreach collection="list" item="item" index="index">
            WHEN id = #{item.id} THEN #{item.royaltyAmount}
        </foreach>
        ELSE royalty_amount
        END
        WHERE id IN
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.id}
        </foreach>
    </update>
    <delete id="deleteByProjectId" parameterType="java.lang.String">
        delete from inspection_project_royalty where project_id = #{projectId}
    </delete>
    <select id="selectListByProjrctId" resultType="cn.iocoder.yudao.module.inspection.entity.ProjectRoyalty"
            parameterType="java.lang.String">
        SELECT g.id id,
               g.title title,
               COALESCE(r.royalty_amount / 100, 0) AS royalty_amount
        FROM shop_inspection_goods g
                 LEFT JOIN inspection_project_royalty r
                           ON g.id = r.goods_id
                               AND r.deleted = 0
                               AND r.project_id = #{projectId}
        WHERE  g.deleted = 0
          AND g.partner_id = #{parentId}
    </select>

</mapper>