package com.liquidnet.service.adam.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.liquidnet.commons.lang.util.DateUtil;
import com.liquidnet.service.adam.dto.vo.AdamUserMemberVo;
import com.liquidnet.service.adam.service.IAdamMemberOrderService;
import com.liquidnet.service.adam.service.IAdamUserMemberService;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.adam.dto.vo.AdamMemberVo;
import com.liquidnet.service.adam.service.IAdamMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@ApiSupport(order = 10030)
@Api(tags = "会员信息")
@Slf4j
@RestController
@RequestMapping("/member")
public class AdamMemberController {

    @Autowired
    IAdamMemberService adamMemberService;
    @Autowired
    IAdamMemberOrderService adamMemberOrderService;
    @Autowired
    IAdamUserMemberService adamUserMemberService;

    @ApiOperationSupport(order = 0)
    @ApiOperation(value = "会员信息")
    @GetMapping("info")
    public ResponseDto<AdamMemberVo> query() {
        AdamMemberVo info = adamMemberService.queryMemberInfo();
        return ResponseDto.success(info);
    }

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "判断是否会员")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "uid", value = "用户ID[64]", example = "1"),
    })
    @GetMapping("check/{uid}")
    public ResponseDto<Boolean> isMember(@PathVariable String uid) {
        if (StringUtils.isBlank(uid)) return ResponseDto.success(false);

        AdamUserMemberVo userMemberInfo = adamUserMemberService.getUserMemberInfo(uid);
        if (null != userMemberInfo) {
            Integer state = userMemberInfo.getState();
            if (null != state && state == 1) {
                // TODO: 2021/5/25 时间界定
                Date expiryAtDate = DateUtil.parse(userMemberInfo.getExpiryAt(), DateUtil.DATE_SMALL_STR);

                return ResponseDto.success(null != expiryAtDate && expiryAtDate.after(DateUtil.now()));
            }
        }
        return ResponseDto.success(false);
    }
}
