package com.huatek.busi.controller;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.common.util.FileUtils;
import com.huatek.busi.entity.GlobalSettingItem;
import com.huatek.busi.entity.WiringDiagram;
import com.huatek.busi.service.*;
import com.huatek.busi.where.*;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

@Api(tags={"一次接线图管理接口"})
@RestController
@RequestMapping("/api/wiringDiagramController")
public class WiringDiagramController {


    @Reference(check = false)
    ISysFileDiagramService iSysFileDiagramService;

    @Reference(check = false)
    IGlobalSettingItemService globalSettingItemService;

    @Reference(check = false)
    IWiringDiagramService iWiringDiagramService;





    @ApiOperation(value = "一次接线图管理列表", httpMethod="POST", notes= "一次接线图管理列表", produces = "application/json;charset=utf-8",response= Map.class)
    @PostMapping(value = "/list", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> list(@RequestBody SysFileDiagramSelectVo SysFileDiagramSelectVo){
        PageInfo<WiringDiagram> wiringDiagrams= iWiringDiagramService.list(SysFileDiagramSelectVo);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, wiringDiagrams.getTotal(), "", wiringDiagrams.getList());

    }


    @ApiOperation(value = "新增import", httpMethod="POST", notes= "新增import", produces = "application/json;charset=utf-8",response= Map.class)
    @PostMapping("/import")
    public Map<String, Object> importFile(@RequestParam("file") MultipartFile file,
                                          @RequestHeader("token") String token,
                                          @RequestParam("type") String type,
                                          @RequestParam("fileName") String fileName) throws IOException {
        GlobalSettingItem globalSettingItem = globalSettingItemService.findOne();
        if(globalSettingItem == null || (globalSettingItem.getFileUploadNumber() == null && globalSettingItem.getFileUploadNumber() <= 0)){
            return ResultUtil.put(ConstantUtil.REQUEST_FAIL, "BUSINESS.UPLOAD.ANNEX.NUM", "");
        }
        String fileNamenew = file.getOriginalFilename();
        // 上传根目录
        String rootPath = globalSettingItem.getRootPath();
        return iWiringDiagramService.importFile(file.getBytes(), token, "0", fileNamenew, rootPath,type,fileName);

    }


    @ApiOperation(value = "删除")
    @PostMapping(value = "/deleteWiringDiagram", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> deleteWiringDiagram(@RequestHeader("token") String token,
                                                          @RequestBody List<String> ids) {
        int count = iWiringDiagramService. deleteWiringDiagram(token, ids);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, count >0  ?"删除成功" :"删除失败", "");
    }

    @ApiOperation(value = "新增Model", httpMethod="POST", notes= "新增Model", produces = "application/json;charset=utf-8",response= Map.class)
    @PostMapping("/importModel")
    public Map<String, Object> importModelFile(@RequestParam("file") MultipartFile file,
                                          @RequestHeader("token") String token,
                                          @RequestParam("modelType") String  modelType,
                                          @RequestParam("name") String  name,
                                          @RequestParam("siteId") String siteId,
                                          @RequestParam("status") String status
                                               ) throws IOException {
        GlobalSettingItem globalSettingItem = globalSettingItemService.findOne();
        if(globalSettingItem == null || (globalSettingItem.getFileUploadNumber() == null && globalSettingItem.getFileUploadNumber() <= 0)){
            return ResultUtil.put(ConstantUtil.REQUEST_FAIL, "BUSINESS.UPLOAD.ANNEX.NUM", "");
        }
        String fileNamenew = file.getOriginalFilename();
        // 上传根目录
        String rootPath = globalSettingItem.getRootPath();
        return iWiringDiagramService.importModelFile(file.getBytes(), token, modelType, fileNamenew, rootPath,modelType,name,siteId, status);

    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/deleteModel", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> deleteModel(@RequestHeader("token") String token,
                                                   @RequestBody List<String> ids) {
        int count = iWiringDiagramService.deleteModel(token, ids);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, count >0  ?"删除成功" :"删除失败", "");
    }

}
