package com.diagbot.facade;

import com.diagbot.dto.RespDTO;
import com.diagbot.entity.OpenedProducts;
import com.diagbot.entity.ProductService;
import com.diagbot.entity.ServiceToken;
import com.diagbot.enums.StatusEnum;
import com.diagbot.enums.TokenTypeEnum;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.service.impl.ServiceTokenServiceImpl;
import com.diagbot.util.ListUtil;
import com.diagbot.vo.ServiceTokenVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 用户端token业务层
 * @author: zhaops
 * @time: 2018/9/18 16:31
 */
@Component
public class ServiceTokenFacade extends ServiceTokenServiceImpl {


    @Autowired
    ProductServiceFacade productServiceFacade;
    @Autowired
    OpenedProductsFacade openedProductsFacade;


    /**
     * 获取资源的url
     * @param serviceTokenVo 参数
     * @return 是否有权限
     */
    public RespDTO<Boolean> hasPermission(ServiceTokenVo serviceTokenVo) {
        String appkey = serviceTokenVo.getAppkey();
        String secret = serviceTokenVo.getSecret();
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appkey", appkey);
        paramMap.put("secret", secret);
        ServiceToken st = this.getServiceToken(paramMap);
        if(null == st) {
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "appkey或secret错误，appkey=【" + appkey + "】," + "secret=【" + secret + "】");
        }
        if(TokenTypeEnum.Trial.getKey() == st.getType()) {
            ProductService ps = productServiceFacade.getById(st.getProductServiceId());
            if (ps == null) {
                throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "无权限访问!");
            }
            if (!ps.getProductId().equals(serviceTokenVo.getProductId())) {
                throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "无权限访问!");
            }
            Date date = new Date();
            if (st.getExpiringDate().getTime() < date.getTime()) {
                throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "访问权限已过期!");
            }
        } else {
            List<OpenedProducts> opList = openedProductsFacade.getByAppkeyAndSecret(paramMap);
            if(ListUtil.isEmpty(opList)) {
                throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "无权限访问!");
            }
            if(opList.size() != 1) {
                throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "当前appkey和secret对应多条数据!");
            }
            OpenedProducts op = opList.get(0);// 正常只有一条数据
            Date date = new Date();
            if(!(StatusEnum.Enable.getKey() == op.getServiceStatus())) {
                throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "当前服务已停用!");
            }
            if(op.getEndTime().getTime() < date.getTime()) {
                throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "访问权限已过期!");
            }
        }
        return RespDTO.onSuc(true);
    }
}
