<?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.UnifiedBillStartMapper">
  <resultMap id="BaseResultMap" type="cn.superdesk.uniorder.dao.entity.operationCenter.UnifiedCustomerInvoiceTitle">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="customer_id" jdbcType="BIGINT" property="customerId" />
    <result column="type" jdbcType="TINYINT" property="type" />
    <result column="invoice_type" jdbcType="TINYINT" property="invoiceType" />
    <result column="bill_name" jdbcType="VARCHAR" property="billName" />
    <result column="taxpayer_sn" jdbcType="VARCHAR" property="taxpayerSn" />
    <result column="address" jdbcType="VARCHAR" property="address" />
    <result column="mobile" jdbcType="VARCHAR" property="mobile" />
    <result column="bank_name" jdbcType="VARCHAR" property="bankName" />
    <result column="bank_account" jdbcType="VARCHAR" property="bankAccount" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
    <result column="is_default" jdbcType="TINYINT" property="isDefault" />
    <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" />
    <result column="data_sources" jdbcType="TINYINT" property="dataSources" />
  </resultMap>
  <sql id="Base_Column_List">
    id, customer_id, type, invoice_type, bill_name, taxpayer_sn, address, mobile, bank_name, 
    bank_account, email, remark, is_default, create_time, create_user_id, update_time, 
    update_user_id, deleted,data_sources
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from unified_customer_invoice_title
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    update unified_customer_invoice_title set deleted=1
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" parameterType="cn.superdesk.uniorder.dao.entity.operationCenter.UnifiedCustomerInvoiceTitle">
    insert into unified_customer_invoice_title (id, customer_id, type,
      invoice_type, bill_name, taxpayer_sn, 
      address, mobile, bank_name, 
      bank_account, email, remark, 
      is_default, create_time, create_user_id, 
      update_time, update_user_id, deleted,data_sources
      )
    values (#{id,jdbcType=BIGINT}, #{customerId,jdbcType=BIGINT}, #{type,jdbcType=TINYINT}, 
      #{invoiceType,jdbcType=TINYINT}, #{billName,jdbcType=VARCHAR}, #{taxpayerSn,jdbcType=VARCHAR}, 
      #{address,jdbcType=VARCHAR}, #{mobile,jdbcType=VARCHAR}, #{bankName,jdbcType=VARCHAR}, 
      #{bankAccount,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}, 
      #{isDefault,jdbcType=TINYINT}, #{createTime,jdbcType=TIMESTAMP}, #{createUserId,jdbcType=BIGINT}, 
      #{updateTime,jdbcType=TIMESTAMP}, #{updateUserId,jdbcType=BIGINT}, #{deleted,jdbcType=TINYINT},
            #{data_sources,jdbcType=TINYINT}
      )
  </insert>
  <insert id="insertSelective" parameterType="cn.superdesk.uniorder.dao.entity.operationCenter.UnifiedCustomerInvoiceTitle">
    insert into unified_customer_invoice_title
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="customerId != null">
        customer_id,
      </if>
      <if test="type != null">
        type,
      </if>
      <if test="invoiceType != null">
        invoice_type,
      </if>
      <if test="billName != null">
        bill_name,
      </if>
      <if test="taxpayerSn != null">
        taxpayer_sn,
      </if>
      <if test="address != null">
        address,
      </if>
      <if test="mobile != null">
        mobile,
      </if>
      <if test="bankName != null">
        bank_name,
      </if>
      <if test="bankAccount != null">
        bank_account,
      </if>
      <if test="email != null">
        email,
      </if>
      <if test="remark != null">
        remark,
      </if>
      <if test="isDefault != null">
        is_default,
      </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>
      <if test="dataSources != null">
        data_sources,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=BIGINT},
      </if>
      <if test="customerId != null">
        #{customerId,jdbcType=BIGINT},
      </if>
      <if test="type != null">
        #{type,jdbcType=TINYINT},
      </if>
      <if test="invoiceType != null">
        #{invoiceType,jdbcType=TINYINT},
      </if>
      <if test="billName != null">
        #{billName,jdbcType=VARCHAR},
      </if>
      <if test="taxpayerSn != null">
        #{taxpayerSn,jdbcType=VARCHAR},
      </if>
      <if test="address != null">
        #{address,jdbcType=VARCHAR},
      </if>
      <if test="mobile != null">
        #{mobile,jdbcType=VARCHAR},
      </if>
      <if test="bankName != null">
        #{bankName,jdbcType=VARCHAR},
      </if>
      <if test="bankAccount != null">
        #{bankAccount,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="remark != null">
        #{remark,jdbcType=VARCHAR},
      </if>
      <if test="isDefault != null">
        #{isDefault,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>
      <if test="dataSources != null">
        #{dataSources,jdbcType=TINYINT},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="cn.superdesk.uniorder.dao.entity.operationCenter.UnifiedCustomerInvoiceTitle">
    update unified_customer_invoice_title
    <set>
      <if test="customerId != null">
        customer_id = #{customerId,jdbcType=BIGINT},
      </if>
      <if test="type != null">
        type = #{type,jdbcType=TINYINT},
      </if>
      <if test="invoiceType != null">
        invoice_type = #{invoiceType,jdbcType=TINYINT},
      </if>
      <if test="billName != null">
        bill_name = #{billName,jdbcType=VARCHAR},
      </if>
      <if test="taxpayerSn != null">
        taxpayer_sn = #{taxpayerSn,jdbcType=VARCHAR},
      </if>
      <if test="address != null">
        address = #{address,jdbcType=VARCHAR},
      </if>
      <if test="mobile != null">
        mobile = #{mobile,jdbcType=VARCHAR},
      </if>
      <if test="bankName != null">
        bank_name = #{bankName,jdbcType=VARCHAR},
      </if>
      <if test="bankAccount != null">
        bank_account = #{bankAccount,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="remark != null">
        remark = #{remark,jdbcType=VARCHAR},
      </if>
      <if test="isDefault != null">
        is_default = #{isDefault,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>
      <if test="dataSources != null">
        data_sources = #{dataSources,jdbcType=TINYINT},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="cn.superdesk.uniorder.dao.entity.operationCenter.UnifiedCustomerInvoiceTitle">
    update unified_customer_invoice_title
    set customer_id = #{customerId,jdbcType=BIGINT},
      type = #{type,jdbcType=TINYINT},
      invoice_type = #{invoiceType,jdbcType=TINYINT},
      bill_name = #{billName,jdbcType=VARCHAR},
      taxpayer_sn = #{taxpayerSn,jdbcType=VARCHAR},
      address = #{address,jdbcType=VARCHAR},
      mobile = #{mobile,jdbcType=VARCHAR},
      bank_name = #{bankName,jdbcType=VARCHAR},
      bank_account = #{bankAccount,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      remark = #{remark,jdbcType=VARCHAR},
      is_default = #{isDefault,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}
      data_sources = #{dataSources,jdbcType=TINYINT}
    where id = #{id,jdbcType=BIGINT}
  </update>


  <select id="getBillStartListByCustomerId"
          parameterType="cn.superdesk.uniorder.dto.param.operationCenter.base.OperationCenterBaseParam"
          resultType="cn.superdesk.uniorder.dto.vo.operationCenter.customer.OperationCenterBillStartVo">
    SELECT
      t.id,
      t.customer_id customerId ,
      t.bill_name billName,
      t.type,
      t.invoice_type invoiceType ,
      t.taxpayer_sn taxpayerSn,
      t.address,
      t.mobile,
      t.bank_name bankName,
      t.bank_account bankAccount,
      t.email,
      t.remark,
      t.is_default isDefault,
      t.create_time createTime,
      t.update_time updateTime,
      t1.fullname createUser,
      t2.fullname updateUser

    FROM
      unified_customer_invoice_title t
        LEFT JOIN
      sys_user t1
      ON
        t.create_user_id =t1.id
        LEFT JOIN
      sys_user t2
      ON t.update_user_id=t2.id

    where t.deleted=0
    <if test="baseName != null and baseName !=''">
        and t.bill_name like concat('%',#{baseName},'%')
    </if>
    <if test="id != null and id !=''">
        and t.customer_id=#{id}
    </if>
    ORDER BY t.update_time desc
  </select>

  <update id="updateBillStartIsDefault">
    update unified_customer_invoice_title
    set update_time =NOW(),
    update_user_id=#{userId},
    is_default=#{isDefault}
    where
    deleted = 0 and is_default != #{isDefault}
    and customer_id=#{customerId}
    <if test="id !=null and id !=''">
      and id=#{id}
    </if>
  </update>

</mapper>