<?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.example.his.api.db.dao.GoodsDao">

    <resultMap id="BaseResultMap" type="com.example.his.api.db.pojo.GoodsEntity">
        <id property="id" column="id" jdbcType="INTEGER"/>
        <result property="code" column="code" jdbcType="VARCHAR"/>
        <result property="title" column="title" jdbcType="VARCHAR"/>
        <result property="description" column="description" jdbcType="VARCHAR"/>
        <result property="checkup_1" column="checkup_1" jdbcType="OTHER"/>
        <result property="checkup_2" column="checkup_2" jdbcType="OTHER"/>
        <result property="checkup_3" column="checkup_3" jdbcType="OTHER"/>
        <result property="checkup_4" column="checkup_4" jdbcType="OTHER"/>
        <result property="checkup" column="checkup" jdbcType="OTHER"/>
        <result property="image" column="image" jdbcType="VARCHAR"/>
        <result property="initialPrice" column="initial_price" jdbcType="DECIMAL"/>
        <result property="currentPrice" column="current_price" jdbcType="DECIMAL"/>
        <result property="salesVolume" column="sales_volume" jdbcType="INTEGER"/>
        <result property="type" column="type" jdbcType="OTHER"/>
        <result property="tag" column="tag" jdbcType="OTHER"/>
        <result property="partId" column="part_id" jdbcType="TINYINT"/>
        <result property="ruleId" column="rule_id" jdbcType="INTEGER"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="md5" column="md5" jdbcType="VARCHAR"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!--    <sql id="Base_Column_List">-->
    <!--        id,code,title,-->
    <!--        description,checkup_1,checkup_2,-->
    <!--        checkup_3,checkup_4,checkup,-->
    <!--        image,initial_price,current_price,-->
    <!--        sales_volume,type,tag,-->
    <!--        part_id,rule_id,status,-->
    <!--        md5,update_time,create_time-->
    <!--    </sql>-->

    <!-- 体检套餐-分页查询 -->
    <select id="searchGoodsByPage" parameterType="Map" resultType="HashMap">
        SELECT g.id,
        g.`code`,
        g.title,
        FORMAT( g.initial_price, 0 ) AS "initialPrice",
        FORMAT( g.current_price, 0 ) AS "currentPrice",
        CASE
        WHEN LENGTH( g.sales_volume ) &lt;= 4 THEN FORMAT( g.sales_volume, 0 )
        ELSE CONCAT(FORMAT( MID( g.sales_volume, 1, LENGTH( g.sales_volume )-4), 0),"万+")
        END AS "salesVolume",
        g.type,
        g.part_id AS "partId",
        r.name AS "ruleName",
        (g.checkup IS NOT NULL) AS "hasCheckup",
        g.`status`
        FROM tb_goods g
        LEFT JOIN tb_rule r ON g.rule_id = r.id
        <where>
            <if test="keyword!=null">
                AND g.title LIKE CONCAT("%",#{keyword},"%")
            </if>
            <if test="code!=null">
                AND g.code = #{code}
            </if>
            <if test="type!=null">
                AND g.type = #{type}
            </if>
            <if test="partId!=null">
                AND g.part_id = #{partId}
            </if>
            <if test="ruleId!=null">
                AND r.id = #{ruleId}
            </if>
            <if test="status!=null">
                AND g.status = #{status}
            </if>
        </where>
    </select>

    <!-- 体检套餐-查询全部 -->
    <select id="searchGoods" parameterType="Map" resultType="HashMap">
        SELECT g.id,
               g.`code`,
               g.title,
               FORMAT(g.initial_price, 0) AS "initialPrice",
               FORMAT(g.current_price, 0) AS "currentPrice",
               CASE
                   WHEN LENGTH(g.sales_volume) &lt;= 4 THEN FORMAT(g.sales_volume, 0)
                   ELSE CONCAT(
                           FORMAT(MID(g.sales_volume, 1, LENGTH(g.sales_volume) - 4), 0),
                           "万+"
                        )
                   END                    AS "salesVolume",
               g.type,
               g.part_id                  AS "partId",
               r.name                     AS "ruleName",
               (g.checkup IS NOT NULL)    AS "hasCheckup",
               g.`status`
        FROM tb_goods g
                 LEFT JOIN tb_rule r ON g.rule_id = r.id
    </select>

    <!-- 体检套餐-查询id -->
    <select id="searchGoodsById" resultType="HashMap">
        SELECT g.`code`,
        g.title,
        g.description,
        g.image,
        CAST(g.initial_price AS CHAR) AS initialPrice,
        CAST(g.current_price AS CHAR) AS currentPrice,
        r.`id` AS ruleId,
        r.`name` AS ruleName,
        g.type,
        g.tag,
        g.part_id AS partId,
        g.checkup_1,
        g.status,
        IFNULL(JSON_LENGTH(g.checkup_1), 0) AS count_1,
        g.checkup_2,
        IFNULL(JSON_LENGTH(g.checkup_2), 0) AS count_2,
        g.checkup_3,
        IFNULL(JSON_LENGTH(g.checkup_3), 0) AS count_3,
        g.checkup_4,
        IFNULL(JSON_LENGTH(g.checkup_4), 0) AS count_4
        FROM tb_goods g
        LEFT JOIN tb_rule r ON g.rule_id = r.id
        WHERE g.id = #{id}
        <if test="status!=null">
            AND status = #{status}
        </if>
    </select>

    <select id="searchGoodsAllById" resultMap="BaseResultMap">
        select *
        from tb_goods
        where id = #{id};
    </select>

    <!-- 体检套餐-新增 -->
    <insert id="insertGoods">
        INSERT INTO tb_goods
        SET code          = #{code},
            title         = #{title},
            description   = #{description},
            checkup_1     = #{checkup_1},
            checkup_2     = #{checkup_2},
            checkup_3     = #{checkup_3},
            checkup_4     = #{checkup_4},
            image         = #{image},
            initial_price = #{initialPrice},
            current_price = #{currentPrice},
            sales_volume  = 0,
            type          = #{type},
            tag           = #{tag},
            part_id       = #{partId},
            rule_id       = #{ruleId},
            status        = #{status},
            `md5`         = #{md5}
    </insert>

    <!-- 体检套餐-更新  -->
    <update id="updateGoods">
        UPDATE tb_goods
        SET title         = #{title},
            code          = #{code},
            description   = #{description},
            checkup_1     = #{checkup_1},
            checkup_2     = #{checkup_2},
            checkup_3     = #{checkup_3},
            checkup_4     = #{checkup_4},
            image         = #{image},
            initial_price = #{initialPrice},
            current_price = #{currentPrice},
            type          = #{type},
            tag           = #{tag},
            part_id       = #{partId},
            rule_id       = #{ruleId},
            status        = #{status},
            `md5`         = #{md5}
        WHERE id = #{id}
    </update>

    <update id="updateGoodsCheckup">
        update tb_goods
        set md5     = #{md5},
            checkup = #{checkup}
        where id = #{id}
    </update>

    <update id="updateGoodsStatus">
        update tb_goods
        set status = #{status}
        where id = #{id}
    </update>

    <!-- 体检套餐-删除 -->
    <delete id="deleteBatchGoods">
        delete from tb_goods where id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">#{id}</foreach>
    </delete>

    <select id="deleteGoodsImage" resultType="String">
        select image from tb_goods where id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">#{id}</foreach>
    </select>

    <!--  体检套餐-推荐  -->
    <select id="searchGoodsByPartId" resultType="HashMap">
        SELECT id,
               `code`,
               title,
               description,
               image,
               initial_price AS "initialPrice",
               current_price AS "currentPrice",
               sales_volume  AS "salesVolume"
        FROM tb_goods
        WHERE `status` = 1
          AND part_id = #{partId}
        ORDER BY sales_volume DESC, id DESC
        LIMIT 4
    </select>

    <!--  体检套餐-查询商品  -->
    <select id="searchFrontGoodsByPage" parameterType="Map" resultType="HashMap">
        SELECT id,
        `code`,
        title,
        description,
        image,
        initial_price AS "initialPrice",
        current_price AS "currentPrice",
        sales_volume AS "salesVolume"
        FROM tb_goods
        WHERE `status` = 1
        <if test="keyword!=null">
            AND (title LIKE CONCAT("%",#{keyword},"%") OR code LIKE CONCAT("%",#{keyword},"%"))
        </if>
        <if test="type!=null">
            AND type = #{type}
        </if>
        <if test="sex!=null">
            AND JSON_CONTAINS (tag, CONCAT('"',#{sex},'"'))
        </if>
        <choose>
            <when test="priceType==1">
                AND current_price &gt;= 0 AND current_price &lt; 100
            </when>
            <when test="priceType==2">
                AND current_price &gt;= 100 AND current_price &lt; 500
            </when>
            <when test="priceType==3">
                AND current_price &gt;= 500 AND current_price &lt; 1000
            </when>
            <when test="priceType==4">
                AND current_price &gt;= 1000
            </when>
        </choose>
        <choose>
            <when test="orderType==1">
                ORDER BY id DESC
            </when>
            <when test="orderType==2">
                ORDER BY sales_volume DESC
            </when>
            <when test="orderType==3">
                ORDER BY currentPrice ASC
            </when>
            <when test="orderType==4">
                ORDER BY currentPrice DESC
            </when>
            <otherwise>
                ORDER BY sales_volume DESC, id DESC
            </otherwise>
        </choose>
    </select>


    <!--  体检套餐-front查询当前订单的商品信息  -->
    <select id="searchSnapshotNeededById" parameterType="int" resultType="HashMap">
        SELECT g.code,
               g.title,
               g.description,
               g.image,
               CAST(g.initial_price AS CHAR) AS initialPrice,
               CAST(g.current_price as CHAR) AS currentPrice,
               r.name                        AS ruleName,
               r.rule,
               g.type,
               g.tag,
               g.checkup_1,
               g.checkup_2,
               g.checkup_3,
               g.checkup_4,
               g.checkup,
               g.md5
        FROM tb_goods g
                 LEFT JOIN tb_rule r on g.rule_id = r.id
        WHERE g.id = #{id}
    </select>

    <!--  更新商品销量  -->
    <update id="updateSalesVolume" parameterType="int">
        UPDATE tb_goods
        SET sales_volume = sales_volume + 1
        WHERE id = #{id}
    </update>


</mapper>
