package com.bonus.zlpt.company.controller;

import com.bonus.zlpt.common.core.web.controller.BaseController;
import com.bonus.zlpt.common.core.web.domain.AjaxResult;
import com.bonus.zlpt.common.security.utils.SecurityUtils;
import com.bonus.zlpt.system.api.RemoteUserService;
import com.bonus.zlpt.system.api.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/sun")
@Api(value = "子账号",tags = {"子账号管理"})
public class SunAccountController extends BaseController {

    @Autowired
    private RemoteUserService remoteUserService;

    /**
     * 获取此账号下的子账号信息
     * @param
     * @return
     */
    @GetMapping("/selectUserList")
    @ApiOperation("获取此账号下的子账号信息")
    public List<SysUser> selectUserList() {
        SysUser sysUser = new SysUser();
        int parentId = Math.toIntExact(SecurityUtils.getUserId());
        sysUser.setParentId(parentId);
        List<SysUser> sysUsersList = (List<SysUser>) remoteUserService.list(sysUser).getRows();
        return sysUsersList;
    }

    /**
     * 添加子账号
     * @param sysUser
     * @return
     */
    @PostMapping("/sunAdd")
    @ApiOperation("添加子账号")
    public AjaxResult sunAdd(@RequestBody SysUser sysUser) {

        int parentId = Math.toIntExact(SecurityUtils.getUserId());
        sysUser.setParentId(parentId);
        remoteUserService.add(sysUser);

        return success("添加成功！");
    }

    /**
     * 修改子账号状态
     * @param sysUser
     * @return
     */
    @PostMapping("/updateUserStatus")
    @ApiOperation("修改子账号状态")
    public AjaxResult updateUserStatus(@RequestBody SysUser sysUser) {

        remoteUserService.edit(sysUser);
        return success("修改成功！");
    }

    /**
     * 删除子账号
     * @param
     * @return
     */
    @PostMapping("/deleteUserById/{id}")
    @ApiOperation("删除子账号")
    public AjaxResult deleteUserById(@PathVariable Long id) {

        remoteUserService.remove(new Long[]{id});
        return success("删除成功！");
    }


}
