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.query.APPDeviceQueryCriteria;
import com.fuyuanshen.app.service.equipment.APPDeviceService;
import com.fuyuanshen.common.core.domain.R;
import com.fuyuanshen.common.core.domain.ResponseVO;
import com.fuyuanshen.common.core.validate.EditGroup;
import com.fuyuanshen.common.idempotent.annotation.RepeatSubmit;
import com.fuyuanshen.common.log.annotation.Log;
import com.fuyuanshen.common.log.enums.BusinessType;
import com.fuyuanshen.common.mybatis.core.page.PageQuery;
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;
import com.fuyuanshen.common.web.core.BaseController;
import com.fuyuanshen.equipment.domain.bo.UserAppBo;
import com.fuyuanshen.equipment.domain.query.DeviceQueryCriteria;
import com.fuyuanshen.equipment.domain.vo.AppDeviceVo;
import com.fuyuanshen.equipment.service.AppUserService;
import com.fuyuanshen.equipment.service.DeviceService;
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 extends BaseController {

    private final APPDeviceService appDeviceService;
    private final AppUserService appUserService;
    private final DeviceService deviceService;


    @PostMapping(value = "/list")
    @Operation(summary = "APP客户设备列表")
    public TableDataInfo<APPDevice> appDeviceList(@RequestBody APPDeviceQueryCriteria criteria) {
        Page<APPDevice> page = new Page<>(criteria.getPage(), criteria.getSize());

        return appDeviceService.appDeviceList(page, criteria);
    }


    @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 TableDataInfo<AppDeviceVo> queryAPPDevice(DeviceQueryCriteria bo, PageQuery pageQuery) {
        return deviceService.queryAppDeviceList(bo, pageQuery);
    }


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

    /**
     * app设备解绑
     *
     * @param id
     * @return
     */
    @Operation(summary = "设备解绑")
    @DeleteMapping("/unBind")
    public R<Void> unBind(Long id) {
        return toAjax(deviceService.unBindDevice(id));
    }


    /**
     * 修改APP用户信息
     */
    // @SaCheckPermission("app:user:edit")
    @Log(title = "APP用户信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserAppBo bo) {
        return toAjax(appUserService.updateByBo(bo));
    }


}
