package com.govmade.modules.system.controller;

import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.govmade.common.utils.Constant;
import com.govmade.common.utils.PageUtils;
import com.govmade.common.utils.R;
import com.govmade.common.validator.Assert;
import com.govmade.modules.basic.controller.AbstractController;
import com.govmade.modules.system.entity.UserEntity;
import com.govmade.modules.system.service.UserService;

import cn.hutool.crypto.SecureUtil;

/**
 * 系统管理 - 用户设置
 * 
 * @author Fred
 * @email fangtaosh@qq.com
 * @date 2018年8月3日
 */
@RestController
@RequestMapping("/system/user")
public class UserController extends AbstractController {

	@Autowired
	private UserService userService;

	/**
	 * 保存或更新用户
	 */
	@PostMapping("/save")
	public R save(UserEntity ue) {

		if (null == ue.getId()) {
			if (this.userService.checkUser(ue.getUsername()) > 0) {
				return R.error(ue.getUsername() + " 已存在!");
			}
			String password = SecureUtil.md5(Constant.DEFAULT_PASSWORD);
			ue.setPassword(password);
		}
		userService.save(ue);

		return R.ok();
	}

	/**
	 * 用户列表
	 */
	@GetMapping("/list")
	public R list(Map<String, Object> params) {

		// 只有超级管理员,才能查看所有用户列表
		if (getUserId() == Constant.SUPER_ADMIN) {
			params.put("createBy", getUserId());
		} else {
			// params.put("deptId", getUser().getDeptId()); // 部门管理员,查看本部门用户列表
		}
		PageUtils page = userService.queryPage(params);

		return R.ok().put("page", page);

	}

	/**
	 * 根据ID查询用户
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("info/{id}")
	public R info(@PathVariable("id") Long id) {
		return R.ok().put("user", userService.queryById(id));
	}

	/**
	 * 批量删除用户
	 * 
	 * @param ids
	 * @return
	 */
	@DeleteMapping("/delete")
	public R deleteUser(@RequestBody Set<Long> ids) {
		Assert.isNull(ids, "删除项不能为空");

		userService.deleteBatch(ids);
		return R.ok();
	}

	/**
	 * 重置密码
	 * 
	 * @param id
	 * @return
	 */
	@PutMapping("/reset")
	public R reset(Long id) {
		Assert.isNull(id, "必选选择一个用户");

		String password = SecureUtil.md5(Constant.DEFAULT_PASSWORD);
		UserEntity ue = new UserEntity();
		ue.setPassword(password);
		userService.updateById(ue);

		return R.ok();
	}

	/**
	 * 修改登录用户密码
	 */
	@PutMapping("updatePassword")
	public R password(String password, String newPassword) {
		Assert.isBlank(newPassword, "新密码不为能空");

		password = SecureUtil.md5(password);
		newPassword = SecureUtil.md5(newPassword);

		// 更新密码
		boolean flag = userService.updatePassword(getUserId(), password, newPassword);

		if (!flag) {
			return R.error("原密码不正确");
		}
		return R.ok();
	}

}