package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.MD5Util;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.SecUser;
import com.hfkj.model.ResponseData;
import com.hfkj.service.SecUserLoginLogService;
import com.hfkj.service.SecUserService;
import com.hfkj.sysenum.SecUserObjectTypeEnum;
import com.hfkj.sysenum.SecUserStatusEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value="/secUser")
@Api(value="系统用户管理")
public class SecUserController {
	Logger log = LoggerFactory.getLogger(SecUserController.class);

	@Resource
	private SecUserService secUserService;
	@Resource
	private SecUserLoginLogService secUserLoginLogService;

	@RequestMapping(value="/editUser",method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "编辑用户")
	public ResponseData editUser(@RequestBody JSONObject body) {
		try {
			if (body == null
				|| StringUtils.isBlank(body.getString("userName"))
				|| StringUtils.isBlank(body.getString("loginName"))
				|| body.getLong("roleId") == null
			) {
				throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
			}
			SecUser secUser;
			if (body.getLong("id") != null) {
				// 查询账户
				secUser = secUserService.getDetail(body.getLong("id"));
				// 校验重复登录账户
				SecUser user = secUserService.getDetailByLoginName(body.getString("loginName"));
				if (user != null && !user.getId().equals(body.getLong("id"))) {
					throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "登录账户已存在");
				}
			} else {
				secUser = new SecUser();
				secUser.setPassword(MD5Util.encode("123456".getBytes()));
				secUser.setObjectType(SecUserObjectTypeEnum.type1.getCode());
				secUser.setStatus(SecUserStatusEnum.status1.getCode());

				// 校验重复登录账户
				if (secUserService.getDetailByLoginName(body.getString("loginName")) != null) {
					throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "登录账户已存在");
				}
			}

			secUser.setUserName(body.getString("userName"));
			secUser.setLoginName(body.getString("loginName"));
			secUser.setPlatformCode(body.getString("platformCode"));
			secUser.setTelephone(body.getString("telephone"));
			secUser.setRoleId(body.getLong("roleId"));
			secUserService.editUser(secUser);

			return ResponseMsgUtil.success("操作成功");

		} catch (Exception e) {
			log.error("error!",e);
			return ResponseMsgUtil.exception(e);
		}
	}

	@RequestMapping(value="/delete",method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "删除用户")
	public ResponseData delete(@RequestBody JSONObject body) {
		try {
			if (body == null || body.getLong("userId") == null) {
				throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
			}

			// 查询用户详情
			SecUser secUser = secUserService.getDetail(body.getLong("userId"));
			if (secUser == null) {
				throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
			}
			secUser.setStatus(SecUserStatusEnum.status0.getCode());
			secUserService.editUser(secUser);

			return ResponseMsgUtil.success("操作成功");

		} catch (Exception e) {
			log.error("error!",e);
			return ResponseMsgUtil.exception(e);
		}
	}

	@RequestMapping(value="/restore",method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "恢复")
	public ResponseData restore(@RequestBody JSONObject body) {
		try {
			if (body == null || body.getLong("userId") == null) {
				throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
			}

			// 查询用户详情
			SecUser secUser = secUserService.getDetail(body.getLong("userId"));
			if (secUser == null) {
				throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
			}
			secUser.setStatus(SecUserStatusEnum.status1.getCode());
			secUserService.editUser(secUser);

			return ResponseMsgUtil.success("操作成功");

		} catch (Exception e) {
			log.error("error!",e);
			return ResponseMsgUtil.exception(e);
		}
	}

	@RequestMapping(value="/resetPwd",method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "账户密码重置")
	public ResponseData resetPwd(@RequestBody JSONObject body) {
		try {
			if (body == null || body.getLong("userId") == null) {
				throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
			}

			secUserService.resetPwd(body.getLong("userId"));

			return ResponseMsgUtil.success("操作成功");

		} catch (Exception e) {
			log.error("error!",e);
			return ResponseMsgUtil.exception(e);
		}
	}


	@RequestMapping(value="/disable",method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "禁用")
	public ResponseData disable(@RequestBody JSONObject body) {
		try {
			if (body == null || body.getLong("userId") == null) {
				throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
			}
			// 查询用户详情
			SecUser secUser = secUserService.getDetail(body.getLong("userId"));
			if (secUser == null) {
				throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
			}
			secUser.setStatus(SecUserStatusEnum.status2.getCode());
			secUserService.editUser(secUser);

			return ResponseMsgUtil.success("操作成功");

		} catch (Exception e) {
			log.error("error!",e);
			return ResponseMsgUtil.exception(e);
		}
	}

	@RequestMapping(value="/queryDetail",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "查询详情")
	public ResponseData queryDetail(@RequestParam(value = "userId" , required = true) Long userId) {
		try {
			// 查询详情
			SecUser secUser = secUserService.getDetail(userId);
			if (secUser != null) {
				secUser.setPassword(null);
			}
			return ResponseMsgUtil.success(secUser);

		} catch (Exception e) {
			log.error("error!",e);
			return ResponseMsgUtil.exception(e);
		}
	}

	@RequestMapping(value="/queryList",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "查询列表")
	public ResponseData queryList(@RequestParam(value = "userName", required = false) String userName,
								  @RequestParam(value = "loginName", required = false) String loginName,
								  @RequestParam(value = "telephone", required = false) String telephone,
								  @RequestParam(value = "objectType", required = false) Integer objectType,
								  @RequestParam(value = "status", required = false) Integer status,
								  @RequestParam(value = "pageNum", required = true) Integer pageNum,
								  @RequestParam(value = "pageSize", required = true) Integer pageSize) {
		try {

			Map<String,Object> param = new HashMap<>();
			param.put("userName", userName);
			param.put("loginName", loginName);
			param.put("telephone", telephone);
			param.put("objectType", objectType);
			param.put("status", status);

			PageHelper.startPage(pageNum, pageSize);
			return ResponseMsgUtil.success(new PageInfo<>(secUserService.getList(param)));

		} catch (Exception e) {
			log.error("error!",e);
			return ResponseMsgUtil.exception(e);
		}
	}


	@RequestMapping(value="/queryLoginLogList",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "查询登录日志列表")
	public ResponseData queryLoginLogList(@RequestParam(value = "userId", required = true) String userId,
										  @RequestParam(value = "status", required = false) Integer status,
										  @RequestParam(value = "pageNum", required = true) Integer pageNum,
										  @RequestParam(value = "pageSize", required = true) Integer pageSize) {
		try {

			Map<String,Object> param = new HashMap<>();
			param.put("userId", userId);
			param.put("status", status);

			PageHelper.startPage(pageNum, pageSize);
			return ResponseMsgUtil.success(secUserLoginLogService.getLogList(param));

		} catch (Exception e) {
			log.error("error!",e);
			return ResponseMsgUtil.exception(e);
		}
	}


}
