<?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.MeterReplaceLogMapper">
  <resultMap id="BaseResultMap" type="com.zcxk.rmcp.core.entity.MeterReplaceLog">
    <!--@mbg.generated-->
    <!--@Table rmcp_meter_replace_log-->
    <id column="id" property="id" />
    <result column="tenant_id" property="tenantId" />
    <result column="device_id" property="deviceId" />
    <result column="replace_method" property="replaceMethod" />
    <result column="new_product_id" property="newProductId" />
    <result column="old_product_id" property="oldProductId" />
    <result column="new_meter_no" property="newMeterNo" />
    <result column="old_meter_no" property="oldMeterNo" />
    <result column="new_begin_degree" property="newBeginDegree" />
    <result column="old_end_degree" property="oldEndDegree" />
    <result column="new_device_no" property="newDeviceNo" />
    <result column="old_device_no" property="oldDeviceNo" />
    <result column="remark" property="remark" />
    <result column="status" property="status" />
    <result column="replace_username" property="replaceUsername" />
    <result column="replace_user_phone" property="replaceUserPhone" />
    <result column="sync_status" property="syncStatus" />
    <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, replace_method, new_product_id, old_product_id, new_meter_no, 
    old_meter_no, new_begin_degree, old_end_degree, new_device_no, old_device_no, remark, 
    `status`, replace_username, replace_user_phone, sync_status, create_date, update_date,
    create_by, update_by
  </sql>
  <sql id="permissionCondition">
    <if test="userCondition.tenantId != null">and d.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 ( d.dept_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
        or
        d.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 d.dept_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
      </if>
      <if test="userCondition.permissionType == 1">
        and d.company_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
        and (d.dept_org_id is null or d.dept_org_id =0)
      </if>
    </if>
  </sql>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.zcxk.rmcp.core.entity.MeterReplaceLog" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into rmcp_meter_replace_log
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="tenantId != null">
        tenant_id,
      </if>
      <if test="deviceId != null">
        device_id,
      </if>
      <if test="replaceMethod != null">
        replace_method,
      </if>
      <if test="newProductId != null">
        new_product_id,
      </if>
      <if test="oldProductId != null">
        old_product_id,
      </if>
      <if test="newMeterNo != null">
        new_meter_no,
      </if>
      <if test="oldMeterNo != null">
        old_meter_no,
      </if>
      <if test="newBeginDegree != null">
        new_begin_degree,
      </if>
      <if test="oldEndDegree != null">
        old_end_degree,
      </if>
      <if test="newDeviceNo != null">
        new_device_no,
      </if>
      <if test="oldDeviceNo != null">
        old_device_no,
      </if>
      <if test="remark != null">
        remark,
      </if>
      <if test="status != null">
        `status`,
      </if>
      <if test="replaceUsername != null">
        replace_username,
      </if>
      <if test="replaceUserPhone != null">
        replace_user_phone,
      </if>
      <if test="syncStatus != null">
        sync_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="replaceMethod != null">
        #{replaceMethod},
      </if>
      <if test="newProductId != null">
        #{newProductId},
      </if>
      <if test="oldProductId != null">
        #{oldProductId},
      </if>
      <if test="newMeterNo != null">
        #{newMeterNo},
      </if>
      <if test="oldMeterNo != null">
        #{oldMeterNo},
      </if>
      <if test="newBeginDegree != null">
        #{newBeginDegree},
      </if>
      <if test="oldEndDegree != null">
        #{oldEndDegree},
      </if>
      <if test="newDeviceNo != null">
        #{newDeviceNo},
      </if>
      <if test="oldDeviceNo != null">
        #{oldDeviceNo},
      </if>
      <if test="remark != null">
        #{remark},
      </if>
      <if test="status != null">
        #{status},
      </if>
      <if test="replaceUsername != null">
        #{replaceUsername},
      </if>
      <if test="replaceUserPhone != null">
        #{replaceUserPhone},
      </if>
      <if test="syncStatus != null">
        #{syncStatus},
      </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.MeterReplaceLog">
    <!--@mbg.generated-->
    update rmcp_meter_replace_log
    <set>
      <if test="tenantId != null">
        tenant_id = #{tenantId},
      </if>
      <if test="deviceId != null">
        device_id = #{deviceId},
      </if>
      <if test="replaceMethod != null">
        replace_method = #{replaceMethod},
      </if>
      <if test="newProductId != null">
        new_product_id = #{newProductId},
      </if>
      <if test="oldProductId != null">
        old_product_id = #{oldProductId},
      </if>
      <if test="newMeterNo != null">
        new_meter_no = #{newMeterNo},
      </if>
      <if test="oldMeterNo != null">
        old_meter_no = #{oldMeterNo},
      </if>
      <if test="newBeginDegree != null">
        new_begin_degree = #{newBeginDegree},
      </if>
      <if test="oldEndDegree != null">
        old_end_degree = #{oldEndDegree},
      </if>
      <if test="newDeviceNo != null">
        new_device_no = #{newDeviceNo},
      </if>
      <if test="oldDeviceNo != null">
        old_device_no = #{oldDeviceNo},
      </if>
      <if test="remark != null">
        remark = #{remark},
      </if>
      <if test="status != null">
        `status` = #{status},
      </if>
      <if test="replaceUsername != null">
        replace_username = #{replaceUsername},
      </if>
      <if test="replaceUserPhone != null">
        replace_user_phone = #{replaceUserPhone},
      </if>
      <if test="syncStatus != null">
        sync_status = #{syncStatus},
      </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="findMeterReplace" resultType="com.zcxk.rmcp.api.vo.MeterReplaceVo">
    select
    rl.id,
    rl.new_device_no,
    rl.old_device_no,
    rl.new_meter_no,
    rl.old_meter_no,
    rl.new_product_id,
    rl.old_product_id,
    d.file_no,
    rl.new_begin_degree,
    rl.old_end_degree,
    rl.replace_method,
    rl.replace_username,
    rl.replace_user_phone
    from rmcp_meter_replace_log rl
    left join rmcp_device d on(d.id = rl.device_id)
    left join rmcp_product sdt1 on(sdt1.id = rl.new_product_id)
    left join rmcp_manufacturer sdm1 on(sdm1.id = sdt1.manufacturer_id)
    left join rmcp_product sdt2 on(sdt2.id = rl.old_product_id)
    left join rmcp_manufacturer sdm2 on(sdm2.id = sdt2.manufacturer_id)
    where rl.status = 1 and d.status = 1
    and rl.id =#{id}
  </select>
  <select id="listMeterReplace" resultType="com.zcxk.rmcp.api.vo.MeterReplaceVo">
    select
    rl.id,
    rl.new_device_no,
    rl.old_device_no,
    rl.new_meter_no,
    rl.old_meter_no,
    rl.new_product_id,
    rl.old_product_id,
    d.file_no,
    rl.new_begin_degree,
    rl.old_end_degree,
    rl.replace_method,
    rl.replace_username,
    rl.replace_user_phone,
    rl.create_date as "dateCreate"
    from rmcp_meter_replace_log rl
    left join rmcp_device d on(d.id = rl.device_id)
    left join rmcp_product sdt1 on(sdt1.id = rl.new_product_id)
    left join rmcp_manufacturer sdm1 on(sdm1.id = sdt1.manufacturer_id)
    left join rmcp_product sdt2 on(sdt2.id = rl.old_product_id)
    left join rmcp_manufacturer sdm2 on(sdm2.id = sdt2.manufacturer_id)
    where rl.status = 1 and d.status = 1
    <if test="dto.deviceNo != null and dto.deviceNo != ''"> and (
      rl.new_meter_no LIKE concat('%',#{dto.deviceNo},'%') or rl.old_meter_no LIKE concat('%',#{dto.deviceNo},'%') or
      d.file_no LIKE concat('%',#{dto.deviceNo},'%'))
    </if>
    <if test="dto.replaceMethod != null"> and rl.replace_method = #{dto.replaceMethod}</if>
    <if test="dto.startDate != null and dto.startDate!= ''"> and rl.create_date <![CDATA[ >= ]]> #{dto.startDate}</if>
    <if test="dto.endDate != null and dto.endDate != ''"> and rl.create_date <![CDATA[ <= ]]> #{dto.endDate}</if>
    <include refid="permissionCondition"/>
  </select>

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