package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diagbot.dto.ProductServiceDTO;
import com.diagbot.entity.LantoneProduct;
import com.diagbot.entity.ProductService;
import com.diagbot.entity.ServiceInfo;
import com.diagbot.entity.ServiceToken;
import com.diagbot.entity.wrapper.ProductServiceWrapper;
import com.diagbot.enums.IsDeleteEnum;
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.ProductServiceServiceImpl;
import com.diagbot.util.DateUtil;
import com.diagbot.util.GuidUtil;
import com.diagbot.util.UserUtils;
import com.diagbot.vo.ProductServiceSaveVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Description:产品与用户端关联业务层
 * @author: zhaops
 * @time: 2018/9/18 16:33
 */
@Component
public class ProductServiceFacade extends ProductServiceServiceImpl {
    @Autowired
    private ServiceTokenFacade serviceTokenFacade;
    @Autowired
    private ServiceInfoFacade serviceInfoFacade;
    @Autowired
    private LantoneProductFacade lantoneProductFacade;
    @Autowired
    private OpenedProductsFacade openedProductsFacade;

    /**
     * 建立产品服务端关联并生成令牌
     *
     * @param productServiceSaveVO
     * @return
     */
    public ProductServiceDTO genProductService(ProductServiceSaveVO productServiceSaveVO) {
        ProductServiceDTO productServiceDTO = new ProductServiceDTO();
        Long userId = Long.parseLong(UserUtils.getCurrentPrincipleID());
        LantoneProduct lantoneProduct = lantoneProductFacade.getById(productServiceSaveVO.getProductId());
        if (lantoneProduct == null) {
            throw new CommonException(CommonErrorCode.NOT_EXISTS,
                    "找不到产品【" + lantoneProduct.getName() + "】");
        }
        ServiceInfo serviceInfo = serviceInfoFacade.getById(productServiceSaveVO.getServiceId());
        if (serviceInfo == null) {
            throw new CommonException(CommonErrorCode.NOT_EXISTS,
                    "找不到服务端【" + serviceInfo.getName() + "】");
        } else if (serviceInfo.getUserId() != userId) {
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR,
                    "当前登录用户没有关联服务端【" + serviceInfo.getName() + "】");
        }

        QueryWrapper<ProductService> qwps = new QueryWrapper<>();
        qwps.eq("product_id", productServiceSaveVO.getProductId());
        qwps.eq("service_id", productServiceSaveVO.getServiceId());
        qwps.eq("is_deleted", IsDeleteEnum.N.getKey());
        ProductService productService = this.getOne(qwps);
        if (productService == null) {
            productService = new ProductService();
            productService.setProductId(productServiceSaveVO.getProductId());
            productService.setServiceId(productServiceSaveVO.getServiceId());
            productService.setCreator(userId.toString());
            productService.setGmtCreate(new Date());
            this.save(productService);
        } else {
            productService.setModifier(userId.toString());
            productService.setGmtModified(new Date());
            this.updateById(productService);
        }

        // 生成令牌
        // 如果已有令牌，先删除原令牌，再重新生成
        // 试用产品 type=3 需生成7天有效期，重新生成令牌，有效期继承，超出有效期不允许再生成令牌
        ServiceToken old_serviceToken = serviceTokenFacade.getByProductServiceId(productService.getId());
        //删除原有token
        if (old_serviceToken != null) {
            serviceTokenFacade.deleteByProductServiceId(old_serviceToken.getProductServiceId());
        }

        ServiceToken serviceToken = new ServiceToken();
        serviceToken.setCreator(userId.toString());
        serviceToken.setGmtCreate(new Date());
        serviceToken.setProductServiceId(productService.getId());
        serviceToken.setType(productServiceSaveVO.getType());
        serviceToken.setAppKeyId(GuidUtil.App_key());
        serviceToken.setAppKeySecret(GuidUtil.App_screct());
        serviceToken.setStatus(StatusEnum.Enable.getKey());
        if (old_serviceToken != null && old_serviceToken.getType().equals(TokenTypeEnum.Trial.getKey())) {
            //如果试用已过期，不允许再生成
            if (DateUtil.after(DateUtil.now(), old_serviceToken.getExpiringDate())) {
                throw new CommonException(CommonErrorCode.SERVER_IS_ERROR,
                        "当前试用账号已过期，不允许再生成令牌");
            }
            serviceToken.setStartingDate(old_serviceToken.getStartingDate());
            serviceToken.setExpiringDate(old_serviceToken.getExpiringDate());
        } else if (productServiceSaveVO.getType().equals(TokenTypeEnum.Trial.getKey())) {
            serviceToken.setStartingDate(new Date());
            serviceToken.setExpiringDate(DateUtil.addDay(serviceToken.getStartingDate(), 7));
        }
        serviceTokenFacade.save(serviceToken);
        productServiceDTO.setServiceToken(serviceToken);

        productServiceDTO.setProductId(productService.getProductId());
        productServiceDTO.setServiceId(productService.getServiceId());

        productServiceDTO.setServiceName(serviceInfo.getName());
        productServiceDTO.setServiceDescription(serviceInfo.getDescription());

        productServiceDTO.setProductName(lantoneProduct.getName());
        productServiceDTO.setProductDecription(lantoneProduct.getDecription());
        return productServiceDTO;
    }

    /**
     * 删除产品服务端关联，级联删除令牌
     *
     * @param productServiceId
     * @return
     */
    public Boolean deleteProductService(Long productServiceId) {
        ProductService productService = this.getById(productServiceId);
        if (productService == null || productService.getIsDeleted().equals(IsDeleteEnum.Y.getKey())) {
            throw new CommonException(CommonErrorCode.NOT_EXISTS, "找不到产品服务端");
        }
        //删除关联令牌
        serviceTokenFacade.deleteByProductServiceId(productService.getId());
        //删除产品服务端
        productService.setIsDeleted(IsDeleteEnum.Y.getKey());
        this.updateById(productService);
        return true;
    }


    /**
     * 获取当前用户产品服务关联列表
     *
     * @param page
     * @return
     */
    public IPage<ProductServiceWrapper> selectProductServiceByCurrentUserPage(Page<ProductServiceWrapper> page) {
        Long userId = Long.parseLong(UserUtils.getCurrentPrincipleID());
        ProductServiceWrapper productServiceVO = new ProductServiceWrapper();
        productServiceVO.setUserId(userId);
        IPage<ProductServiceWrapper> psPage = this.selectProductServicePage(page, productServiceVO);
        return psPage;
    }
}