<?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.unis.safeStorage.modular.device.mapper.DeviceMapper">

    <resultMap id="BaseResultMap" type="com.unis.safeStorage.modular.device.entity.Devicerepair">
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="org_id" property="orgId" jdbcType="INTEGER" />
        <result column="device_name" property="deviceName" jdbcType="VARCHAR" />
        <result column="model" property="model" jdbcType="VARCHAR" />
        <result column="number" property="number" jdbcType="VARCHAR" />
        <result column="repair_start" property="repairStart" jdbcType="TIMESTAMP" />
        <result column="repair_end" property="repairEnd" jdbcType="TIMESTAMP" />
        <result column="repairer" property="repairer" jdbcType="VARCHAR" />
        <result column="device_manager" property="deviceManager" jdbcType="VARCHAR" />
        <result column="repair_sumnumber" property="repairSumnumber" jdbcType="INTEGER" />
        <result column="repair_status" property="repairStatus" jdbcType="INTEGER" />
        <result column="repair_number" property="repairNumber" jdbcType="INTEGER" />
        <result column="remark" property="remark" jdbcType="VARCHAR" />
        <result column="handle_status" property="handleStatus" jdbcType="INTEGER" />
    </resultMap>

    <sql id="Base_Column_List">
        id, org_id, device_name, model, number, repair_start, repair_end, repairer, device_manager,
        repair_sumnumber, repair_status, repair_number, remark,handle_status
    </sql>

    <select id="findPage" resultType="java.util.Map">
        SELECT
            a.id,
            b.org_name orgName,
            a.device_name deviceName,
            a.repairer repairer,
            a.handle_status handleStatus,
            case a.handle_status when '0' then '未申请' when '1' then '未处理' when '2' then '已处理' end handleStatusName, -- 0：未申请；1：已申请（未处理）；2：已处理；
            a.model model,
            a.number number,
            DATE_FORMAT(a.repair_start,'%Y-%m-%d %H:%i:%s') repairStart,
            DATE_FORMAT(a.repair_end,'%Y-%m-%d %H:%i:%s') repairEnd,
            case a.repair_status when '0' then '正常' when '1' then '维修中' when '2' then '故障' end repairStatus   -- 0：正常，1：维修中，2：故障
        FROM
         depot_qh.storage_devicerepair a
         LEFT JOIN depot_qh.org_info b ON a.org_id = b.org_id
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            where  ${ew.sqlSegment}
        </if>
        order  by a.id desc
    </select>

    <select id="loadDataById" parameterType="java.lang.String" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from depot_qh.storage_devicerepair
        where id = #{id}
    </select>

    <update id="updateById" parameterType="java.util.Map" >
        update depot_qh.storage_devicerepair set handle_status = #{handleStatus} -- 处理状态 0：未申请；1：已申请（未处理）；2：已处理
        where id = #{id}
    </update>

    <select id="getwxsbzs" resultType="java.lang.String">
        select sum(repair_number) repairNumber from depot_qh.storage_devicerepair
    </select>

    <select id="getwxzsbsl" resultType="java.lang.String">
        select IFNULL(sum(repair_number),0) repairNumber from depot_qh.storage_devicerepair where repair_status = 1 -- 维修状态（0：正常，1：维修中，2：故障）
    </select>

    <select id="getsjclts" resultType="java.lang.String">
        select IFNULL(sum(repair_number),0) repairNumber from depot_qh.storage_devicerepair where handle_status = 2 -- 处理状态 0：未申请；1：已申请（未处理）；2：已处理；
    </select>
</mapper>