package cn.hanghui.outapi.platform.tenant.controller;

import cn.hanghui.outapi.platform.common.config.log.Log;
import cn.hanghui.outapi.platform.common.controller.BaseController;
import cn.hanghui.outapi.platform.common.entity.rest.RestCode;
import cn.hanghui.outapi.platform.common.entity.rest.RestResponse;
import cn.hanghui.outapi.platform.common.entity.rest.RestResult;
import cn.hanghui.outapi.platform.common.utils.AppTokenUtil;
import cn.hanghui.outapi.platform.tenant.entity.dto.TenantDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhomc.HhOmcDeviceDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhomc.HhOmcDeviceSearch;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhomc.HhOmcDeviceTemplateDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhomc.HhOmcDeviceTemplateLessVo;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhomc.HhOmcDeviceTemplateSearch;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhomc.HhOmcDeviceTemplateVo;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhomc.HhOmcDeviceVo;
import cn.hanghui.outapi.platform.tenant.entity.enums.BusinessTypeEnum;
import cn.hanghui.outapi.platform.tenant.entity.enums.LogTagEnum;
import cn.hanghui.outapi.platform.tenant.service.HhOmcDeviceService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author shisl
 * @package cn.hanghui.outapi.platform.tenant.controller
 * @class HhOmcDeviceController
 * @date 2024/12/31 上午10:38
 * @description
 */
@Slf4j
@Api(tags = "HHOmc设备配置")
@RestController
@RequestMapping(value = "/api/v1/hh/omc/device")
public class HhOmcDeviceController extends BaseController {

    @Autowired
    private HhOmcDeviceService hhOmcDeviceService;

    @ApiOperation("模版-新增/编辑")
    @PostMapping("template/edit")
    @ApiOperationSupport(order = 100)
    @Log(tag = LogTagEnum.HHOMC,title = "添加/编辑模版",businessTypeEnum = BusinessTypeEnum.INSERT_UPDATE)
    public RestResult<Boolean> templateEdit(@RequestBody @Valid HhOmcDeviceTemplateDto dto) {
        return RestResponse.ok(hhOmcDeviceService.templateEdit(dto));
    }

    @ApiOperation("模版列表")
    @GetMapping("template/list")
    @ApiOperationSupport(order = 110)
    public RestResult<List<HhOmcDeviceTemplateLessVo>> templateList() {
        return RestResponse.ok(hhOmcDeviceService.templateList());
    }

    @ApiOperation("模版列表(分页)")
    @PostMapping("template/page")
    @ApiOperationSupport(order = 120)
    public RestResult<IPage<HhOmcDeviceTemplateVo>> templatePage(@RequestBody HhOmcDeviceTemplateSearch dto) {
        return RestResponse.ok(hhOmcDeviceService.templatePage(dto));
    }

    @ApiOperation("模版-删除")
    @GetMapping("template/{deviceTemplateId}")
    @ApiOperationSupport(order = 130)
    @Log(tag = LogTagEnum.HHOMC,title = "删除模版",businessTypeEnum = BusinessTypeEnum.DELETE)
    public RestResult<Boolean> templateDelete(@PathVariable(value = "deviceTemplateId") Long deviceTemplateId) {
        return RestResponse.ok(hhOmcDeviceService.templateDelete(deviceTemplateId));
    }

    @ApiOperation("模版-模版详情")
    @GetMapping("template/detail")
    @ApiOperationSupport(order = 140)
    public RestResult<HhOmcDeviceTemplateVo> templateDetail(@RequestParam(value = "deviceTemplateId") Long deviceTemplateId) {
        return RestResponse.ok(hhOmcDeviceService.templateDetail(deviceTemplateId));
    }

    @ApiOperation(value = "设备配置-添加/编辑")
    @PostMapping("/add")
    @ApiOperationSupport(order = 150)
    @Log(tag = LogTagEnum.HHOMC,title = "添加/编辑设备",businessTypeEnum = BusinessTypeEnum.INSERT_UPDATE)
    public RestResult<Boolean> add(@RequestBody HhOmcDeviceDto dto) {
        return hhOmcDeviceService.add(dto);
    }

    @ApiOperation(value = "设备列表（分页）")
    @PostMapping("/page")
    @ApiOperationSupport(order = 160)
    public RestResult<IPage<HhOmcDeviceVo>> page(@RequestBody HhOmcDeviceSearch dto) {
        return hhOmcDeviceService.page(dto);
    }

    @ApiOperation(value = "获取设备编号列表")
    @PostMapping("/sn/list")
    public RestResult<List<String>> snlist() {
        return hhOmcDeviceService.snlist();
    }

    @ApiOperation(value = "设备删除")
    @PostMapping("/del/{deviceId}")
    @ApiOperationSupport(order = 170)
    @Log(tag = LogTagEnum.HHOMC,title = "删除设备",businessTypeEnum = BusinessTypeEnum.DELETE)
    public RestResult<Boolean> del(@PathVariable(value = "deviceId") @ApiParam(value = "deviceId", required = true) Long deviceId) {
        return hhOmcDeviceService.del(deviceId);
    }

    @ApiOperation(value = "设备授权")
    @GetMapping("/enable")
    @ApiOperationSupport(order = 180)
    @Log(tag = LogTagEnum.HHOMC,title = "设备授权",businessTypeEnum = BusinessTypeEnum.AUTH)
    public RestResult<Boolean> enable(@RequestParam(value = "deviceId") @ApiParam(value = "deviceId", required = true) Long deviceId) {
        return hhOmcDeviceService.opeDeviceAuth(deviceId, Boolean.TRUE);
    }

    @ApiOperation(value = "设备取消授权")
    @GetMapping("/disable")
    @ApiOperationSupport(order = 190)
    @Log(tag = LogTagEnum.HHOMC,title = "设备取消授权",businessTypeEnum = BusinessTypeEnum.CANCEL_AUTH)
    public RestResult<Boolean> disable(@RequestParam(value = "deviceId") @ApiParam(value = "deviceId", required = true) Long deviceId) {
        return hhOmcDeviceService.opeDeviceAuth(deviceId, Boolean.FALSE);
    }

    @ApiOperation(value = "HHOmc设备-获取配置信息")
    @PostMapping("/config/query")
    @ApiOperationSupport(order = 200)
    public RestResult<HhOmcDeviceVo> queryConfig(@RequestBody TenantDto dto) {
        log.info("--->HHOmc设备-获取配置信息:{}", JSONObject.toJSONString(dto));
        //HHOmc设备 2915ac041cb759bfdc4968d4fce1c437
        List<String> tokens = AppTokenUtil.getTokens();
        if (!tokens.contains(dto.getToken().trim())) {
            return RestResult.error(RestCode.FORBIDDEN.getMsg());
        }
        return hhOmcDeviceService.queryConfig(dto);
    }

}
