package com.njcn.user.controller.app;


import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.user.pojo.param.AppInfoSetParam;
import com.njcn.user.pojo.po.app.AppInfoSet;
import com.njcn.user.service.IAppInfoSetService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
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.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 * 用户推送消息配置
 * @author xuyang
 * @since 2023-08-21
 */
@RestController
@RequestMapping("/appInfoSet")
@Api(tags = "用户推送消息配置")
@AllArgsConstructor
@Validated
public class AppInfoSetController extends BaseController {

    private final IAppInfoSetService appInfoSetService;

    @ApiIgnore
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/add")
    @ApiOperation("新增用户消息推送配置")
    @ApiImplicitParam(name = "appInfoSet", value = "参数实体", required = true)
    public HttpResult<String> add(@RequestBody AppInfoSet appInfoSet){
        String methodDescribe = getMethodDescribe("add");
        appInfoSetService.save(appInfoSet);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, "success", methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryByUserId")
    @ApiOperation("查看用户消息推送配置")
    public HttpResult<AppInfoSet> queryByUserId(){
        String methodDescribe = getMethodDescribe("queryByUserId");
        AppInfoSet appInfoSet = appInfoSetService.lambdaQuery().eq(AppInfoSet::getUserId, RequestUtil.getUserIndex()).one();
        if (Objects.isNull(appInfoSet)) {
            throw new BusinessException("非手机号注册的用户,需联系管理员添加配置!");
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, appInfoSet, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/update")
    @ApiOperation("更新消息推送配置")
    @ApiImplicitParam(name = "param", value = "参数实体", required = true)
    public HttpResult<String> update(@RequestBody AppInfoSetParam.AppInfoSetUpdateParam param){
        String methodDescribe = getMethodDescribe("update");
        appInfoSetService.updateAppInfo(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getListById")
    @ApiOperation("根据用户id查询消息配置信息")
    @ApiImplicitParam(name = "list", value = "用户集合", required = true)
    public HttpResult<List<AppInfoSet>> getListById(@RequestBody List<String> list){
        String methodDescribe = getMethodDescribe("getListById");
        List<AppInfoSet> result = appInfoSetService.getListById(list);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

}

