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

import com.bocloud.cmp.params.azure.FlavorGoodsParams;
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.common.utils.ResultTools;
import com.bocloud.ims.booter.utils.ExcelReader;
import com.bocloud.ims.entity.resource.Flavor;
import com.bocloud.ims.interfaces.resource.FlavorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/v1/flavors")
@Api(value = "虚拟机规格", tags = "虚拟机规格")
public class FlavorController {

    @Autowired
    private FlavorService flavorService;

    /**
     * 查询规格列表
     *
     * @param pager
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "查询规格列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "虚拟机规格", operation = "查询规格列表")
    @GetMapping
    public BocloudResult<GridBean<Flavor>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return flavorService.list(pager, context);
    }

    /**
     * 查询规格列表
     *
     * @param condition
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "查询规格列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "虚拟机规格", operation = "查询规格列表")
    @GetMapping("/condition")
    public BocloudResult<List<Flavor>> condition(
            @RequestParam(value = Common.CONDITION, required = false) String condition,
            @Value(Common.REQ_CONTEXT) RequestContext context) {
        return flavorService.condition(condition, context);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "查询规格详情",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "虚拟机规格", operation = "查看规格详情")
    @GetMapping("/{id}")
    public BocloudResult<Flavor> detail(@PathVariable(value = Common.ID) Long id) {
        return flavorService.detail(id);
    }

    /**
     * 创建
     *
     * @param flavor
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "创建规格",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "虚拟机规格", operation = "创建规格")
    @PostMapping
    public BocloudResult<String> create(@RequestBody Flavor flavor, @Value(Common.REQ_CONTEXT) RequestContext context) {
        flavor.setId(null);
        return flavorService.create(flavor, context);
    }

    /**
     * 修改
     *
     * @param id
     * @param flavor
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "修改规格",  httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "虚拟机规格", operation = "修改规格")
    @PutMapping("/{id}")
    public BocloudResult<String> modify(@PathVariable(value = Common.ID) Long id, @RequestBody Flavor flavor,
                                        @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null == flavor) {
            return ResultTools.formatErrResult();
        }
        flavor.setId(id);
        return flavorService.modify(flavor, context);
    }

    /**
     * 删除
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "删除规格",  httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "虚拟机规格", operation = "删除规格")
    @DeleteMapping("/{id}")
    public BocloudResult<String> remove(@PathVariable(value = Common.ID) Long id,
                                        @Value(Common.REQ_CONTEXT) RequestContext context) {
        return flavorService.remove(id, context);
    }

    /**
     * 批量删除虚拟机规格
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "虚拟机规格批量删除",  httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "虚拟机规格", operation = "批量删除虚拟机规格")
    @DeleteMapping
    public BocloudResult batchRemove(@RequestBody List<Long> idArray,
                                     @Value(Common.REQ_CONTEXT) RequestContext context) {
        return flavorService.batchRemove(idArray, context);
    }


    /**
     * 规格上下架
     *
     * @param id
     * @param flavorGoodsParams
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "虚拟机规格上下架",  httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "虚拟机规格", operation = "虚拟机规格上下架")
    @PutMapping("/{id}/status")
    public BocloudResult status(@PathVariable(value = Common.ID) Long id,
                                @RequestBody FlavorGoodsParams flavorGoodsParams) {
        return flavorService.status(id, flavorGoodsParams.getStatus());
    }


    /**
     * 批量规格上下架
     *
     * @param
     * @param flavorGoodsParams
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "虚拟机规格上下架",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "虚拟机规格", operation = "虚拟机批量规格上下架")
    @PostMapping("/status/batch")
    public BocloudResult statusBatch(@RequestBody FlavorGoodsParams flavorGoodsParams) {
        for (Long id : flavorGoodsParams.getIds()) {
            flavorService.status(id, flavorGoodsParams.getStatus());
        }
        return new BocloudResult(true, "操作成功");
    }

    /**
     * 下载导入模板
     * @param response
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "规格下载导入模板",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "虚拟机规格", operation = "下载导入模板")
    @GetMapping("/download")
    public void downloadTemplate(HttpServletResponse response
            , @RequestParam(value = Common.VENDORID, required = false) Long vendorId) {
        flavorService.downloadTemplate(response, vendorId);
    }

    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "规格下载导入模板",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "虚拟机规格", operation = "定价信息导入")
    @PostMapping("/import")
    public BocloudResult importMessage( @RequestParam(value = Common.VENDORID, required = false) Long vendorId, @RequestParam("file") MultipartFile file) {
        ExcelReader reader = new ExcelReader();
        try {
            List<Map<String, String>> flavors = reader.getFlavor(file.getInputStream());
            if (CollectionUtils.isEmpty(flavors)) {
                return new BocloudResult(false, "上传规格信息失败");
            }
            return flavorService.importCreate(flavors, vendorId);
        } catch (IOException e) {
            return new BocloudResult(false, e.getMessage());
        }
    }


}
