<?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.InstallPlanMapper">
  <resultMap id="BaseResultMap" type="com.zcxk.rmcp.core.entity.InstallPlan">
    <!--@mbg.generated-->
    <!--@Table rmcp_install_plan-->
    <id column="id" property="id" />
    <result column="tenant_id" property="tenantId" />
    <result column="plan_name" property="planName" />
    <result column="community_id" property="communityId" />
    <result column="company_org_id" property="companyOrgId" />
    <result column="dept_org_id" property="deptOrgId" />
    <result column="product_id" property="productId" />
    <result column="door_num" property="doorNum" />
    <result column="install_num" property="installNum" />
    <result column="accepted_num" property="acceptedNum" />
    <result column="unit" property="unit" />
    <result column="principal" property="principal" />
    <result column="phone" property="phone" />
    <result column="plan_status" property="planStatus" />
    <result column="accept_status" property="acceptStatus" />
    <result column="platform_type" property="platformType" />
    <result column="enable_unit" property="enableUnit" />
    <result column="file_path" property="filePath" />
    <result column="enable_download" property="enableDownload" />
    <result column="remark" property="remark" />
    <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, plan_name, community_id, company_org_id, dept_org_id, product_id, 
    door_num, install_num, accepted_num, unit, principal, phone, plan_status, accept_status, 
    platform_type, enable_unit, file_path, enable_download, remark, `status`, create_date, 
    update_date, create_by, update_by
  </sql>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.zcxk.rmcp.core.entity.InstallPlan" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into rmcp_install_plan
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="tenantId != null">
        tenant_id,
      </if>
      <if test="planName != null">
        plan_name,
      </if>
      <if test="communityId != null">
        community_id,
      </if>
      <if test="companyOrgId != null">
        company_org_id,
      </if>
      <if test="deptOrgId != null">
        dept_org_id,
      </if>
      <if test="productId != null">
        product_id,
      </if>
      <if test="doorNum != null">
        door_num,
      </if>
      <if test="installNum != null">
        install_num,
      </if>
      <if test="acceptedNum != null">
        accepted_num,
      </if>
      <if test="unit != null">
        unit,
      </if>
      <if test="principal != null">
        principal,
      </if>
      <if test="phone != null">
        phone,
      </if>
      <if test="planStatus != null">
        plan_status,
      </if>
      <if test="acceptStatus != null">
        accept_status,
      </if>
      <if test="platformType != null">
        platform_type,
      </if>
      <if test="enableUnit != null">
        enable_unit,
      </if>
      <if test="filePath != null">
        file_path,
      </if>
      <if test="enableDownload != null">
        enable_download,
      </if>
      <if test="remark != null">
        remark,
      </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="planName != null">
        #{planName},
      </if>
      <if test="communityId != null">
        #{communityId},
      </if>
      <if test="companyOrgId != null">
        #{companyOrgId},
      </if>
      <if test="deptOrgId != null">
        #{deptOrgId},
      </if>
      <if test="productId != null">
        #{productId},
      </if>
      <if test="doorNum != null">
        #{doorNum},
      </if>
      <if test="installNum != null">
        #{installNum},
      </if>
      <if test="acceptedNum != null">
        #{acceptedNum},
      </if>
      <if test="unit != null">
        #{unit},
      </if>
      <if test="principal != null">
        #{principal},
      </if>
      <if test="phone != null">
        #{phone},
      </if>
      <if test="planStatus != null">
        #{planStatus},
      </if>
      <if test="acceptStatus != null">
        #{acceptStatus},
      </if>
      <if test="platformType != null">
        #{platformType},
      </if>
      <if test="enableUnit != null">
        #{enableUnit},
      </if>
      <if test="filePath != null">
        #{filePath},
      </if>
      <if test="enableDownload != null">
        #{enableDownload},
      </if>
      <if test="remark != null">
        #{remark},
      </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.InstallPlan">
    <!--@mbg.generated-->
    update rmcp_install_plan
    <set>
      <if test="tenantId != null">
        tenant_id = #{tenantId},
      </if>
      <if test="planName != null">
        plan_name = #{planName},
      </if>
      <if test="communityId != null">
        community_id = #{communityId},
      </if>
      <if test="companyOrgId != null">
        company_org_id = #{companyOrgId},
      </if>
      <if test="deptOrgId != null">
        dept_org_id = #{deptOrgId},
      </if>
      <if test="productId != null">
        product_id = #{productId},
      </if>
      <if test="doorNum != null">
        door_num = #{doorNum},
      </if>
      <if test="installNum != null">
        install_num = #{installNum},
      </if>
      <if test="acceptedNum != null">
        accepted_num = #{acceptedNum},
      </if>
      <if test="unit != null">
        unit = #{unit},
      </if>
      <if test="principal != null">
        principal = #{principal},
      </if>
      <if test="phone != null">
        phone = #{phone},
      </if>
      <if test="planStatus != null">
        plan_status = #{planStatus},
      </if>
      <if test="acceptStatus != null">
        accept_status = #{acceptStatus},
      </if>
      <if test="platformType != null">
        platform_type = #{platformType},
      </if>
      <if test="enableUnit != null">
        enable_unit = #{enableUnit},
      </if>
      <if test="filePath != null">
        file_path = #{filePath},
      </if>
      <if test="enableDownload != null">
        enable_download = #{enableDownload},
      </if>
      <if test="remark != null">
        remark = #{remark},
      </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="findById" resultMap="BaseResultMap">
    select <include refid="Base_Column_List"/> from rmcp_install_plan where status = 1 and id = #{planId}
  </select>

  <select id="findList" resultType="com.zcxk.rmcp.api.vo.InstallPlanVo">
    select
    rip.id,
    rip.plan_name,
    (select count(1) from rmcp_install_list where rc.status = 1 and install_plan_id = rip.id) as door_num,
    (select count(1) from rmcp_install_list where rc.status = 1 and install_plan_id = rip.id and is_accepted = 1) as accepted_num,
    (select count(1) from rmcp_install_list where rc.status = 1 and install_plan_id = rip.id and is_installed = 1) as install_num,
    rip.create_date,
    rp.product_name,
    rp.product_model,
    rm.name as manufacturer_name,
    roc.ORG_NAME as company_name,
    rod.ORG_NAME as dept_name,
    rc.name as community_name,
    a1.name as province_name,
    a2.name as city_name,
    a3.name as region_name
    from rmcp_install_plan rip
    left join rmcp_product rp on (rp.id = rip.product_id)
    left join rmcp_manufacturer rm on (rm.id = rp.manufacturer_id)
    left join rmcp_org roc on (roc.ID = rip.company_org_id)
    left join rmcp_org rod on (rod.ID = rip.dept_org_id)
    left join rmcp_community rc on (rc.id = rip.community_id)
    left join rmcp_area a1 on a1.id = rc.province
    left join rmcp_area a2 on a2.id = rc.city
    left join rmcp_area a3 on a3.id = rc.region
    where rip.status = 1
    <if test="condition.name != null and condition.name != ''">and 1 = 1</if>
    <if test="condition.companyOrgId != null">and rip.company_org_id = #{condition.companyOrgId}</if>
    <if test="condition.deptOrgId != null">and rip.dept_org_id = #{condition.deptOrgId}</if>
    <if test="condition.acceptStatus != null">and rip.accept_status = #{condition.acceptStatus}</if>
    <if test="condition.planStatus != null">and rip.plan_status = #{condition.planStatus}</if>
    <include refid="permissionCondition"/>
  </select>

  <sql id="permissionCondition">
    <if test="userCondition.tenantId != null">and rip.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 ( rip.dept_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
        or
        rip.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 rip.dept_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
      </if>
      <if test="userCondition.permissionType == 1">
        and rip.company_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
        and (rip.dept_org_id is null or rip.dept_org_id =0)
      </if>
    </if>
  </sql>

  <update id="batchDelete">
    update rmcp_install_plan set status = 0,update_date = now()
    where status = 1
    and id in <foreach collection="ids" item="item" open="(" separator="," close=")">#{item}</foreach>
  </update>

  <select id="totalDeviceStatus" resultType="com.zcxk.rmcp.api.vo.StatisticsDeviceStatusVo">
    SELECT
    ifnull(SUM(IF(rd.`device_status` = 1, 1, 0)),0) as normal_count,
    ifnull(SUM(IF(rd.`device_status` = 2, 1, 0)),0) as offline_count,
    ifnull(SUM(IF(rd.`device_status` = 3, 1, 0)),0) as alarm_count,
    ifnull(SUM(IF(rd.`device_status` = 4, 1, 0)),0) as unused_count
    FROM
    rmcp_install_list ril
    INNER JOIN rmcp_device rd on rd.id = ril.device_id
    WHERE
    ril.status = 1 and rd.status = 1
    and ril.install_plan_id = #{planId}
  </select>

  <select id="findDetail" resultType="com.zcxk.rmcp.api.dto.install.InstallDetailDto">
    select
      rip.id,
      rip.plan_name,
      (select count(1) from rmcp_install_list where rc.status = 1 and install_plan_id = rip.id) as door_num,
      (select count(1) from rmcp_install_list where rc.status = 1 and install_plan_id = rip.id and is_accepted = 1) as accepted_num,
      (select count(1) from rmcp_install_list where rc.status = 1 and install_plan_id = rip.id and is_installed = 1) as install_num,
      rip.create_date,
      rp.product_name,
      rp.product_model,
      rm.name as manufacturer_name,
      roc.ORG_NAME as company_name,
      rod.ORG_NAME as dept_name,
      rc.name as community_name,
      a1.name as province_name,
      a2.name as city_name,
      a3.name as region_name,
      rip.enable_unit,
      rc.address,
      rc.lat,
      rc.lng
    from rmcp_install_plan rip
      left join rmcp_product rp on (rp.id = rip.product_id)
      left join rmcp_manufacturer rm on (rm.id = rp.manufacturer_id)
      left join rmcp_org roc on (roc.ID = rip.company_org_id)
      left join rmcp_org rod on (rod.ID = rip.dept_org_id)
      left join rmcp_community rc on (rc.id = rip.community_id)
      left join rmcp_area a1 on a1.id = rc.province
      left join rmcp_area a2 on a2.id = rc.city
      left join rmcp_area a3 on a3.id = rc.region
    where rip.status = 1 and rip.id = #{planId}
  </select>
</mapper>