package com.neusoft.smart.pos.user.controller;

import com.neusoft.smart.pos.dto.AlterUserRequest;
import com.neusoft.smart.pos.dto.user.CreateUserRequest;
import com.neusoft.smart.pos.dto.user.UserInfoResponse;
import com.neusoft.smart.pos.dto.user.UserQuery;
import com.neusoft.smart.pos.framework.bean.PaginationExtendBean;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.user.dto.request.ChangePasswordRequest;
import com.neusoft.smart.pos.user.dto.request.PermissionGroupRequest;
import com.neusoft.smart.pos.user.persistent.domain.Permission;
import com.neusoft.smart.pos.user.persistent.domain.PermissionGroup;
import com.neusoft.smart.pos.user.persistent.domain.User;
import com.neusoft.smart.pos.user.service.PermissionService;
import com.neusoft.smart.pos.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(value = "用户接口", description = "用户相关的Restful接口。")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    PermissionService permissionService;

    @GetMapping
    @ApiOperation(value = "查询用户", notes = "根据用户名查询用户详情")
    public User getUser(@RequestParam("username") String username) {
        return userService.selectByUsername(username);
    }

    @GetMapping("/me")
    public ResponseData<UserInfoResponse> userinfo(@RequestHeader("X-AUTH-USERNAME") String username) {
        return new ResponseData<>(userService.userinfo(username));
    }

    @GetMapping("/permissions")
    @ApiOperation(value = "查询权限", notes = "根据用户名查询权限列表")
    public List<Permission> getPermissions(@RequestParam("username") String username) {
        return userService.selectPersissionsByUsername(username);
    }

    @GetMapping("/all")
    public ResponseData<PaginationExtendBean<User>> all(UserQuery query) {
        PaginationExtendBean<User> users = new PaginationExtendBean<>();
        users.setRows(userService.list(query));
        users.setTotal(userService.count(query));
        return new ResponseData<>(users);
    }

    @GetMapping("/single")
    public ResponseData single(Integer id) {
        return new ResponseData(userService.getSingle(id));
    }

    @GetMapping("/permissionGroups")
    @ApiOperation(value = "查询权限组", notes = "根据用户名查询权限组列表")
    public List<PermissionGroup> getPermissionGroups(String username) {
        return userService.selectPersissionGroupssByUsername(username);
    }

    @PostMapping("/createUser")
    @ApiOperation(value = "创建用户", notes = "创建用户")
    public ResponseData createUser(@Valid @RequestBody CreateUserRequest createUserRequest) {
        User user = new User();
        user.setUsername(createUserRequest.getUsername());
        user.setPassword(createUserRequest.getPassword());
        user.setRole(createUserRequest.getRole());
        user.setOrg(createUserRequest.getOrg());
        user.setNickname(createUserRequest.getNickname());
        userService.createUser(user);
        if (createUserRequest.getShopIds() != null && !createUserRequest.getShopIds().isEmpty())
            userService.addShops(user, createUserRequest.getShopIds());
        return new ResponseData(user.getId());
    }

    @PostMapping("/alter")
    public ResponseData alterUser(@RequestBody AlterUserRequest alterUserRequest) {
        userService.alterUser(alterUserRequest);
        return new ResponseData();
    }

    @DeleteMapping("/del/{id}")
    public ResponseData deleteUser(@PathVariable Integer id) {
        userService.deleteUser(id);
        return new ResponseData();
    }

    @PostMapping("/changePassword")
    @ApiOperation(value = "修改密码", notes = "修改密码")
    public ResponseData changePassword(@Valid @RequestBody ChangePasswordRequest changePasswordRequest) {
        int i = userService.changePassword(changePasswordRequest.getUsername(), changePasswordRequest.getPassword());
        if (i > 0) return new ResponseData();
        return new ResponseData("操作失败");
    }

    @PostMapping("/addUserPermissionGroup")
    @ApiOperation(value = "分配权限组", notes = "为用户分配新的权限组")
    public ResponseData addUserPermissionGroup(@Valid @RequestBody PermissionGroupRequest permissionGroupRequest) {
        int i = permissionService.addUserPermissionGroup(permissionGroupRequest.getUserId(), permissionGroupRequest.getPermissionGroupId());
        if (i > 0) return new ResponseData();
        return new ResponseData("操作失败");
    }

    @PostMapping("/removeUserPermissionGroup")
    @ApiOperation(value = "移除权限组", notes = "将用户移出权限组")
    public int removeUserPermissionGroup(@Valid @RequestBody PermissionGroupRequest permissionGroupRequest) {
        return permissionService.removeUserPermissionGroup(permissionGroupRequest.getUserId(), permissionGroupRequest.getPermissionGroupId());
    }

    @GetMapping("/loadUserData2Redis")
    @ApiOperation(value = "全部用户", notes = "全部用户")
    public String loadUserData2Redis() {
        userService.loadUserData2Redis();
        return "OK";
    }

    @GetMapping("/loadUserData2RedisByShop")
    public String loadUserData2RedisByShop(@RequestParam("shopId") Integer shopId) {
        userService.loadUserData2RedisByShop(shopId);
        return "OK";
    }

    @GetMapping("/loadUserData2RedisByOrg")
    public String loadUserData2RedisByOrg(@RequestParam("orgId") Integer orgId) {
        userService.loadUserData2RedisByOrg(orgId);
        return "OK";
    }
}
