package com.ynfy.app.api.v1.controller;

import com.alibaba.fastjson.JSONObject;
import com.ynfy.app.api.v1.annoation.IgnoreAuth;
import com.ynfy.app.api.v1.entity.dto.UserDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.TokenUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/api/v1/user")
@Slf4j
public class ApiUserController extends ApiBaseController {

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 用户注册
     *
     * @return
     */
    @PostMapping("/register")
    @IgnoreAuth
    public Result<?> userRegister(@RequestBody UserDTO dto) {
        if (StringUtils.isBlank(dto.getUsername())) {
            return Result.error("账号不能为空");
        }
        SysUser user1 = sysUserService.getUserByName(dto.getUsername());
        if (!Objects.isNull(user1)) {
            return Result.error("账号已被注册");
        }
        if (StringUtils.isNotBlank(dto.getPhone())) {
            SysUser user2 = sysUserService.getUserByPhone(dto.getPhone());
            if (!Objects.isNull(user2)) {
                return Result.error("手机号已被注册");
            }
        }
        if (StringUtils.isNotBlank(dto.getEmail())) {
            SysUser user3 = sysUserService.getUserByEmail(dto.getEmail());
            if (!Objects.isNull(user3)) {
                return Result.error("邮箱已被注册");
            }
        }
        SysUser user = new SysUser();
        user.setCreateTime(new Date());
        String salt = oConvertUtils.randomGen(8);
        String passwordEncode = PasswordUtil.encrypt(dto.getUsername(), dto.getPassword(), salt);
        user.setSalt(salt);
        user.setUsername(dto.getUsername());
        user.setRealname(dto.getRealname());
        user.setAvatar(dto.getAvatar());
        user.setSex(dto.getSex());
        user.setPassword(passwordEncode);
        user.setEmail(dto.getEmail());
        user.setPhone(dto.getPhone());
        user.setStatus(CommonConstant.USER_UNFREEZE);
        user.setDelFlag(CommonConstant.DEL_FLAG_0);
        sysUserService.save(user);
        return Result.ok("注册成功");
    }

    /**
     * 查询当前用户的所有部门/当前部门编码
     *
     * @return
     */
    @GetMapping(value = "/getCurrentUserDeparts")
    public Result<Map<String, Object>> getCurrentUserDeparts() {
        LoginUser sysUser = sysUserService.getLoginUser(TokenUtil.getToken(request));
        Map<String, Object> map = sysUserService.getCurrentUserDeparts(sysUser);
        return Result.OK(map);
    }

    /**
     * 切换部门
     *
     * @param user
     * @return
     */
    @PostMapping(value = "/selectDepart")
    public Result<JSONObject> selectDepart(@RequestBody SysUser user) {
        String username = user.getUsername();
        if (oConvertUtils.isEmpty(username)) {
            LoginUser sysUser = sysUserService.getLoginUser(TokenUtil.getToken(request));
            username = sysUser.getUsername();
        }
        JSONObject obj = sysUserService.updateSelectDept(user, username);
        return Result.OK(obj);
    }
}
