package com.jsowell.netty.service.yunkuaichong.impl;

import com.jsowell.common.core.domain.ykc.YKCDataProtocol;
import com.jsowell.common.core.domain.ykc.YKCFrameTypeCode;
import com.jsowell.common.enums.ykc.PileChannelEntity;
import com.jsowell.common.enums.ykc.PileConnectorDataBaseStatusEnum;
import com.jsowell.common.util.StringUtils;
import com.jsowell.common.util.YKCUtils;
import com.jsowell.netty.factory.YKCOperateFactory;
import com.jsowell.netty.handler.AbstractHandler;
import com.jsowell.netty.service.yunkuaichong.YKCBusinessService;
import com.jsowell.pile.service.YKCPushCommandService;
import com.jsowell.pile.service.IOrderBasicInfoService;
import com.jsowell.pile.service.IPileConnectorInfoService;
import com.jsowell.pile.service.IPileMsgRecordService;
import io.netty.channel.Channel;
import io.netty.channel.ChannelId;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class YKCBusinessServiceImpl implements YKCBusinessService {

    @Autowired
    private IPileMsgRecordService pileMsgRecordService;

    @Autowired
    private IPileConnectorInfoService pileConnectorInfoService;

    @Autowired
    private IOrderBasicInfoService orderBasicInfoService;

    @Autowired
    private YKCPushCommandService ykcPushCommandService;

    @Override
    public byte[] process(byte[] msg, Channel channel) {
        if (!YKCUtils.checkMsg(msg)) {
            // 校验不通过，丢弃消息
            return null;
        }
        YKCDataProtocol ykcDataProtocol = new YKCDataProtocol(msg);
        // 获取帧类型
        String frameType = YKCUtils.frameType2Str(ykcDataProtocol.getFrameType());
        // 获取业务处理handler
        AbstractHandler invokeStrategy = YKCOperateFactory.getInvokeStrategy(frameType);
        return invokeStrategy.supplyProcess(ykcDataProtocol, channel);
    }

    @Override
    public void exit(ChannelId channelId) {
        // 获取桩编号
        String pileSn = PileChannelEntity.getPileSnByChannelId(channelId.asLongText());
        if (StringUtils.isBlank(pileSn)) {
            return;
        }
        log.info("充电桩退出:{}, channelId:{}", pileSn, PileChannelEntity.getChannelByPileSn(pileSn).id());

        // 充电桩断开连接，所有枪口都设置为【离线】
        pileConnectorInfoService.updateConnectorStatusByPileSn(pileSn, PileConnectorDataBaseStatusEnum.OFF_NETWORK.getValue());

        // 将此桩正在进行充电的订单状态改为 异常
        orderBasicInfoService.updateOrderStatusAsAbnormal(pileSn);
        // List<OrderListVO> orderListVOS = orderBasicInfoService.selectChargingOrder(pileSn);
        // if (CollectionUtils.isNotEmpty(orderListVOS)) {
        //     for (OrderListVO orderListVO : orderListVOS) {
        //         if (StringUtils.equals(orderListVO.getOrderStatus(), OrderStatusEnum.IN_THE_CHARGING.getValue())) {
        //             // 修改数据库订单状态
        //             OrderBasicInfo info = OrderBasicInfo.builder()
        //                     .id(Long.parseLong(orderListVO.getId()))
        //                     .orderStatus(OrderStatusEnum.ABNORMAL.getValue())
        //                     .build();
        //             orderBasicInfoService.updateOrderBasicInfo(info);
        //             log.info("充电桩:{}退出, 修改充电桩正在充电的订单状态为异常, orderCode: {}", pileSn, orderListVO.getOrderCode());
        //         }
        //     }
        // }

        // 记录充电桩退出msg
        // 保存报文
        String type = YKCFrameTypeCode.PILE_LOG_OUT.getCode() + "";
        String jsonMsg = YKCFrameTypeCode.PILE_LOG_OUT.getValue();
        pileMsgRecordService.save(pileSn, pileSn, type, jsonMsg, "");

        // 自动重启 发送重启指令
        // RebootCommand command = RebootCommand.builder().pileSn(pileSn).build();
        // ykcPushCommandService.pushRebootCommand(command);

        // 删除桩编号和channel的关系
        PileChannelEntity.removeByPileSn(pileSn);
    }

}
