package com.sy.coinage.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sy.coinage.core.annotation.CurrentUser;
import com.sy.coinage.core.aop.annotation.Operation;
import com.sy.coinage.core.aop.annotation.Permission;
import com.sy.coinage.core.constants.Constants;
import com.sy.coinage.core.controller.BaseController;
import com.sy.coinage.core.exception.RetException;
import com.sy.coinage.core.interceptor.Create;
import com.sy.coinage.core.interceptor.Update;
import com.sy.coinage.core.req.IdReq;
import com.sy.coinage.core.util.MD5Util;
import com.sy.coinage.core.vo.CacheUserVO;
import com.sy.coinage.core.vo.OptVO;
import com.sy.coinage.sys.model.User;
import com.sy.coinage.sys.model.UserExt;
import com.sy.coinage.sys.req.AdminUserListReq;
import com.sy.coinage.sys.req.AdminUserOptReq;
import com.sy.coinage.sys.req.AdminUserReq;
import com.sy.coinage.sys.req.ResetPwdReq;
import com.sy.coinage.sys.req.UserListReq;
import com.sy.coinage.sys.req.UserOptReq;
import com.sy.coinage.sys.req.UserReq;
import com.sy.coinage.sys.service.IUserExtService;
import com.sy.coinage.sys.service.IUserService;
import com.sy.coinage.sys.vo.UserListVO;
import com.sy.coinage.sys.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户Controller
 */
@Api(value = "用户接口", tags = "014_用户")
@RestController
@RequestMapping(value = "/user")
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserExtService userExtService;

    /**
     * 获取用户详情
     */
    @PostMapping("/get")
    public UserVO get(@RequestBody @Valid IdReq req) {
        UserVO data = null;
        User model = this.userService.getById(req.getId());
        data = new UserVO();
        BeanUtils.copyProperties(model, data);
        UserExt userExt = userExtService.getById(req.getId());
        if (userExt != null) {
            BeanUtils.copyProperties(userExt, data);
        }
        data.setRoleIds(this.userService.listRoleIds(req.getId()));
        return data;
    }

    /**
     * 用户新增
     */
    @Operation(value = "新增用户")
    @PostMapping("/add")
    @Permission("user:add")
    public Object add(@RequestBody @Validated({Create.class}) UserReq req, @CurrentUser CacheUserVO user) {
        this.userService.validUsername(null, req.getUsername());
        this.userService.add(req, user);
        return "";
    }

    /**
     * 用户分页列表
     */
    @PostMapping("/list")
    public IPage<UserListVO> list(@RequestBody UserListReq req, @CurrentUser CacheUserVO user) {
        IPage<UserListVO> data = this.userService.queryPageList(req, user);
        return data;
    }

    /**
     * 用户修改
     */
    @Operation(value = "修改用户")
    @PostMapping("/update")
    @Permission("user:update")
    public Object update(@RequestBody @Validated({Update.class}) UserReq req, @CurrentUser CacheUserVO user) {
        User model = this.userService.getById(req.getId());
        // 验证是否相同租户
        this.validTenant(user, model.getTenantId());
        this.userService.validUsername(req.getId(), req.getUsername());
        this.userService.update(req, user);
        return "";
    }

    /**
     * 用户修改状态
     */
    @Operation(value = "修改用户状态")
    @PostMapping("/status")
    @Permission("user:update")
    public Integer status(@RequestBody @Valid IdReq req, @CurrentUser CacheUserVO user) {
        User model = this.userService.getById(req.getId());
        // 验证是否相同租户
        this.validTenant(user, model.getTenantId());
        if (null != model.getStatus() && model.getStatus().intValue() == Constants.STATUS_FORBIDDEN) {
            model.setStatus(Constants.STATUS_USABLE);
        } else {
            model.setStatus(Constants.STATUS_FORBIDDEN);
        }
        this.userService.updateById(model);
        return model.getStatus();
    }

    /**
     * 删除用户
     */
    @Operation(value = "删除用户")
    @PostMapping("/delete")
    @Permission("user:delete")
    public Object delete(@RequestBody @Valid IdReq req, @CurrentUser CacheUserVO user) {
        if (user.getId().intValue() == req.getId().intValue()) {
            throw new RetException("不能删除当前用户");
        }
        User model = this.userService.getById(req.getId());
        // 验证是否相同租户
        this.validTenant(user, model.getTenantId());
        model.setDelFlag(Constants.STATUS_FORBIDDEN);
        this.userService.updateById(model);
        return "";
    }

    /**
     * 根据用户id获取用户的角色id集合
     */
    @PostMapping("/roleIds")
    public List<Integer> listRoleIds(@RequestBody @Valid IdReq req) {
        List<Integer> data = this.userService.listRoleIds(req.getId());
        return data;
    }

    /**
     * 用户列表项
     */
    @ApiOperation(value = "用户列表项")
    @PostMapping("/opt")
    public List<OptVO> opt(@RequestBody UserOptReq req, @CurrentUser CacheUserVO user) {
        List<OptVO> data = this.userService.opt(req.getOrgId(), req.getDeptId(), user.getTenantId());
        return data;
    }

    /**
     * 重置用户密码
     */
    @ApiOperation(value = "重置用户密码")
    @PostMapping("/resetPwd")
    @Permission("user:pwd")
    public Object resetPwd(@RequestBody @Valid ResetPwdReq req, @CurrentUser CacheUserVO user) {
        User model = this.userService.getById(req.getId());
        // 验证是否相同租户
        this.validTenant(user, model.getTenantId());
        if (req.getId().equals(user.getId())) {
            throw new RetException("不能重置当前账号的密码");
        }
        // 密码加密
        String pwd = MD5Util.MD5(req.getPassword() + model.getSalt());
        model.setPassword(pwd);
        this.userService.updateById(model);
        return "";
    }

    /**
     * 根据角色获取用户列表
     */
    @PostMapping("/opt/byRoleId")
    public List<OptVO> listByRoleId(@RequestBody @Valid IdReq req) {
        List<OptVO> data = this.userService.byRoleId(req.getId());
        return data;
    }


    /**
     * 租户管理员-用户分页列表
     */
    @ApiOperation(value = "租户管理员-用户分页列表")
    @PostMapping("/admin/list")
    public IPage<UserListVO> adminList(@RequestBody AdminUserListReq req) {
        IPage<UserListVO> data = this.userService.adminQueryPageList(req);
        return data;
    }

    /**
     * 租户管理员-新增用户
     */
    @ApiOperation(value = "租户管理员-新增用户")
    @Operation(value = "租户管理员-新增用户")
    @PostMapping("/admin/add")
    public Object adminAdd(@RequestBody @Validated({Create.class}) AdminUserReq req, @CurrentUser CacheUserVO user) {
        // 验证是否租户管理员
        this.validAdmin(user);
        this.userService.validUsername(null, req.getUsername());
        this.userService.adminAdd(req, user);
        return "";
    }

    /**
     * 租户管理员-新增用户
     */
    @ApiOperation(value = "租户管理员-用户列表项")
    @Operation(value = "租户管理员-用户列表项")
    @PostMapping("/admin/opt")
    public List<OptVO> adminOpt(@RequestBody @Valid AdminUserOptReq req) {
        List<OptVO> data = this.userService.opt(req.getOrgId(), req.getDeptId(), req.getTenantId());
        return data;
    }

}
