package com.yida.data.system.controller;

import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.system.SystemUser;
import com.yida.data.system.service.IUserRoleService;
import com.yida.data.system.service.IUserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/in/user")
public class InUserController {

    private final IUserRoleService userRoleService;
    private final IUserService userService;

    @ApiOperation("根据手机号绑定角色关系")
    @GetMapping("/bindRoleByMobile")
    public ResultBean bindRoleByMobile(@RequestParam String mobile, @RequestParam String roleName) {
        userRoleService.bindRoleByMobile(mobile, roleName);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("根据微信号绑定角色关系")
    @GetMapping("/bindRoleByUser")
    public ResultBean bindRoleByUser(@RequestParam Long sysUserId, @RequestParam String roleName) {
        userRoleService.bindRoleByUser(sysUserId, roleName);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("/查询用户信息")
    @GetMapping("/getInfoByUsername")
    public ResultBean<SystemUser> getInfoByUsername(String username) {
        return ResultBean.buildSuccess(userService.findUserDetail(username));
    }

    @ApiOperation("添加用户")
    @PostMapping("/add")
    public ResultBean add(@RequestBody SystemUser systemUser) {
        return ResultBean.buildSuccess(userService.createUser(systemUser));
    }

    @ApiOperation("订阅易达app")
    @GetMapping("/subscribeYidaApp")
    public ResultBean subscribeYidaApp(@ApiParam("易达app用户id") String userId,
        @ApiParam("学校id") Long schoolId,
        @ApiParam("订阅号类型，0-家长，1-老师") Integer type) {
        userService.subscribeYidaApp(userId, schoolId, type);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("根据角色名修改用户角色")
    @PostMapping("/updateUserRoleByRoleName")
    public ResultBean updateUserRoleByRoleName(@RequestParam Long userId, @RequestBody List<String> roleNames) {
        userService.updateUserRoleByRoleName(userId, roleNames);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("获取企业微信用户授权后创建职工用户信息")
    @GetMapping("createStaff")
    public ResultBean<String> createStaff(@ApiParam("学校Id") Long schoolId,
                                          @ApiParam("通过成员授权获取到的code") String code) {
        userService.createStaff(schoolId, code);
        return ResultBean.buildSuccess();
    }
}
