package com.usky.eg.controller.web;


import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.log.annotation.Log;
import com.usky.common.log.enums.BusinessType;
import com.usky.eg.domain.EgDevice;
import com.usky.eg.service.EgDeviceService;
import com.usky.eg.service.vo.EgDeviceBindFacePersonQueryVO;
import com.usky.eg.service.vo.EgDeviceBindFacePersonVO;
import com.usky.eg.service.vo.EgDeviceRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 门禁_设备信息表 前端控制器
 * </p>
 *
 * @author zyj
 * @since 2024-11-27
 */
@RestController
@RequestMapping("/egDevice")
public class EgDeviceController {
    @Autowired
    private EgDeviceService egDeviceService;

    @PostMapping("page")
    public ApiResult<CommonPage<EgDevice>> page(@RequestBody EgDeviceRequestVO requestVO){
        return ApiResult.success(egDeviceService.page(requestVO));
    }

    @PostMapping("/bindFacePerson/page")
    public ApiResult<CommonPage<EgDeviceBindFacePersonVO>> pageBindFacePersons(@RequestBody EgDeviceBindFacePersonQueryVO queryVO) {
        return ApiResult.success(egDeviceService.pageBindFacePersons(queryVO));
    }

    @PostMapping("wePage")
    public ApiResult<CommonPage<EgDevice>> wePage(@RequestBody EgDeviceRequestVO requestVO){
        return ApiResult.success(egDeviceService.wePage(requestVO));
    }

    /**
     * 新增
     * @param egDevice
     * @return
     */
    @PostMapping
    public ApiResult<Void> add(@RequestBody EgDevice egDevice){
        egDeviceService.add(egDevice);
        return ApiResult.success();
    }

    /**
     * 修改
     * @param egDevice
     * @return
     */
    @PutMapping
    public ApiResult<Void> update(@RequestBody EgDevice egDevice){
        egDeviceService.update(egDevice);
        return ApiResult.success();
    }

    /**
     * 附加功能更新
     * @param egDevice
     * @return
     */
    @PutMapping("attachUpdate")
    public ApiResult<Void> attachUpdate(@RequestBody EgDevice egDevice){
        egDeviceService.attachUpdate(egDevice);
        return ApiResult.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ApiResult<Void> remove(@PathVariable("id") Integer id){
        egDeviceService.remove(id);
        return ApiResult.success();
    }

    /**
     * 设备绑定人员
     * @param params 包含 deviceId, personIds, isLoginNotify 的参数对象
     * @return
     */
    @PostMapping("/bindPerson")
    public ApiResult<Void> bindPerson(@RequestBody Map<String, Object> params){
        Integer deviceId = (Integer) params.get("deviceId");
        String personIds = (String) params.get("personIds");
        Integer isLoginNotify = params.get("isLoginNotify") != null ? (Integer) params.get("isLoginNotify") : 0;
        egDeviceService.bindPerson(deviceId, personIds, isLoginNotify);
        return ApiResult.success();
    }

    /**
     * 下发设备控制命令
     */
    @Log(title = "门禁系统人员校验开门", businessType = BusinessType.OTHER)
    @GetMapping("/control")
    public ApiResult<Map<String,Object>> control(@RequestParam("productCode") String productCode,
                                                 @RequestParam("deviceUuid") String deviceUuid,
                                                 @RequestParam("commandCode") String commandCode,
                                                 @RequestParam("commandValue") String commandValue,
                                                 @RequestParam(value = "userId",required = false) Long userId,
                                                 @RequestParam(value = "userName",required = false) String userName,
                                                 @RequestParam(value = "categoryType",required = false , defaultValue = "1") Integer categoryType,
                                                 @RequestParam(value = "gatewayUuid",required = false) String gatewayUuid,
                                                 @RequestParam(value = "skipCheck",required = false, defaultValue = "false") Boolean skipCheck,
                                                 @RequestParam(value = "passType",required = false) Integer passType){
        return ApiResult.success(egDeviceService.control(productCode,deviceUuid,commandCode,commandValue,userId,userName,categoryType,gatewayUuid,skipCheck,passType));
    }

}

