package com.govmade.modules.system.controller;

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

import org.apache.shiro.crypto.hash.Sha256Hash;
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.google.common.collect.Sets;
import com.govmade.common.utils.Constant;
import com.govmade.common.utils.PageUtils;
import com.govmade.common.utils.R;
import com.govmade.common.utils.ShiroUtils;
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("123456");
			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() {
		
		Set<Long> ids = Sets.newHashSet();
		ids.add(1L);
		ids.add(2L);
		userService.deleteBatch(ids);
		return R.ok();
	}

	/**
	 * 重置密码
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("/reset")
	public R reset(UserEntity ue) {
		if (null != ue.getId()) {

			String password = SecureUtil.md5("123456");
			ue.setPassword(password);
			userService.updateById(ue);
		}

		return R.ok();
	}

	/**
	 * 修改登录用户密码
	 */
	@PutMapping("updatePassword")
	public R password(String password, String newPassword) {

		password = new Sha256Hash(password).toHex();
		newPassword = new Sha256Hash(newPassword).toHex();

		// int count = userService.updatePassword(ShiroUtils.getUserId(), password,
		// newPassword);
		// if (count == 0) {
		// return R.error("原密码不正确");
		// }

		ShiroUtils.logout();

		return R.ok();
	}

}