package com.fuyuanshen.app.controller.equipment;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyuanshen.app.domain.APPDevice;
import com.fuyuanshen.app.domain.APPDeviceType;
import com.fuyuanshen.app.domain.dto.APPUnbindDTO;
import com.fuyuanshen.app.domain.query.APPDeviceQueryCriteria;
import com.fuyuanshen.app.service.equipment.APPDeviceService;
import com.fuyuanshen.common.core.domain.PageResult;
import com.fuyuanshen.common.core.domain.ResponseVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author: WY
 * @Date: 2025/5/16
 **/
@Slf4j
@Tag(name = "APP：客户设备管理", description = "APP：客户设备管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/app/device")
public class APPDeviceController {

    private final APPDeviceService appDeviceService;


    @PostMapping(value = "/list")
    @Operation(summary = "APP客户设备列表")
    public ResponseVO<PageResult<APPDevice>> appDeviceList(@RequestBody APPDeviceQueryCriteria criteria) {
        Page<APPDevice> page = new Page<>(criteria.getPage(), criteria.getSize());
        PageResult<APPDevice> devices = null;
        try {
            devices = appDeviceService.appDeviceList(page, criteria);
        } catch (Exception e) {
            log.error("queryDevice error: " + e.getMessage());
            return ResponseVO.fail("");
        }
        return ResponseVO.success(devices);
    }


    @PostMapping(value = "/typeList")
    @Operation(summary = "APP客户设备类型列表")
    public ResponseVO<List<APPDeviceType>> appTypeList(@RequestBody APPDeviceQueryCriteria criteria) {
        List<APPDeviceType> typeList = appDeviceService.appTypeList(criteria);
        return ResponseVO.success(typeList);
    }


    @PostMapping(value = "/bind")
    @Operation(summary = "APP客户设备绑定")
    public ResponseVO<String> appBindDevice(@RequestBody APPDeviceQueryCriteria criteria) {
        appDeviceService.appBindDevice(criteria);
        return ResponseVO.success("绑定成功！");
    }


    @GetMapping
    @Operation(summary = "WEB端查看APP客户设备绑定")
    public ResponseVO<PageResult<APPDevice>> queryAPPDevice(APPDeviceQueryCriteria criteria) {
        Page<APPDevice> page = new Page<>(criteria.getPage(), criteria.getSize());
        PageResult<APPDevice> devices = null;
        try {
            devices = appDeviceService.queryAll(page, criteria);
        } catch (Exception e) {
            log.error("queryDevice error: " + e.getMessage());
            return ResponseVO.fail("");
        }
        return ResponseVO.success(devices);
    }


    @PostMapping(value = "/unbind")
    @Operation(summary = "WEB端APP客户设备解绑")
    public ResponseVO<String> unbindAPPDevice(@Validated @ModelAttribute APPUnbindDTO deviceForm) {
        appDeviceService.unbindAPPDevice(deviceForm);
        return ResponseVO.success("解绑成功！！！");
    }


}
