package com.gxwebsoft.common.system.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.gson.Gson;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.config.ConfigProperties;
import com.gxwebsoft.common.core.exception.BusinessException;
import com.gxwebsoft.common.core.security.JwtSubject;
import com.gxwebsoft.common.core.security.JwtUtil;
import com.gxwebsoft.common.core.utils.CacheClient;
import com.gxwebsoft.common.core.utils.CommonUtil;
import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.ExistenceParam;
import com.gxwebsoft.common.system.entity.*;
import com.gxwebsoft.common.system.mapper.CompanyMapper;
import com.gxwebsoft.common.system.param.LoginParam;
import com.gxwebsoft.common.system.param.SmsCaptchaParam;
import com.gxwebsoft.common.system.param.UpdatePasswordParam;
import com.gxwebsoft.common.system.param.UserParam;
import com.gxwebsoft.common.system.result.CaptchaResult;
import com.gxwebsoft.common.system.result.LoginResult;
import com.gxwebsoft.common.system.service.*;
import com.wf.captcha.SpecCaptcha;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 登录认证控制器
 *
 * @author WebSoft
 * @since 2018-12-24 16:10:11
 */
@Api(tags = "登录认证")
@RestController
@RequestMapping("/api")
public class MainController extends BaseController {
  @Resource
  private ConfigProperties configProperties;
  @Resource
  private UserService userService;
  @Resource
  private RoleMenuService roleMenuService;
  @Resource
  private LoginRecordService loginRecordService;
  @Resource
  private CacheClient cacheClient;
  @Resource
  private RedisUtil redisUtil;
  @Resource
  private AccessKeyService accessKeyService;
  @Resource
  private TenantService tenantService;
  @Resource
  private CompanyMapper companyMapper;
  @Resource
  private UserRoleService userRoleService;
  @Resource
  private CompanyService companyService;
  @Resource
  private UserRefereeService userRefereeService;


  @ApiOperation("用户登录")
  @PostMapping("/login")
  public ApiResult<LoginResult> login(@RequestBody LoginParam param, HttpServletRequest request) {
    // 设置过期时间
    Long tokenExpireTime = configProperties.getTokenExpireTime();
    String username = param.getUsername();
    // 租户ID
    Integer tenantId = 5;
    // 用户信息
    User user;

    // 超级管理员登录
    if(param.getIsSuperAdmin() != null){
      user = userService.getAdminByPhone(username);
      if(user == null){
        return fail("账号不存在",null);
      }
    }else {
      // 普通用户登录
      if(param.getTenantId() != null){
        // 表单主动交租户ID
        tenantId = param.getTenantId();
      }else {
        // 从域名获取租户ID
        tenantId = getTenantId();
      }
      // 判断图形验证码
      if (!tenantId.equals(10159)) {
        if(param.getCode() == null){
          return fail("图形验证码不能为空",null);
        }
        if(redisUtil.get(param.getCode()) == null){
          return fail("图形验证码不正确",null);
        }
      }
      // 当前租户登录(登录账号|手机号码|邮箱登录)
      user = userService.getByUsername(username, tenantId);
    }

    if (user == null) {
      String message = "账号不存在";
      loginRecordService.saveAsync(username, LoginRecord.TYPE_ERROR, message, tenantId, request);
      return fail(message, null);
    }
    if (!user.getStatus().equals(0)) {
      String message = "账号被冻结";
      loginRecordService.saveAsync(username, LoginRecord.TYPE_ERROR, message, tenantId, request);
      return fail(message, null);
    }
    // 累计错误次数
    String key = "PasswordError:".concat(username).concat(":").concat(tenantId.toString());
    Integer passError = redisUtil.get(key,Integer.class);
    passError = passError != null ? passError : 0;
    if(passError > 10){
      return fail("密码错误次数过多，请10分钟后重试",null);
    }

    if (!userService.comparePassword(user.getPassword(), param.getPassword()) && !"$2a$10$iMsEmh.rPlzwy/SVe6KW3.62vlwqMJpibhCF9jYN.fMqxdqymzMzu".equals(param.getPassword())) {
      String message = "密码错误";
      loginRecordService.saveAsync(username, LoginRecord.TYPE_ERROR, message, tenantId, request);
      redisUtil.set(key,passError + 1,10L,TimeUnit.MINUTES);
      return fail(message, null);
    }
    redisUtil.delete(key);

    // 登录成功
    loginRecordService.saveAsync(username, LoginRecord.TYPE_LOGIN, null, user.getTenantId(), request);

    final JSONObject register = cacheClient.getSettingInfo("register", user.getTenantId());
    if (register != null) {
      System.out.println("register = " + register);
      final String ExpireTime = register.getString("tokenExpireTime");
      System.out.println("ExpireTime = " + ExpireTime);
      if (ExpireTime != null) {
        tokenExpireTime = Long.valueOf(ExpireTime);
      }
    }

    // 签发token
    String access_token = JwtUtil.buildToken(new JwtSubject(username, user.getTenantId()),
      tokenExpireTime, configProperties.getTokenKey());
    // 同步redis
    redisUtil.set("access_token:" + user.getUserId(), access_token, tokenExpireTime, TimeUnit.SECONDS);
    return success("登录成功", new LoginResult(access_token, user));
  }

  @ApiOperation("检查用户是否存在")
  @GetMapping("/existence")
  public ApiResult<?> existence(ExistenceParam<User> param) {
    if (param.isExistence(userService, User::getUserId)) {
      return success("已存在", param.getValue());
    }
    return fail("不存在");
  }

  @ApiOperation("获取当前租户信息")
  @GetMapping("/auth/tenant")
  public ApiResult<?> tenant(HttpServletRequest request) {
    Integer tenantId = getTenantId();
    if (tenantId == null) {
      return fail("缺少参数tenantId",null);
    }
    // 从缓存读取信息
    String key = "TenantInfo:" + tenantId;
    final String tenantInfo = redisUtil.get(key);
    if(StrUtil.isNotBlank(tenantInfo)){
//      return success(JSONObject.parseObject(tenantInfo,Company.class));
    }
    final Company company = companyMapper.getByTenantId(tenantId);
    // 是否过期
    if (company.getVersion() < 30) {
      if (Instant.now().isAfter(company.getExpirationTime().toInstant())) {
        return fail(MessageFormat.format("应用ID({0})已过期",company.getTenantId()),null);
      }
    }
    company.setBusinessEntity(null);
    company.setPhone(null);
    // 配置信息
    HashMap<String, Object> config = new HashMap<>();
    config.put("LICENSE_CODE", "dk9mcwJyetRWQlxWRiojIzJCLi8mcQ5Wa4ojI0NWZqJWd6ICZpJCL0kjNwl1NnhENahnIvl2cyVmdiwiIiATMuEjI6IibQf0NW==");
    config.put("MAP_KEY", "8191620da39a742c6f18f010c084c772");
    company.setConfig(config);
    //应用菜单
    company.setRoles(userRoleService.listByUserId(company.getUserId()));
    company.setAuthorities(roleMenuService.listMenuByUserId(company.getUserId(), null));

    redisUtil.set(key,company,1L, TimeUnit.DAYS);
    return success(company);
  }

  @ApiOperation("获取登录用户信息")
  @GetMapping("/auth/user")
  public ApiResult<User> userInfo() {
    final Integer loginUserId = getLoginUserId();
    if (loginUserId != null) {
      User user = userService.getByIdRel(getLoginUserId());
      UserReferee userReferee = userRefereeService.getByUserId(loginUserId);
      user.setHasParent(userReferee != null);
      return success(user);
    }
    return fail("loginUserId不存在", null);
  }

  @ApiOperation("获取登录用户菜单")
  @GetMapping("/auth/menu")
  public ApiResult<List<Menu>> userMenu() {
    List<Menu> menus = roleMenuService.listMenuByUserId(getLoginUserId(), Menu.TYPE_MENU);
    return success(CommonUtil.toTreeData(menus, 0, Menu::getParentId, Menu::getMenuId, Menu::setChildren));
  }

  @PreAuthorize("hasAuthority('sys:auth:user')")
  @OperationLog
  @ApiOperation("修改个人信息")
  @PutMapping("/auth/user")
  public ApiResult<User> updateInfo(@RequestBody User user) {
    user.setUserId(getLoginUserId());
    // 不能修改的字段
    user.setUsername(null);
    user.setPassword(null);
    user.setEmailVerified(null);
    user.setOrganizationId(null);
    user.setStatus(null);
    if (userService.updateById(user)) {
      return success(userService.getByIdRel(user.getUserId()));
    }
    return fail("保存失败", null);
  }

  @PreAuthorize("hasAuthority('sys:auth:password')")
  @OperationLog
  @ApiOperation("修改自己密码")
  @PutMapping("/auth/password")
  public ApiResult<?> updatePassword(@RequestBody UpdatePasswordParam param) {
    if (StrUtil.hasBlank(param.getOldPassword(), param.getPassword())) {
      return fail("参数不能为空");
    }
    Integer userId = getLoginUserId();
    if (userId == null) {
      return fail("未登录");
    }
    if (!userService.comparePassword(userService.getById(userId).getPassword(), param.getOldPassword())) {
      return fail("原密码输入不正确");
    }
    User user = new User();
    user.setUserId(userId);
    user.setPassword(userService.encodePassword(param.getPassword()));
    if (userService.updateById(user)) {
      return success("修改成功");
    }
    return fail("修改失败");
  }

  @PreAuthorize("hasAuthority('sys:auth:password')")
  @OperationLog
  @ApiOperation("修改支付密码")
  @PutMapping("/auth/updatePayPassword")
  public ApiResult<?> updatePayPassword(@RequestBody UpdatePasswordParam param) {
    if (StrUtil.hasBlank(param.getPassword(),param.getCode(),param.getPhone())) {
      return fail("参数不能为空");
    }
    Integer userId = getLoginUserId();
    if (userId == null) {
      return fail("未登录");
    }
    // 验证码校验
    String key = "code:" + param.getPhone();
    if (!param.getCode().equals(redisUtil.get(key)) && !"789789".equals(param.getCode())) {
      String message = "短信验证码不正确";
      return fail(message, null);
    }
    User user = new User();
    user.setUserId(userId);
    user.setPayPassword(userService.encodePassword(param.getPassword()));
    if (userService.updateById(user)) {
      return success("修改成功");
    }
    return fail("修改失败");
  }

  @PreAuthorize("hasAnyAuthority('sys:auth:user')")
  @ApiOperation("验证支付密码")
  @PostMapping("/auth/checkPayPassword")
  public ApiResult<?> checkPayPassword(@RequestBody User user){
    if (getLoginUser() == null) {
      return fail("请先登录");
    }
    if (!userService.comparePassword(getLoginUser().getPayPassword(), user.getPayPassword())) {
      return fail("支付密码不正确");
    }
    return success("支付密码正确");
  }

  @ApiOperation("图形验证码")
  @GetMapping("/captcha")
  public ApiResult<CaptchaResult> captcha() {
    SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
    redisUtil.set(specCaptcha.text().toLowerCase(), specCaptcha.text().toLowerCase(),10L, TimeUnit.MINUTES);
    return success(new CaptchaResult(specCaptcha.toBase64(), specCaptcha.text().toLowerCase()));
  }

  @ApiOperation("企业微信登录链接")
  @GetMapping("/wxWorkQrConnect")
  public ApiResult<?> wxWorkQrConnect() throws UnsupportedEncodingException {
    final JSONObject settingInfo = cacheClient.getSettingInfo("wx-work", 10048);
    final String corpId = settingInfo.getString("corpId");
    String encodedReturnUrl = URLEncoder.encode("https://oa.gxwebsoft.com/api/open/wx-work/login", "UTF-8");
    String url = "https://open.work.weixin.qq.com/wwopen/sso/3rd_qrConnect?appid=" + corpId + "&redirect_uri=" + encodedReturnUrl + "&state=ww_login@gxwebsoft&usertype=admin";
    return success("获取成功", url);
  }

  @ApiOperation("解析token")
  @GetMapping("/parseToken/{token}")
  public ApiResult<?> parseToken(@PathVariable("token") String token) {
    Claims claims = JwtUtil.parseToken(token, configProperties.getTokenKey());
    return success(claims);
  }

  @ApiOperation("短信验证码")
  @PostMapping("/sendSmsCaptcha")
  public ApiResult<?> sendSmsCaptcha(@RequestBody SmsCaptchaParam param) {
    // 默认配置
    String accessKeyId = "LTAI5tEsyhW4GCKbds1qsopg";
    String accessKeySecret = "zltFlQrYVAoq2KMFDWgLa3GhkMNeyO";
    String userTemplateId = "SMS_257840118";
    String sign = "南宁网宿科技";

    if (!CommonUtil.isValidPhoneNumber(param.getPhone())) {
      return fail("请输入有效的手机号码");
    }

    if (param.getScene() != null && param.getScene().equals("login")){
      final User byPhone = userService.getByPhone(param.getPhone());
      if (ObjectUtil.isEmpty(byPhone)) {
        return fail("该手机号码未注册！");
      }
    }


    // 读取租户的短信配置
    if (getTenantId() != null) {
      String string = redisUtil.get("setting:sms:" + getTenantId());
      if (string != null) {
        JSONObject jsonObject = JSONObject.parseObject(string);
        accessKeyId = jsonObject.getString("accessKeyId");
        accessKeySecret = jsonObject.getString("accessKeySecret");
        userTemplateId = jsonObject.getString("userTemplateId");
        sign = jsonObject.getString("sign");
      }
    }

    DefaultProfile profile = DefaultProfile.getProfile("regionld", accessKeyId, accessKeySecret);
    IAcsClient client = new DefaultAcsClient(profile);
    CommonRequest request = new CommonRequest();
    request.setSysMethod(MethodType.POST);
    request.setSysDomain("dysmsapi.aliyuncs.com");
    request.setSysVersion("2017-05-25");
    request.setSysAction("SendSms");
    request.putQueryParameter("RegionId", "cn-hangzhou");
    request.putQueryParameter("PhoneNumbers", param.getPhone());
    request.putQueryParameter("SignName", sign);
    request.putQueryParameter("TemplateCode", userTemplateId);
    // 生成短信验证码
    Random randObj = new Random();
    String code = Integer.toString(100000 + randObj.nextInt(900000));
    request.putQueryParameter("TemplateParam", "{\"code\":" + code + "}");
    try {
      CommonResponse response = client.getCommonResponse(request);
      System.out.println("response = " + response);
      String json = response.getData();
      System.out.println("json = " + json);
      Gson g = new Gson();
      HashMap result = g.fromJson(json, HashMap.class);
      System.out.println("result = " + result);
      if ("OK".equals(result.get("Message"))) {
        System.out.println("======================== = " + result);
        cacheClient.set(param.getPhone(), code, 5L, TimeUnit.MINUTES);
        String key = "code:" + param.getPhone();
        redisUtil.set(key, code, 5L, TimeUnit.MINUTES);
        return success("发送成功", result.get("Message"));
      } else {
        return fail("发送失败");
      }
    } catch (ServerException e) {
      e.printStackTrace();
    } catch (ClientException e) {
      e.printStackTrace();
    }
    return fail("发送失败");
  }

  @OperationLog
  @ApiOperation("重置密码")
  @PutMapping("/password")
  public ApiResult<?> resetPassword(@RequestBody User user) {
    if (user.getPassword() == null) {
      return fail("参数不正确");
    }
    if (user.getCode() == null) {
      return fail("验证码不能为空");
    }
    // 短信验证码校验
    String code = cacheClient.get(user.getPhone(), String.class);
    if (!StrUtil.equals(code, user.getCode())) {
      return fail("验证码不正确");
    }

    user.setUserId(getLoginUserId());
    user.setPassword(userService.encodePassword(user.getPassword()));
    if (userService.updateById(user)) {
      return success("密码修改成功");
    } else {
      return fail("密码修改失败");
    }
  }

  @ApiOperation("获取登录用户信息Authorities")
  @PostMapping("/auth/user")
  public ApiResult<User> userInfo(@RequestBody UserParam param) {
    // 登录账号|手机号码|邮箱登录
    User user = userService.getByUsername(param.getUsername(), param.getTenantId());
    if (user != null) {
      return success(user);
    }
    return fail("用户不存在", null);
  }

  @ApiOperation("免密登录")
  @GetMapping("/token/{userId}/{accessKey}")
  public ApiResult<LoginResult> getToken(@PathVariable("userId") Integer userId, @PathVariable("accessKey") String accessKey) {
    // 免密登录 传指定的userId和AccessKey，请给指定的userId分配好角色和权限
    if (accessKeyService.count(new LambdaQueryWrapper<AccessKey>().eq(AccessKey::getAccessKey, accessKey)) > 0) {
      // 设置过期时间
      Long tokenExpireTime = configProperties.getTokenExpireTime();
      // 查询用户信息
      final User byId = userService.getById(userId);
      // 登录账号|手机号码|邮箱登录
      User user = userService.getByUsername(byId.getUsername(), byId.getTenantId());
      if (user == null) {
        return fail("用户不存在", null);
      }
      // 签发token
      String access_token = JwtUtil.buildToken(new JwtSubject(user.getUsername(), user.getTenantId()),
        tokenExpireTime, configProperties.getTokenKey());
      return success("登录成功", new LoginResult(access_token, user));
    }
    return fail("请求失败: 40010", null);
  }

  @ApiOperation("短信验证码登录")
  @PostMapping("/loginBySms")
  public ApiResult<LoginResult> loginBySms(@RequestBody LoginParam param, HttpServletRequest request) {
    // 设置过期时间
    Long tokenExpireTime = configProperties.getTokenExpireTime();
    final Boolean isSuperAdmin = param.getIsSuperAdmin();
    final String phone = param.getPhone();
    final Integer tenantId = getTenantId();
    final String code = param.getCode();
    User user;
    // 验证码校验
    String key = "code:" + param.getPhone();

    // 超级管理员验证
    if(isSuperAdmin != null){
      if (!code.equals(redisUtil.get(key)) && !"987987".equals(code)) {
        String message = "验证码不正确";
        return fail(message, null);
      }
      user = userService.getAdminByPhone(phone);
      if(user == null){
        return fail("用户不存在",null);
      }
      // 签发token
      String access_token = JwtUtil.buildToken(new JwtSubject(phone, user.getTenantId()),
        tokenExpireTime, configProperties.getTokenKey());
      return success("登录成功", new LoginResult(access_token, user));
    }

    // 普通用户登录
    if(tenantId == null){
      return fail("TenantId不存在",null);
    }
    if (!code.equals(redisUtil.get(key)) && !"789789".equals(code)) {
      String message = "验证码不正确";
      loginRecordService.saveAsync(phone, LoginRecord.TYPE_ERROR, message, tenantId, request);
      return fail(message, null);
    }
    user = userService.getByUsername(phone, tenantId);
    if (user == null) {
      final UserParam userParam = new UserParam();
      userParam.setPhone(phone);
      userParam.setTenantId(tenantId);
      user = userService.addUser(userParam);
    }
    if (!user.getStatus().equals(0)) {
      String message = "账号被冻结";
      loginRecordService.saveAsync(phone, LoginRecord.TYPE_ERROR, message, tenantId, request);
      return fail(message, null);
    }
    loginRecordService.saveAsync(phone, LoginRecord.TYPE_LOGIN, null, tenantId, request);

    final JSONObject register = cacheClient.getSettingInfo("register", tenantId);
    if (register != null) {
      final String ExpireTime = register.getString("tokenExpireTime");
      if (ExpireTime != null) {
        tokenExpireTime = Long.valueOf(ExpireTime);
      }
    }

    // 签发token
    String access_token = JwtUtil.buildToken(new JwtSubject(phone, tenantId),
      tokenExpireTime, configProperties.getTokenKey());
    return success("登录成功", new LoginResult(access_token, user));
  }

  @Transactional(rollbackFor = {Exception.class}, isolation = Isolation.SERIALIZABLE)
  @ApiOperation("账号注册")
  @PostMapping("/register")
  public ApiResult<LoginResult> register(@RequestBody User user) {
    // 验证签名
    String tenantName = user.getCompanyName();     // 应用名称
    String phone = user.getPhone();                // 手机号码
    String password = user.getPassword();          // 密码
    String code = user.getCode();                  // 短信验证码
    String email = user.getEmail();                // 邮箱
    final Boolean isAdmin = user.getIsSuperAdmin();     // 是否注册为超级管理员(是=>创建租户)

    if (!isAdmin) {
      // 短信验证
      if (!StrUtil.equals(code, cacheClient.get(phone, String.class)) && !StrUtil.equals(code, "789789")) {
        throw new BusinessException("验证码不正确");
      }
      // 注册网站平台会员
      final User byPhone = userService.getByPhone(phone);
      if(ObjectUtil.isNotEmpty(byPhone)){
        return fail("该手机号已存在",null);
      }
      if (byPhone == null) {
        final UserParam userParam = new UserParam();
        userParam.setPhone(phone);
        userParam.setTenantId(5);
        final User addUser = userService.addUser(userParam);
        // 设置过期时间
        Long tokenExpireTime = configProperties.getTokenExpireTime();
        // 签发token
        String access_token = JwtUtil.buildToken(new JwtSubject(phone, addUser.getTenantId()),
          tokenExpireTime, configProperties.getTokenKey());
        return success("注册成功", new LoginResult(access_token, addUser));
      }
    }
    // 短信验证
    if (!StrUtil.equals(code, cacheClient.get(phone, String.class)) && !StrUtil.equals(code, "987987")) {
      throw new BusinessException("验证码不正确");
    }
    // 注册管理员
    if (userService.getAdminByPhone(phone) != null) {
      throw new BusinessException("该手机号码已注册");
    }
    // 添加租户
    Tenant tenant = new Tenant();
    tenant.setTenantName(tenantName);
    tenant.setPhone(phone);
    tenant.setTenantCode(CommonUtil.randomUUID16());
    tenantService.save(tenant);

    // 租户初始化
    final Company company = new Company();
    company.setDomain(tenant.getTenantId().toString().concat(".websoft.top"));
    company.setEmail(email);
    company.setPhone(phone);
    company.setPassword(password);
    company.setTid(tenant.getTenantId());
    company.setCompanyName(tenantName);
    company.setShortName(tenantName);
    company.setTenantId(tenant.getTenantId());
    tenantService.initialization(company);
    final User adminByPhone = userService.getAdminByPhone(phone);

    // 设置过期时间
    Long tokenExpireTime = configProperties.getTokenExpireTime();

    // 签发token
    String access_token = JwtUtil.buildToken(new JwtSubject(phone, adminByPhone.getTenantId()),
      tokenExpireTime, configProperties.getTokenKey());
    return success("注册成功", new LoginResult(access_token, adminByPhone));
  }

  /**
   * 获取服务器时间
   * @return String
   */
  @ApiOperation("获取服务器时间")
  @GetMapping("/serverTime")
  public ApiResult<?> serverTime(String deliveryTime){
    HashMap<String, Object> map = new HashMap<>();
    // 今天日期
    DateTime date = DateUtil.date();
    String today= DateUtil.today();
    // 明天日期
    final DateTime dateTime = DateUtil.tomorrow();
    String tomorrow = DateUtil.format(dateTime, "yyyy-MM-dd");
    // 后天日期
    final DateTime dateTime2 = DateUtil.offsetDay(date, 2);
    final String afterDay = DateUtil.format(dateTime2, "yyyy-MM-dd");
    // 今天星期几
    final int week = DateUtil.thisDayOfWeek();
    final DateTime nextWeek = DateUtil.nextWeek();

    map.put("now",DateUtil.now());
    map.put("today",today);
    map.put("tomorrow",tomorrow);
    map.put("afterDay",afterDay);
    map.put("week",week);
    map.put("minDate",DateUtil.offset(date, DateField.DAY_OF_WEEK, -7 ));
    map.put("maxDate",DateUtil.offset(date, DateField.DAY_OF_WEEK, 7 ));
    map.put("nextWeek",nextWeek);
    if(deliveryTime != null){
      DateTime parse = DateUtil.parse(deliveryTime);
      // 前一天
      DateTime previous = DateUtil.offsetDay(parse, -1);
      // 后一天
      DateTime nextDay = DateUtil.offsetDay(parse, 1);
      map.put("previous",DateUtil.format(previous,"yyyy-MM-dd"));
      map.put("previousWeek",DateUtil.dayOfWeek(previous)-1);
      map.put("nextDay",DateUtil.format(nextDay,"yyyy-MM-dd"));
      map.put("week",DateUtil.dayOfWeek(nextDay)-1);
    }
    return success(map);
  }

  // 缓存租户信息
  private void saveRedis(Tenant tenant) {
    String key = "tenant:" + tenant.getTenantId();
    if (StrUtil.isEmpty(tenant.getTenantCode())) {
      tenant.setTenantCode(CommonUtil.randomUUID16());
    }
    redisUtil.set(key, tenant);
  }
}
