<?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.delivery.mapper.PrinterMapper">
  <resultMap id="printerMap" type="com.tmerclub.cloud.delivery.model.Printer">
    <id column="printer_id" property="printerId" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="shop_id" property="shopId"/>
    <result column="supplier_id" property="supplierId"/>
    <result column="printer_name" property="printerName"/>
    <result column="siid" property="siid"/>
    <result column="printer_remark" property="printerRemark"/>
    <result column="is_default" property="isDefault"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `printer_id`,`create_time`,`update_time`,`shop_id`,`supplier_id`,`printer_name`,`siid`,`printer_remark`,`is_default`
  </sql>
  <select id="list" resultMap="printerMap">
    select <include refid="Vo_Column_List"/> from printer
    <where>
      <if test="printer.printerName != null and printer.printerName != ''">
        printer_name like concat('%',#{printer.printerName},'%')
      </if>
      <if test="printer.shopId != null">
        AND shop_id = #{printer.shopId}
      </if>
      <if test="printer.supplierId != null">
        AND supplier_id = #{printer.supplierId}
      </if>
    </where>
    order by printer_id desc
  </select>
  <select id="getByPrinterId" resultMap="printerMap">
    select <include refid="Vo_Column_List"/> from printer where printer_id = #{printerId}
  </select>
  <insert id="save">
    insert into printer (`shop_id`,`supplier_id`,`printer_name`,`siid`,`printer_remark`,`is_default`)
    values (#{printer.shopId},#{printer.supplierId},#{printer.printerName},#{printer.siid},#{printer.printerRemark},#{printer.isDefault});
  </insert>
  <update id="update">
    update printer
    <set>
      <if test="printer.shopId != null">
        `shop_id` = #{printer.shopId},
      </if>
      <if test="printer.supplierId != null">
        `supplier_id` = #{printer.supplierId},
      </if>
      <if test="printer.printerName != null">
        `printer_name` = #{printer.printerName},
      </if>
      <if test="printer.siid != null">
        `siid` = #{printer.siid},
      </if>
      <if test="printer.printerRemark != null">
        `printer_remark` = #{printer.printerRemark},
      </if>
      <if test="printer.isDefault != null">
        `is_default` = #{printer.isDefault},
      </if>
    </set>
    where printer_id = #{printer.printerId}
  </update>
  <delete id="deleteById">
    delete from printer where printer_id = #{printerId}
  </delete>
  <update id="cancelDefault">
    UPDATE `printer` SET is_default = 0
    <where>
      <if test="shopId != null">
        shop_id = #{shopId}
      </if>
      <if test="supplierId != null">
        AND supplier_id = #{supplierId}
      </if>
    </where>
  </update>
  <update id="setDefault">
    UPDATE `printer` SET is_default = ABS(is_default - 1) WHERE printer_id = #{printerId}
  </update>
  <select id="listByShopId" resultType="com.tmerclub.cloud.delivery.vo.PrinterVO">
    SELECT printer_id, printer_name, is_default
    FROM `printer`
    <where>
      <if test="sysType == 1 or sysType == 2">
        shop_id = #{shopId}
      </if>
      <if test="sysType == 3">
        AND supplier_id = #{shopId}
      </if>
    </where>
  </select>
  <select id="countByName" resultType="int">
    SELECT COUNT(printer_id) num
    FROM `printer`
    <where>
      printer_name = #{name}
      <if test="shopId != null">
        AND shop_id = #{shopId}
      </if>
      <if test="supplierId != null">
        AND supplier_id = #{supplierId}
      </if>
      <if test="printerId != null">
        AND printer_id != #{printerId}
      </if>
    </where>
  </select>
</mapper>
