package com.xhpc.order.controller;

import com.xhpc.common.api.PowerPileService;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.redis.service.RedisService;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.order.service.IXhpcChargeOrderService;
import com.xhpc.order.service.IXhpcStopChargingOrderService;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * program: ruoyi
 * User: HongYun
 * Date:2021-09-07 15
 */
@RestController
@RequestMapping(value = "/stopChargingOrder")
public class XhpcStopChargingOrderController extends BaseController {

    @Autowired
    private IXhpcStopChargingOrderService iXhpcStopChargingOrderService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private PowerPileService powerPileService;

    @Autowired
    private IXhpcChargeOrderService xhpcChargeOrderService;

    @Autowired
    private TokenService tokenService;

    @GetMapping("/list")
    public TableDataInfo list(Long userId, Long chargingStationId, Long terminalId){
        startPage();
        return getDataTable(iXhpcStopChargingOrderService.list(userId, chargingStationId, terminalId));
    }

    /**
     * 停止充电
     * @param serialNumber   终端编号
     * @return
     */
    @GetMapping("/stopUp")
    public AjaxResult stopUp(@RequestParam String serialNumber,@RequestParam Long chargingPileId){
        //查询缓存是否该终端在充电
        Map<String, Object> cacheMap = redisService.getCacheMap("gun:" + serialNumber);

        Map<String, Object> serialNumberOrder = xhpcChargeOrderService.getSerialNumberOrder(serialNumber);
        if(cacheMap !=null && serialNumberOrder!=null && serialNumberOrder.get("serialNumber") !=null){
            if(cacheMap.get("orderkey") !=null){
                String serialNumberOrderMap = serialNumberOrder.get("serialNumber").toString();

                String version ="0C";
                if(cacheMap.get("version") !=null){
                    version =cacheMap.get("version").toString();
                }
                System.out.println("=================111========停止充电命令已下发==================停止 充电=================="+serialNumberOrderMap);

                R r = powerPileService.stopCharging(serialNumberOrderMap, serialNumberOrderMap.substring(0,14), serialNumberOrderMap.substring(0,16), version);

                if(r.getCode()==200){
                    return AjaxResult.success("停止充电命令已下发");
                }else{
                    return AjaxResult.success(r.getMsg());
                }
            }
        }
        return AjaxResult.success("无车辆充电");
    }


    /**
     * 停止充电
     * @return
     */
    @GetMapping("/stopUpAll")
    public void stopUpAll(Long chargingStationId,Long chargingPileId){
        if(chargingStationId!=null || chargingPileId !=null ){
            List<Map<String,Object>> xhpcTerminalList =new ArrayList<>();
            LoginUser loginUser = tokenService.getLoginUser();
            SysUser sysUser = loginUser.getSysUser();
            Long sysUserId = sysUser.getUserId();
            if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
                startPage();
                if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                    Long logOperatorId = sysUser.getOperatorId();
                    //运营商看自己的场站
                    xhpcTerminalList= xhpcChargeOrderService.getXhpcTerminalList(chargingStationId, chargingPileId,1,logOperatorId);
                }else{
                    //查询赋值的场站
                    xhpcTerminalList= xhpcChargeOrderService.getXhpcTerminalList(chargingStationId, chargingPileId,2,sysUserId);
                }
            }else{
                xhpcTerminalList= xhpcChargeOrderService.getXhpcTerminalList(chargingStationId, chargingPileId,0,sysUserId);
            }

            for (int i = 0; i < xhpcTerminalList.size(); i++) {
                Map<String,Object> map = xhpcTerminalList.get(i);
                String serialNumber = map.get("serialNumber").toString();
                //查询缓存是否该终端在充电
                Map<String, Object> cacheMap = redisService.getCacheMap("gun:" + serialNumber);

                Map<String, Object> serialNumberOrder = xhpcChargeOrderService.getSerialNumberOrder(serialNumber);
                if(cacheMap !=null && serialNumberOrder!=null && serialNumberOrder.get("serialNumber") !=null){
                    if(cacheMap.get("orderkey") !=null){
                        String serialNumberOrderMap = serialNumberOrder.get("serialNumber").toString();
                        String version ="0C";
                        if(cacheMap.get("version") !=null){
                            version =cacheMap.get("version").toString();
                        }
                        powerPileService.stopCharging(serialNumberOrderMap, serialNumberOrderMap.substring(0,14), serialNumberOrderMap.substring(0,16), version);
                    }
                }
            }
        }
    }

    //小程序停止
    @GetMapping("/stopUpY")
    public AjaxResult stopUpY(@RequestParam String serialNumber){
        String version ="0C";
        R r = powerPileService.stopCharging(serialNumber, serialNumber.substring(0,14), serialNumber.substring(0,16), version);
        if(r.getCode()==200){
            return AjaxResult.success("停止充电命令已下发");
        }else{
            return AjaxResult.success(r.getMsg());
        }
    }
}
