<?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.organisation.SysEnterpriseMapper">
  <resultMap id="BaseResultMap" type="cn.superdesk.uniorder.dao.entity.organisation.SysEnterprise">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="source_from" jdbcType="VARCHAR" property="sourceFrom" />
    <result column="organization_id" jdbcType="BIGINT" property="organizationId" />
    <result column="enterprise_id" jdbcType="BIGINT" property="enterpriseId" />
    <result column="enterprise_no" jdbcType="VARCHAR" property="enterpriseNo" />
    <result column="enterprise_name" jdbcType="VARCHAR" property="enterpriseName" />
    <result column="sync_time" jdbcType="TIMESTAMP" property="syncTime" />
    <result column="other_field1" jdbcType="VARCHAR" property="otherField1" />
    <result column="other_field2" jdbcType="VARCHAR" property="otherField2" />
    <result column="other_field3" jdbcType="VARCHAR" property="otherField3" />
    <result column="enabled" jdbcType="INTEGER" property="enabled" />
    <result column="deleted" jdbcType="TINYINT" property="deleted" />
    <result column="is_has_order" jdbcType="TINYINT" property="isHasOrder" />
    <result column="is_has_ext_invoice" jdbcType="TINYINT" property="isHasExtInvoice" />
    <result column="service_no" jdbcType="VARCHAR" property="serviceNo" />
    <result column="biz_platform_code" jdbcType="VARCHAR" property="bizPlatformCode" />
    <result column="biz_platform_name" jdbcType="VARCHAR" property="bizPlatformName" />
    <result column="biz_operator_id" jdbcType="VARCHAR" property="bizOperatorId" />
  </resultMap>

  <resultMap id="VirtSelectResultMap" type="cn.superdesk.uniorder.dto.vo._function.select.SelectOptionVirt">
    <result column="sys_virt_id" jdbcType="BIGINT" property="sysVirtId"/>
    <result column="enterprise_id" jdbcType="BIGINT" property="virtId"/>
    <result column="enterprise_name" jdbcType="VARCHAR" property="virtName"/>
  </resultMap>

  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, source_from, organization_id, enterprise_id, enterprise_no, enterprise_name, 
    sync_time, other_field1, other_field2, other_field3, enabled, deleted, is_has_order, 
    is_has_ext_invoice, service_no,biz_platform_code,biz_operator_id,biz_platform_name
  </sql>
  <select id="selectByExample" parameterType="cn.superdesk.uniorder.dao.entity.organisation.SysEnterpriseExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from sys_enterprise
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from sys_enterprise
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from sys_enterprise
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByExample" parameterType="cn.superdesk.uniorder.dao.entity.organisation.SysEnterpriseExample">
    delete from sys_enterprise
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="cn.superdesk.uniorder.dao.entity.organisation.SysEnterprise">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into sys_enterprise (source_from, organization_id, enterprise_id, 
      enterprise_no, enterprise_name, sync_time, 
      other_field1, other_field2, other_field3, 
      enabled, deleted, is_has_order, 
      is_has_ext_invoice, service_no,biz_platform_code,biz_operator_id,biz_platform_name)
    values (#{sourceFrom,jdbcType=VARCHAR}, #{organizationId,jdbcType=BIGINT}, #{enterpriseId,jdbcType=BIGINT}, 
      #{enterpriseNo,jdbcType=VARCHAR}, #{enterpriseName,jdbcType=VARCHAR}, #{syncTime,jdbcType=TIMESTAMP}, 
      #{otherField1,jdbcType=VARCHAR}, #{otherField2,jdbcType=VARCHAR}, #{otherField3,jdbcType=VARCHAR}, 
      #{enabled,jdbcType=INTEGER}, #{deleted,jdbcType=TINYINT}, #{isHasOrder,jdbcType=TINYINT}, 
      #{isHasExtInvoice,jdbcType=TINYINT}, #{serviceNo,jdbcType=VARCHAR},#{bizPlatformCode,jdbcType=VARCHAR},
      #{bizOperatorId,jdbcType=VARCHAR},#{bizPlatformName,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="cn.superdesk.uniorder.dao.entity.organisation.SysEnterprise">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into sys_enterprise
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="sourceFrom != null">
        source_from,
      </if>
      <if test="organizationId != null">
        organization_id,
      </if>
      <if test="enterpriseId != null">
        enterprise_id,
      </if>
      <if test="enterpriseNo != null">
        enterprise_no,
      </if>
      <if test="enterpriseName != null">
        enterprise_name,
      </if>
      <if test="syncTime != null">
        sync_time,
      </if>
      <if test="otherField1 != null">
        other_field1,
      </if>
      <if test="otherField2 != null">
        other_field2,
      </if>
      <if test="otherField3 != null">
        other_field3,
      </if>
      <if test="enabled != null">
        enabled,
      </if>
      <if test="deleted != null">
        deleted,
      </if>
      <if test="isHasOrder != null">
        is_has_order,
      </if>
      <if test="isHasExtInvoice != null">
        is_has_ext_invoice,
      </if>
      <if test="serviceNo != null">
        service_no,
      </if>
      <if test="bizPlatformCode != null">
        biz_platform_code,
      </if>
      <if test="bizOperatorId != null">
        biz_operator_id,
      </if>
      <if test="bizPlatformName != null">
        biz_platform_name,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="sourceFrom != null">
        #{sourceFrom,jdbcType=VARCHAR},
      </if>
      <if test="organizationId != null">
        #{organizationId,jdbcType=BIGINT},
      </if>
      <if test="enterpriseId != null">
        #{enterpriseId,jdbcType=BIGINT},
      </if>
      <if test="enterpriseNo != null">
        #{enterpriseNo,jdbcType=VARCHAR},
      </if>
      <if test="enterpriseName != null">
        #{enterpriseName,jdbcType=VARCHAR},
      </if>
      <if test="syncTime != null">
        #{syncTime,jdbcType=TIMESTAMP},
      </if>
      <if test="otherField1 != null">
        #{otherField1,jdbcType=VARCHAR},
      </if>
      <if test="otherField2 != null">
        #{otherField2,jdbcType=VARCHAR},
      </if>
      <if test="otherField3 != null">
        #{otherField3,jdbcType=VARCHAR},
      </if>
      <if test="enabled != null">
        #{enabled,jdbcType=INTEGER},
      </if>
      <if test="deleted != null">
        #{deleted,jdbcType=TINYINT},
      </if>
      <if test="isHasOrder != null">
        #{isHasOrder,jdbcType=TINYINT},
      </if>
      <if test="isHasExtInvoice != null">
        #{isHasExtInvoice,jdbcType=TINYINT},
      </if>
      <if test="serviceNo != null">
        #{serviceNo,jdbcType=VARCHAR},
      </if>
      <if test="bizPlatformCode != null">
        #{bizPlatformCode,jdbcType=VARCHAR},
      </if>
      <if test="bizOperatorId != null">
        #{bizOperatorId,jdbcType=VARCHAR},
      </if>
      <if test="bizPlatformName != null">
        #{bizPlatformName,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="cn.superdesk.uniorder.dao.entity.organisation.SysEnterpriseExample" resultType="java.lang.Long">
    select count(*) from sys_enterprise
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
    <select id="queryQchjcCustomerList" resultMap="VirtSelectResultMap">
      select
        id as sys_virt_id,
        enterprise_id,
        enterprise_name
      from
        sys_enterprise where service_no = 'QYSC_QCH' order by sync_time
    </select>
    <update id="updateByExampleSelective" parameterType="map">
    update sys_enterprise
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=BIGINT},
      </if>
      <if test="record.sourceFrom != null">
        source_from = #{record.sourceFrom,jdbcType=VARCHAR},
      </if>
      <if test="record.organizationId != null">
        organization_id = #{record.organizationId,jdbcType=BIGINT},
      </if>
      <if test="record.enterpriseId != null">
        enterprise_id = #{record.enterpriseId,jdbcType=BIGINT},
      </if>
      <if test="record.enterpriseNo != null">
        enterprise_no = #{record.enterpriseNo,jdbcType=VARCHAR},
      </if>
      <if test="record.enterpriseName != null">
        enterprise_name = #{record.enterpriseName,jdbcType=VARCHAR},
      </if>
      <if test="record.syncTime != null">
        sync_time = #{record.syncTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.otherField1 != null">
        other_field1 = #{record.otherField1,jdbcType=VARCHAR},
      </if>
      <if test="record.otherField2 != null">
        other_field2 = #{record.otherField2,jdbcType=VARCHAR},
      </if>
      <if test="record.otherField3 != null">
        other_field3 = #{record.otherField3,jdbcType=VARCHAR},
      </if>
      <if test="record.enabled != null">
        enabled = #{record.enabled,jdbcType=INTEGER},
      </if>
      <if test="record.deleted != null">
        deleted = #{record.deleted,jdbcType=TINYINT},
      </if>
      <if test="record.isHasOrder != null">
        is_has_order = #{record.isHasOrder,jdbcType=TINYINT},
      </if>
      <if test="record.isHasExtInvoice != null">
        is_has_ext_invoice = #{record.isHasExtInvoice,jdbcType=TINYINT},
      </if>
      <if test="record.serviceNo != null">
        service_no = #{record.serviceNo,jdbcType=VARCHAR},
      </if>
      <if test="record.bizPlatformCode != null">
        biz_platform_code = #{bizPlatformCode,jdbcType=VARCHAR},
      </if>
      <if test="record.bizOperatorId != null">
        biz_operator_id = #{bizOperatorId,jdbcType=VARCHAR},
      </if>
      <if test="record.bizPlatformName != null">
        biz_platform_name = #{bizPlatformName,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update sys_enterprise
    set id = #{record.id,jdbcType=BIGINT},
      source_from = #{record.sourceFrom,jdbcType=VARCHAR},
      organization_id = #{record.organizationId,jdbcType=BIGINT},
      enterprise_id = #{record.enterpriseId,jdbcType=BIGINT},
      enterprise_no = #{record.enterpriseNo,jdbcType=VARCHAR},
      enterprise_name = #{record.enterpriseName,jdbcType=VARCHAR},
      sync_time = #{record.syncTime,jdbcType=TIMESTAMP},
      other_field1 = #{record.otherField1,jdbcType=VARCHAR},
      other_field2 = #{record.otherField2,jdbcType=VARCHAR},
      other_field3 = #{record.otherField3,jdbcType=VARCHAR},
      enabled = #{record.enabled,jdbcType=INTEGER},
      deleted = #{record.deleted,jdbcType=TINYINT},
      is_has_order = #{record.isHasOrder,jdbcType=TINYINT},
      is_has_ext_invoice = #{record.isHasExtInvoice,jdbcType=TINYINT},
      service_no = #{record.serviceNo,jdbcType=VARCHAR},
    biz_platform_code = #{record.bizPlatformCode,jdbcType=VARCHAR},
    biz_platform_name = #{record.bizPlatformName,jdbcType=VARCHAR},
    biz_operator_id = #{record.bizOperatorId,jdbcType=VARCHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="cn.superdesk.uniorder.dao.entity.organisation.SysEnterprise">
    update sys_enterprise
    <set>
      <if test="sourceFrom != null">
        source_from = #{sourceFrom,jdbcType=VARCHAR},
      </if>
      <if test="organizationId != null">
        organization_id = #{organizationId,jdbcType=BIGINT},
      </if>
      <if test="enterpriseId != null">
        enterprise_id = #{enterpriseId,jdbcType=BIGINT},
      </if>
      <if test="enterpriseNo != null">
        enterprise_no = #{enterpriseNo,jdbcType=VARCHAR},
      </if>
      <if test="enterpriseName != null">
        enterprise_name = #{enterpriseName,jdbcType=VARCHAR},
      </if>
      <if test="syncTime != null">
        sync_time = #{syncTime,jdbcType=TIMESTAMP},
      </if>
      <if test="otherField1 != null">
        other_field1 = #{otherField1,jdbcType=VARCHAR},
      </if>
      <if test="otherField2 != null">
        other_field2 = #{otherField2,jdbcType=VARCHAR},
      </if>
      <if test="otherField3 != null">
        other_field3 = #{otherField3,jdbcType=VARCHAR},
      </if>
      <if test="enabled != null">
        enabled = #{enabled,jdbcType=INTEGER},
      </if>
      <if test="deleted != null">
        deleted = #{deleted,jdbcType=TINYINT},
      </if>
      <if test="isHasOrder != null">
        is_has_order = #{isHasOrder,jdbcType=TINYINT},
      </if>
      <if test="isHasExtInvoice != null">
        is_has_ext_invoice = #{isHasExtInvoice,jdbcType=TINYINT},
      </if>
      <if test="serviceNo != null">
        service_no = #{serviceNo,jdbcType=VARCHAR},
      </if>
      <if test="bizPlatformCode != null">
        biz_platform_code = #{bizPlatformCode,jdbcType=VARCHAR},
      </if>
      <if test="bizOperatorId != null">
        biz_operator_id = #{bizOperatorId,jdbcType=VARCHAR},
      </if>
      <if test="bizPlatformName != null">
        biz_platform_name = #{bizPlatformName,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="cn.superdesk.uniorder.dao.entity.organisation.SysEnterprise">
    update sys_enterprise
    set source_from = #{sourceFrom,jdbcType=VARCHAR},
      organization_id = #{organizationId,jdbcType=BIGINT},
      enterprise_id = #{enterpriseId,jdbcType=BIGINT},
      enterprise_no = #{enterpriseNo,jdbcType=VARCHAR},
      enterprise_name = #{enterpriseName,jdbcType=VARCHAR},
      sync_time = #{syncTime,jdbcType=TIMESTAMP},
      other_field1 = #{otherField1,jdbcType=VARCHAR},
      other_field2 = #{otherField2,jdbcType=VARCHAR},
      other_field3 = #{otherField3,jdbcType=VARCHAR},
      enabled = #{enabled,jdbcType=INTEGER},
      deleted = #{deleted,jdbcType=TINYINT},
      is_has_order = #{isHasOrder,jdbcType=TINYINT},
      is_has_ext_invoice = #{isHasExtInvoice,jdbcType=TINYINT},
      service_no = #{serviceNo,jdbcType=VARCHAR},
        biz_platform_code = #{record.bizPlatformCode,jdbcType=VARCHAR},
        biz_platform_name = #{record.bizPlatformName,jdbcType=VARCHAR},
        biz_operator_id = #{record.bizOperatorId,jdbcType=VARCHAR}
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="getVirtList" resultType="cn.superdesk.uniorder.dto.vo.sys.SysVirtVo"
          parameterType="cn.superdesk.uniorder.dto.param.organisation.SysVirtQueryParam">
    SELECT
      t.`id`,
      t.`enterprise_id` enterpriseId,
      t.`enterprise_name` enterpriseName,
      t.`sync_time` virtSyncTime,
      t.`deleted` virtDeleted,
      t.`biz_platform_code` bizPlatformCode,
      t.`biz_platform_name` bizPlatformName,
      t.`biz_operator_id` bizOperatorId,
      t2.`name` bizOperatorName,
      t.`organization_id` orgId,
      t3.organization_name orgName
    FROM
      sys_enterprise t
        LEFT JOIN unified_operators t2 ON t.biz_operator_id = t2.biz_operator_id
        LEFT JOIN sys_organization t3 ON t.organization_id = t3.organization_id
    WHERE 1=1
      <if test="null != compId and '' != compId">
        AND t2.id in (${compId})
      </if>
    <if test="null != orgId">
      AND t3.id = #{orgId}
    </if>
    <if test="null != bizPlatformCode and '' != bizPlatformCode">
      AND t.`biz_platform_code`=#{bizPlatformCode}
    </if>
    <if test="null != virtDeleted">
      AND t.`deleted`=#{virtDeleted}
    </if>
    <if test="null != virtName  and '' != virtName">
      AND t.`enterprise_name`  LIKE CONCAT ('%',#{virtName},'%')
    </if>
    ORDER BY t.`sync_time` DESC
  </select>

  <select id="selectList" resultType="cn.superdesk.uniorder.dto.vo.sys.SysSelectVo" parameterType="Map">
    SELECT
      t.`id`,
      CONCAT(t.`enterprise_name`,'(',IFNULL(t.biz_platform_name,'福利商城'),')') name
    FROM
      sys_enterprise t
    LEFT JOIN sys_organization t1 ON t.organization_id = t1.organization_id
    WHERE
      1 = 1
    <if test="orgIdList !=null and orgIdList.size>0">
      and t1.id in
      <foreach collection="orgIdList" index="index" item="orgId" open="(" close=")" separator=",">
        #{orgId}
      </foreach>
    </if>
    <if test="orgIds !=null and orgIds !=''">
      and t1.id in (${orgIds})
    </if>
    <if test="null != deleted">
      AND t.`deleted`=#{deleted}
    </if>
    <if test="null != enabled">
      AND t.`enabled`=#{enabled}
    </if>
    ORDER BY
      t.`id`
  </select>
</mapper>