<?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.SysOrganizationMapper">
  <resultMap id="BaseResultMap" type="cn.superdesk.uniorder.dao.entity.organisation.SysOrganization">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="organization_id" jdbcType="BIGINT" property="organizationId" />
    <result column="organization_no" jdbcType="VARCHAR" property="organizationNo" />
    <result column="organization_name" jdbcType="VARCHAR" property="organizationName" />
    <result column="province_code" jdbcType="VARCHAR" property="provinceCode" />
    <result column="province_name" jdbcType="VARCHAR" property="provinceName" />
    <result column="city_code" jdbcType="VARCHAR" property="cityCode" />
    <result column="city_name" jdbcType="VARCHAR" property="cityName" />
    <result column="address" jdbcType="VARCHAR" property="address" />
    <result column="lng" jdbcType="DECIMAL" property="lng" />
    <result column="lat" jdbcType="DECIMAL" property="lat" />
    <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="deleted" jdbcType="TINYINT" property="deleted" />
    <result column="is_has_order" jdbcType="TINYINT" property="isHasOrder" />
    <result column="service_no" jdbcType="VARCHAR" property="serviceNo" />
    <result column="source_from" jdbcType="VARCHAR" property="sourceFrom" />
    <result column="biz_platform_code" jdbcType="VARCHAR" property="bizPlatformCode" />
    <result column="biz_operator_id" jdbcType="VARCHAR" property="bizOperatorId" />
    <result column="biz_platform_name" jdbcType="VARCHAR" property="bizPlatformName" />
  </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, organization_id, organization_no, organization_name, province_code, province_name, 
    city_code, city_name, address, lng, lat, sync_time, other_field1, other_field2, other_field3, 
    deleted, is_has_order, service_no, source_from,biz_platform_code,biz_operator_id,biz_platform_name
  </sql>
  <select id="selectByExample" parameterType="cn.superdesk.uniorder.dao.entity.organisation.SysOrganizationExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from sys_organization
    <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_organization
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from sys_organization
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByExample" parameterType="cn.superdesk.uniorder.dao.entity.organisation.SysOrganizationExample">
    delete from sys_organization
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="cn.superdesk.uniorder.dao.entity.organisation.SysOrganization">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into sys_organization (organization_id, organization_no, organization_name, 
      province_code, province_name, city_code, 
      city_name, address, lng, 
      lat, sync_time, other_field1, 
      other_field2, other_field3, deleted, 
      is_has_order, service_no, source_from,biz_platform_code,biz_operator_id,
    biz_platform_name
      )
    values (#{organizationId,jdbcType=BIGINT}, #{organizationNo,jdbcType=VARCHAR}, #{organizationName,jdbcType=VARCHAR}, 
      #{provinceCode,jdbcType=VARCHAR}, #{provinceName,jdbcType=VARCHAR}, #{cityCode,jdbcType=VARCHAR}, 
      #{cityName,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR}, #{lng,jdbcType=DECIMAL}, 
      #{lat,jdbcType=DECIMAL}, #{syncTime,jdbcType=TIMESTAMP}, #{otherField1,jdbcType=VARCHAR}, 
      #{otherField2,jdbcType=VARCHAR}, #{otherField3,jdbcType=VARCHAR}, #{deleted,jdbcType=TINYINT}, 
      #{isHasOrder,jdbcType=TINYINT}, #{serviceNo,jdbcType=VARCHAR}, #{sourceFrom,jdbcType=VARCHAR}
    ,#{bizPlatformCode,jdbcType=VARCHAR},
    #{bizOperatorId,jdbcType=VARCHAR},#{bizPlatformName,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="cn.superdesk.uniorder.dao.entity.organisation.SysOrganization">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into sys_organization
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="organizationId != null">
        organization_id,
      </if>
      <if test="organizationNo != null">
        organization_no,
      </if>
      <if test="organizationName != null">
        organization_name,
      </if>
      <if test="provinceCode != null">
        province_code,
      </if>
      <if test="provinceName != null">
        province_name,
      </if>
      <if test="cityCode != null">
        city_code,
      </if>
      <if test="cityName != null">
        city_name,
      </if>
      <if test="address != null">
        address,
      </if>
      <if test="lng != null">
        lng,
      </if>
      <if test="lat != null">
        lat,
      </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="deleted != null">
        deleted,
      </if>
      <if test="isHasOrder != null">
        is_has_order,
      </if>
      <if test="serviceNo != null">
        service_no,
      </if>
      <if test="sourceFrom != null">
        source_from,
      </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="organizationId != null">
        #{organizationId,jdbcType=BIGINT},
      </if>
      <if test="organizationNo != null">
        #{organizationNo,jdbcType=VARCHAR},
      </if>
      <if test="organizationName != null">
        #{organizationName,jdbcType=VARCHAR},
      </if>
      <if test="provinceCode != null">
        #{provinceCode,jdbcType=VARCHAR},
      </if>
      <if test="provinceName != null">
        #{provinceName,jdbcType=VARCHAR},
      </if>
      <if test="cityCode != null">
        #{cityCode,jdbcType=VARCHAR},
      </if>
      <if test="cityName != null">
        #{cityName,jdbcType=VARCHAR},
      </if>
      <if test="address != null">
        #{address,jdbcType=VARCHAR},
      </if>
      <if test="lng != null">
        #{lng,jdbcType=DECIMAL},
      </if>
      <if test="lat != null">
        #{lat,jdbcType=DECIMAL},
      </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="deleted != null">
        #{deleted,jdbcType=TINYINT},
      </if>
      <if test="isHasOrder != null">
        #{isHasOrder,jdbcType=TINYINT},
      </if>
      <if test="serviceNo != null">
        #{serviceNo,jdbcType=VARCHAR},
      </if>
      <if test="sourceFrom != null">
        #{sourceFrom,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.SysOrganizationExample" resultType="java.lang.Long">
    select count(*) from sys_organization
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update sys_organization
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=BIGINT},
      </if>
      <if test="record.organizationId != null">
        organization_id = #{record.organizationId,jdbcType=BIGINT},
      </if>
      <if test="record.organizationNo != null">
        organization_no = #{record.organizationNo,jdbcType=VARCHAR},
      </if>
      <if test="record.organizationName != null">
        organization_name = #{record.organizationName,jdbcType=VARCHAR},
      </if>
      <if test="record.provinceCode != null">
        province_code = #{record.provinceCode,jdbcType=VARCHAR},
      </if>
      <if test="record.provinceName != null">
        province_name = #{record.provinceName,jdbcType=VARCHAR},
      </if>
      <if test="record.cityCode != null">
        city_code = #{record.cityCode,jdbcType=VARCHAR},
      </if>
      <if test="record.cityName != null">
        city_name = #{record.cityName,jdbcType=VARCHAR},
      </if>
      <if test="record.address != null">
        address = #{record.address,jdbcType=VARCHAR},
      </if>
      <if test="record.lng != null">
        lng = #{record.lng,jdbcType=DECIMAL},
      </if>
      <if test="record.lat != null">
        lat = #{record.lat,jdbcType=DECIMAL},
      </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.deleted != null">
        deleted = #{record.deleted,jdbcType=TINYINT},
      </if>
      <if test="record.isHasOrder != null">
        is_has_order = #{record.isHasOrder,jdbcType=TINYINT},
      </if>
      <if test="record.serviceNo != null">
        service_no = #{record.serviceNo,jdbcType=VARCHAR},
      </if>
      <if test="record.sourceFrom != null">
        source_from = #{record.sourceFrom,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_organization
    set id = #{record.id,jdbcType=BIGINT},
      organization_id = #{record.organizationId,jdbcType=BIGINT},
      organization_no = #{record.organizationNo,jdbcType=VARCHAR},
      organization_name = #{record.organizationName,jdbcType=VARCHAR},
      province_code = #{record.provinceCode,jdbcType=VARCHAR},
      province_name = #{record.provinceName,jdbcType=VARCHAR},
      city_code = #{record.cityCode,jdbcType=VARCHAR},
      city_name = #{record.cityName,jdbcType=VARCHAR},
      address = #{record.address,jdbcType=VARCHAR},
      lng = #{record.lng,jdbcType=DECIMAL},
      lat = #{record.lat,jdbcType=DECIMAL},
      sync_time = #{record.syncTime,jdbcType=TIMESTAMP},
      other_field1 = #{record.otherField1,jdbcType=VARCHAR},
      other_field2 = #{record.otherField2,jdbcType=VARCHAR},
      other_field3 = #{record.otherField3,jdbcType=VARCHAR},
      deleted = #{record.deleted,jdbcType=TINYINT},
      is_has_order = #{record.isHasOrder,jdbcType=TINYINT},
      service_no = #{record.serviceNo,jdbcType=VARCHAR},
      source_from = #{record.sourceFrom,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.SysOrganization">
    update sys_organization
    <set>
      <if test="organizationId != null">
        organization_id = #{organizationId,jdbcType=BIGINT},
      </if>
      <if test="organizationNo != null">
        organization_no = #{organizationNo,jdbcType=VARCHAR},
      </if>
      <if test="organizationName != null">
        organization_name = #{organizationName,jdbcType=VARCHAR},
      </if>
      <if test="provinceCode != null">
        province_code = #{provinceCode,jdbcType=VARCHAR},
      </if>
      <if test="provinceName != null">
        province_name = #{provinceName,jdbcType=VARCHAR},
      </if>
      <if test="cityCode != null">
        city_code = #{cityCode,jdbcType=VARCHAR},
      </if>
      <if test="cityName != null">
        city_name = #{cityName,jdbcType=VARCHAR},
      </if>
      <if test="address != null">
        address = #{address,jdbcType=VARCHAR},
      </if>
      <if test="lng != null">
        lng = #{lng,jdbcType=DECIMAL},
      </if>
      <if test="lat != null">
        lat = #{lat,jdbcType=DECIMAL},
      </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="deleted != null">
        deleted = #{deleted,jdbcType=TINYINT},
      </if>
      <if test="isHasOrder != null">
        is_has_order = #{isHasOrder,jdbcType=TINYINT},
      </if>
      <if test="serviceNo != null">
        service_no = #{serviceNo,jdbcType=VARCHAR},
      </if>
      <if test="sourceFrom != null">
        source_from = #{sourceFrom,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.SysOrganization">
    update sys_organization
    set organization_id = #{organizationId,jdbcType=BIGINT},
      organization_no = #{organizationNo,jdbcType=VARCHAR},
      organization_name = #{organizationName,jdbcType=VARCHAR},
      province_code = #{provinceCode,jdbcType=VARCHAR},
      province_name = #{provinceName,jdbcType=VARCHAR},
      city_code = #{cityCode,jdbcType=VARCHAR},
      city_name = #{cityName,jdbcType=VARCHAR},
      address = #{address,jdbcType=VARCHAR},
      lng = #{lng,jdbcType=DECIMAL},
      lat = #{lat,jdbcType=DECIMAL},
      sync_time = #{syncTime,jdbcType=TIMESTAMP},
      other_field1 = #{otherField1,jdbcType=VARCHAR},
      other_field2 = #{otherField2,jdbcType=VARCHAR},
      other_field3 = #{otherField3,jdbcType=VARCHAR},
      deleted = #{deleted,jdbcType=TINYINT},
      is_has_order = #{isHasOrder,jdbcType=TINYINT},
      service_no = #{serviceNo,jdbcType=VARCHAR},
      source_from = #{sourceFrom,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="getOrgList" resultType="cn.superdesk.uniorder.dto.vo.sys.SysOrgVo"
          parameterType="cn.superdesk.uniorder.dto.param.organisation.SysOrgQueryParam">
    SELECT
      t.`id`,
      t.`organization_id` organizationId,
      t.`organization_no` organizationNo,
      t.`organization_name` organizationName,
      t.`sync_time` syncTime,
      t.`deleted` deleted,
      t.`biz_platform_code` bizPlatformCode,
      t.`biz_platform_name` bizPlatformName,
      t.`biz_operator_id` bizOperatorId,
      t2.`name` bizOperatorName
    FROM
      sys_organization t
        LEFT JOIN unified_operators t2 ON t.biz_operator_id = t2.biz_operator_id
    WHERE
      1 = 1
    <if test="null != compId and '' != compId">
      AND t2.id in (${compId})
    </if>
    <if test="null != bizPlatformCode and '' != bizPlatformCode">
      AND t.`biz_platform_code`=#{bizPlatformCode}
    </if>
    <if test="null != deleted">
      AND t.`deleted`=#{virtDeleted}
    </if>
    <if test="null != organizationNo  and '' != organizationNo">
      AND t.`organization_no`  LIKE CONCAT ('%',#{organizationNo},'%')
    </if>
    <if test="null != organizationName  and '' != organizationName">
      AND t.`organization_name`  LIKE CONCAT ('%',#{organizationName},'%')
    </if>
    ORDER BY
      t.sync_time DESC
  </select>


  <select id="selectOrgList"
          resultType="cn.superdesk.uniorder.dto.vo.sys.SysSelectVo">
     select t.id,t.organization_name name
    from sys_organization t
    where 1=1
    <if test="orgIds !=null and orgIds.size>0">
     and   t.id in
    <foreach collection="orgIds" item="orgId" separator="," open="(" close=")"  index="index">
      #{orgId}
    </foreach>

    </if>
    <if test="null != deleted">
      AND t.`deleted`=#{deleted}
    </if>
    <if test="null != enabled">
      AND t.`enabled`=#{enabled}
    </if>

    <if test="null !=compId">
      and t.biz_operator_id=#{compId}
    </if>
    <if test="null !=bizPlatformCode">
      and t.biz_platform_code=#{bizPlatformCode}
    </if>
<!--    <if test="null !=notOrgIds and notOrgIds !=''">-->
<!--      and t1.id not in  ( ${notOrgIds} )-->
<!--    </if>-->


  </select>
  <select id="getOrgListByEnterprise"

          resultType="cn.superdesk.uniorder.dto.vo.sys.SysSelectVo">
    SELECT
      t1.id,
      t1.organization_name `name`
    FROM
      sys_enterprise t
        INNER  JOIN
      sys_organization t1
      ON
        t.organization_id =t1.organization_id
    where 1=1

    <if test="null !=compId">
       and t.biz_operator_id=#{compId}
    </if>
    <if test="null !=bizPlatformCode">
      and t.biz_platform_code=#{bizPlatformCode}
    </if>
<!--    <if test="null !=notOrgIds and notOrgIds !=''">-->
<!--      and t1.id not in  ( ${notOrgIds} )-->
<!--    </if>-->

    <if test="null != deleted">
      AND t1.`deleted`=#{deleted}
    </if>

    GROUP BY
      t1.id

    order by t1.id

  </select>
</mapper>