<?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.OutletConfigMapper">
  <resultMap id="outletConfigMap" type="com.tmerclub.cloud.delivery.model.OutletConfig">
    <id column="outlet_config_id" property="outletConfigId" />
    <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="delivery_company_type" property="deliveryCompanyType"/>
    <result column="shipper" property="shipper"/>
    <result column="mobile" property="mobile"/>
    <result column="partner_id" property="partnerId"/>
    <result column="partner_key" property="partnerKey"/>
    <result column="net" property="net"/>
    <result column="paper_size" property="paperSize"/>
    <result column="is_default" property="isDefault"/>
    <result column="print_addr" property="printAddr"/>
  </resultMap>
  <resultMap id="outletConfigInfoMap" type="com.tmerclub.cloud.delivery.vo.OutletConfigInfoVO">
    <result column="delivery_company_type" property="deliveryCompanyType"/>
    <collection property="shopAddrList" ofType="com.tmerclub.cloud.delivery.vo.OutletConfigAddrVO">
      <result column="print_addr" property="printAddr"/>
      <result column="outlet_config_id" property="outletConfigId"/>
      <result column="is_default" property="isDefault"/>
    </collection>
  </resultMap>
  <sql id="Vo_Column_List">
    `outlet_config_id`,`create_time`,`update_time`,`shop_id`,`supplier_id`,`delivery_company_type`,
    `shipper`,`mobile`,`partner_id`,`partner_key`,`net`,`paper_size`,`is_default`, `print_addr`
  </sql>
  <select id="list" resultType="com.tmerclub.cloud.delivery.vo.OutletConfigVO">
    select <include refid="Vo_Column_List"/> from outlet_config
    <where>
      <if test="outletConfig.deliveryCompanyType != null">
        delivery_company_type = #{outletConfig.deliveryCompanyType}
      </if>
      <if test="outletConfig.shopId != null">
        AND shop_id = #{outletConfig.shopId}
      </if>
      <if test="outletConfig.supplierId != null">
        AND supplier_id = #{outletConfig.supplierId}
      </if>
    </where>
    order by outlet_config_id desc
  </select>
  <select id="getByOutletConfigId" resultMap="outletConfigMap">
    select <include refid="Vo_Column_List"/>
    from outlet_config where outlet_config_id = #{outletConfigId}
  </select>
  <insert id="save">
    insert into outlet_config (`shop_id`,`supplier_id`,`delivery_company_type`,`shipper`,`mobile`,`partner_id`,`partner_key`,
    `net`,`paper_size`,`is_default`,`print_addr`)
    values (#{outletConfig.shopId},#{outletConfig.supplierId},#{outletConfig.deliveryCompanyType},#{outletConfig.shipper},
    #{outletConfig.mobile},#{outletConfig.partnerId},#{outletConfig.partnerKey},#{outletConfig.net},#{outletConfig.paperSize},
    #{outletConfig.isDefault},#{outletConfig.printAddr});
  </insert>
  <update id="update">
    update outlet_config
    <set>
      <if test="outletConfig.shopId != null">
        `shop_id` = #{outletConfig.shopId},
      </if>
      <if test="outletConfig.supplierId != null">
        `supplier_id` = #{outletConfig.supplierId},
      </if>
      <if test="outletConfig.deliveryCompanyType != null">
        `delivery_company_type` = #{outletConfig.deliveryCompanyType},
      </if>
      <if test="outletConfig.shipper != null">
        `shipper` = #{outletConfig.shipper},
      </if>
      <if test="outletConfig.mobile != null">
        `mobile` = #{outletConfig.mobile},
      </if>
      <if test="outletConfig.partnerId != null">
        `partner_id` = #{outletConfig.partnerId},
      </if>
      <if test="outletConfig.partnerKey != null">
        `partner_key` = #{outletConfig.partnerKey},
      </if>
      <if test="outletConfig.net != null">
        `net` = #{outletConfig.net},
      </if>
      <if test="outletConfig.paperSize != null">
        `paper_size` = #{outletConfig.paperSize},
      </if>
      <if test="outletConfig.isDefault != null">
        `is_default` = #{outletConfig.isDefault},
      </if>
    </set>
      where outlet_config_id = #{outletConfig.outletConfigId}
  </update>
  <delete id="deleteById">
    delete from outlet_config where outlet_config_id = #{outletConfigId}
  </delete>
  <select id="checkIsExist" resultType="int">
    SELECT COUNT(outlet_config_id) num
    FROM `outlet_config`
    WHERE
      delivery_company_type = #{outletConfig.deliveryCompanyType}
      AND print_addr = #{outletConfig.printAddr}
      <if test="outletConfig.outletConfigId != null">
        AND outlet_config_id != #{outletConfig.outletConfigId}
      </if>
      <if test="outletConfig.shopId != null">
        AND shop_id = #{outletConfig.shopId}
      </if>
      <if test="outletConfig.supplierId != null">
        AND supplier_id = #{outletConfig.supplierId}
      </if>
  </select>
  <update id="cancelDefault">
    UPDATE `outlet_config` 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 `outlet_config` SET is_default = ABS(is_default - 1) WHERE outlet_config_id = #{outletConfigId}
  </update>
  <select id="listByShopId" resultMap="outletConfigInfoMap">
    SELECT delivery_company_type, is_default, `print_addr`, outlet_config_id
    FROM `outlet_config`
    <where>
      <if test="sysType == 1 or sysType == 2">
        shop_id = #{shopId}
      </if>
      <if test="sysType == 3">
        AND supplier_id = #{shopId}
      </if>
    </where>
  </select>
</mapper>
