package com.ruoyi.iot.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.iot.domain.TDevicePlan;
import com.ruoyi.iot.domain.dto.TDevicePlanAddNewDTO;
import com.ruoyi.iot.service.ITDevicePlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/plan")
public class TDevicePlanController extends BaseController {

    @Autowired
    private ITDevicePlanService tDevicePlanService;

    // http://localhost:8080/add-new
    @PostMapping("/add")
    public AjaxResult addNew(@RequestBody JSONObject params) {
        log.info("开始处理【添加预约内容】的请求，参数：{}", params);
        log.info(String.valueOf(params));
        String resultJsonString = JSON.toJSONString(params);
        JSONObject data = JSON.parseObject(resultJsonString);
        JSONObject realData = data.getJSONObject("params");
        TDevicePlanAddNewDTO tDevicePlanAddNewDTO=new TDevicePlanAddNewDTO();
// 将 params 字段中的各个子字段进行赋值
        String createTime = String.valueOf(LocalDateTime.now());
        long planType = realData.getLongValue("planType");
        long status = realData.getLongValue("status");
        LoginUser loginUser = getLoginUser();
        Long userId = loginUser.getUserId();
        String runTime = realData.getString("runTime");
        String ccid = realData.getString("ccid");
        LocalDateTime parsedRunTime = null; // 初始化为null
        if (runTime != null && !runTime.isEmpty()) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
            parsedRunTime = LocalDateTime.parse(runTime, formatter);
        }
        String repeatDays = realData.getString("repeatDays");
        String repeatTime = realData.getString("repeatTime");
        String deviceId = realData.getString("deviceId");
        tDevicePlanAddNewDTO.setCreateTime(LocalDateTime.parse(createTime));
        tDevicePlanAddNewDTO.setPlanType(planType);
        tDevicePlanAddNewDTO.setStatus(status);
        tDevicePlanAddNewDTO.setUserId(userId);
        tDevicePlanAddNewDTO.setRunTime(parsedRunTime);
        tDevicePlanAddNewDTO.setRepeatDays(repeatDays);
        tDevicePlanAddNewDTO.setRepeatTime(repeatTime);
        tDevicePlanAddNewDTO.setDeviceId(deviceId);
        tDevicePlanAddNewDTO.setCcid(ccid);
//        TDevicePlanAddNewDTO tDevicePlanAddNewDTO=new TDevicePlanAddNewDTO();
//        BeanUtils.copyProperties(params, tDevicePlanAddNewDTO);

        tDevicePlanService.addNew(tDevicePlanAddNewDTO);
        return AjaxResult.success();
    }

    @PostMapping("/list")
    public AjaxResult planList(){
        startPage();
        LambdaQueryWrapper<TDevicePlan> objectLambdaQueryWrapper = Wrappers.lambdaQuery();
        objectLambdaQueryWrapper.orderByDesc(TDevicePlan::getCreateTime);
        LoginUser loginUser = getLoginUser();
        Long userId = loginUser.getUserId();
        objectLambdaQueryWrapper.eq(TDevicePlan::getUserId,userId);
        List<TDevicePlan> list = tDevicePlanService.list(objectLambdaQueryWrapper);
        return AjaxResult.success(list);
    }

}
