package com.diagbot.facade;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.diagbot.dto.EnShowOptionDTO;
import com.diagbot.dto.GetConsoleOnTrialDTO;
import com.diagbot.dto.GetConsoleOpenedDTO;
import com.diagbot.dto.OpenUpOnTrialDTO;
import com.diagbot.dto.ProductServiceDTO;
import com.diagbot.entity.OpenedProducts;
import com.diagbot.entity.ProductService;
import com.diagbot.entity.ServiceInfo;
import com.diagbot.service.impl.OpenedProductsServiceImpl;
import com.diagbot.util.UserUtils;
import com.diagbot.vo.OpenUpOnTrialVO;
import com.diagbot.vo.ProductServiceSaveVO;

/**
 * @Description:已开通产品业务层
 * @author: zhaops
 * @time: 2018/9/19 16:55
 */
@Component
public class OpenedProductsFacade extends OpenedProductsServiceImpl {
	@Autowired
	private ServiceInfoFacade serviceInfoFacade;
	@Autowired
	private ProductServiceFacade productServiceFacade;

    public List<OpenedProducts> getByAppkeyAndSecretFac(Map map) {
        return this.getByAppkeyAndSecret(map);
    }


    public List<GetConsoleOpenedDTO> getConsoleOpened(){
    	Long userId = Long.parseLong(UserUtils.getCurrentPrincipleID());
        return baseMapper.getConsoleOpened(userId);
    }

    public List<GetConsoleOnTrialDTO> getConsoleOnTrial(){
    	Long userId = Long.parseLong(UserUtils.getCurrentPrincipleID());
        return baseMapper.getConsoleOnTrial(userId);
    }
    
    public OpenUpOnTrialDTO openUpOnTrial(OpenUpOnTrialVO openUpOnTrialVO){
    	Long userId = Long.parseLong(UserUtils.getCurrentPrincipleID());
    	String userName = UserUtils.getCurrentPrinciple();
    	Date now = new Date();
    	ServiceInfo serviceInfo = new ServiceInfo();
    	serviceInfo.setType(2);
    	serviceInfo.setUserId(userId);
    	serviceInfo.setDescription("用户试用产品-虚拟服务");
    	serviceInfo.setName("虚拟服务");
    	serviceInfo.setCreator(userName);
    	serviceInfo.setGmtCreate(now);
    	serviceInfo.setIsDeleted("N");
    	serviceInfoFacade.save(serviceInfo);
    	
    	ProductService productService = new ProductService();
    	productService.setServiceId(serviceInfo.getId());
    	productService.setProductId(openUpOnTrialVO.getProductId());
    	productService.setCreator(userName);
    	productService.setGmtCreate(now);
    	productService.setIsDeleted("N");
    	productServiceFacade.save(productService);
    	
    	ProductServiceSaveVO productServiceSaveVO = new ProductServiceSaveVO();
    	productServiceSaveVO.setProductId(openUpOnTrialVO.getProductId());
    	productServiceSaveVO.setServiceId(serviceInfo.getId());
    	productServiceSaveVO.setType(3);
    	ProductServiceDTO poroductServiceDTO = productServiceFacade.genProductService(productServiceSaveVO);
    	
    	OpenUpOnTrialDTO openUpOnTrialDTO = new OpenUpOnTrialDTO();
    	openUpOnTrialDTO.setServiceToken(poroductServiceDTO.getServiceToken());
    	
    	return openUpOnTrialDTO;	
    }
    
    public EnShowOptionDTO enShowOption(){
    	Long userId = Long.parseLong(UserUtils.getCurrentPrincipleID());
    	int openedCount = baseMapper.getConsoleOpenedCount(userId);
    	int onTrialCount = baseMapper.getConsoleOnTrialCount(userId);
    	
    	EnShowOptionDTO enShowOptionDTO = new EnShowOptionDTO();
    	enShowOptionDTO.setOpenedCount(openedCount);
    	enShowOptionDTO.setOnTrialCount(onTrialCount);
    	
    	return enShowOptionDTO;
    }

}
