<?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.tmerclub.cloud.product.mapper.StockChangeReasonMapper">
  <resultMap id="stockChangeReasonMap" type="com.tmerclub.cloud.product.model.StockChangeReason">
    <id column="stock_change_reason_id" property="stockChangeReasonId" />
    <result column="type" property="type"/>
    <result column="shop_id" property="shopId"/>
    <result column="status" property="status"/>
    <result column="sys_type" property="sysType"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `stock_change_reason_id`,`type`,`shop_id`,`status`,`sys_type`
  </sql>
  <select id="pageByParams" resultType="com.tmerclub.cloud.product.vo.StockChangeReasonVO">
    SELECT scr.stock_change_reason_id, scr.`type`, scrl.`reason`,
    if(#{stockChangeReason.shopId} = 0, scr.status, if(scrs.stock_change_reason_status_id is null, scr.status, scrs.status)) as status,
    scrl.`remark`, IF(scr.shop_id = 0, 1, 0) AS sys_set
    FROM stock_change_reason scr
    left JOIN stock_change_reason_lang scrl ON scr.stock_change_reason_id = scrl.stock_change_reason_id AND scrl.lang = 0
    LEFT JOIN stock_change_reason_status scrs ON scrs.shop_id = #{stockChangeReason.shopId} AND scr.stock_change_reason_id = scrs.stock_change_reason_id
    <where>
      <if test="stockChangeReason.shopId != 0">
        AND ((scr.shop_id = #{stockChangeReason.shopId} and scr.sys_type = #{stockChangeReason.sysType}) OR scr.shop_id = 0)
        <if test="stockChangeReason.status != null">
          AND if(scrs.stock_change_reason_status_id is null, scr.status, scrs.status) = #{stockChangeReason.status}
        </if>
      </if>
      <if test="stockChangeReason.shopId == 0">
        AND scr.shop_id = #{stockChangeReason.shopId}
      </if>
      <if test="stockChangeReason.type != null">
        AND scr.`type` = #{stockChangeReason.type}
      </if>
      <if test="stockChangeReason.reason != null and stockChangeReason.reason != ''">
        AND scrl.reason LIKE CONCAT('%',#{stockChangeReason.reason},'%')
      </if>
      <if test="stockChangeReason.sysSet != null">
        <if test="stockChangeReason.sysSet == 1">
          AND scr.shop_id = 0
        </if>
        <if test="stockChangeReason.sysSet == 0">
          AND scr.shop_id = #{stockChangeReason.shopId}
        </if>
      </if>
      AND if(scrs.stock_change_reason_status_id is null, scr.status, scrs.status) > -1
    </where>
    ORDER BY scr.stock_change_reason_id DESC
  </select>
    <select id="getByShopId" resultType="com.tmerclub.cloud.product.vo.StockChangeReasonVO">
      SELECT scr.stock_change_reason_id, scr.`type`, scrl.`reason`, scr.shop_id, scr.sys_type,
              scr.status,  scrl.`remark`, IF(scr.shop_id = 0, 1, 0) AS sys_set
      FROM stock_change_reason scr
      JOIN stock_change_reason_lang scrl ON scr.stock_change_reason_id = scrl.stock_change_reason_id AND scrl.lang = 0
      LEFT JOIN stock_change_reason_status scrs ON scr.stock_change_reason_id = scrs.stock_change_reason_id
      AND scrs.shop_id = #{stockChangeReasonDTO.shopId}
      <where>
        <if test="stockChangeReasonDTO.shopId != 0">
          and ((scr.shop_id = #{stockChangeReasonDTO.shopId} and scr.sys_type = #{stockChangeReasonDTO.sysType}) OR scr.shop_id = 0)
        </if>
        <if test="stockChangeReasonDTO.type != null">
          and scr.type = #{stockChangeReasonDTO.type}
        </if>
        <if test="stockChangeReasonDTO.status != null">
          and (
            ( scr.shop_id != 0 and scr.`status` = #{stockChangeReasonDTO.status} )
            or ( scr.shop_id = 0 and if( scrs.`status` is null, scr.`status`, scrs.`status` ) = #{stockChangeReasonDTO.status} )
          )
        </if>
        <if test="stockChangeReasonDTO.status == null">
          and scr.status != -1
        </if>
      </where>
    </select>
  <select id="getByStockChangeReasonId" resultType="com.tmerclub.cloud.product.model.StockChangeReason">
    SELECT <include refid="Vo_Column_List"/> from stock_change_reason where stock_change_reason_id = #{stockChangeReasonId}
  </select>
  <insert id="save" useGeneratedKeys="true" keyProperty="stockChangeReasonId">
    insert into stock_change_reason (`type`,`shop_id`,`status`,`sys_type`)
    values (#{stockChangeReason.type},#{stockChangeReason.shopId},#{stockChangeReason.status},#{stockChangeReason.sysType});
  </insert>
  <update id="update">
    update stock_change_reason
    <set>
      <if test="stockChangeReason.type != null">
        `type` = #{stockChangeReason.type},
      </if>
      <if test="stockChangeReason.shopId != null">
        `shop_id` = #{stockChangeReason.shopId},
      </if>
      <if test="stockChangeReason.status != null">
        `status` = #{stockChangeReason.status},
      </if>
      <if test="stockChangeReason.sysType != null">
        `sys_type` = #{stockChangeReason.sysType},
      </if>
    </set>
    where stock_change_reason_id = #{stockChangeReason.stockChangeReasonId}
  </update>
  <delete id="deleteById">
    delete from stock_change_reason where stock_change_reason_id = #{stockChangeReasonId}
  </delete>

</mapper>
