<?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.multishop.mapper.ShopDetailMapper">
  <resultMap id="shopDetailMap" type="com.tmerclub.cloud.multishop.model.ShopDetail">
    <id column="shop_id" property="shopId" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="type" property="type"/>
    <result column="shop_name" property="shopName"/>
    <result column="intro" property="intro"/>
    <result column="notice_mobile" property="noticeMobile"/>
    <result column="shop_logo" property="shopLogo"/>
    <result column="shop_status" property="shopStatus"/>
    <result column="merchant_name" property="merchantName"/>
    <result column="contact_name" property="contactName"/>
    <result column="contact_phone" property="contactPhone"/>
    <result column="contract_start_time" property="contractStartTime"/>
    <result column="contract_end_time" property="contractEndTime"/>
    <result column="is_create_member" property="isCreateMember"/>
    <result column="is_bind_phone" property="isBindPhone"/>
    <result column="company_info_process_status" property="companyInfoProcessStatus"/>
    <result column="id_card_collect_process_status" property="idCardCollectProcessStatus"/>
    <result column="allinpay_shop_status" property="allinpayShopStatus"/>
    <result column="company_acct_protocol_no" property="companyAcctProtocolNo"/>
    <result column="legal_acct_protocol_no" property="legalAcctProtocolNo"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `shop_id`,`create_time`,`update_time`,`type`,`shop_name`,`intro`,`notice_mobile`,`shop_logo`,`shop_status`,
    `is_preferred`,`merchant_name`,`contact_name`,`contact_phone`,`contract_start_time`,`contract_end_time`,
    `is_create_member`,`is_bind_phone`,`company_info_process_status`,`id_card_collect_process_status`,`allinpay_shop_status`,
    `company_acct_protocol_no`,`legal_acct_protocol_no`
  </sql>
  <select id="list" resultType="com.tmerclub.cloud.api.multishop.vo.ShopDetailVO">
    select <include refid="Vo_Column_List"/>
    from shop_detail
    where shop_status != -1
        <if test="shopDetail.paySysType == null or shopDetail.paySysType != 1">
          and shop_status != 3 and shop_status != 4
        </if>
        <if test="shopDetail.shopName != null and shopDetail.shopName != ''">
            and shop_name like concat('%', #{shopDetail.shopName}, '%')
        </if>
        <if test="shopDetail.shopStatus != null">
          <if test="shopDetail.shopStatus == 5 and shopDetail.paySysType == 1">
            and allinpay_shop_status = 3 or shop_status = 5
          </if>
          <if test="shopDetail.shopStatus == 2 and shopDetail.paySysType == 1">
            and allinpay_shop_status = 2 or shop_status = 2
          </if>
          <if test="shopDetail.shopStatus == 3 and shopDetail.paySysType == 1">
            and allinpay_shop_status = 4 or shop_status = 3
          </if>
          <if test="shopDetail.shopStatus == 6 and shopDetail.paySysType == 1">
            and allinpay_shop_status = 2 and company_info_process_status = 3
          </if>
          <if test="shopDetail.shopStatus != 5 and shopDetail.shopStatus != 2 and shopDetail.shopStatus != 3  and shopDetail.shopStatus != 6 and shopDetail.paySysType == 1">
            and shop_status = #{shopDetail.shopStatus}
          </if>
          <if test="shopDetail.paySysType == 0">
            and shop_status = #{shopDetail.shopStatus}
          </if>
        </if>
        <if test="shopDetail.type != null">
            and type = #{shopDetail.type}
        </if>
        <if test="shopDetail.isPreferred != null">
            and is_preferred = #{shopDetail.isPreferred}
        </if>
    order by update_time desc
  </select>
  <select id="getByShopId" resultType="com.tmerclub.cloud.api.multishop.vo.ShopDetailVO">
    select `shop_id`,`type`,`shop_name`,`intro`,`notice_mobile`,`shop_logo`,`shop_status`,`is_preferred`,
           mobile_background_pic,pc_background_pic,`contact_name`,`merchant_name`,
           `contact_name`,`contact_phone`,`contract_start_time`,`contract_end_time`,
            `is_create_member`,`is_bind_phone`,`company_info_process_status`,`id_card_collect_process_status`,`allinpay_shop_status`,
        `company_acct_protocol_no`,`legal_acct_protocol_no`
    from shop_detail where shop_id = #{shopId} and shop_status != -1 limit 1
  </select>
  <select id="getShopExtensionsByShopId" resultType="com.tmerclub.cloud.api.multishop.vo.ShopDetailVO">
    select s.`shop_id`,s.intro,s.`type`,s.`shop_name`,s.`intro`,s.`notice_mobile`,s.`shop_logo`,s.`shop_status`,
           s.is_preferred,s.type,se.collection_num,s.mobile_background_pic,s.pc_background_pic,s.contract_start_time,s.contract_end_time
    from shop_detail s
    left join shop_extension se on se.shop_id = s.shop_id
    where s.shop_id = #{shopId}
  </select>

  <select id="getShopByShopId" resultType="com.tmerclub.cloud.api.multishop.vo.ShopDetailVO">
    select <include refid="Vo_Column_List"/> from shop_detail sd
    where sd.shop_id = #{shopId}
  </select>
  <insert id="save" useGeneratedKeys="true" keyProperty="shopId">
    insert into shop_detail (`shop_name`,`intro`,`notice_mobile`,`shop_logo`,`mobile_background_pic`,`pc_background_pic`,`shop_status`,`type`,`is_preferred`,`merchant_name`,`contact_name`,`contact_phone`,`contract_start_time`,`contract_end_time`)
    values (#{shopDetail.shopName},#{shopDetail.intro},#{shopDetail.noticeMobile},#{shopDetail.shopLogo},#{shopDetail.mobileBackgroundPic},#{shopDetail.pcBackgroundPic},#{shopDetail.shopStatus},#{shopDetail.type},#{shopDetail.isPreferred},#{shopDetail.merchantName},#{shopDetail.contactName},#{shopDetail.contactPhone},#{shopDetail.contractStartTime},#{shopDetail.contractEndTime});
  </insert>
  <update id="update">
    update shop_detail
    <set>
      <if test="shopDetail.shopName != null">
        `shop_name` = #{shopDetail.shopName},
      </if>
      <if test="shopDetail.intro != null">
        `intro` = #{shopDetail.intro},
      </if>
      <if test="shopDetail.noticeMobile != null">
        `notice_mobile` = #{shopDetail.noticeMobile},
      </if>
      <if test="shopDetail.shopLogo != null">
        `shop_logo` = #{shopDetail.shopLogo},
      </if>
      <if test="shopDetail.mobileBackgroundPic != null">
        `mobile_background_pic` = #{shopDetail.mobileBackgroundPic},
      </if>
      <if test="shopDetail.pcBackgroundPic != null">
        `pc_background_pic` = #{shopDetail.pcBackgroundPic},
      </if>
      <if test="shopDetail.shopStatus != null">
        `shop_status` = #{shopDetail.shopStatus},
      </if>
      <if test="shopDetail.type != null">
        `type` = #{shopDetail.type},
      </if>
      <if test="shopDetail.isPreferred != null">
        `is_preferred` = #{shopDetail.isPreferred},
      </if>
      <if test="shopDetail.merchantName != null">
        `merchant_name` = #{shopDetail.merchantName},
      </if>
      <if test="shopDetail.contactName != null">
        `contact_name` = #{shopDetail.contactName},
      </if>
      <if test="shopDetail.contactPhone != null">
        `contact_phone` = #{shopDetail.contactPhone},
      </if>
      <if test="shopDetail.contractStartTime != null">
        `contract_start_time` = #{shopDetail.contractStartTime},
      </if>
      `contract_end_time` = #{shopDetail.contractEndTime},
    </set>
    where shop_id = #{shopDetail.shopId} limit 1
  </update>
  <delete id="deleteById">
    delete from shop_detail where shop_id = #{shopId}
  </delete>

  <select id="listByShopIds" resultMap="shopDetailMap">
    select `shop_id`,`shop_name`,`shop_logo`,`type`,contact_phone, contact_name from shop_detail where shop_id in
    <foreach collection="shopIds" item="shopId" open="(" close=")" separator=",">
        #{shopId}
    </foreach>
  </select>
  <select id="shopSearchList" resultType="com.tmerclub.cloud.multishop.vo.ShopDetailAppVO">
    select sd.shop_id,sd.type,sd.shop_name,sd.intro,sd.shop_logo,sd.mobile_background_pic,sd.pc_background_pic,se.collection_num from shop_detail sd
    LEFT JOIN shop_extension se ON sd.shop_id = se.shop_id
    where sd.shop_status = 1
    <if test="shopDetail.paySysType == 1">
      and sd.allinpay_shop_status = 1
    </if>
    <if test="shopDetail.shopName != null and shopDetail.shopName != ''">
        and sd.shop_name like concat('%', #{shopDetail.shopName}, '%')
    </if>
    <if test="shopDetail.isPreferred != null ">
        and sd.is_preferred = #{shopDetail.isPreferred}
    </if>
    order by sd.type,sd.is_preferred desc
  </select>

  <select id="countShopName" resultType="int">
    select ifnull(count(*),0) from shop_detail where shop_name = #{shopName} and shop_status != -1
    <if test="shopId != null">
        and shop_id != #{shopId}
    </if>
  </select>
  <update id="changeShopStatus">
    update shop_detail set `shop_status` = #{shopStatus} where shop_id = #{shopId}
  </update>
  <update id="changeShopStatusByContractTime">
    update shop_detail
    <if test="now != null">
      set `shop_status` =
      if(#{now} &gt;= contract_start_time and (#{now} &lt; contract_end_time or contract_end_time is null), 1, 0)
    </if>
    where `shop_status` = 1 or `shop_status` = 0
  </update>

  <select id="shopExtensionData" resultType="com.tmerclub.cloud.api.multishop.bo.EsShopDetailBO">
    SELECT ad.shop_id,ad.shop_name,ad.type,ad.shop_logo,se.sale_num,se.collection_num FROM shop_detail ad
    JOIN shop_extension se ON ad.shop_id = se.shop_id
    WHERE ad.shop_id = #{shopId}
  </select>

  <select id="getShopDetailByShopIdsShopName" resultType="com.tmerclub.cloud.api.multishop.vo.ShopDetailVO">
    SELECT ad.shop_id,ad.shop_name,ad.merchant_name,ad.type,ad.shop_logo,ad.shop_status FROM shop_detail ad
    <where>
    <if test="shopIds != null and shopIds.size() > 0">
      and ad.shop_id in
      <foreach collection="shopIds" item="shopId" open="(" separator="," close=")">
        #{shopId}
      </foreach>
    </if>
    <if test="shopName != null and shopName != ''">
      and ad.shop_name like concat('%',#{shopName},'%')
    </if>
    </where>
  </select>

  <select id="countByMobile" resultType="int">
    SELECT ifnull(count(*),0) FROM shop_detail WHERE shop_status = 1 AND notice_mobile = #{mobile}
  </select>
  <select id="countByShopStatus" resultType="java.lang.Long">
    SELECT ifnull(count(*), 0) FROM shop_detail WHERE shop_status = #{shopStatus}
  </select>
  <select id="listShopIdsOfStatusChangeToStopByContractTime" resultType="java.lang.Long">
    SELECT shop_id FROM shop_detail
    WHERE (#{now} &lt; contract_start_time or #{now} &gt;= contract_end_time) and shop_status = 1
  </select>
  <select id="listShopIdsOfStatusChangeToOpenByContractTime" resultType="java.lang.Long">
    SELECT shop_id FROM shop_detail
    WHERE #{now} &gt;= contract_start_time and (#{now} &lt; contract_end_time or contract_end_time is null) and shop_status = 0
  </select>
  <select id="listSimple" resultType="com.tmerclub.cloud.api.multishop.bo.ShopSimpleBO">
    SELECT shop_id, shop_name, shop_status FROM shop_detail
    <where>
      <if test="shopSimpleBO.shopName != null and shopSimpleBO.shopName != ''">
        AND shop_name like concat('%', #{shopSimpleBO.shopName}, '%')
      </if>
      <if test="shopSimpleBO.shopStatus != null">
        AND shop_status = #{shopSimpleBO.shopStatus}
      </if>
      <if test="shopSimpleBO.type != null">
        AND `type` = #{shopSimpleBO.type}
      </if>
      <if test="shopSimpleBO.shopIds != null">
        AND shop_id in
        <foreach collection="shopSimpleBO.shopIds" item="shopId" open="(" close=")" separator=",">
          #{shopId}
        </foreach>
      </if>
    </where>
    <if test="shopSimpleBO.seq == 1">
      ORDER BY shop_id ASC
    </if>
    <if test="shopSimpleBO.seq == 2">
      ORDER BY shop_id DESC
    </if>
  </select>
  <select id="getShopIdsByShopStatus" resultType="java.lang.Long">
    SELECT `shop_id` FROM `shop_detail` WHERE `shop_status` = #{shopStatus}
  </select>
  <select id="selectShopIdsLikeName" resultType="java.lang.Long">
    SELECT `shop_id` FROM `shop_detail` WHERE `shop_name` LIKE CONCAT('%', #{name}, '%')
  </select>
  <select id="searchShops" resultType="com.tmerclub.cloud.multishop.vo.ShopHeadInfoVO">
    SELECT sd.shop_id, sd.shop_name,sd.shop_logo,sd.shop_status,sd.type,sd.is_preferred,se.collection_num
    FROM shop_detail sd
    left join shop_extension se on sd.shop_id = se.shop_id
    <where>
      <if test="shopSearchDTO.shopIds == null or shopSearchDTO.shopIds.size() == 0">
        sd.shop_status BETWEEN 0 AND 1
        <if test="shopSearchDTO.paySysType == 1">
          and sd.allinpay_shop_status = 1
        </if>
        <if test="shopSearchDTO.shopName != null and shopSearchDTO.shopName != ''">
          AND sd.shop_name LIKE CONCAT('%', #{shopSearchDTO.shopName}, '%')
        </if>
      </if>
      <if test="shopSearchDTO.shopIds != null and shopSearchDTO.shopIds.size() > 0">
        AND sd.shop_id IN
        <foreach collection="shopSearchDTO.shopIds" item="shopId" open="(" close=")" separator=",">
          #{shopId}
        </foreach>
      </if>
    </where>
    GROUP BY sd.`shop_id`
  </select>
  <select id="apiSearchShops" resultType="com.tmerclub.cloud.multishop.vo.ShopHeadInfoVO">
    SELECT
    sd.`shop_id`, sd.`shop_name`, sd.`shop_logo`, sd.shop_status, sd.`type`, sd.is_preferred,se.collection_num,
    ifnull(sr.renovation_id, 0) as renovation_id
    FROM shop_detail sd
    LEFT JOIN `shop_renovation` sr ON sr.`shop_id` = sd.`shop_id` and sr.home_status = 1
    left join shop_extension se on sd.shop_id = se.shop_id
    <where>
      sd.shop_status = 1
      <if test="shopSearchDTO.paySysType == 1">
        and sd.allinpay_shop_status = 1
      </if>
      <if test="shopSearchDTO.isPreferred != null ">
        and sd.is_preferred = #{shopSearchDTO.isPreferred}
      </if>
      <if test="shopSearchDTO.shopName != null and shopSearchDTO.shopName != ''">
        and sd.`shop_name` like concat('%', #{shopSearchDTO.shopName}, '%')
      </if>
    </where>
    GROUP BY sd.`shop_id`
    order by se.collection_num desc
  </select>
  <select id="listRenovationShop" resultType="com.tmerclub.cloud.multishop.vo.ShopHeadInfoVO">
    SELECT sd.shop_id, sd.shop_name,sd.shop_logo,sd.shop_status,sd.type,sd.is_preferred,se.collection_num
    FROM shop_detail sd
    left join shop_extension se on sd.shop_id = se.shop_id
    WHERE sd.shop_id IN
    <foreach collection="shopSearchDTO.shopIds" item="shopId" open="(" close=")" separator=",">
      #{shopId}
    </foreach>
    GROUP BY sd.`shop_id`
  </select>
  <select id="listUnCreateMemberShopIds" resultType="java.lang.Long">
    SELECT shop_id FROM shop_detail WHERE is_create_member = 0 and shop_status != -1
  </select>
  <update id="updateShopToStop">
    UPDATE shop_detail set allinpay_shop_status = 2
    where shop_status in (0,1,2,5) and shop_id in
    <foreach collection="shopIds" item="shopId" open="(" close=")" separator=",">
      #{shopId}
    </foreach>
  </update>
  <update id="updateShopToApplying">
    UPDATE shop_detail set allinpay_shop_status = 0, shop_status = 4
    where shop_status = 3 and shop_id in
    <foreach collection="shopIds" item="shopId" open="(" close=")" separator=",">
      #{shopId}
    </foreach>
  </update>
  <update id="updateShopIsCreate">
    UPDATE shop_detail set is_create_member = 1
    where shop_id in
    <foreach collection="shopIds" item="shopId" open="(" close=")" separator=",">
      #{shopId}
    </foreach>
  </update>
  <update id="updateAllinpayShopStatus">
    update shop_detail set allinpay_shop_status = #{status}
    where shop_id = #{shopId} and is_create_member = 1
  </update>
  <update id="updateShopAuditSuccess">
    update shop_detail set company_info_process_status = 2
    where shop_id = #{shopId} and is_create_member = 1
  </update>
  <update id="updateShopAuditFail">
    update shop_detail set company_info_process_status = 3, allinpay_shop_status = 2
    where shop_id = #{shopId} and is_create_member = 1
  </update>
  <update id="updateAllinpayIdCardStatus">
    update shop_detail set id_card_collect_process_status = #{status}
    where shop_id = #{shopId}
  </update>
  <update id="updateShopToOpen">
    update shop_detail set allinpay_shop_status = 1
    where shop_id = #{shopId}
    and is_create_member = 1 and company_info_process_status = 2 and id_card_collect_process_status = 4
  </update>
  <update id="updateShopToWaitAudit">
    update shop_detail set allinpay_shop_status = 3 where shop_id = #{shopId} and is_create_member = 1 and company_info_process_status = 2
  </update>
  <update id="updateShopBindPhone">
    update shop_detail set is_bind_phone = #{status}
    where shop_id = #{shopId}
  </update>
  <update id="updateAcctProtocolNoByType">
    update shop_detail
    <set>
      <if test="type == 2">
        legal_acct_protocol_no = #{acctProtocolNo},
      </if>
      <if test="type == 3">
        company_acct_protocol_no = #{acctProtocolNo}
      </if>
    </set>
    where shop_id = #{shopId}
  </update>

  <select id="listShopIds" resultType="com.tmerclub.cloud.api.multishop.vo.ShopDetailVO">
    SELECT shop_id, contact_name, contact_phone  FROM shop_detail WHERE shop_status != -1
  </select>
</mapper>
