package com.njcn.cssystem.controller;


import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.common.utils.LogUtil;
import com.njcn.cssystem.pojo.param.AppMsgSetParam;
import com.njcn.cssystem.service.IAppMsgSetService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 设备发送短信配置表 前端控制器
 * </p>
 *
 * @author xy
 * @since 2026-04-21
 */
@RestController
@RequestMapping("/appMsgSet")
@Slf4j
@Api(tags = "短信配置")
@AllArgsConstructor
public class AppMsgSetController extends BaseController {

    private final IAppMsgSetService appMsgSetService;

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("/addUserDevices")
    @ApiOperation("新增用户设备关联")
    @ApiImplicitParam(name = "param", value = "参数{userId:用户ID, deviceIds:设备ID列表}", required = true)
    public HttpResult<Void> addUserDevices(@RequestBody @Validated AppMsgSetParam param) {
        String methodDescribe = getMethodDescribe("addUserDevices");
        LogUtil.njcnDebug(log, "{}，参数：{}", methodDescribe, param);
        appMsgSetService.addUserDevices(param.getUserId(), param.getDeviceIds());
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.DELETE)
    @PostMapping("/deleteByUserId")
    @ApiOperation("根据用户ID删除设备关联")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true)
    public HttpResult<Void> deleteByUserId(@RequestParam @Validated String userId) {
        String methodDescribe = getMethodDescribe("deleteByUserId");
        LogUtil.njcnDebug(log, "{}，用户ID：{}", methodDescribe, userId);
        appMsgSetService.deleteByUserId(userId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.QUERY)
    @PostMapping("/queryDeviceIdsByUserId")
    @ApiOperation("根据用户ID查询设备列表")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true)
    public HttpResult<List<String>> queryDeviceIdsByUserId(@RequestParam @Validated String userId) {
        String methodDescribe = getMethodDescribe("queryDeviceIdsByUserId");
        LogUtil.njcnDebug(log, "{}，用户ID：{}", methodDescribe, userId);
        List<String> deviceIds = appMsgSetService.queryDeviceIdsByUserId(userId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, deviceIds, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.QUERY)
    @PostMapping("/queryUserIdsByDeviceId")
    @ApiOperation("根据设备ID查询用户列表")
    @ApiImplicitParam(name = "deviceId", value = "设备ID", required = true)
    public HttpResult<List<String>> queryUserIdsByDeviceId(@RequestParam @Validated String deviceId) {
        String methodDescribe = getMethodDescribe("queryUserIdsByDeviceId");
        LogUtil.njcnDebug(log, "{}，设备ID：{}", methodDescribe, deviceId);
        List<String> userIds = appMsgSetService.queryUserIdsByDeviceId(deviceId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, userIds, methodDescribe);
    }

}

