package com.yuchen.saas.park.service.controller.door;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.entity.door.DoorDeviceParameter;
import com.yuchen.saas.park.service.service.door.DoorDeviceParameterService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 门参数 前端控制器
 * </p>
 *
 * @author miaoqs
 * @since 2023-12-19
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/doorDeviceParameter")
public class DoorDeviceParameterController {

    private final DoorDeviceParameterService deviceParameterService;

    @PostMapping("/saveOrUpdate")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "门参数 新增/修改", notes = "门参数 新增/修改")
    public R saveOrUpdate(@Valid @RequestBody DoorDeviceParameter doorDeviceParameter){
        if(Func.isEmpty(doorDeviceParameter)) {
            return R.error("门参数不能为空");
        }
        String msg = deviceParameterService.saveOrUpdateIot(doorDeviceParameter);
        if(Func.isNotEmpty(msg)){
            return R.fail(msg);
        }else{
            return R.success();
        }
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "门参数 详情", notes = "门参数详情")
    public R detail(Long id) {
        return R.data(deviceParameterService.getById(id));
    }

    /**
     * 根据主表id删除门参数
     * @param id
     * @return
     */
    @PostMapping("/removeId")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据主表id 门参数 删除", notes = "根据主表id 门参数 删除")
    public R removeId(Long id) {
        deviceParameterService.removeById(id);
        return R.success();
    }

    /**
     * 门参数 分页
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "门参数 分页", notes = "门参数 分页")
    public R<IPage<DoorDeviceParameter>> page(Query query) {
        IPage<DoorDeviceParameter> pages = deviceParameterService.page(Condition.getPage(query),
                new LambdaQueryWrapper<DoorDeviceParameter>()
                        .orderByDesc(DoorDeviceParameter::getCreateTime)
        );
        return  R.data(pages);
    }

    /**
     * 根据设备id获取参数详情
     * @param equipmentId
     * @return
     */
    @GetMapping("/getDetailByEquipmentId")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "根据设备id获取参数详情", notes = "根据设备id获取参数详情")
    public R getDetailByEquipmentId(Long equipmentId) {
        return R.data(deviceParameterService.getOne(new LambdaQueryWrapper<DoorDeviceParameter>()
                .eq(DoorDeviceParameter::getEquipmentId,equipmentId)
                .last("limit 1")));
    }

    /**
     * 调用iot接口 恢复默认参数
     * @param id
     * @return
     */
    @PostMapping("/setDefaultParam")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "调用iot接口 恢复默认参数", notes = "调用iot接口 恢复默认参数")
    public R setDefaultParam(Long id){
        if(Func.isEmpty(id)) {
            return R.error("id不能为空");
        }
        String msg = deviceParameterService.setDefaultParamIot(id);
        if(Func.isNotEmpty(msg)){
            return R.fail(msg);
        }else{
            return R.success();
        }
    }


    /**
     * 查看Iot门参数
     * @param doorId
     * @return
     */
    @GetMapping("/getIotDoorParams")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "查看Iot门参数", notes = "查看Iot门参数")
    public R getDoorParams(String doorId) {
        return R.data(deviceParameterService.getDoorParams(doorId));
    }

}
