package com.pj.project4sp.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pj.current.satoken.AuthConst;
import com.pj.project4sp.SP;
import com.pj.project4sp.admin4password.SpAdminPasswordService;
import com.pj.project4sp.role.SpRoleUtil;
import com.pj.utils.sg.AjaxError;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.so.SoMap;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;

/**
 * Controller -- 系统管理员表
 * @author kong
 */
@RestController
@RequestMapping("/admin/")
public class SpAdminController {

	@Autowired
	SpAdminMapper spAdminMapper;
	
	@Autowired
	SpAdminService spAdminService;
	
	@Autowired
	SpAdminPasswordService spAdminPasswordService;

	/** 增  */
	@RequestMapping("add")
	@SaCheckPermission(AuthConst.ADMIN_ADD)
	AjaxJson add(SpAdmin admin){
		long id = spAdminService.add(admin);
		return AjaxJson.getSuccessData(id);
	}

	/** 删 */
	@RequestMapping("delete")
	@SaCheckPermission(AuthConst.ADMIN_LIST)
	AjaxJson delete(long id){
		// 不能自己删除自己
		if(StpUtil.getLoginIdAsLong() == id) {
			return AjaxJson.getError("不能自己删除自己");
		}
		int line = spAdminMapper.delete(id);
		return AjaxJson.getByLine(line);
	}

	/** 删 - 根据id列表 */
	@RequestMapping("deleteByIds")
	@SaCheckPermission(AuthConst.ADMIN_LIST)
	AjaxJson deleteByIds(){
		// 不能自己删除自己
		List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class); 
		if(ids.contains(StpUtil.getLoginIdAsLong())) {
			return AjaxJson.getError("不能自己删除自己");
		}
		// 开始删除 
		int line = SP.publicMapper.deleteByIds("sp_admin", ids);
		return AjaxJson.getByLine(line);
	}
	
	/** 修改 */
	@RequestMapping("update")
	@SaCheckPermission(AuthConst.ADMIN_LIST)
	AjaxJson update(SpAdmin obj){
		SpAdminUtil.checkAdmin(obj);

		// 如果不能修改
		if(obj.getId() == StpUtil.getLoginIdAsLong()) {
			SpAdmin a2 = spAdminMapper.getById(obj.getId());
			if((long)a2.getRoleId() != obj.getRoleId()) {
				return AjaxJson.getError("不能修改自己的角色");
			}
		}
		
		// 修改
		int line = spAdminMapper.update(obj);
		AjaxError.notLine(line, "修改失败");

		// 清除这个账号的角色id值缓存 
		SpRoleUtil.clearRoleIdCache(obj.getId());
		
		// 返回  
		return AjaxJson.getSuccess();
	}

	/** 改密码 */
	@RequestMapping("updatePassword")
	@SaCheckPermission({AuthConst.ADMIN_LIST, AuthConst.ROOT})
	AjaxJson updatePassword(long id, String password){
		int line = spAdminPasswordService.updatePassword(id, password);
		return AjaxJson.getByLine(line);
	}
	
	/** 改状态  */
	@RequestMapping("updateStatus")
	@SaCheckPermission({AuthConst.ADMIN_LIST, AuthConst.ROOT})
	public AjaxJson updateStatus(long id, int status) {

		// 验证对方是否具有最高权限 
		if(StpUtil.hasPermission(id, AuthConst.ROOT)){
			return AjaxJson.getError("抱歉，对方账号拥有系统最高权限，您暂时无法完成此操作");
		}
		
		// 修改状态 
		SP.publicMapper.updateColumnById("sp_admin", "status", status, id);
		// 如果是禁用，就将其强制注销 
		if(status == 2) {
			StpUtil.logout(id);
		}
		return AjaxJson.getSuccess();
	}
	
	/** 查  */
	@RequestMapping("getById")
	@SaCheckPermission(AuthConst.ADMIN_LIST)
	AjaxJson getById(long id){
		Object data = spAdminMapper.getById(id);
		return AjaxJson.getSuccessData(data);
	}

	/** 返回当前 Admin 信息  */
	@RequestMapping("getByCurr")
	AjaxJson getByCurr() {
		SpAdmin admin = SpAdminUtil.getCurrAdmin();
		return AjaxJson.getSuccessData(admin);
	}
	
	/** 查 - 集合 */
	@RequestMapping("getList")
	@SaCheckPermission(AuthConst.ADMIN_LIST)
	AjaxJson getList(){
		SoMap so = SoMap.getRequestSoMap();
		List<SpAdmin> list = spAdminMapper.getList(so.startPage());
		return AjaxJson.getPageData(so.getDataCount(), list);
	}

	/** 当前 Admin 修改自己信息 */
	@RequestMapping("updateInfo")
	AjaxJson updateInfo(SpAdmin s){
		// 校验 
		s.setId(StpUtil.getLoginIdAsLong());
		s.setRoleId(SpRoleUtil.getCurrRoleId());
		SpAdminUtil.checkAdmin(s);
		
		// 修改
		int line = spAdminMapper.update(s);
		return AjaxJson.getByLine(line);
	}

}
