package com.xhpc.order.api;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.xhpc.common.api.WebSocketService;
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.order.service.IXhpcChargeOrderService;
import com.xhpc.order.service.IXhpcHistoryOrderService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.math.BigDecimal;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yuyang
 * @date 2021/8/4 9:59
 */
@RestController
@RequestMapping("/api/chargeOrder")
@Api(value = "充电订单接口", tags = "充电订单接口")
public class XhpcChargeOrderController extends BaseController {

    @Autowired
    private IXhpcChargeOrderService iXhpcChargeOrderService;
    @Autowired
    private IXhpcHistoryOrderService xhpcHistoryOrderService;
    @Autowired
    private WebSocketService webSocketService;

    private static final Logger logger = LoggerFactory.getLogger(XhpcChargeOrderController.class);
    /**
     * 实时订单(微信)
     */
    @GetMapping("/getHistotyChargeOrderMessage")
    public AjaxResult getHistotyChargeOrderMessage(@RequestParam Long userId)
    {

        return iXhpcChargeOrderService.getHistotyChargeOrderMessage(userId);
    }

    /**
     * 异常订单(微信)
     */
    @GetMapping("/getHistotyChargeOrderStatusList")
    public TableDataInfo getHistotyChargeOrderStatusList(@RequestParam Long userId)
    {
        startPage();
        List<Map<String, Object>> list = iXhpcChargeOrderService.getHistotyChargeOrderStatusList(userId);
        return getDataTable(list);
    }



    /**
     * 启动充电
     * @param userId
     * @param serialNumber   终端编码
     * @param type           1 微信 2支付宝
     * @return
     */
    @GetMapping("/startUp")
    public AjaxResult startUp(@RequestParam Long userId,@RequestParam String serialNumber,@RequestParam Integer type){
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<启动充电>>>>>>>>>>>>>>>>>");
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<启动充电>>>>>>>>>>>>>>>>>");
        return iXhpcChargeOrderService.startUp(userId, serialNumber, type);
    }


    /**
     * 停止充电
     * @param userId
     * @param serialNumber   终端编码
     * @param chargingOrderId   充电id
     * @return
     */
    @GetMapping("/stopUp")
    public AjaxResult stopUp(@RequestParam Long userId,@RequestParam String serialNumber,@RequestParam Long chargingOrderId){
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<停止充电>>>>>>>>>>>>>>>>>");
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<停止充电>>>>>>>>>>>>>>>>>");
        return iXhpcChargeOrderService.stopUp(userId, serialNumber, chargingOrderId);
    }


    /**
     * 用户历史订单列表接口
     */
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam Long userId)
    {
        startPage();
        List<Map<String,Object>> list = xhpcHistoryOrderService.list(userId);
        return getDataTable(list);
    }


    /**
     * 用户订单详情接口
     * @param userId                 用户id
     * @param historyOrderId         历史订单id
     * @param type                   1 历史订单id 2 充电订单id (暂时没用)
     * @param chargingOrderId        充电订单id
     * @return
     */
    @GetMapping("/gethistotyOrderMessage")
    public AjaxResult gethistotyOrderMessage(@RequestParam Long userId,Long historyOrderId,Integer type,Long chargingOrderId)
    {
        return xhpcHistoryOrderService.gethistotyOrderMessage(userId,historyOrderId,type,chargingOrderId);
    }


    /**
     * 实时数据接口
     * @param userId                 用户id
     * @return
     */
    @GetMapping("/getOrderMessage")
    public void getOrderMessage(@RequestParam Long userId)
    {
        logger.info("<<<<<<<<<<再次<<<<<<<<<<<<<<实时数据接口>>>>>>>>>>>>>>>>>");

        Map<String, Object> orderMessage = iXhpcChargeOrderService.getOrderMessage(userId);
        Map<String,Object> map =new HashMap<>();
        map.put("code", 200);
        map.put("message", "实时数据");
        map.put("userId", userId);
        if(orderMessage ==null){
            Map<String, Object> data = new HashMap<>();
            BigDecimal decimal = new BigDecimal(0);
            data.put("amountCharged",decimal);
            data.put("gunNumber","0");
            data.put("chargingOrderId",0L);
            data.put("soc",0);
            data.put("chargingTime","0");
            data.put("electricCurrent",decimal);
            data.put("voltage", decimal);
            data.put("power", "0");
            data.put("chargingDegree", decimal);
            data.put("remainingTime","0");
            data.put("chargingTimeNumber",0);
            data.put("serialNumber","0");
            data.put("type","-1");
            data.put("parkingInstructions","充电完成请尽快离场,超时车位占用费10元/小时");
            map.put("data", data);
        }else{
            try{
                String c = orderMessage.get("electricCurrent").toString();
                String v = orderMessage.get("voltage").toString();
                BigDecimal decimal1 = new BigDecimal(v).multiply(new BigDecimal(c)).setScale(2, BigDecimal.ROUND_DOWN);
                orderMessage.put("power",decimal1.divide(new BigDecimal(1000),2,BigDecimal.ROUND_DOWN));
            }catch (Exception e){
                e.printStackTrace();
            }
            map.put("data",orderMessage);
        }
        JSONObject json = new JSONObject(map);
        webSocketService.getMessage(""+userId,json.toString());
        logger.info("<<<<<<<<<<发送数据<<<<<<<<<<<<<<"+ json.toString()+">>>>>>>>>>>>>>>>>");
        logger.info("<<<<<<<<<<发送时间<<<<<<<<<<<<<<"+  DateUtil.format(Calendar.getInstance().getTime(), "yyyy-MM-dd HH:mm:ss")+">>>>>>>>>>>>>>>>>");
    }
}
