package com.dk.mdm.controller.core;

import com.alibaba.fastjson.JSONObject;
import com.dk.common.model.pojo.PageList;
import com.dk.common.model.vo.core.StaffEntity;
import com.dk.common.model.vo.core.UserVO;
import com.dk.common.response.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.common.service.BaseService;
import com.dk.mdm.infrastructure.util.AuthUtils;
import com.dk.mdm.model.pojo.core.User;
import com.dk.mdm.model.query.core.UserQuery;
import com.dk.common.model.response.core.UserResponse;
import com.dk.mdm.model.vo.core.UserRightVO;
import com.dk.mdm.service.core.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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

@Api(tags = "用户API接口")
@RestController
@RequestMapping("/core/user")
public class UserController{

    public BaseService<User> getService() {
        return userService;
    }

    @Autowired
    private UserService userService;

    @Autowired
    private AuthUtils authUtils;

    /**
     * @desc   : 条件查询
     * @author : 姜宁
     * @date   : 2023/1/29 11:14
     */
    @ApiOperation( value = "分页、关联、条件查询", notes = "分页、关联、条件查询" )
    @PostMapping({"list_by"})
    public ResponseResultVO<PageList<UserResponse>> selectByCond(@RequestBody UserQuery userQuery) {
        return userService.selectByCond(userQuery);
    }

    /**
     * @desc   : 登录
     * @author : 周兴
     * @date   : 2023/1/5 10:09
     */
    @PostMapping("login")
    public ResponseResultVO login(@Valid @RequestBody UserVO userVO) {
        return userService.login(userVO);
    }

    /**
     * @desc : 退出登录
     * @author : 周兴
     * @date : 2023/1/5 10:09
     */
    @PostMapping("logout")
    public ResponseResultVO logout() {
        return userService.logout();
    }

    /**
     * @desc   : 登录后校验
     * @author : 周兴
     * @date   : 2023/1/6 9:20
     */
    @PostMapping("login_after_check_password")
    public ResponseResultVO<JSONObject> loginAfterCheckPassword(@RequestBody JSONObject jsonObject) {
        return userService.loginAfterCheckPassword(jsonObject.getObject("user", UserResponse.class),
                jsonObject.getObject("accessToken",String.class));
    }

    /**
     * @desc : 获取当前用户
     * @author : 洪旭东
     * @date : 2022-06-07 16:10
     */
    @PostMapping("get_current")
    public ResponseResultVO<StaffEntity> getCurrent() {
        return ResponseResultUtil.success(authUtils.getStaff());
    }

    /**
     * @desc : 修改密码
     * @author : 常皓宁
     * @date : 2023-01-31 10:15
     */
    @PostMapping("update_password")
    public ResponseResultVO updatePassword(@RequestBody UserVO userVO){
        return userService.updatePassword(userVO);
    }

    /**
     * @desc   : 新建用户
     * @author : 姜宁
     * @date   : 2023/2/1 14:55
     */
    @ApiOperation( value = "新建用户", notes = "新建用户" )
    @PostMapping({"insert"})
    @Transactional(rollbackFor = Exception.class)
    public ResponseResultVO<?> insert(@RequestBody com.dk.mdm.model.vo.core.UserVO userVO) {
        return userService.insert(userVO);
    }

    /**
     * @desc   : 编辑用户
     * @author : 姜宁
     * @date   : 2023/2/1 14:56
     */
    @ApiOperation( value = "编辑用户", notes = "编辑用户" )
    @PostMapping({"update"})
    @Transactional(rollbackFor = Exception.class)
    public ResponseResultVO<Boolean> update(@RequestBody com.dk.mdm.model.vo.core.UserVO userVO) {
        return userService.update(userVO);
    }

    /**
     * @desc   : 停用
     * @author : 姜宁
     * @date   : 2023/2/1 14:58
     */
    @ApiOperation(value = "停用", notes = "停用")
    @PostMapping("disable/{id}")
    public ResponseResultVO<Boolean> disable(@PathVariable String id) {
        return this.getService().disable(id);
    }

    /**
     * @desc   : 启用
     * @author : 姜宁
     * @date   : 2023/2/1 14:58
     */
    @ApiOperation(value = "启用", notes = "启用")
    @PostMapping("enable/{id}")
    public ResponseResultVO<Boolean> enable(@PathVariable String id) {
        return this.getService().enable(id);
    }

   /**
    * @desc   : 重置密码
    * @author : 姜宁
    * @date   : 2023/2/2 9:51
    */
    @ApiOperation(value = "重置密码", notes = "重置密码")
    @PostMapping("reset_password")
    public ResponseResultVO<Boolean> resetPassword(@RequestBody com.dk.mdm.model.vo.core.UserVO userVO) {
        return userService.resetPassword(userVO);
    }

    /**
     * @desc   : 解锁
     * @author : 姜宁
     * @date   : 2023/2/2 11:29
     */
    @ApiOperation(value = "解锁", notes = "解锁")
    @PostMapping("un_lock")
    public ResponseResultVO<Boolean> unLock(@RequestBody com.dk.mdm.model.vo.core.UserVO userVO) {
        return userService.unLock(userVO);
    }

    /**
     * @desc   : 获取用户权限
     * @author : 姜宁
     * @date   : 2023/2/2 15:23
     */
    @ApiOperation(value = "获取用户权限", notes = "获取用户权限")
    @PostMapping("get_user_right")
    public ResponseResultVO getUserRight(@RequestBody Map<String, Object> param) {
        return userService.getUserRight(param);
    }

    /**
     * @desc   : 获取已选的用户权限
     * @author : 周兴
     * @date   : 2023/2/7 15:23
     */
    @ApiOperation(value = "获取已选的用户权限", notes = "获取已选的用户权限")
    @PostMapping("get_user_right_selected")
    public ResponseResultVO<List<Map<String, Object>>> getUserRightSelected(@RequestBody Map<String, Object> param) {
        return userService.getUserRightSelected(param);
    }

    /**
     * @desc   : 通过ID查询
     * @author : 姜宁
     * @date   : 2023/2/3 13:32
     */
    @PostMapping({"{id}"})
    public ResponseResultVO selectById(@PathVariable Integer id) {
        return userService.selectById(id);
    }

    /**
     * @desc   : 保存用户权限
     * @author : 姜宁
     * @date   : 2023/2/3 15:14
     */
    @ApiOperation( value = "保存用户权限", notes = "保存用户权限" )
    @PostMapping({"save_user_right"})
    public ResponseResultVO<?> saveUserRight(@RequestBody List<UserRightVO> userRightVO) {
        return userService.saveUserRight(userRightVO);
    }

    /**
     * @desc   : 通过ID查询班组信息
     * @author : 姜宁
     * @date   : 2023/2/7 9:27
     */
    @ApiOperation( value = "通过ID查询班组信息", notes = "通过ID查询班组信息" )
    @PostMapping({"get_work_team_by_user_id/{id}"})
    public ResponseResultVO<List<Map<String, Object>>> getWorkTeamByUserId(@PathVariable Integer id) {
        return userService.getWorkTeamByUserId(id);
    }

   /**
    * @desc   : 批量启用用户
    * @author : 夏常明
    * @date   : 2023/2/10 11:15
    */
    @ApiOperation( value = "批量启用用户", notes = "批量启用用户" )
    @PostMapping({"user_enable"})
    public ResponseResultVO<String> userEnable(@RequestBody List<UserVO> param) {
        return userService.userEnable(param);
    }

    /**
     * @desc   : 批量禁用用户
     * @author : 姜宁
     * @date   : 2023/2/10 11:15
     */
    @ApiOperation( value = "批量禁用用户", notes = "批量禁用用户" )
    @PostMapping({"user_disable"})
    public ResponseResultVO<String> userDisable(@RequestBody List<UserVO> param) {
        return userService.userDisable(param);
    }
}
