package cn.wisenergy.web.admin.controller.app;

import cn.wisenergy.common.utils.exception.*;
import cn.wisenergy.model.dto.OrganizationDto;
import cn.wisenergy.model.dto.ResultUser;
import cn.wisenergy.service.WorkUserService;
import cn.wisenergy.web.admin.controller.common.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: nh
 * @create: 2021-01-19 17:13
 **/
@RestController
@Api(tags = "用户模块")
@Slf4j
public class WorkUserController extends BaseController {
    @Autowired
    private WorkUserService workUserService;


    @ApiOperation(value = "根据id获取用户信息", notes = "根据id获取用户信息", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "用户id", dataType = "int", required = true)
    @GetMapping(value = "/getUserInfo")
    public Result<ResultUser> getUserInfo(Integer id) {
        log.info("UserController[]getUserInfo[]input.param.id" + id);
        if (null == id) {
            throw new CustomException(RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        ResultUser resultUser = workUserService.getResultUserById(id);
        return getResult(resultUser);
    }

    @ApiOperation(value = "用户登录", notes = "用户登录", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginName", value = "登录名", dataType = "string", required = true),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "string", required = true),
            @ApiImplicitParam(name = "source", value = "登录来源：PC/WAP", dataType = "string", required = true)
    })
    @GetMapping(value = "/login")
    public Result<ResultUser> login(String loginName, String password, String source) {
        log.info("WorkUserController[]login[]input.param" + loginName + password + source);
        if (StringUtils.isEmpty(loginName) || StringUtils.isEmpty(password) || StringUtils.isEmpty(source)) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        ResultUser resultUser = workUserService.login(loginName, password, source);
        return getResult(resultUser);
    }

    @ApiOperation(value = "修改密码", notes = "修改用户登录密码", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户主键", dataType = "integer"),
            @ApiImplicitParam(name = "oldPassword", value = "原密码", dataType = "string"),
            @ApiImplicitParam(name = "newPassword", value = "新密码", dataType = "string"),
    })
    @PutMapping(value = "/changePassword")
    public Result<ResultUser> changePassword(Integer userId, String oldPassword, String newPassword) {
        log.info("WorkUserController[]changePassword[]input.param{}" + userId + oldPassword + newPassword);
        if (userId == null || StringUtils.isEmpty(oldPassword) || StringUtils.isEmpty(newPassword)) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        ResultUser resultUser = workUserService.changePassword(userId, oldPassword, newPassword);
        return getResult(resultUser);
    }

    @ApiOperation(value = "组织架构", notes = "组织架构", httpMethod = "GET")
    @GetMapping(value = "/getOrganizationStructureDto")
    public Result<OrganizationDto> getOrganizationStructureDto(){
        log.info("WorkUserController[]getOrganizationStructureDto[]");
        OrganizationDto organizationDto = workUserService.getOrganizationStructure();
        return getResult(organizationDto);
    }

}
