package com.xhpc.charging.station.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import com.xhpc.charging.station.service.IXhpcRequirementService;
import com.xhpc.charging.station.utils.img.JSONUtil;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.domain.Requirement;
import com.xhpc.common.domain.RequirementTask;
import com.xhpc.common.domain.RequirementTime;
import com.xhpc.common.util.LogUserUtils;
import com.xhpc.system.api.model.LoginUser;
import org.dom4j.tree.BackedList;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 监管平台有序充电调控需求
 *
 * @author yuyang
 * @Date 2025-07-11 12:04
 */
@RestController
@RequestMapping("/requirementInfo")
public class XhpcRequirementInfoController extends BaseController {

    @Resource
    IXhpcRequirementService xhpcRequirementServicel;
    @Resource
    LogUserUtils logUserUtils;


    @GetMapping("/getList")
    public TableDataInfo getPage(HttpServletRequest request,
                                 String name,
                                 String area,
                                 String code
                                 ){
        LoginUser loginUser = logUserUtils.getLogUser(request);
        startPage();
        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", loginUser.getTenantId());
        params.put("name", name);
        params.put("area", area);
        params.put("code", code);
        return getDataTable(xhpcRequirementServicel.getLsit(params));
    }


    @GetMapping("/detail")
    public R getRequirementById(@RequestParam("requirementId")Long requirementId){
        return R.ok(xhpcRequirementServicel.getRequirementById(requirementId));
    }


    @GetMapping("/getTaskList")
    public TableDataInfo getTaskList(HttpServletRequest request,
                                 String taskName,
                                 String taskCode,
                                 String demandDate,
                                 Long stationID,
                                 String operatorID,
                                 String deliveryTime,
                                 String equipmentOwnerID,
                                 String eleNo
                                     ){
        startPage();
        Map<String, Object> params = new HashMap<>();
        params.put("taskName", taskName);
        params.put("taskCode", taskCode);
        params.put("demandDate", demandDate);
        params.put("stationID", stationID);
        params.put("operatorID", operatorID);
        params.put("deliveryTime", deliveryTime);
        params.put("equipmentOwnerID", equipmentOwnerID);
        params.put("eleNo", eleNo);
        return getDataTable(xhpcRequirementServicel.getTaskList(params));
    }

    @GetMapping("/taskDetail")
    public R taskDetail(@RequestParam("requirementTaskId")Long requirementTaskId){
        return R.ok(xhpcRequirementServicel.getRequirementTaskById(requirementTaskId));
    }

    @GetMapping("/updateTaskById")
    public R updateTaskById(@RequestParam("requirementTaskId")Long requirementTaskId,@RequestParam("status")Integer status){
        return xhpcRequirementServicel.updateTaskById(requirementTaskId,status);
    }


    //================监管平台接口调控===========================

    @PostMapping("/addRequirementInfo")
    public R addRequirementInfo(@RequestParam("name") String name, @RequestParam("area") String area, @RequestParam("code") String code, @RequestParam("list") String list, @RequestParam("requireDate") String requireDate, @RequestParam("releaseTime") String releaseTime, @RequestParam("deliveryTime") String deliveryTime){

        Requirement requirement = new Requirement();
        requirement.setName(name);
        requirement.setArea(area);
        requirement.setCode(code);
        requirement.setRequireDate(DateUtil.parse(requireDate,"yyyy-MM-dd"));
        requirement.setReleaseTime(DateUtil.parse(releaseTime,"yyyy-MM-dd HH:mm:ss"));
        requirement.setDeliveryTime(DateUtil.parse(deliveryTime,"yyyy-MM-dd HH:mm:ss"));
        requirement.setCreateTime(new Date());
        xhpcRequirementServicel.addRequirement(requirement);

        List<Map<String,String>> mapList =JSONUtil.readParams(list,List.class);
        if(list !=null && mapList.size()>0){
            for (int i = 0; i <mapList.size() ; i++) {
                Map<String, String> map = mapList.get(i);
                RequirementTime requirementTime = new RequirementTime();
                requirementTime.setStartTime(DateUtil.parse(map.get("startTime"),"yyyy-MM-dd HH:mm:ss"));
                requirementTime.setEndTime(DateUtil.parse(map.get("endTime"),"yyyy-MM-dd HH:mm:ss"));
                requirementTime.setRes(new BigDecimal(map.get("res")).doubleValue());
                requirementTime.setRequirementId(requirement.getRequirementId());
                requirementTime.setCreateTime(new Date());
                xhpcRequirementServicel.addRequirementTime(requirementTime);
            }
        }
        return R.ok();
    }

    @PostMapping("/addNotificationTask")
    public R addNotificationTask(@RequestParam("taskName") String taskName,@RequestParam("taskCode") String taskCode,@RequestParam("demandDate") String demandDate,@RequestParam("stationID") String stationID,@RequestParam("operatorID") String operatorID,@RequestParam("equipmentOwnerID") String equipmentOwnerID,@RequestParam("eleNo") String eleNo,@RequestParam("list") String list,@RequestParam("deliveryTime") String deliveryTime){

        RequirementTask requirementTask = new RequirementTask();
        requirementTask.setTaskName(taskName);
        requirementTask.setTaskCode(taskCode);
        requirementTask.setDemandDate(DateUtil.parse(demandDate,"yyyy-MM-dd"));
        requirementTask.setStationID(stationID);
        requirementTask.setOperatorID(operatorID);
        requirementTask.setEquipmentOwnerID(equipmentOwnerID);
        requirementTask.setEleNo(eleNo);
        requirementTask.setDeliveryTime(DateUtil.parse(deliveryTime,"yyyy-MM-dd HH:mm:ss"));
        requirementTask.setCreateTime(new Date());
        xhpcRequirementServicel.addRequirementTask(requirementTask);

        List<Map<String,String>> mapList =JSONUtil.readParams(list,List.class);
        if(list !=null && mapList.size()>0){
            for (int i = 0; i <mapList.size() ; i++) {
                Map<String, String> map = mapList.get(i);
                RequirementTime requirementTime = new RequirementTime();
                requirementTime.setStartTime(DateUtil.parse(map.get("startTime"),"yyyy-MM-dd HH:mm:ss"));
                requirementTime.setEndTime(DateUtil.parse(map.get("endTime"),"yyyy-MM-dd HH:mm:ss"));
                requirementTime.setRes(new BigDecimal(map.get("res")).doubleValue());
                requirementTime.setRequirementTaskId(requirementTask.getRequirementTaskId());
                requirementTime.setCreateTime(new Date());
                xhpcRequirementServicel.addRequirementTime(requirementTime);
            }
        }

        return R.ok();
    }
}
