package com.xhpc.evcs.api;

import cn.hutool.core.date.DateUtil;
import com.xhpc.common.api.PileOrderService;
import com.xhpc.common.core.domain.R;
import com.xhpc.evcs.domain.PlaceholderRules;
import com.xhpc.evcs.domain.PriceInfo;
import com.xhpc.evcs.dto.*;
import com.xhpc.evcs.encryption.EvcsConst;
import com.xhpc.evcs.jpa.PlaceholderIOrderRepository;
import com.xhpc.evcs.jpa.PlaceholderIRepository;
import com.xhpc.evcs.jpa.PlaceholderITimeRepository;
import com.xhpc.evcs.utils.JSONUtil;
import com.xhpc.order.domain.XhpcPlaceholderITime;
import com.xhpc.order.domain.XhpcPlaceholderOrder;
import com.xhpc.order.domain.XhpcPlacreholder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 新电图 占位费规则查询
 *
 * @author yuyang
 * @Date 2025-07-07 11:19
 */
@RestController
public class QueryPlaceholderRulesController {
    @Resource
    private PileOrderService pileOrderService;

    @Autowired
    private PlaceholderIOrderRepository placeholderIOrderRepository;
    @Autowired
    private PlaceholderIRepository placeholderIRepository;
    @Autowired
    private PlaceholderITimeRepository placeholderITimeRepository;
    /**
     * @param commonRequest
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/v1/query_placeholder_rules")
    public CommonResponse queryPlaceholderRules(@RequestBody CommonRequest<XDTPlaceholderRules> commonRequest) throws Exception {

        CommonResponse resp = new CommonResponse();
        resp.setRet(EvcsConst.RET_FAIL);

        Map<String,Object> map =new HashMap<>();
        map.put("SuccStat",1);
        XDTPlaceholderRules placeholderRules = JSONUtil.readParams(commonRequest.getData(), XDTPlaceholderRules.class);
        if (placeholderRules == null || placeholderRules.getStationIDs().length==0) {
            resp.setMsg("Request or token params validation failed");
        } else {
            //获取占位规则信息
            String[] stationIds = placeholderRules.getStationIDs();
            map.put("SuccStat",0);

            PlaceholderRulesXinDianTu[] placeholderRulesYu = new PlaceholderRulesXinDianTu[stationIds.length];
            for (int i = 0; i <stationIds.length ; i++) {
                String stationId = stationIds[i];
                XhpcPlacreholder xhpcPlacreholder = placeholderIRepository.findByChargingStationIdAndDelFlag(Long.valueOf(stationId), 0);

                PlaceholderRulesXinDianTu placeholderRulesXinDianTu = new PlaceholderRulesXinDianTu();

                placeholderRulesXinDianTu.setStationID(stationId);
                String startTime = DateUtil.format(xhpcPlacreholder.getStartTime(), "yyyy.MM.dd HH:mm:ss");
                String endTime = DateUtil.format(xhpcPlacreholder.getEndTime(), "yyyy.MM.dd HH:mm:ss");
                placeholderRulesXinDianTu.setEffectStartTime(startTime);
                placeholderRulesXinDianTu.setEffectEndTime(endTime);
                placeholderRulesXinDianTu.setStatus(xhpcPlacreholder.getType());
                placeholderRulesXinDianTu.setFreeTime(xhpcPlacreholder.getFreeTime());
                placeholderRulesXinDianTu.setMaxFee(xhpcPlacreholder.getMaxFee().doubleValue());

                List<PriceInfo> priceInfos =new ArrayList<>();
                List<XhpcPlaceholderITime> xhpcPlaceholderITimeList = placeholderITimeRepository.findByPlaceholderIdAndDelFlagAndChargingStationId(xhpcPlacreholder.getPlaceholderId(), 0,Long.valueOf(stationId));
                if(xhpcPlaceholderITimeList !=null && xhpcPlaceholderITimeList.size()>0){
                    for (int j = 0; j < xhpcPlaceholderITimeList.size(); j++) {
                        XhpcPlaceholderITime xhpcPlaceholderTimeOrder = xhpcPlaceholderITimeList.get(j);
                        PriceInfo placeHolderPrice = new PriceInfo();
                        placeHolderPrice.setStartTime(DateUtil.format(xhpcPlaceholderTimeOrder.getStartTime(),"HH:mm:ss"));
                        placeHolderPrice.setPrice(xhpcPlaceholderTimeOrder.getPrice().doubleValue());
                        priceInfos.add(placeHolderPrice);
                    }
                }
                placeholderRulesXinDianTu.setPriceInfos(priceInfos);

                placeholderRulesYu[i] = placeholderRulesXinDianTu;
            }

            map.put("PlaceholderRules",placeholderRulesYu);
            resp.setRet(EvcsConst.RET_SUCC);
            resp.setData(JSONUtil.toJSONString(map));

//            R r = pileOrderService.getPlaceholderIDs(placeholderRules.getStationIDs());
//            if(r.getCode()==200){
//                map.put("SuccStat",0);
//               List<PlaceholderRules> placeholderIDs = (List<PlaceholderRules>) r.getData();
//                map.put("PlaceholderRules",placeholderIDs);
//                resp.setRet(EvcsConst.RET_SUCC);
//                resp.setData(JSONUtil.toJSONString(map));
//            }
        }
        return resp;
    }


    @PostMapping(value = "/v1/notification_placeHolder_order_status")
    public CommonResponse notificationPlaceHolderOrderStatus(@RequestBody CommonRequest<XDTPlaceHolderOrderStatus> commonRequest) throws Exception {

        CommonResponse resp = new CommonResponse();
        resp.setRet(EvcsConst.RET_FAIL);

        Map<String,Object> map =new HashMap<>();
        map.put("SuccStat",1);
        XDTPlaceHolderOrderStatus placeholderRules = JSONUtil.readParams(commonRequest.getData(), XDTPlaceHolderOrderStatus.class);
        if (placeholderRules == null ) {
            resp.setMsg("Request or token params validation failed");
        } else {
            String placeholderOrderID = placeholderRules.getPlaceholderOrderID();

            XhpcPlaceholderOrder xhpcPlaceholderOrder = placeholderIOrderRepository.findByPlaceholderOrderNumber(placeholderOrderID);

            if(xhpcPlaceholderOrder!=null){
                map.put("SuccStat",0);
                resp.setRet(EvcsConst.RET_SUCC);
                xhpcPlaceholderOrder.setUserStatus(placeholderRules.getStatus());
                if(placeholderRules.getStatus()==1){
                    xhpcPlaceholderOrder.setRechargeMoney(new BigDecimal(placeholderRules.getRefundMoney()));
                }else{
                    xhpcPlaceholderOrder.setRefundMoney(xhpcPlaceholderOrder.getTotalMoney());
                }
                placeholderIOrderRepository.save(xhpcPlaceholderOrder);
            }else{
                map.put("SuccStat",1);
                map.put("FailReason","无此订单");
            }
            resp.setData(JSONUtil.toJSONString(map));
        }
        return resp;
    }


}
