<?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.jsowell.pile.mapper.PileBasicInfoMapper">
    
    <resultMap type="com.jsowell.pile.domain.PileBasicInfo" id="PileBasicInfoResult">
        <result property="id"    column="id"    />
        <result property="sn"    column="sn"    />
        <result property="name"    column="name"    />
        <result property="businessType"    column="business_type"    />
        <result property="secretKey"    column="secret_key"    />
        <result property="softwareProtocol"    column="software_protocol"    />
        <result property="productionDate"    column="production_date"    />
        <result property="licenceId"    column="licence_id"    />
        <result property="modelId"    column="model_id"    />
        <result property="simId"    column="sim_id"    />
        <result property="merchantId"    column="merchant_id"    />
        <result property="stationId"    column="station_id"    />
        <result property="faultReason"    column="fault_reason"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id, sn, name, business_type, secret_key, software_protocol, production_date, licence_id, model_id, sim_id,
        merchant_id, station_id, fault_reason, create_by, create_time, update_by, update_time, del_flag, remark
    </sql>

    <sql id="selectPileBasicInfoVo">
        select <include refid="Base_Column_List"/>
        from pile_basic_info
    </sql>

    <select id="selectPileBasicInfoList" parameterType="com.jsowell.pile.domain.PileBasicInfo" resultMap="PileBasicInfoResult">
        <include refid="selectPileBasicInfoVo"/>
        <where>  
            <if test="sn != null  and sn != ''"> and sn = #{sn}</if>
            <if test="name != null  and name != ''"> and name = #{name}</if>
            <if test="businessType != null  and businessType != ''"> and business_type = #{businessType}</if>
            <if test="secretKey != null  and secretKey != ''"> and secret_key = #{secretKey}</if>
            <if test="softwareProtocol != null  and softwareProtocol != ''"> and software_protocol = #{softwareProtocol}</if>
            <!--<if test="params.beginProductionDate != null and params.beginProductionDate != '' and params.endProductionDate != null and params.endProductionDate != ''"> and production_date between #{params.beginProductionDate} and #{params.endProductionDate}</if>
-->         <if test="licenceId != null "> and licence_id = #{licenceId}</if>
            <if test="modelId != null "> and model_id = #{modelId}</if>
            <if test="simId != null "> and sim_id = #{simId}</if>
            <if test="merchantId != null "> and merchant_id = #{merchantId}</if>
            <if test="stationId != null "> and station_id = #{stationId}</if>
            <if test="faultReason != null  and faultReason != ''"> and fault_reason = #{faultReason}</if>
        </where>
    </select>
    
    <select id="selectPileBasicInfoById" parameterType="Long" resultMap="PileBasicInfoResult">
        <include refid="selectPileBasicInfoVo"/>
        where id = #{id}
    </select>

    <select id="selectPileBasicInfoBySn" resultMap="PileBasicInfoResult">
        select <include refid="Base_Column_List"/>
        from pile_basic_info
        where sn = #{pileSn,jdbcType=VARCHAR}
    </select>
        
    <insert id="insertPileBasicInfo" parameterType="com.jsowell.pile.domain.PileBasicInfo" useGeneratedKeys="true" keyProperty="id">
        insert into pile_basic_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sn != null">sn,</if>
            <if test="name != null">name,</if>
            <if test="businessType != null">business_type,</if>
            <if test="secretKey != null">secret_key,</if>
            <if test="softwareProtocol != null">software_protocol,</if>
            <if test="productionDate != null">production_date,</if>
            <if test="licenceId != null">licence_id,</if>
            <if test="modelId != null">model_id,</if>
            <if test="simId != null">sim_id,</if>
            <if test="merchantId != null">merchant_id,</if>
            <if test="stationId != null">station_id,</if>
            <if test="faultReason != null">fault_reason,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sn != null">#{sn},</if>
            <if test="name != null">#{name},</if>
            <if test="businessType != null">#{businessType},</if>
            <if test="secretKey != null">#{secretKey},</if>
            <if test="softwareProtocol != null">#{softwareProtocol},</if>
            <if test="productionDate != null">#{productionDate},</if>
            <if test="licenceId != null">#{licenceId},</if>
            <if test="modelId != null">#{modelId},</if>
            <if test="simId != null">#{simId},</if>
            <if test="merchantId != null">#{merchantId},</if>
            <if test="stationId != null">#{stationId},</if>
            <if test="faultReason != null">#{faultReason},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updatePileBasicInfo" parameterType="com.jsowell.pile.domain.PileBasicInfo">
        update pile_basic_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="sn != null">sn = #{sn},</if>
            <if test="name != null">name = #{name},</if>
            <if test="businessType != null">business_type = #{businessType},</if>
            <if test="secretKey != null">secret_key = #{secretKey},</if>
            <if test="softwareProtocol != null">software_protocol = #{softwareProtocol},</if>
            <if test="productionDate != null">production_date = #{productionDate},</if>
            <if test="licenceId != null">licence_id = #{licenceId},</if>
            <if test="modelId != null">model_id = #{modelId},</if>
            <if test="simId != null">sim_id = #{simId},</if>
            <if test="merchantId != null">merchant_id = #{merchantId},</if>
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="faultReason != null">fault_reason = #{faultReason},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePileBasicInfoById" parameterType="Long">
        delete from pile_basic_info where id = #{id}
    </delete>

    <delete id="deletePileBasicInfoByIds" parameterType="String">
        delete from pile_basic_info where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="queryPileInfos" resultType="com.jsowell.pile.vo.web.PileDetailVO">
        SELECT
            t1.id as pileId,
            t1.sn as pileSn,
            t1.business_type as businessType,
            t1.station_id AS stationId,
            t3.station_name as stationName,
            t1.sim_id,
            t1.secret_key as secretKey,
            t5.iccid as ICCID,
            t5.sim_supplier as simSupplier,
            t1.merchant_id as merchantId,
            t2.merchant_name as merchantName,
            count(t4.id)as gunNum,
            t1.create_time as registrationTime,
            IF(t6.charger_pile_type = '2','3',(IF(t6.speed_type = '1','1','2'))) AS pileType
        FROM
            pile_basic_info t1
        LEFT JOIN pile_merchant_info t2 ON (t2.id = t1.merchant_id and t2.del_flag = '0')
        LEFT JOIN pile_station_info t3 ON (t3.id = t1.station_id and t3.del_flag = '0')
        LEFT JOIN pile_connector_info t4 on (t4.pile_sn = t1.sn and t4.del_flag = '0')
        LEFT JOIN pile_sim_info t5 on (t5.id = t1.sim_id and t5.del_flag = '0')
        LEFT join pile_model_info t6 on t6.id = t1.model_id
        where t1.del_flag = '0'
        <if test="dto.pileSn != null and dto.pileSn != ''">
            and (t1.sn = #{dto.pileSn,jdbcType=VARCHAR} or t1.sn like concat('%', #{dto.pileSn,jdbcType=VARCHAR}, '%'))
        </if>
        <if test="dto.stationId != null and dto.stationId != ''">
            and t1.station_Id = #{dto.stationId,jdbcType=VARCHAR}
        </if>
        <if test="dto.pileSns != null and dto.pileSns.size() != 0">
            and t1.sn in
            <foreach collection="dto.pileSns" item="item" open="(" close=")" separator=",">
                #{item,jdbcType=VARCHAR}
            </foreach>
        </if>
        <!-- 数据范围过滤 -->
        <if test="dto.merchantDeptIds != null and dto.merchantDeptIds.size() != 0">
            and t2.dept_id in
            <foreach collection="dto.merchantDeptIds" item="merchantDeptId" open="(" separator="," close=")">
                #{merchantDeptId}
            </foreach>
        </if>
        <if test="dto.stationDeptIds != null and dto.stationDeptIds.size() != 0">
            and t3.dept_id in
            <foreach collection="dto.stationDeptIds" item="stationDeptId" open="(" separator="," close=")">
                #{stationDeptId}
            </foreach>
        </if>
        group by t1.id
        order by t1.sn
    </select>

    <insert id="batchInsertPileBasicInfo">
        insert into pile_basic_info
            (sn, name, business_type, secret_key, software_protocol, production_date, licence_id, model_id, sim_id,
            merchant_id, station_id, fault_reason, create_by, update_by, del_flag, remark)
        values
        <foreach collection="infoList" item="item" separator=",">
            (
                #{item.sn,jdbcType=VARCHAR},
                #{item.name,jdbcType=VARCHAR},
                #{item.businessType,jdbcType=VARCHAR},
                #{item.secretKey,jdbcType=VARCHAR},
                #{item.softwareProtocol,jdbcType=VARCHAR},
                #{item.productionDate,jdbcType=TIMESTAMP},
                #{item.licenceId,jdbcType=BIGINT},
                #{item.modelId,jdbcType=BIGINT},
                #{item.simId,jdbcType=BIGINT},
                #{item.merchantId,jdbcType=BIGINT},
                #{item.stationId,jdbcType=BIGINT},
                #{item.faultReason,jdbcType=VARCHAR},
                #{item.createBy,jdbcType=VARCHAR},
                #{item.updateBy,jdbcType=VARCHAR},
                #{item.delFlag,jdbcType=VARCHAR},
                #{item.remark,jdbcType=VARCHAR}
            )
        </foreach>
    </insert>

    <update id="replaceMerchantStationByPileIds">
        update pile_basic_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="merchantId != null">merchant_id = #{merchantId},</if>
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="chargerPileType != null">
                business_type = #{chargerPileType,jdbcType=VARCHAR},
            </if>
            <if test="chargerPileType == '1'.toString() "> secret_key = null,</if>
            <if test="chargerPileType == '2'.toString() "> secret_key = substring(uuid(), 1, 8),</if>
            <if test="modelId != null">model_id = #{modelId},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id in
        <foreach collection="pileIdList" item="pileId" open="(" separator="," close=")">
            #{pileId,jdbcType=BIGINT}
        </foreach>
    </update>

    <select id="selectBasicInfoById" resultType="com.jsowell.pile.vo.web.PileDetailVO">
        SELECT
            t1.id as pileId,
            t1.sn AS pileSn,
            t1.name as name,
            t1.merchant_id AS merchantId,
            t2.merchant_name AS merchantName,
            t1.station_id AS stationId,
            t3.station_name AS stationName,
            t1.model_id AS modelId,
            t4.rated_power AS ratedPower,
            t4.rated_current AS ratedCurrent,
            t1.sim_id AS simId,
            t5.iccid AS iccid ,
            t5.sim_supplier as simSupplier,
            t5.operator
        FROM
            pile_basic_info t1
        left JOIN pile_merchant_info t2 ON t1.merchant_id = t2.id
        left JOIN pile_station_info t3 ON t1.station_id = t3.id
        left JOIN pile_model_info t4 ON t1.model_id = t4.id
        left JOIN pile_sim_info t5 ON t1.sim_id = t5.id
        WHERE
            t1.del_flag = '0'
        and t1.id = #{id}
    </select>
    
    <select id="selectPileListByStationIds" resultType="com.jsowell.pile.vo.web.PileDetailVO">
        select 
            t1.id as pileId,
            t1.sn as pileSn
        from
            pile_basic_info t1
        where
            t1.del_flag = '0'
        <if test="stationIds != null and stationIds.size() != 0">
            and t1.station_id in
            <foreach collection="stationIds" item="stationId" open="(" separator="," close=")">
                #{stationId,jdbcType=BIGINT}
            </foreach>
        </if>
    </select>

    <select id="selectByIdList" resultMap="PileBasicInfoResult">
        <include refid="selectPileBasicInfoVo"/>
        where id in
        <foreach collection="pileIdList" item="pileId" open="(" separator="," close=")">
            #{pileId,jdbcType=BIGINT}
        </foreach>
    </select>

    <select id="queryPileConnectorDetail" resultType="com.jsowell.pile.vo.uniapp.customer.PileConnectorDetailVO">
        SELECT
            t1.id AS pileId,
            t1.sn AS pileSn,
            t1.merchant_id as merchantId,
            t1.station_id AS stationId,
            t2.id AS connectorId,
            t2.pile_connector_code AS pileConnectorCode,
            t2.status AS connectorStatus,
            t1.business_type AS businessType,
            t1.software_protocol AS softwareProtocol
        FROM
            pile_basic_info t1
        JOIN pile_connector_info t2 ON t1.sn = t2.pile_sn
        WHERE
            t2.pile_connector_code = #{pileConnectorCode,jdbcType=VARCHAR}
    </select>

    <select id="getPileInfoByMemberId" resultType="com.jsowell.pile.vo.uniapp.customer.PersonalPileInfoVO">
        SELECT
        t1.pile_sn as pileSn,
        t1.member_id as memberId,
        t1.type,
        t3.model_name as modelName,
        t3.rated_power as ratedPower,
        t3.rated_current as ratedCurrent,
        t3.rated_voltage as ratedVoltage,
        t3.speed_type as speedType,
        count(t4.id) as connectorNum
        FROM
        pile_member_relation t1
        JOIN pile_basic_info t2 ON t1.pile_sn = t2.sn
        JOIN pile_model_info t3 ON t2.model_id = t3.id
        JOIN pile_connector_info t4 on t2.sn = t4.pile_sn
        WHERE
        t1.member_id = #{memberId,jdbcType=VARCHAR}
        group by
        t1.pile_sn,
        t1.member_id,
        t1.type,
        t3.model_name,
        t3.rated_power,
        t3.rated_current,
        t3.rated_voltage,
        t3.speed_type
    </select>

    <select id="getPileListByStationId" resultMap="PileBasicInfoResult">
        select <include refid="Base_Column_List"/>
        from pile_basic_info
        where station_id = #{stationId,jdbcType=VARCHAR}
    </select>

    <select id="queryPileDetailList" resultType="com.jsowell.pile.vo.base.PileInfoVO">
        SELECT
            t1.id as pileId,
            t1.sn AS pileSn,
            t1.merchant_id as merchantId,
            t1.station_id as stationId,
            t2.model_name as modelName,
            t2.rated_power as ratedPower,
            t2.rated_current as ratedCurrent,
            t2.rated_voltage as ratedVoltage,
            t2.speed_type as speedType,
            t3.pile_connector_code as pileConnectorCode
        FROM
            pile_basic_info t1
            LEFT JOIN pile_model_info t2 on t1.model_id = t2.id
            LEFT JOIN pile_connector_info t3 on t1.sn = t3.pile_sn
        WHERE
            t1.station_id in 
            <foreach collection="stationIdList" item="item" open="(" separator="," close=")">
                #{item,jdbcType=VARCHAR}
            </foreach>
    </select>

    <update id="updatePileMerchantBatch">
        update
            pile_basic_info
        set
            merchant_id = #{newMerchantId,jdbcType=VARCHAR}
        where
            id in
        <foreach collection="pileIdList" item="item" open="(" separator="," close=")">
            #{item,jdbcType=BIGINT}
        </foreach>
    </update>

    <select id="getGeneralSituationInfo" resultType="com.jsowell.pile.vo.web.IndexGeneralSituationVO">
        select
            ifnull(sum(t1.total_amount), 0) as totalChargingAmount,
            ifnull(sum(t1.use_electricity), 0) as totalChargingDegree,
            ifnull(sum(t1.charge_num), 0) as totalChargingQuantity
        from
            settle_order_report t1
        where
            t1.del_flag = '0'
        <if test="IndexQueryDTO.stationId != null and IndexQueryDTO.stationId != ''">
            and t1.station_id = #{IndexQueryDTO.stationId,jdbcType=VARCHAR}
        </if>
        <!-- 数据范围过滤 -->
        <if test="IndexQueryDTO.stationIdList != null and IndexQueryDTO.stationIdList.size() != 0">
            and t1.station_id in
            <foreach collection="IndexQueryDTO.stationIdList" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="getMaxNumPileInfo" resultMap="PileBasicInfoResult">
        select
            id,
            sn
        from
            pile_basic_info
        order by
            id desc
        limit 1
    </select>
</mapper>