package com.ssyf.module.robot.controller.auth;

import cn.hutool.core.bean.BeanUtil;
import com.ssyf.common.web.aop.RequestSynchronization;
import com.ssyf.common.web.result.Result;
import com.ssyf.module.auth.domain.entity.User;
import com.ssyf.module.auth.domain.vo.UserInfoVo;
import com.ssyf.module.robot.domain.entity.RRobotAccount;
import com.ssyf.module.robot.domain.entity.UserRobotRelation;
import com.ssyf.module.robot.domain.query.UserRobotAssignQuery;
import com.ssyf.module.robot.domain.query.UserRobotRelationQuery;
import com.ssyf.module.robot.domain.vo.RRobotAccountVo;
import com.ssyf.module.robot.service.UserRobotRelationService;
import com.ssyf.plugs.logger.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.List;
import java.util.stream.Collectors;

/**
 * 用户机器人关系管理接口
 *
 * @author Lingma
 */
@Api(tags = "基础-用户机器人关系管理")
@RestController
@RequestMapping("/auth/UserRobotRelation")
public class UserRobotRelationAuthController {

    @Autowired
    private UserRobotRelationService baseService;

    @Log(title = "用户机器人关系管理-分配粉丝")
    @RequestSynchronization
    @ApiOperation(value = "分配粉丝", notes = "将指定粉丝分配给用户")
    @PostMapping("/assignRobot")
    public Result<Void> assignRobotToUser(@RequestBody @Validated UserRobotAssignQuery query) {
        baseService.assignRobotToUser(query);
        return Result.success();
    }

    @Log(title = "用户机器人关系管理-清空")
    @RequestSynchronization
    @ApiOperation(value = "清空", notes = "清空用户机器人关系")
    @PostMapping("/clear")
    public Result<Void> clear(@RequestBody @Validated UserRobotAssignQuery query) {
        baseService.clear(query);
        return Result.success();
    }

    @Log(title = "用户机器人关系管理-获取列表数据")
    @ApiOperation(value = "获取列表数据", notes = "获取列表数据")
    @PostMapping(value = "/queryList")
    public Result<List<UserRobotRelation>> getList(@RequestBody UserRobotRelationQuery query) {
        List<UserRobotRelation> list = this.baseService.getList(query);
        return Result.success(list);
    }

    @Log(title = "用户机器人关系管理-获取列表数据")
    @ApiOperation(value = "获取列表数据", notes = "获取列表数据")
    @PostMapping(value = "/getRobotList")
    public Result<List<RRobotAccountVo>> getRobotList(@RequestBody UserRobotRelationQuery query) {
        List<RRobotAccount> list = this.baseService.getAccountList(query);
        List<RRobotAccountVo> voList = list.stream()
                .map(item -> BeanUtil.copyProperties(item, RRobotAccountVo.class))
                .collect(Collectors.toList());
        return Result.success(voList);
    }

    @Log(title = "用户机器人关系管理-获取关联用户信息")
    @ApiOperation(value = "获取关联用户信息", notes = "根据主播和粉丝ID获取关联的用户信息")
    @PostMapping("/getUserInfoByRobot")
    public Result<List<UserInfoVo>> getUserInfoByRobot(@RequestBody UserRobotRelationQuery query) {
        List<User> userList = baseService.getUserListByRobot(query);
        List<UserInfoVo> userVoList = userList.stream()
                .map(user -> BeanUtil.copyProperties(user, UserInfoVo.class))
                .collect(Collectors.toList());
        return Result.success(userVoList);
    }

}


