<?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="com.xhpc.activity.mapper.XhpcWorkOrderMapper">
  <resultMap id="BaseResultMap" type="com.xhpc.activity.domain.XhpcWorkOrderDomain">
    <id column="work_order_id" jdbcType="BIGINT" property="workOrderId" />
    <result column="type" jdbcType="SMALLINT" property="type" />
    <result column="title" jdbcType="VARCHAR" property="title" />
    <result column="content" jdbcType="VARCHAR" property="content" />
    <result column="fault_time" jdbcType="TIMESTAMP" property="faultTime" />
    <result column="device_type" jdbcType="VARCHAR" property="deviceType" />
    <result column="serial_number" jdbcType="VARCHAR" property="serialNumber" />
    <result column="reason" jdbcType="VARCHAR" property="reason" />
    <result column="disposal_method" jdbcType="VARCHAR" property="disposalMethod" />
    <result column="status" jdbcType="SMALLINT" property="status" />
    <result column="del_flag" jdbcType="SMALLINT" property="delFlag" />
    <result column="tenant_id" jdbcType="VARCHAR" property="tenantId" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="create_by" jdbcType="VARCHAR" property="createBy" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />

  </resultMap>
  <sql id="Base_Column_List">
    o.work_order_id, o.`type`, o.title, o.content, o.fault_time, o.device_type, o.serial_number,
    o.reason, o.disposal_method, o.`status`, o.del_flag, o.tenant_id, o.create_time, o.create_by, o.update_time,
    o.update_by, o.remark
  </sql>


  <insert id="insertOrderUser">
    insert into xhpc_work_order_user (order_id, user_id)
    values
    <foreach collection="domainList" item="domain" separator=",">
      (#{domain.orderId}, #{domain.userId})
    </foreach>
  </insert>


  <select id="selectByTypeAndTitle" resultType="com.xhpc.activity.domain.XhpcWorkOrderDomain">
    select
    <include refid="Base_Column_List" />
    from xhpc_work_order o
    where o.del_flag=0
      and o.type =#{type}
      and o.title=#{title}
      and o.status=1
        order by create_time desc limit 1
  </select>


  <select id="findOrderListByParams" resultType="java.util.Map">
    SELECT
    o.work_order_id as 'workOrderId',
    o.title as 'title',
    o.type as 'typeId',
    td.name as 'typeName',
    GROUP_CONCAT(u.dept_id) as 'deptId',
    GROUP_CONCAT(d.dept_name) as 'deptName',
    GROUP_CONCAT(u.work_user_id) as 'userId',
    GROUP_CONCAT(u.user_name, '(', d.dept_name, ')') as 'userName',
    o.`status` as 'status',
    o.create_time as createTime
    from xhpc_work_order o
    left join xhpc_work_type_dict td on td.work_type_id=o.type
    LEFT JOIN xhpc_work_order_user ou on o.work_order_id=ou.order_id
    LEFT JOIN xhpc_work_user u on ou.user_id=u.work_user_id
    LEFT JOIN xhpc_work_dept d on d.work_dept_id=u.dept_id
    where o.del_flag=0
    <if test="params.orderType != null and params.orderType != ''">
      and o.type=#{params.orderType}
    </if>
    <if test="params.userName!=null and params.userName!=''">
     and u.user_name like concat ('%', #{params.userName}, '%')
    </if>
    <if test="params.tenantId!=null and params.tenantId!=''">
     and o.tenant_id = #{params.tenantId}
    </if>
    GROUP BY o.work_order_id
    ORDER BY o.status ASC,o.create_time desc
  </select>


  <select id="selectByPrimaryKey" resultType="com.xhpc.activity.domain.XhpcWorkOrderDomain">
    select
    <include refid="Base_Column_List" />,
    d.device_name, s.name, co.start_time, co.source, td.name as 'type_name'
    from xhpc_work_order o
    LEFT JOIN xhpc_station_device d on o.serial_number=d.serial_number
    LEFT JOIN xhpc_work_station s on d.station_id=s.work_station_id
    left join xhpc_charge_order co on co.serial_number=o.serial_number
    left join xhpc_work_type_dict td on td.work_type_id=o.type
    where o.work_order_id = #{workOrderId,jdbcType=BIGINT}
  </select>
  <update id="deleteByPrimaryKey" parameterType="java.lang.Long">
    update xhpc_work_order set del_flag=2
    where work_order_id = #{workOrderId,jdbcType=BIGINT}
  </update>
  <insert id="insert" keyColumn="work_order_id" keyProperty="workOrderId" parameterType="com.xhpc.activity.domain.XhpcWorkOrderDomain" useGeneratedKeys="true">
    insert into xhpc_work_order (`type`, title, content, fault_time
      device_type, serial_number, reason, disposal_method,
      `status`, del_flag, tenant_id,
      create_time, create_by, update_time,
      update_by, remark)
    values (#{type,jdbcType=SMALLINT}, #{title,jdbcType=VARCHAR}, #{content,jdbcType=VARCHAR}, #{faultTime, jdbcType=TIMESTAMP}
      #{deviceType,jdbcType=VARCHAR}, #{serialNumber,jdbcType=VARCHAR},
      #{reason,jdbcType=VARCHAR}, #{disposalMethod,jdbcType=VARCHAR},
      1, 0, #{tenantId,jdbcType=VARCHAR},
      sysdate(), #{createBy,jdbcType=VARCHAR},sysdate(),
      #{updateBy,jdbcType=VARCHAR}, #{remark})
  </insert>
  <insert id="insertSelective" keyColumn="work_order_id" keyProperty="workOrderId" parameterType="com.xhpc.activity.domain.XhpcWorkOrderDomain" useGeneratedKeys="true">
    insert into xhpc_work_order
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="type != null">
        `type`,
      </if>
      <if test="title != null">
        title,
      </if>
      <if test="content != null">
        content,
      </if>
        <if test="faultTime != null">
          fault_time,
        </if>
      <if test="deviceType != null">
        device_type,
      </if>
      <if test="serialNumber != null">
        serial_number,
      </if>
      <if test="reason != null">
        reason,
      </if>
      <if test="disposalMethod != null">
        disposal_method,
      </if>
        `status`,
        del_flag,
      <if test="tenantId != null">
        tenant_id,
      </if>
        create_time,
      <if test="createBy != null">
        create_by,
      </if>
        update_time,
      <if test="updateBy != null">
        update_by,
      </if>
      <if test="remark != null">
        remark,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="type != null">
        #{type,jdbcType=SMALLINT},
      </if>
      <if test="title != null">
        #{title,jdbcType=VARCHAR},
      </if>
      <if test="content != null">
        #{content,jdbcType=VARCHAR},
      </if>
        <if test="faultTime!=null">
          #{faultTime, jdbcType=TIMESTAMP},
        </if>
      <if test="deviceType != null">
        #{deviceType,jdbcType=VARCHAR},
      </if>
      <if test="serialNumber != null">
        #{serialNumber,jdbcType=VARCHAR},
      </if>
      <if test="reason != null">
        #{reason,jdbcType=VARCHAR},
      </if>
      <if test="disposalMethod != null">
        #{disposalMethod,jdbcType=VARCHAR},
      </if>
        <choose>
          <when test="status == null">
            1,
          </when>
        <otherwise>
          #{status},
        </otherwise>
        </choose>
        0,
      <if test="tenantId != null">
        #{tenantId,jdbcType=VARCHAR},
      </if>
        sysdate(),
      <if test="createBy != null">
        #{createBy,jdbcType=VARCHAR},
      </if>
        sysdate(),
      <if test="updateBy != null">
        #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="remark != null">
        #{remark},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.xhpc.activity.domain.XhpcWorkOrderDomain">
    update xhpc_work_order
    <set>
      <if test="type != null">
        `type` = #{type,jdbcType=SMALLINT},
      </if>
      <if test="title != null">
        title = #{title,jdbcType=VARCHAR},
      </if>
      <if test="content != null">
        content = #{content,jdbcType=VARCHAR},
      </if>
        <if test="faultTime!=null">
          fault_time = #{faultTime, jdbcType=TIMESTAMP},
        </if>
      <if test="deviceType != null">
        device_type = #{deviceType,jdbcType=VARCHAR},
      </if>
      <if test="serialNumber != null">
        serial_number = #{serialNumber,jdbcType=VARCHAR},
      </if>
      <if test="reason != null">
        reason = #{reason,jdbcType=VARCHAR},
      </if>
      <if test="disposalMethod != null">
        disposal_method = #{disposalMethod,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        `status` = #{status,jdbcType=SMALLINT},
      </if>
      <if test="tenantId != null">
        tenant_id = #{tenantId,jdbcType=VARCHAR},
      </if>
        update_time = sysdate(),
      <if test="updateBy != null">
        update_by = #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="remark != null">
        remark=#{remark},
      </if>
    </set>
    where work_order_id = #{workOrderId,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.xhpc.activity.domain.XhpcWorkOrderDomain">
    update xhpc_work_order
    set `type` = #{type,jdbcType=SMALLINT},
      title = #{title,jdbcType=VARCHAR},
      content = #{content,jdbcType=VARCHAR},
        fault_time = #{faultTime, jdbcType=TIMESTAMP},
      device_type = #{deviceType,jdbcType=VARCHAR},
      serial_number = #{serialNumber,jdbcType=VARCHAR},
      reason = #{reason,jdbcType=VARCHAR},
      disposal_method = #{disposalMethod,jdbcType=VARCHAR},
      `status` = #{status,jdbcType=SMALLINT},
      tenant_id = #{tenantId,jdbcType=VARCHAR},
      update_time = sysdate(),
      update_by = #{updateBy,jdbcType=VARCHAR},
        remark=#{remark}
    where work_order_id = #{workOrderId,jdbcType=BIGINT}
  </update>

  <delete id="deleteOrderUserByOrderId">
    delete from xhpc_work_order_user where order_id=#{orderId}
  </delete>

  <select id="workOrderMessage" resultType="int">
    select count(work_order_id) from xhpc_work_order where type=#{type} and create_time like concat('%', #{time}, '%') and status !=2
  </select>

</mapper>