<?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.cool.store.mapper.OpenNewShopRecordMapper">
  <resultMap id="BaseResultMap" type="com.cool.store.entity.OpenNewShopRecordDO">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="line_id" jdbcType="BIGINT" property="lineId" />
    <result column="new_want_shop_num" jdbcType="INTEGER" property="newWantShopNum" />
    <result column="fund" jdbcType="INTEGER" property="fund" />
    <result column="audit_result" jdbcType="TINYINT" property="auditResult" />
    <result column="deleted" jdbcType="BIT" property="deleted" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
  </resultMap>
  
  <select id="getPartnerOpenNewShopPage" resultType="com.cool.store.dto.partner.PartnerOpenNewShopPageDTO">
    select
      a.id as recordId,
      b.id as lineId,
      b.partner_num as partnerNum,
      b.username,
      b.mobile,
      b.want_shop_area_id as wantShopAreaId,
      b.user_portrait as userPortrait,
      b.want_shop_num as wantShopNum,
      b.development_manager as developmentManager,
      b.line_source as lineSource,
      a.audit_result as auditResult
    from xfsg_open_new_shop_record a inner join xfsg_line_info b on a.line_id = b.id 
    where
      a.deleted = 0 and b.deleted = 0 and b.investment_manager = #{request.investmentManager}
    <if test="request.keyword != null and request.keyword != ''">
      and (b.username like concat('%',#{request.keyword},'%') or b.mobile like concat('%',#{request.keyword},'%'))
    </if>
    <if test="request.areaPath != null and request.areaPath != '' ">
      and b.want_shop_area_id in (select id from xfsg_open_area_info where area_path like concat(#{request.areaPath}, '%'))
    </if>
    <if test="request.auditResult != null">
      and a.audit_result = #{request.auditResult}
    </if>
  </select>
  
  <select id="getWaitAuditOpenNewShopRecord" resultMap="BaseResultMap">
    select
      id,
      line_id,
      new_want_shop_num,
      fund,
      audit_result,
      deleted,
      create_time,
      update_time
    from xfsg_open_new_shop_record
    where
      deleted = 0 and audit_result = 0 and line_id = #{lineId} limit 1
  </select>

  <select id="getOpenNewShopRecordList" resultMap="BaseResultMap">
    select id, audit_result from xfsg_open_new_shop_record where line_id = #{lineId} and deleted = 0
  </select>

  <update id="deleteOpenNewShopRecord">
    update xfsg_open_new_shop_record set deleted = 1, update_time = now() where id in
    <foreach collection="deleteIds" item="id" open="(" separator="," close=")">
      #{id}
    </foreach>
  </update>
 </mapper>