<?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.ShopCompanyMapper">
  <resultMap id="shopCompanyMap" type="com.tmerclub.cloud.multishop.model.ShopCompany">
    <id column="shop_company_id" property="shopCompanyId" />
    <result column="shop_id" property="shopId"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="credit_code" property="creditCode"/>
    <result column="firm_name" property="firmName"/>
    <result column="residence" property="residence"/>
    <result column="representative" property="representative"/>
    <result column="legal_ids" property="legalIds"/>
    <result column="legal_phone" property="legalPhone"/>
    <result column="capital" property="capital"/>
    <result column="found_time" property="foundTime"/>
    <result column="start_time" property="startTime"/>
    <result column="end_time" property="endTime"/>
    <result column="business_scope" property="businessScope"/>
    <result column="business_license" property="businessLicense"/>
    <result column="identity_card_front" property="identityCardFront"/>
    <result column="identity_card_later" property="identityCardLater"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `shop_company_id`,`shop_id`,`create_time`,`update_time`,`credit_code`,`firm_name`,`residence`,`representative`,`capital`,`found_time`,`start_time`,
    `end_time`,`business_scope`,`business_license`,`identity_card_front`,`identity_card_later`,`status`,`legal_ids`,`legal_phone`
  </sql>
  <select id="getByShopCompanyId" resultMap="shopCompanyMap">
    select <include refid="Vo_Column_List"/> from shop_company where shop_company_id = #{shopCompanyId}
  </select>
  <select id="getShopCompanyByShopIdAndStatus" resultType="com.tmerclub.cloud.multishop.vo.ShopCompanyVO">
    select <include refid="Vo_Column_List"/> from shop_company where shop_id = #{shopId} and status = #{status} order by create_time desc limit 1
  </select>
  <select id="getShopIdByCreditCode" resultType="java.lang.Long">
      select `shop_id` from shop_company where `credit_code` = #{creditCode} and `status` != -1
  </select>

  <insert id="save" useGeneratedKeys="true" keyProperty="shopCompanyId">
    insert into shop_company (`shop_id`,`credit_code`,`firm_name`,`residence`,`representative`,`capital`,`found_time`,`start_time`,`end_time`,
    `business_scope`,`business_license`,`identity_card_front`,`identity_card_later`, `status`,`legal_ids`,`legal_phone`)
    values (#{shopCompany.shopId},#{shopCompany.creditCode},#{shopCompany.firmName},#{shopCompany.residence},
    #{shopCompany.representative},#{shopCompany.capital},#{shopCompany.foundTime},#{shopCompany.startTime},
    #{shopCompany.endTime},#{shopCompany.businessScope},#{shopCompany.businessLicense},#{shopCompany.identityCardFront},
    #{shopCompany.identityCardLater},#{shopCompany.status},#{shopCompany.legalIds},#{shopCompany.legalPhone});
  </insert>

  <update id="update">
    update shop_company
    <set>
      <if test="shopCompany.shopId != null">
        `shop_id` = #{shopCompany.shopId},
      </if>
      <if test="shopCompany.creditCode != null">
        `credit_code` = #{shopCompany.creditCode},
      </if>
      <if test="shopCompany.firmName != null">
        `firm_name` = #{shopCompany.firmName},
      </if>
      <if test="shopCompany.residence != null">
      `residence` = #{shopCompany.residence},
      </if>
      <if test="shopCompany.representative != null">
      `representative` = #{shopCompany.representative},
      </if>
      <if test="shopCompany.legalIds != null">
        `legal_ids` = #{shopCompany.legalIds},
      </if>
      <if test="shopCompany.legalPhone != null">
        `legal_phone` = #{shopCompany.legalPhone},
      </if>
      <if test="shopCompany.capital != null">
      `capital` = #{shopCompany.capital},
      </if>
      <if test="shopCompany.foundTime != null">
      `found_time` = #{shopCompany.foundTime},
      </if>
      <if test="shopCompany.startTime != null">
        `start_time` = #{shopCompany.startTime},
      </if>
      `end_time` = #{shopCompany.endTime},
      <if test="shopCompany.businessScope != null">
        `business_scope` = #{shopCompany.businessScope},
      </if>
      <if test="shopCompany.businessLicense != null">
        `business_license` = #{shopCompany.businessLicense},
      </if>
      <if test="shopCompany.identityCardFront != null">
        `identity_card_front` = #{shopCompany.identityCardFront},
      </if>
      <if test="shopCompany.identityCardLater != null">
        `identity_card_later` = #{shopCompany.identityCardLater},
      </if>
      <if test="shopCompany.status != null">
        `status` = #{shopCompany.status},
      </if>
    </set>
    where shop_company_id = #{shopCompany.shopCompanyId}
  </update>
  <update id="updateByShopId">
    update shop_company
    <set>
      <if test="shopCompany.shopId != null">
        `shop_id` = #{shopCompany.shopId},
      </if>
      <if test="shopCompany.creditCode != null">
        `credit_code` = #{shopCompany.creditCode},
      </if>
      <if test="shopCompany.firmName != null">
        `firm_name` = #{shopCompany.firmName},
      </if>
      <if test="shopCompany.residence != null">
        `residence` = #{shopCompany.residence},
      </if>
      <if test="shopCompany.representative != null">
        `representative` = #{shopCompany.representative},
      </if>
      <if test="shopCompany.legalIds != null">
        `legal_ids` = #{shopCompany.legalIds},
      </if>
      <if test="shopCompany.legalPhone != null">
        `legal_phone` = #{shopCompany.legalPhone},
      </if>
      <if test="shopCompany.capital != null">
        `capital` = #{shopCompany.capital},
      </if>
      <if test="shopCompany.foundTime != null">
        `found_time` = #{shopCompany.foundTime},
      </if>
      <if test="shopCompany.startTime != null">
        `start_time` = #{shopCompany.startTime},
      </if>
      `end_time` = #{shopCompany.endTime},
      <if test="shopCompany.businessScope != null">
        `business_scope` = #{shopCompany.businessScope},
      </if>
      <if test="shopCompany.businessLicense != null">
        `business_license` = #{shopCompany.businessLicense},
      </if>
      <if test="shopCompany.identityCardFront != null">
        `identity_card_front` = #{shopCompany.identityCardFront},
      </if>
      <if test="shopCompany.identityCardLater != null">
        `identity_card_later` = #{shopCompany.identityCardLater},
      </if>
      <if test="shopCompany.status != null">
        `status` = #{shopCompany.status},
      </if>
    </set>
    where shop_id = #{shopId}
    <if test="temp == 1">
      and status = 1
    </if>
    <if test="temp == 0">
      limit 1
    </if>
  </update>
  <delete id="deleteByShopIdAndStatus">
    delete from shop_company where shop_id = #{shopId} and `status` = #{status}
  </delete>
  <update id="batchUpdateStatus">
    update shop_company set `status` = #{status}
        where shop_id = #{shopId}
    <if test="dbStatus != null">
      and `status` = #{dbStatus}
    </if>
  </update>
  <select id="getLatestRecord" resultType="java.lang.Integer">
    select `status` from shop_company where  shop_id = #{shopId} order by create_time desc limit 1
  </select>

  <update id="updateById">
    update shop_company
    <set>
      `update_time` = NOW(),
      <if test="shopCompany.status != null">
        `status` = #{shopCompany.status},
      </if>
    </set>
    where shop_company_id = #{shopCompany.shopCompanyId}
  </update>
  <update id="updateIdCardById">
    update shop_company
    <set>
      <if test="shopCompany.businessLicense != null">
        `business_license` = #{shopCompany.businessLicense},
      </if>
      <if test="shopCompany.identityCardFront != null">
        `identity_card_front` = #{shopCompany.identityCardFront},
      </if>
      <if test="shopCompany.identityCardLater != null">
        `identity_card_later` = #{shopCompany.identityCardLater},
      </if>
    </set>
    where shop_company_id = #{shopCompany.shopCompanyId}
  </update>

  <update id="updateStatusToWaitAudit">
    update shop_company set `status` = 0 where `status` = 1
  </update>
</mapper>
