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.config.fkj.device.template.FkjDeviceDto;
import cn.hanghui.outapi.platform.tenant.entity.config.fkj.device.template.FkjDeviceMiniDto;
import cn.hanghui.outapi.platform.tenant.entity.config.fkj.device.template.FkjDeviceSearch;
import cn.hanghui.outapi.platform.tenant.entity.config.fkj.device.template.FkjDeviceTemplateDto;
import cn.hanghui.outapi.platform.tenant.entity.config.fkj.device.template.FkjDeviceTemplateLessVo;
import cn.hanghui.outapi.platform.tenant.entity.config.fkj.device.template.FkjDeviceTemplateVo;
import cn.hanghui.outapi.platform.tenant.entity.config.fkj.device.template.FkjDeviceTemplateSearch;
import cn.hanghui.outapi.platform.tenant.entity.config.fkj.device.template.FkjDeviceUpdateMiniDto;
import cn.hanghui.outapi.platform.tenant.entity.config.fkj.device.template.FkjDeviceVo;
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.FkjDeviceService;
import cn.hutool.core.util.StrUtil;
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.DeleteMapping;
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 FkjDeviceController
 * @date 2024/3/5 下午2:11
 * @description
 */
@Slf4j
@Api(tags = "访客机配置")
@RestController
@RequestMapping(value = "/api/v1/fkj/device")
public class FkjDeviceController extends BaseController {

    @Autowired
    private FkjDeviceService fkjDeviceService;

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

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

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

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

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

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

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

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

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

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

    @ApiOperation(value = "访客机设备-获取配置信息")
    @PostMapping("/sn/config")
    @ApiOperationSupport(order = 200)
    public RestResult<FkjDeviceVo> snConfig(@RequestBody FkjDeviceMiniDto dto) {
        log.info("--->访客机设备-获取配置信息:{}", JSONObject.toJSONString(dto));
        //ampe容器管理
        List<String> tokens = AppTokenUtil.getTokens();
        if (!tokens.contains(dto.getToken().trim())) {
            return RestResult.error(RestCode.FORBIDDEN.getMsg());
        }
        return RestResult.ok(fkjDeviceService.snConfig(dto));
    }

    @ApiOperation(value = "访客机设备-修改配置信息")
    @PostMapping("/sn/updateConfig")
    @ApiOperationSupport(order = 210)
    public RestResult<Boolean> updateConfig(@RequestBody FkjDeviceUpdateMiniDto dto) {
        log.info("--->访客机设备-修改配置信息:{}", JSONObject.toJSONString(dto));
        //ampe容器管理
        List<String> tokens = AppTokenUtil.getTokens();
        if (StrUtil.isBlank(dto.getToken()) || !tokens.contains(dto.getToken())) {
            return RestResult.error(RestCode.FORBIDDEN.getMsg());
        }
        fkjDeviceService.updateConfig(dto);
        return RestResult.ok(Boolean.TRUE);
    }

}
