<?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="cn.superdesk.uniorder.dao.mapper.operationCenter.UnifiedCenterCustomerMapper">
  <resultMap id="BaseResultMap" type="cn.superdesk.uniorder.dao.entity.operationCenter.UnifiedCenterCustomer">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="type" jdbcType="TINYINT" property="type" />
    <result column="customer_sn" jdbcType="VARCHAR" property="customerSn" />
    <result column="customer_name" jdbcType="VARCHAR" property="customerName" />
    <result column="company_name" jdbcType="VARCHAR" property="companyName" />
    <result column="social_credit_code" jdbcType="VARCHAR" property="socialCreditCode" />
    <result column="address" jdbcType="VARCHAR" property="address" />
    <result column="contact" jdbcType="VARCHAR" property="contact" />
    <result column="mobile" jdbcType="VARCHAR" property="mobile" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <result column="business_id" jdbcType="BIGINT" property="businessId" />
    <result column="business_name" jdbcType="VARCHAR" property="businessName" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
    <result column="status" jdbcType="TINYINT" property="status" />
    <result column="is_data_permission" jdbcType="TINYINT" property="isDataPermission" />
    <result column="is_settlement_method" jdbcType="TINYINT" property="isSettlementMethod" />
    <result column="is_bill_start" jdbcType="TINYINT" property="isBillStart" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="create_user_id" jdbcType="BIGINT" property="createUserId" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="update_user_id" jdbcType="BIGINT" property="updateUserId" />
    <result column="deleted" jdbcType="TINYINT" property="deleted" />
  </resultMap>

  <sql id="Base_Column_List">
    id, type, customer_sn, customer_name, company_name, social_credit_code, address, 
    contact, mobile, email, business_id, business_name, remark, status, is_data_permission,
    is_settlement_method, is_bill_start, create_time, create_user_id, update_time, update_user_id,
    deleted
  </sql>

  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from unified_center_customer
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    update  unified_center_customer set deleted = 1
    where id = #{id,jdbcType=BIGINT}
  </delete>

  <insert id="insert" parameterType="cn.superdesk.uniorder.dao.entity.operationCenter.UnifiedCenterCustomer">
    insert into unified_center_customer (id, type, customer_sn, 
      customer_name, company_name, social_credit_code, 
      address, contact, mobile, 
      email, business_id, business_name, 
      remark, status, is_data_permission,
      is_settlement_method, is_bill_start, create_time,
      create_user_id, update_time, update_user_id, 
      deleted)
    values (#{id,jdbcType=BIGINT}, #{type,jdbcType=TINYINT}, #{customerSn,jdbcType=VARCHAR}, 
      #{customerName,jdbcType=VARCHAR}, #{companyName,jdbcType=VARCHAR}, #{socialCreditCode,jdbcType=VARCHAR}, 
      #{address,jdbcType=VARCHAR}, #{contact,jdbcType=VARCHAR}, #{mobile,jdbcType=VARCHAR}, 
      #{email,jdbcType=VARCHAR}, #{businessId,jdbcType=BIGINT}, #{businessName,jdbcType=VARCHAR}, 
      #{remark,jdbcType=VARCHAR}, #{status,jdbcType=TINYINT}, #{isDataPermission,jdbcType=TINYINT},
      #{isSettlementMethod,jdbcType=TINYINT}, #{isBillStart,jdbcType=TINYINT}, #{createTime,jdbcType=TIMESTAMP},
      #{createUserId,jdbcType=BIGINT}, #{updateTime,jdbcType=TIMESTAMP}, #{updateUserId,jdbcType=BIGINT}, 
      #{deleted,jdbcType=TINYINT})
  </insert>
  <insert id="insertSelective" parameterType="cn.superdesk.uniorder.dao.entity.operationCenter.UnifiedCenterCustomer">
    insert into unified_center_customer
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="type != null">
        type,
      </if>
      <if test="customerSn != null">
        customer_sn,
      </if>
      <if test="customerName != null">
        customer_name,
      </if>
      <if test="companyName != null">
        company_name,
      </if>
      <if test="socialCreditCode != null">
        social_credit_code,
      </if>
      <if test="address != null">
        address,
      </if>
      <if test="contact != null">
        contact,
      </if>
      <if test="mobile != null">
        mobile,
      </if>
      <if test="email != null">
        email,
      </if>
      <if test="businessId != null">
        business_id,
      </if>
      <if test="businessName != null">
        business_name,
      </if>
      <if test="remark != null">
        remark,
      </if>
      <if test="status != null">
        status,
      </if>
      <if test="isDataPermission != null">
        is_data_permission,
      </if>
      <if test="isSettlementMethod != null">
        is_settlement_method,
      </if>
      <if test="isBillStart != null">
        is_bill_start,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="createUserId != null">
        create_user_id,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="updateUserId != null">
        update_user_id,
      </if>
      <if test="deleted != null">
        deleted,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=BIGINT},
      </if>
      <if test="type != null">
        #{type,jdbcType=TINYINT},
      </if>
      <if test="customerSn != null">
        #{customerSn,jdbcType=VARCHAR},
      </if>
      <if test="customerName != null">
        #{customerName,jdbcType=VARCHAR},
      </if>
      <if test="companyName != null">
        #{companyName,jdbcType=VARCHAR},
      </if>
      <if test="socialCreditCode != null">
        #{socialCreditCode,jdbcType=VARCHAR},
      </if>
      <if test="address != null">
        #{address,jdbcType=VARCHAR},
      </if>
      <if test="contact != null">
        #{contact,jdbcType=VARCHAR},
      </if>
      <if test="mobile != null">
        #{mobile,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="businessId != null">
        #{businessId,jdbcType=BIGINT},
      </if>
      <if test="businessName != null">
        #{businessName,jdbcType=VARCHAR},
      </if>
      <if test="remark != null">
        #{remark,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=TINYINT},
      </if>
      <if test="isDataPermission != null">
        #{isDataPermission,jdbcType=TINYINT},
      </if>
      <if test="isSettlementMethod != null">
        #{isSettlementMethod,jdbcType=TINYINT},
      </if>
      <if test="isBillStart != null">
        #{isBillStart,jdbcType=TINYINT},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="createUserId != null">
        #{createUserId,jdbcType=BIGINT},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUserId != null">
        #{updateUserId,jdbcType=BIGINT},
      </if>
      <if test="deleted != null">
        #{deleted,jdbcType=TINYINT},
      </if>
    </trim>
  </insert>


  <update id="updateByPrimaryKeySelective" parameterType="cn.superdesk.uniorder.dao.entity.operationCenter.UnifiedCenterCustomer">
    update unified_center_customer
    <set>
      <if test="type != null">
        type = #{type,jdbcType=TINYINT},
      </if>
      <if test="customerSn != null">
        customer_sn = #{customerSn,jdbcType=VARCHAR},
      </if>
      <if test="customerName != null">
        customer_name = #{customerName,jdbcType=VARCHAR},
      </if>
      <if test="companyName != null">
        company_name = #{companyName,jdbcType=VARCHAR},
      </if>
      <if test="socialCreditCode != null">
        social_credit_code = #{socialCreditCode,jdbcType=VARCHAR},
      </if>
      <if test="address != null">
        address = #{address,jdbcType=VARCHAR},
      </if>
      <if test="contact != null">
        contact = #{contact,jdbcType=VARCHAR},
      </if>
      <if test="mobile != null">
        mobile = #{mobile,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="businessId != null">
        business_id = #{businessId,jdbcType=BIGINT},
      </if>
      <if test="businessName != null">
        business_name = #{businessName,jdbcType=VARCHAR},
      </if>
      <if test="remark != null">
        remark = #{remark,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        status = #{status,jdbcType=TINYINT},
      </if>
      <if test="isDataPermission != null">
        is_data_permission = #{isDataPermission,jdbcType=TINYINT},
      </if>
      <if test="isSettlementMethod != null">
        is_settlement_method = #{isSettlementMethod,jdbcType=TINYINT},
      </if>
      <if test="isBillStart != null">
        is_bill_start = #{isBillStart,jdbcType=TINYINT},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="createUserId != null">
        create_user_id = #{createUserId,jdbcType=BIGINT},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUserId != null">
        update_user_id = #{updateUserId,jdbcType=BIGINT},
      </if>
      <if test="deleted != null">
        deleted = #{deleted,jdbcType=TINYINT},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="cn.superdesk.uniorder.dao.entity.operationCenter.UnifiedCenterCustomer">
    update unified_center_customer
    set type = #{type,jdbcType=TINYINT},
      customer_sn = #{customerSn,jdbcType=VARCHAR},
      customer_name = #{customerName,jdbcType=VARCHAR},
      company_name = #{companyName,jdbcType=VARCHAR},
      social_credit_code = #{socialCreditCode,jdbcType=VARCHAR},
      address = #{address,jdbcType=VARCHAR},
      contact = #{contact,jdbcType=VARCHAR},
      mobile = #{mobile,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      business_id = #{businessId,jdbcType=BIGINT},
      business_name = #{businessName,jdbcType=VARCHAR},
      remark = #{remark,jdbcType=VARCHAR},
      status = #{status,jdbcType=TINYINT},
      is_data_permission = #{isDataPermission,jdbcType=TINYINT},
      is_settlement_method = #{isSettlementMethod,jdbcType=TINYINT},
      is_bill_start = #{isBillStart,jdbcType=TINYINT},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      create_user_id = #{createUserId,jdbcType=BIGINT},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      update_user_id = #{updateUserId,jdbcType=BIGINT},
      deleted = #{deleted,jdbcType=TINYINT}
    where id = #{id,jdbcType=BIGINT}
  </update>


  <select id="getOperationCenterCustomerList"
          parameterType="cn.superdesk.uniorder.dto.param.operationCenter.customer.OperationCenterCustomerParam"
          resultType="cn.superdesk.uniorder.dto.vo.operationCenter.customer.OperationCenterCustomerVo">
    SELECT
           t.id,
           t.type,t.customer_sn customerSn ,
           t.customer_name customerName,
           t.company_name companyName,
           t.social_credit_code  socialCreditCode,
           t.address,
           t.contact,
           t.mobile,
           t.email,
           t.business_id businessId,
           t.business_name businessName,
           t.remark,
           t.`status`,
           t.is_data_permission isPermission,
           t.is_settlement_method isSettlement,
           t.is_bill_start isBillStart,
           IF(is_data_permission=0,'未关联','已关联') permission,
           IF(is_settlement_method=0,'未配置','已配置') settlement,
           IF(is_bill_start=0,'未配置','已配置') billStart,
           t.create_time createTime,
           t1.fullname createUser,
           t.update_time updateTime,
           t2.fullname  updateUser
    FROM
      unified_center_customer  t
        LEFT JOIN
      sys_user t1
      ON t1.id=t.create_user_id
        LEFT JOIN
      sys_user t2
      ON t2.id=t.update_user_id
      where t.deleted =0
    <if test="id !=null and id !=''">
      and t.id =#{id}
    </if>
    <if test="type != null and type !=''">
     and t.TYPE=#{type}
    </if>
    <if test="status != null ">
     and t.STATUS=#{status}
    </if>
    <if test="customerName != null and customerName !=''">
     and  t.customer_name like concat ('%',#{customerName},'%')
    </if>
    <if test="customerSn != null and customerSn !=''">
     and  t.customer_sn=#{customerSn}
    </if>
    <if test="customerContact != null and customerContact !=''">
     and  t.contact like concat ('%',#{customerContact},'%')
    </if>
    <if test="business != null and business !=''">
     and  t.business_name=#{business}
    </if>
     order by t.update_time desc
  </select>
  <select id="findAllCustomer"
          parameterType="cn.superdesk.uniorder.dto.param.operationCenter.customer.OperationCenterCustomerParam"
          resultType="cn.superdesk.uniorder.dto.vo.operationCenter.customer.OperationCenterPaymentCustomerVo">
    select
      t.id,
      t.customer_name customerName,
      t.customer_sn customerSn,
      t.`status`,
      t.deleted
    FROM
      unified_center_customer t
    where 1=1
    <if test="customerName != null and customerName !=''">
      and  t.customer_name like concat ('%',#{customerName},'%')
    </if>


  </select>
  <select id="getLinkOperatorAndOrg"
          resultType="cn.superdesk.uniorder.dto.vo.operationCenter.customer.OpCenterCustomerOperatorOrgVo">
    SELECT t.id customerId,
           t.type,
           t.customer_name customerName,
           t1.is_all_org isAllOrg,
           t2.id operatorId,
           t2.biz_operator_id bizOperatorId,
           t2.`name` operatorName,
           t2.biz_platform_name bizPlatformName,
           t2.biz_platform_code bizPlatformCode,
           t3.id orgId,
           t3.organization_id bizOrgId,
           t3.organization_name orgName

    FROM
      unified_center_customer t
         LEFT JOIN
      unified_customer_operator_org t1
      ON
        t.id = t1.customer_id
        LEFT JOIN
      unified_operators t2
      ON
        t2.id=t1.operator_id
       LEFT JOIN
      sys_organization t3
      ON
        t3.id=t1.org_id
    where t.deleted = 0 and t1.deleted = 0
    <if test="orgList != null and orgList.size > 0 ">
      and ( t3.id in
      <foreach collection="orgList"  index="index" item="orgId" open="(" close=")" separator=",">
         #{orgId}
      </foreach>
          <if test="operatorIdList !=null and operatorIdList.size>0 ">
                or  ( t2.id in
            <foreach collection="operatorIdList"  index="index" item="operatorId" open="(" close=")" separator=",">
              #{operatorId}
            </foreach>
          </if>
           and  t1.is_all_org =1 )

        )


    </if>

    <if test="customerId != null and customerId !=''">
      and t.id =#{customerId}
    </if>
    <if test="orgId != null and orgId !=''">
      and  t3.id =#{orgId}
    </if>
    <if test="orgIds != null and orgIds !=''">
      and  t3.id in (${orgIds})
    </if>
    <if test="operatorIds != null and operatorIds !=''">
      and  t2.id in (${operatorIds})
    </if>
    <if test="type != null and type !=''">
      and t.type=#{type}
    </if>
    <if test="noExcluding != null and noExcluding !=''">
      and t.id != #{noExcluding}
    </if>

    <if test="limit != null and limit !=''">
      limit  #{limit}
    </if>

  </select>
  <select id="selectCustomerByCustomerName"
          resultMap="BaseResultMap">
    select * from unified_center_customer
             where deleted = 0
     <if test="customerName != null and customerName !=''">
       and  customer_name = #{customerName}
     </if>
    <if test="customerId != null and customerId !=''">
      and  id !=#{customerId}
    </if>

  </select>

</mapper>