package com.ym.mec.auth.web.controller;

import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.auth.config.constant.SecurityConstants;
import com.ym.mec.auth.service.SysRoleService;
import com.ym.mec.auth.service.SysUserRoleService;
import com.ym.mec.auth.service.SysUserService;
import com.ym.mec.auth.web.controller.queryInfo.SysUserQueryInfo;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.security.AuthUser;
import com.ym.mec.common.security.SecurityUtils;
import com.ym.mec.common.validcode.SmsCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController()
@RequestMapping("user")
@Api(tags = "会员服务")
public class UserController extends BaseController {

	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Autowired
	private SmsCodeService smsCodeService;

	@ApiOperation(value = "分页查询用户信息")
	@ApiImplicitParams({ @ApiImplicitParam(name = "userType", value = "用户类型", required = false, dataType = "String"),
			@ApiImplicitParam(name = "createStartDate", value = "开始注册时间", required = true, dataType = "String"),
			@ApiImplicitParam(name = "createEndDate", value = "结束注册时间", required = true, dataType = "String") })
	@GetMapping(value = "/queryPage")
	public Object queryPage(SysUserQueryInfo queryInfo) {
		return succeed(sysUserService.queryPage(queryInfo));
	}

	@ApiOperation(value = "查询用户信息接口")
	@GetMapping("/query")
	public Object query() {
		AuthUser user = SecurityUtils.getUser();
		if(user != null){
			return succeed(sysUserService.get(user.getUserId()));
		}
		return failed();
	}

	@GetMapping(value = "/queryUserByPhone")
	public SysUser queryUserByPhone(String mobile) {
		return sysUserService.queryByPhone(mobile);
	}

	@ApiOperation(value = "新增用户")
	@RequestMapping(value = "/add",method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
	public Object add(@RequestBody SysUser sysUser) {
//		sysUser.setPassword(new BCryptPasswordEncoder().encode(sysUser.getPassword()));
		sysUserService.insert(sysUser);
		return succeed(sysUser.getId());
	}

	@ApiOperation(value = "设置密码")
	@PostMapping(value = "/setPassword",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	@ApiImplicitParams({ @ApiImplicitParam(name = "mobile", value = "手机号", required = true, dataType = "String"),
			@ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String") })
	public Object setPassword(String mobile,String password) {
		if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
			return failed();
		}
		SysUser sysUser = sysUserService.queryByPhone(mobile);
		if(sysUser == null){
			return failed("用户不存在");
		}
		password = new BCryptPasswordEncoder().encode(password);
		sysUserService.updatePassword(mobile,password);
		return succeed();
	}

	@ApiOperation(value = "修改密码")
	@PostMapping(value = "/updatePassword",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	@ApiImplicitParams({ @ApiImplicitParam(name = "mobile", value = "手机号", required = true, dataType = "String"),
			@ApiImplicitParam(name = "authCode", value = "验证码", required = true, dataType = "String"),
			@ApiImplicitParam(name = "newPassword", value = "新密码", required = true, dataType = "String")})
	public Object updatePassword(String mobile,String authCode,String newPassword) {
		if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(authCode) || StringUtils.isEmpty(newPassword)){
			return failed("参数校验异常");
		}
		SysUser sysUser = sysUserService.queryByPhone(mobile);
		if(sysUser == null){
			return failed("用户不存在");
		}
		if(smsCodeService.verifyValidCode(mobile, authCode)){
			sysUserService.updatePassword(mobile,new BCryptPasswordEncoder().encode(newPassword));
			return succeed();
		}else {
			return failed("验证码错误");
		}
	}

	@ApiOperation(value = "修改用户")
	@PostMapping(value = "/update",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public Object update(SysUser sysUser) {
		AuthUser user = SecurityUtils.getUser();
		if(user != null){
			Date date = new Date();
			sysUser.setId(user.getUserId());
			sysUser.setUpdateTime(date);
			sysUserService.update(sysUser);
			return succeed();
		}
		return failed();
	}

	@ApiOperation(value = "获取用户角色列表")
	@GetMapping("/getRole")
	public Object getRole() {
		AuthUser user = SecurityUtils.getUser();
		if(user != null) {
			return succeed(sysRoleService.findRoleByUserId(user.getUserId()));
		}
		return failed();
	}

	@ApiOperation(value = "用户角色新增")
	@ApiImplicitParams({ @ApiImplicitParam(name = "userId", value = "用户编号", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "roleIds", value = "角色id,逗号分隔", required = true, dataType = "String") })
	@PostMapping(value = "/addRole",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public Object getRole(String userId,String roleIds) {
		if(StringUtils.isEmpty(userId) || StringUtils.isEmpty(roleIds)){
			return failed(SecurityConstants.PARAM_VERIFY_EXCEPTION);
		}
		sysUserRoleService.batchInsert(Integer.parseInt(userId),roleIds);
		return succeed();
	}

	@ApiOperation(value = "用户角色删除")
	@ApiImplicitParams({ @ApiImplicitParam(name = "userId", value = "用户编号", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "roleIds", value = "角色id,逗号分隔", required = true, dataType = "String") })
	@PostMapping(value = "/delRole",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public Object delRole(String userId,String roleIds) {
		if(StringUtils.isEmpty(userId) || StringUtils.isEmpty(roleIds)){
			return failed(SecurityConstants.PARAM_VERIFY_EXCEPTION);
		}
		sysUserRoleService.batchDel(Integer.parseInt(userId),roleIds);
		return succeed();
	}
}
