<?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.OperationCenterBusinessMapper">
  <resultMap id="BaseResultMap" type="cn.superdesk.uniorder.dao.entity.operationCenter.OperationCenterBusiness">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="user_name" jdbcType="VARCHAR" property="userName" />
    <result column="user_number" jdbcType="VARCHAR" property="userNumber" />
    <result column="company_name" jdbcType="VARCHAR" property="companyName" />
    <result column="dept_name" jdbcType="VARCHAR" property="deptName" />
    <result column="area_name" jdbcType="VARCHAR" property="areaName" />
    <result column="post_name" jdbcType="VARCHAR" property="postName" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
    <result column="job_status" jdbcType="TINYINT" property="jobStatus" />
    <result column="enabled" jdbcType="TINYINT" property="enabled" />
    <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,
	user_name,
	user_number,
	company_name,
	dept_name,
	area_name,
	post_name,
	phone,
	email,
	job_status,
	remark,
	create_time,
	create_user_id,
	update_time,
	update_user_id,
	deleted,
	enabled
  </sql>

  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from unified_business
    where id = #{id,jdbcType=BIGINT}
  </select>

    <insert id="insertSelective" parameterType="cn.superdesk.uniorder.dao.entity.operationCenter.OperationCenterBusiness" useGeneratedKeys="true" keyColumn="id" keyProperty="id" >
        INSERT INTO `unified_business` (
            `user_name`,
            `user_number`,
            `company_name`,
            `dept_name`,
            `area_name`,
            `post_name`,
            `phone`,
            `email`,
            `job_status`,
            `remark`,
            `create_time`,
            `create_user_id`,
            `update_time`,
            `update_user_id`,
            `deleted`,
            `enabled`
        )
        VALUES
            (
                #{userName,jdbcType=VARCHAR},
                #{userNumber,jdbcType=VARCHAR},
                #{companyName,jdbcType=VARCHAR},
                #{deptName,jdbcType=VARCHAR},
                #{areaName,jdbcType=VARCHAR},
                #{postName,jdbcType=VARCHAR},
                #{phone,jdbcType=VARCHAR},
                #{email,jdbcType=VARCHAR},
                #{jobStatus,jdbcType=TINYINT},
                #{remark,jdbcType=VARCHAR},
                #{createTime,jdbcType=TIMESTAMP},
                #{createUserId,jdbcType=BIGINT},
                #{updateTime,jdbcType=TIMESTAMP},
                #{updateUserId,jdbcType=BIGINT},
                #{deleted,jdbcType=TINYINT},
                #{enabled,jdbcType=TINYINT}
            );
    </insert>

    <update id="updateByPrimaryKey" parameterType="cn.superdesk.uniorder.dao.entity.operationCenter.OperationCenterBusiness">
        UPDATE `unified_business`
        SET `user_name` = #{userName,jdbcType=VARCHAR},
            `user_number` = #{userNumber,jdbcType=VARCHAR},
            `company_name` = #{companyName,jdbcType=VARCHAR},
            `dept_name` = #{deptName,jdbcType=VARCHAR},
            `area_name` = #{areaName,jdbcType=VARCHAR},
            `post_name` = #{postName,jdbcType=VARCHAR},
            `phone` = #{phone,jdbcType=VARCHAR},
            `email` = #{email,jdbcType=VARCHAR},
            `job_status` = #{jobStatus,jdbcType=TINYINT},
            `remark` = #{remark,jdbcType=VARCHAR},
            `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},
            `enabled` = #{enabled,jdbcType=TINYINT}
        WHERE
            `id` = #{id,jdbcType=BIGINT};
    </update>

    <select id="getListByParams"
            parameterType="cn.superdesk.uniorder.dto.param.operationCenter.business.BusinessQueryParam"
            resultType="cn.superdesk.uniorder.dto.vo.operationCenter.business.BusinessVo">
        SELECT
            t.`id` id,
            t.`user_name` userName,
            t.`user_number` userNumber,
            t.`company_name` companyName,
            t.`dept_name` deptName,
            t.`area_name` areaName,
            t.`post_name` postName,
            t.`phone`,
            t.`email`,
            t.`job_status` jobStatus,
            t.`remark`,
            t.`deleted`,
            t.`enabled`,
            t.create_time createTime,
            t1.fullname createUser,
            t.update_time updateTime,
            t2.fullname updateUser,
            count( t3.id ) customerCount
        FROM
        unified_business 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
                LEFT JOIN unified_center_customer t3 ON t3.business_id = t.id
        WHERE
            t.deleted = 0
            <if test="null != enabled">
                AND t.enabled=#{enabled}
            </if>
            <if test="null != jobStatus">
                AND t.job_status=#{jobStatus}
            </if>
            <if test="null != userName and '' != userName">
                AND t.user_name LIKE CONCAT ('%',#{userName},'%')
            </if>
        GROUP BY
            t.id
        ORDER BY t.update_time DESC

    </select>

    <select id="getByBusinessId" parameterType="java.lang.Long" resultType="cn.superdesk.uniorder.dto.vo.operationCenter.business.BusinessCustomerVo">
        SELECT
        c.id,
        c.customer_name,
        c.customer_sn,
        c.contact,
        c.remark,
        c.STATUS,
        c.create_user_id,
        c.create_time,
        t1.fullname
        FROM
        unified_center_customer c
        LEFT JOIN sys_user t1 ON t1.id = c.create_user_id
        WHERE
        c.deleted =0
        <if test="businessId != null and businessId !=''">
            and  c.business_id = #{businessId}
        </if>
        <if test="customerName != null and customerName !=''">
            and  c.customer_name = LIKE CONCAT ('%',#{customerName},'%')
        </if>
        ORDER BY c.create_time DESC
    </select>

    <select id="selectList" resultType="cn.superdesk.uniorder.dto.vo.sys.SysSelectVo" parameterType="Map">
        SELECT
        t.`id`,
        t.user_name name
        FROM
        unified_business t
        WHERE
        1 = 1
        <if test="null != deleted">
            AND t.`deleted`=#{deleted}
        </if>
        <if test="null != enabled">
            AND t.`enabled`=#{enabled}
        </if>
        <if test="null != jobStatus">
            AND t.`job_status`=#{jobStatus}
        </if>
        ORDER BY
        t.`id`
    </select>
</mapper>