<?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.zcxk.rmcp.core.dao.DeviceCommandMapper">
  <resultMap id="BaseResultMap" type="com.zcxk.rmcp.core.entity.DeviceCommand">
    <!--@mbg.generated-->
    <!--@Table rmcp_device_command-->
    <id column="id" property="id" />
    <result column="tenant_id" property="tenantId" />
    <result column="device_id" property="deviceId" />
    <result column="command_type" property="commandType" />
    <result column="command_status" property="commandStatus" />
    <result column="operation_type" property="operationType" />
    <result column="remark" property="remark" />
    <result column="command_id" property="commandId" />
    <result column="issue_date" property="issueDate" />
    <result column="finish_date" property="finishDate" />
    <result column="params" property="params" />
    <result column="platform" property="platform" />
    <result column="relation_id" property="relationId" />
    <result column="push_status" property="pushStatus" />
    <result column="status" property="status" />
    <result column="create_date" property="createDate" />
    <result column="update_date" property="updateDate" />
    <result column="create_by" property="createBy" />
    <result column="update_by" property="updateBy" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    id, tenant_id, device_id, command_type, command_status, operation_type, remark, command_id, 
    issue_date, finish_date, params, platform, relation_id, push_status, `status`, create_date, 
    update_date, create_by, update_by
  </sql>
  <sql id="permissionCondition">
    <if test="userCondition.tenantId != null">and rd.tenant_id = #{userCondition.tenantId}</if>
    <if test="userCondition.userType!=null and userCondition.userType!=-999 and userCondition.userType!=-9999 and  userCondition.programItems != null and userCondition.programItems.size() > 0">
      <if test="userCondition.permissionType == 5 or userCondition.permissionType == 2">
        and ( rd.dept_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
        or
        rd.company_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
        )
      </if>
      <if test="userCondition.permissionType == 4 or userCondition.permissionType == 3">
        and rd.dept_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
      </if>
      <if test="userCondition.permissionType == 1">
        and rd.company_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
        and (rd.dept_org_id is null or rd.dept_org_id =0)
      </if>
    </if>
  </sql>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.zcxk.rmcp.core.entity.DeviceCommand" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into rmcp_device_command
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="tenantId != null">
        tenant_id,
      </if>
      <if test="deviceId != null">
        device_id,
      </if>
      <if test="commandType != null">
        command_type,
      </if>
      <if test="commandStatus != null">
        command_status,
      </if>
      <if test="operationType != null">
        operation_type,
      </if>
      <if test="remark != null">
        remark,
      </if>
      <if test="commandId != null">
        command_id,
      </if>
      <if test="issueDate != null">
        issue_date,
      </if>
      <if test="finishDate != null">
        finish_date,
      </if>
      <if test="params != null">
        params,
      </if>
      <if test="platform != null">
        platform,
      </if>
      <if test="relationId != null">
        relation_id,
      </if>
      <if test="pushStatus != null">
        push_status,
      </if>
      <if test="status != null">
        `status`,
      </if>
      <if test="createDate != null">
        create_date,
      </if>
      <if test="updateDate != null">
        update_date,
      </if>
      <if test="createBy != null">
        create_by,
      </if>
      <if test="updateBy != null">
        update_by,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="tenantId != null">
        #{tenantId},
      </if>
      <if test="deviceId != null">
        #{deviceId},
      </if>
      <if test="commandType != null">
        #{commandType},
      </if>
      <if test="commandStatus != null">
        #{commandStatus},
      </if>
      <if test="operationType != null">
        #{operationType},
      </if>
      <if test="remark != null">
        #{remark},
      </if>
      <if test="commandId != null">
        #{commandId},
      </if>
      <if test="issueDate != null">
        #{issueDate},
      </if>
      <if test="finishDate != null">
        #{finishDate},
      </if>
      <if test="params != null">
        #{params},
      </if>
      <if test="platform != null">
        #{platform},
      </if>
      <if test="relationId != null">
        #{relationId},
      </if>
      <if test="pushStatus != null">
        #{pushStatus},
      </if>
      <if test="status != null">
        #{status},
      </if>
      <if test="createDate != null">
        #{createDate},
      </if>
      <if test="updateDate != null">
        #{updateDate},
      </if>
      <if test="createBy != null">
        #{createBy},
      </if>
      <if test="updateBy != null">
        #{updateBy},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zcxk.rmcp.core.entity.DeviceCommand">
    <!--@mbg.generated-->
    update rmcp_device_command
    <set>
      <if test="tenantId != null">
        tenant_id = #{tenantId},
      </if>
      <if test="deviceId != null">
        device_id = #{deviceId},
      </if>
      <if test="commandType != null">
        command_type = #{commandType},
      </if>
      <if test="commandStatus != null">
        command_status = #{commandStatus},
      </if>
      <if test="operationType != null">
        operation_type = #{operationType},
      </if>
      <if test="remark != null">
        remark = #{remark},
      </if>
      <if test="commandId != null">
        command_id = #{commandId},
      </if>
      <if test="issueDate != null">
        issue_date = #{issueDate},
      </if>
      <if test="finishDate != null">
        finish_date = #{finishDate},
      </if>
      <if test="params != null">
        params = #{params},
      </if>
      <if test="platform != null">
        platform = #{platform},
      </if>
      <if test="relationId != null">
        relation_id = #{relationId},
      </if>
      <if test="pushStatus != null">
        push_status = #{pushStatus},
      </if>
      <if test="status != null">
        `status` = #{status},
      </if>
      <if test="createDate != null">
        create_date = #{createDate},
      </if>
      <if test="updateDate != null">
        update_date = #{updateDate},
      </if>
      <if test="createBy != null">
        create_by = #{createBy},
      </if>
      <if test="updateBy != null">
        update_by = #{updateBy},
      </if>
    </set>
    where id = #{id}
  </update>

  <select id="findDeviceCommand" resultType="com.zcxk.rmcp.api.vo.DeviceCommandVo">
    select
    dc.id,
    rd.meter_no,
    CONCAT(rm.name,"/",rp.product_name,"/",rp.product_model) as deviceNo,
    rd.device_status,
    dc.command_status,
    dc.command_type,
    dc.operation_type,
    dc.finish_date,
    dc.issue_date
    from rmcp_device_command dc
    left join rmcp_device rd  on (dc.device_id = rd.id)
    left join rmcp_product rp on (rp.id = rd.product_id)
    left join rmcp_manufacturer rm on (rm.id = rp.manufacturer_id)
    where dc.status = 1 and rd.status = 1
    and dc.id = #{id}
  </select>

  <select id="listDeviceCommand" resultType="com.zcxk.rmcp.api.vo.DeviceCommandVo">
    select
    dc.id,
    rd.meter_no,
    CONCAT(rm.name,"/",rp.product_name,"/",rp.product_model) as deviceNo,
    rd.device_status,
    dc.command_status,
    dc.command_type,
    dc.operation_type,
    dc.finish_date,
    dc.issue_date,
    dc.create_by
    from rmcp_device_command dc
    left join rmcp_device rd  on (dc.device_id = rd.id)
    left join rmcp_product rp on (rp.id = rd.product_id)
    left join rmcp_manufacturer rm on (rm.id = rp.manufacturer_id)
    where dc.status = 1 and rd.status = 1
    <if test="dto.meterNo != null and dto.meterNo != ''">and rd.meter_no = #{dto.meterNo}</if>
    <if test="dto.type != null">and dc.command_type = #{dto.type}</if>
    <if test="dto.startDate != null and dto.startDate != ''"> and dc.issue_date <![CDATA[ >= ]]> #{dto.startDate}</if>
    <if test="dto.endDate != null and dto.endDate != ''"> and dc.issue_date <![CDATA[ <= ]]> #{dto.endDate}</if>
    <include refid="permissionCondition"/>
  </select>

  <update id="updateStatus">
    update rmcp_device_command set status = #{status} where id in
    <foreach collection="ids" item="id" index="index"  open="(" separator="," close=")">
        #{id}
    </foreach>
  </update>

</mapper>