package com.storlead.system.controller;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.storlead.framework.auth.jwt.JwtUtil;
import com.storlead.framework.auth.vo.LoginUser;
import com.storlead.framework.common.constant.DefContants;
import com.storlead.framework.common.result.Result;
import com.storlead.framework.common.util.ConvertUtils;
import com.storlead.framework.redis.RedisService;
import com.storlead.framework.util.LoginUserUtil;
import com.storlead.system.service.IUserRoleService;
import com.storlead.user.pojo.entity.DeptEntity;
import com.storlead.user.pojo.entity.JobEntity;
import com.storlead.user.pojo.entity.UserEntity;
import com.storlead.user.pojo.entity.UserForm;
import com.storlead.user.pojo.vo.WeChatPhoneInfo;
import com.storlead.user.service.IDepartService;
import com.storlead.user.service.IJobService;
import com.storlead.user.service.IUserService;
import com.storlead.wecom.util.WechatPhoneUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @Author scott
 * @since 2018-12-17
 */
@RestController
@RequestMapping("/lingcun")
@Api(tags="5.公共接口")
@Slf4j
public class LoginController {
	@Resource
	private IUserService sysUserService;

	@Resource
    private IDepartService departService;

	@Resource
	private RedisService redisService;

	@ApiOperation("修改密码")
	@PostMapping("/modifyPass")
	public Object updateUserPass(@RequestBody @Validated UserForm param) {
		return sysUserService.updateUserPass(param);
	}


	/**
	 * 退出登录
	 * @param request
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/logout")
	@ApiOperation(value = "用户退出登录接口", notes = "用户退出登录接口")
	public Result<Object> logout(HttpServletRequest request) {
		//用户退出逻辑
		String token = request.getHeader(DefContants.ACCESS_TOKEN);
		if(ConvertUtils.isEmpty(token)) {
			return Result.error("退出登录失败！");
		}
		Boolean del = redisService.deleteObject(token);
		if (del) {
			return Result.ok("退出登录成功！");
		} else {
			Object obj = redisService.getCacheObject(token);
			if (obj == null) {
				return Result.error("退出登录成功！");
			}
			return Result.error("退出登录失败！");
		}
	}


	/**
	 * 用户信息
	 *
	 * @param request
	 * @return
	 */
	@GetMapping("/getUserBaseInfo")
	@ApiOperation(value = "根据token获取用户基本信息", notes = "根据token获取用户基本信息(header中传token)")
	public Result<JSONObject> getUserBaseInfo(HttpServletRequest request) {

		LoginUser loginUser =  LoginUserUtil.getLoginUser();
		// 生成token
		Result<JSONObject> result = new Result<JSONObject>();
		//用户退出逻辑
		String token = request.getHeader(DefContants.ACCESS_TOKEN);
		UserEntity sysUser = sysUserService.getUserByName(loginUser.getUserName());
		// 获取用户部门信息
		JSONObject obj = new JSONObject();
		DeptEntity dept = departService.getById(sysUser.getDeptId());
		obj.put("dept", dept);
		obj.put("userInfo", sysUser);
		result.setResult(obj);
		result.success("登录成功");
		return result;
	}

//	/**
//	 * 获取校验码
//	 */
//	@GetMapping(value = "/getCheckCode")
//	public Result<Map<String,String>> getCheckCode(){
//		Result<Map<String,String>> result = new Result<Map<String,String>>();
//		Map<String,String> map = new HashMap<String,String>();
//		try {
//			String code = RandomUtil.randomString(BASE_CHECK_CODES,4);
//			String key = MD5Util.MD5Encode(code+System.currentTimeMillis(), "utf-8");
//			ehCacheService.put(CommonConstant.CAPCHA_CACHE,key, code, 60);
//			map.put("key", key);
//			map.put("code",code);
//			result.setResult(map);
//			result.setSuccess(true);
//		} catch (Exception e) {
//			e.printStackTrace();
//			result.setSuccess(false);
//		}
//		return result;
//	}

	/**
	 * 获取手机验证码
	 * @return
	 */
//	@GetMapping(value = "/getPhoneCode")
//	@ApiOperation(value = "获取手机验证码", notes = "获取手机验证码")
//	public Result getPhoneCode(@RequestParam("mobile") @ApiParam(value = "手机号") String mobile){
//		try {
//			int code = RandomCodeUtil.RandomCheckCode();
//			return smsService.sendSms(mobile,code+"", SmsTypeEnum.Login_SMS.getCode(),null);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return Result.error("发送手机验证码失败");
//	}
}
