package org.jeecg.modules.api.workorderController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.apiBean.PageBean;
import org.jeecg.modules.apiBean.UserBean;
import org.jeecg.modules.apiService.UserService;
import org.jeecg.modules.workorderUser.entity.WorkorderUser;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags="用户信息相关接口")
@RestController
@RequestMapping("/workorder/user")
@Slf4j
public class UserController {

    //用户信息
    @Resource
    private UserService userService;
    /****************************************************************************************************/

    @ApiOperation(value="用户信息-查询用户信息列表", notes="用户信息-查询用户信息列表")
    @RequestMapping(value = "/queryUserList", method = {RequestMethod.GET})
    public Result<?> queryUserList(PageBean pageBean){
        return userService.queryUserList(pageBean);
    }


    @ApiOperation(value="用户信息-根据id查询用户信息", notes="用户信息-根据id查询用户信息")
    @RequestMapping(value = "/queryUserById", method = {RequestMethod.GET})
    public Result<?> queryUserById(String userId){

        String message = "";
        //注册信息验证
        if(StringUtils.isEmpty(userId)){
            message = "用户id未填写，请填写用户id!";
        }else {
            //注册信息无误，进入业务流程处理
            return userService.queryUserById(userId);
        }

        //注册信息有误，返回错误信息
        return Result.error(message);
    }

    @ApiOperation(value="用户信息-登录", notes="用户信息-登录")
    @RequestMapping(value = "/login", method = {RequestMethod.POST})
    public Result<?> login(String userName, String password){

        String message = "";
        //注册信息验证
        if(StringUtils.isEmpty(userName)){
            message = "用户名称未填写，请填写用户名称!";
        }else if(StringUtils.isEmpty(password)){
            message = "用户密码未填写，请填写用户密码!";
        }else {
            //注册信息无误，进入业务流程处理
            return userService.login(userName, password);
        }

        //注册信息有误，返回错误信息
        return Result.error(message);
    }

    @ApiOperation(value="用户信息-注册", notes="用户信息-注册")
    @RequestMapping(value = "/register", method = {RequestMethod.POST})
    public Result<?> register(UserBean userBean){

        //将注册信息注入到实体bean中
        WorkorderUser workorderUser = new WorkorderUser();
        workorderUser.setName(userBean.getName());
        workorderUser.setPassword(userBean.getPassword());
        workorderUser.setImage(userBean.getImage());
        if(StringUtils.isNotEmpty(userBean.getRoleId())){
            workorderUser.setRoleId(userBean.getRoleId());
        }

        //返回信息
        String message = "";
        //注册信息验证
        if(StringUtils.isEmpty(userBean.getName())){
            message = "用户姓名未填写，请填写用户姓名!";
        }else if(StringUtils.isEmpty(userBean.getPassword())){
            message = "密码未填写，请填写密码!";
        }else {
            //注册信息无误，进入业务流程处理
            return userService.register(workorderUser);
        }

        //注册信息有误，返回错误信息
        return Result.error(message);
    }

    @ApiOperation(value="用户信息-资料修改", notes="用户信息-资料修改")
    @RequestMapping(value = "/updateUser", method = {RequestMethod.POST})
    public Result<?> updateUser(UserBean userBean){

        //将注册信息注入到实体bean中
        WorkorderUser workorderUser = new WorkorderUser();
        workorderUser.setId(userBean.getId());
        workorderUser.setName(userBean.getName());
        workorderUser.setPassword(userBean.getPassword());
        workorderUser.setImage(userBean.getImage());
        workorderUser.setRoleId(userBean.getRoleId());

        //返回信息
        String message = "";
        //注册信息验证
        if(StringUtils.isEmpty(workorderUser.getId())){
            message = "用户id未填写，请填写用户id!";
        }else if(StringUtils.isEmpty(workorderUser.getName())){
            message = "用户姓名未填写，请填写用户姓名!";
        }else if(StringUtils.isEmpty(workorderUser.getPassword())){
            message = "密码未填写，请填写密码!";
        }else if(StringUtils.isEmpty(workorderUser.getImage())){
            message = "头像未上传，请上传头像!";
        }else if(StringUtils.isEmpty(workorderUser.getRoleId())){
            message = "用户角色id未填写，请填写用户角色id!";
        }else {
            //注册信息无误，进入业务流程处理
            return userService.updateUser(workorderUser);
        }

        //注册信息有误，返回错误信息
        return Result.error(message);

    }

}
