<?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.supplier.mapper.SupplierCompanyMapper">
  <resultMap id="supplierCompanyMap" type="com.tmerclub.cloud.supplier.model.SupplierCompany">
    <id column="supplier_company_id" property="supplierCompanyId" />
    <result column="supplier_id" property="supplierId"/>
    <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">
    `supplier_company_id`,`supplier_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`,`legal_ids`,`legal_phone`
  </sql>
  <select id="getBySupplierCompanyId" resultMap="supplierCompanyMap">
    select <include refid="Vo_Column_List"/> from supplier_company where supplier_company_id = #{supplierCompanyId}
  </select>
  <select id="getSupplierCompanyBySupplierIdAndStatus" resultType="com.tmerclub.cloud.supplier.vo.SupplierCompanyVO">
    select <include refid="Vo_Column_List"/> from supplier_company where `supplier_id` = #{supplierId} and `status` = #{status} order by create_time desc limit 1
  </select>
  <select id="getSupplierIdByCreditCode" resultType="java.lang.Long">
      select `supplier_id` from supplier_company where `credit_code` = #{creditCode} and `status` = 1
  </select>
  <insert id="save" useGeneratedKeys="true" keyProperty="supplierCompanyId">
    insert into supplier_company (`supplier_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 (#{supplierCompany.supplierId},#{supplierCompany.creditCode},#{supplierCompany.firmName},#{supplierCompany.residence},
    #{supplierCompany.representative},#{supplierCompany.capital},#{supplierCompany.foundTime},#{supplierCompany.startTime},#{supplierCompany.endTime},
    #{supplierCompany.businessScope},#{supplierCompany.businessLicense},#{supplierCompany.identityCardFront},#{supplierCompany.identityCardLater},
    #{supplierCompany.status},#{supplierCompany.legalIds},#{supplierCompany.legalPhone});
  </insert>
  <update id="update">
    update supplier_company
    <set>
      <if test="supplierCompany.supplierId != null">
        `supplier_id` = #{supplierCompany.supplierId},
      </if>
      <if test="supplierCompany.creditCode != null">
        `credit_code` = #{supplierCompany.creditCode},
      </if>
      <if test="supplierCompany.firmName != null">
        `firm_name` = #{supplierCompany.firmName},
      </if>
      <if test="supplierCompany.residence != null">
        `residence` = #{supplierCompany.residence},
      </if>
      <if test="supplierCompany.representative != null">
        `representative` = #{supplierCompany.representative},
      </if>
      <if test="supplierCompany.legalIds != null">
        `legal_ids` = #{supplierCompany.legalIds},
      </if>
      <if test="supplierCompany.legalPhone != null">
        `legal_phone` = #{supplierCompany.legalPhone},
      </if>
      <if test="supplierCompany.capital != null">
        `capital` = #{supplierCompany.capital},
      </if>
      <if test="supplierCompany.foundTime != null">
        `found_time` = #{supplierCompany.foundTime},
      </if>
      <if test="supplierCompany.startTime != null">
        `start_time` = #{supplierCompany.startTime},
      </if>
      `end_time` = #{supplierCompany.endTime},
      <if test="supplierCompany.businessScope != null">
        `business_scope` = #{supplierCompany.businessScope},
      </if>
      <if test="supplierCompany.businessLicense != null">
        `business_license` = #{supplierCompany.businessLicense},
      </if>
      <if test="supplierCompany.identityCardFront != null">
        `identity_card_front` = #{supplierCompany.identityCardFront},
      </if>
      <if test="supplierCompany.identityCardLater != null">
        `identity_card_later` = #{supplierCompany.identityCardLater},
      </if>
      <if test="supplierCompany.status != null">
        `status` = #{supplierCompany.status},
      </if>
    </set>
    where supplier_company_id = #{supplierCompany.supplierCompanyId}
  </update>
  <update id="updateBySupplierId">
    update supplier_company
    <set>
      <if test="supplierCompany.supplierId != null">
        `supplier_id` = #{supplierCompany.supplierId},
      </if>
      <if test="supplierCompany.creditCode != null">
        `credit_code` = #{supplierCompany.creditCode},
      </if>
      <if test="supplierCompany.firmName != null">
        `firm_name` = #{supplierCompany.firmName},
      </if>
      `residence` = #{supplierCompany.residence},
      `representative` = #{supplierCompany.representative},
      `capital` = #{supplierCompany.capital},
      `found_time` = #{supplierCompany.foundTime},
      <if test="supplierCompany.legalIds != null">
        `legal_ids` = #{supplierCompany.legalIds},
      </if>
      <if test="supplierCompany.legalPhone != null">
        `legal_phone` = #{supplierCompany.legalPhone},
      </if>
      <if test="supplierCompany.startTime != null">
        `start_time` = #{supplierCompany.startTime},
      </if>
      `end_time` = #{supplierCompany.endTime},
      <if test="supplierCompany.businessScope != null">
        `business_scope` = #{supplierCompany.businessScope},
      </if>
      <if test="supplierCompany.businessLicense != null">
        `business_license` = #{supplierCompany.businessLicense},
      </if>
      <if test="supplierCompany.identityCardFront != null">
        `identity_card_front` = #{supplierCompany.identityCardFront},
      </if>
      <if test="supplierCompany.identityCardLater != null">
        `identity_card_later` = #{supplierCompany.identityCardLater},
      </if>
      <if test="supplierCompany.status != null">
        `status` = #{supplierCompany.status},
      </if>
    </set>
    where supplier_id = #{supplierId}
    <if test="temp == 1">
      and status = 1
    </if>
    <if test="temp == 0">
      limit 1
    </if>
  </update>
  <update id="batchUpdateStatus">
    update supplier_company set `status` = #{status}
    where supplier_id = #{supplierId}
    <if test="dbStatus != null">
      and `status` = #{dbStatus}
    </if>
  </update>
  <delete id="deleteBySupplierIdAndStatus">
    delete from supplier_company where supplier_id = #{supplierId} and `status` = #{status}
  </delete>
  <select id="getLatestRecord" resultType="java.lang.Integer">
    select `status` from supplier_company where supplier_id = #{supplierId} order by create_time desc limit 1
  </select>

  <update id="updateById">
    update supplier_company
    <set>
      `update_time` = NOW(),
      <if test="supplierCompany.status != null">
        `status` = #{supplierCompany.status},
      </if>
    </set>
    where supplier_company_id = #{supplierCompany.supplierCompanyId}
  </update>
  <update id="updateIdCardById">
    update supplier_company
    <set>
      <if test="supplierCompany.businessLicense != null">
        `business_license` = #{supplierCompany.businessLicense},
      </if>
      <if test="supplierCompany.identityCardFront != null">
        `identity_card_front` = #{supplierCompany.identityCardFront},
      </if>
      <if test="supplierCompany.identityCardLater != null">
        `identity_card_later` = #{supplierCompany.identityCardLater},
      </if>
    </set>
    where supplier_company_id = #{supplierCompany.supplierCompanyId}
  </update>
  <update id="updateStatusToWaitAudit">
    update supplier_company set `status` = 0 where `status` = 1
  </update>
</mapper>
