package com.css.simulation.resource.server.adapter.controller.system_manage.account_manage;

import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.param.system.UserParam;
import api.common.pojo.po.system.UserPO;
import api.common.pojo.vo.system.UserVO;
import api.common.util.ObjectUtil;
import com.css.simulation.resource.server.app.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;


@Controller()
@RequestMapping("/user")
@PreAuthorize("@AuthorityCheck.adminAndUser()")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 获取人员列表
     */
    @RequestMapping("/getUserPageList")
    @ResponseBody
    public ResponseBodyVO<PageInfo<UserVO>> getUserPageList(@RequestBody UserParam pageParam){
        ResponseBodyVO<PageInfo<UserVO>> response = new ResponseBodyVO<PageInfo<UserVO>>(ResponseBodyVO.Response.SUCCESS);
        response.setInfo(userService.getUserPageList(pageParam));
        return response;
    }

    /**
     * 新增、修改
     */
    @RequestMapping("/saveUser")
    @ResponseBody
    public ResponseBodyVO<UserPO> saveUser(@RequestBody UserPO userPO) {
        if(ObjectUtil.isNull(userPO)){
            return new ResponseBodyVO<>(false, 400, "参数必传！",null);
        }
        ResponseBodyVO<UserPO> response = new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        userPO = userService.saveUser(userPO);
        if(ObjectUtil.isNull(userPO)){
            return new ResponseBodyVO<>(false, 400, "没有创建账户的权限！",null);
        }
        if("checkUsername".equals(userPO.getId())){
            return new ResponseBodyVO<>(false, 400, "账户名重复！",null);
        }
        if("subUserNum".equals(userPO.getId())){
            return new ResponseBodyVO<>(false, 400, "超出可创建子账户数量！",null);
        }
        response.setInfo(userPO);
        return response;
    }

    /**
     * 人员密码重置
     */
    @RequestMapping("/saveDefaultPassword")
    @ResponseBody
    public ResponseBodyVO<String> saveDefaultPassword(@RequestBody UserPO userPO) {
        if(ObjectUtil.isNull(userPO)||ObjectUtil.isNull(userPO.getId())){
            return new ResponseBodyVO<>(false, 400, "参数必传！",null);
        }
        userService.saveDefaultPassword(userPO);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }

    /**
     * 启用/停用账户
     */
    @RequestMapping("/saveVisible")
    @ResponseBody
    public ResponseBodyVO<String> saveVisible(@RequestBody UserPO userPO) throws Exception {
        if(ObjectUtil.isNull(userPO)){
            return new ResponseBodyVO<>(false, 400, "参数必传！",null);
        }
        if(ObjectUtil.isNull(userPO.getId()) || ObjectUtil.isNull(userPO.getVisible())){
            return new ResponseBodyVO<>(false, 400, "参数必传！",null);
        }
        userService.saveVisible(userPO);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }


}
