package com.bocloud.ims.booter.controller.resource.network;

import com.bocloud.common.annotations.Log;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.GridBean;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.Common;
import com.bocloud.ims.entity.resource.CloudServer;
import com.bocloud.ims.interfaces.resource.ResourceAllocateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Base64;
import java.util.Map;


@RestController
@RequestMapping("/v1/resource/allocation")
@Api(value = "资源分配", tags = "资源分配")
public class ResourceAllocationController {

    private static Logger logger = LoggerFactory.getLogger(ResourceAllocationController.class);


    @Autowired
    private ResourceAllocateService resourceAllocateService;

    /**
     * 资源分配清单
     */
    @ApiOperation(tags = { "CMC", "CSC" }, value = "查询資源分配列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "資源分配", operation = "查询資源分配列表")
    @GetMapping("/resourceList")
    public BocloudResult<GridBean<CloudServer>> resourceList(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return resourceAllocateService.resourceList(pager, context);
    }

    /**
     * 到期云主机列表
     *
     * @param pager
     * @param context
     * @return
     */
    @ApiOperation(tags = { "CMC", "CSC" }, value = "查询到期云主机列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "資源分配", operation = "查询到期云主机列表")
    @GetMapping("/serverList")
    public BocloudResult<GridBean<CloudServer>> serverList(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return resourceAllocateService.serverList(pager, context);
    }

    /**
     * 导出资源清单
     *
     * @param time
     * @param context
     * @return
     */
    @ApiOperation(tags = { "CMC", "CSC" }, value = "导出资源清单",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "資源分配", operation = "导出资源清单")
    @GetMapping("/downLoadResourceList")
    public void downLoadResourceList(@RequestParam( value = "time") String time, @Value(Common.REQ_CONTEXT) RequestContext context, HttpServletRequest request, HttpServletResponse response) {
        BocloudResult<Map<String, Object>> bocloudResult = resourceAllocateService.downLoadResourceList(time, context);
        if (bocloudResult.isSuccess()) {
            String content;
            try {
                Map<String, Object> result = bocloudResult.getData();
                String fileName = result.get("fileName").toString();
                response.setContentType("application/vnd.ms-excel");

                fileName = URLEncoder.encode(fileName, "UTF-8");
                response.setCharacterEncoding("utf-8");
                response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
                OutputStream outputStream = response.getOutputStream();
                content = (String)result.get("data");
                if (content != null) {
                    byte[] bytes = Base64.getDecoder().decode(content);
                    outputStream.write(bytes);
                    outputStream.close();
                }
            } catch (Exception e) {
                logger.error("导出资源清单异常", e);
            }
        }

    }

    /**
     * 导出到期云主机列表
     *
     * @param time
     * @param context
     * @return
     */
    @ApiOperation(tags = { "CMC", "CSC" }, value = "导出到期云主机列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "資源分配", operation = "导出到期云主机列表")
    @GetMapping("/downLoadServerList")
    public void downLoadServerList(@RequestParam( value = "time") String time, @RequestParam( value = "name") String name, @Value(Common.REQ_CONTEXT) RequestContext context, HttpServletRequest request, HttpServletResponse response) {
        BocloudResult<Map<String, Object>> bocloudResult = resourceAllocateService.downLoadServerList(name, time, context);
        if (bocloudResult.isSuccess()) {
            String content;
            try {
                Map<String, Object> result = bocloudResult.getData();
                String fileName = result.get("fileName").toString();
                response.setContentType("application/vnd.ms-excel");

                fileName = URLEncoder.encode(fileName, "UTF-8");
                response.setCharacterEncoding("utf-8");
                response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
                OutputStream outputStream = response.getOutputStream();
                content = (String)result.get("data");
                if (content != null) {
                    byte[] bytes = Base64.getDecoder().decode(content);
                    outputStream.write(bytes);
                    outputStream.close();
                }
            } catch (Exception e) {
                logger.error("导出到期云主机列表异常", e);
            }
        }

    }
}
