package com.huatek.busi.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.common.util.ExcelExport;
import com.huatek.busi.dto.BusiSiteTreeDto;
import com.huatek.busi.service.BusiSiteTreeService;
import com.huatek.busi.vo.BusiTreeNodeVo;
import com.huatek.busi.vo.SensiorIdExportVo;
import com.huatek.torch.frame.log.KerLog;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

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

/**
 * @ClassName: BusiSiteTreeController
 * @FullClassPath: com.huatek.busi.controller.BusiSiteTreeController
 * @Description: 站点树控制层
 * @author: Arno
 * @date: Jun 21, 2019 10:30:55 AM
 * @version: 1.0
 */

@RestController
@RequestMapping("/api/BusiSiteTree")
public class BusiSiteTreeController {

	protected final Logger logger = LoggerFactory.getLogger(BusiSiteTreeController.class);

	@Reference(check = false)
	private BusiSiteTreeService busiSiteTreeService;

//	@GetMapping(value = "/insertSiteNode")
//	public Map<String, Object> insertSiteNode(@RequestParam Map<String, Object> params) {
//		Map<String, Object> map = new HashMap<>();
//		busiSiteTreeService.insertSiteNode(params.get("siteId").toString(), params.get("siteName").toString());
//		return map;
//	}

    @ApiOperation(value="新增设备类型", notes="")
    //@ApiImplicitParam(paramType="query", name = "userNumber", value = "用户编号", required = true, dataType = "Integer")
	@PostMapping(value = "/insertSiteTypeNode")
	public Map<String, Object> insertSiteTypeNode(@RequestBody Map<String, Object> params) {
		Map<String, Object> p = new HashMap<>();
		if (params.containsKey("BYQ")) {
			p.put(SiteConstants.SiteTreeEnum.BYQ.getValue(), params.get("BYQ"));
		}
		if (params.containsKey("GIS")) {
			p.put(SiteConstants.SiteTreeEnum.GIS.getValue(), params.get("GIS"));
		}
		if (params.containsKey("CW")) {
			p.put(SiteConstants.SiteTreeEnum.CW.getValue(), params.get("CW"));
		}
		if (params.containsKey("BLQ")) {
			p.put(SiteConstants.SiteTreeEnum.BLQ.getValue(), params.get("BLQ"));
		}
		//发电机
		if (params.containsKey("GEN")) {
			p.put(SiteConstants.SiteTreeEnum.GEN.getValue(), params.get("GEN"));
		}
		if (params.containsKey("OTHER")) {
            p.put(SiteConstants.SiteTreeEnum.OTHER.getValue(), params.get("OTHER"));
        }
		// 电缆
		if (params.containsKey("DL")) {
			p.put(SiteConstants.SiteTreeEnum.DL.getValue(), params.get("DL"));
		}
		
		String siteId = params.get("nodeId").toString();
		busiSiteTreeService.insertSiteTypeNode(siteId, p);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", null);
	}

    @ApiOperation(value="新增设备", notes="")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType="query", name = "nodeId", value = "设备节点ID", required = true, dataType = "String"),
        @ApiImplicitParam(paramType="query", name = "count", value = "设备数量", required = true, dataType = "String")
    })
	@GetMapping(value = "/insertSiteEquipmentNode")
	public Map<String, Object> insertSiteEquipmentNode(@RequestParam(value="nodeId",required=true)String  nodeId, @RequestParam(value="count", defaultValue ="0", required=true)String count ) {
		busiSiteTreeService.insertSiteEquipmentNode(nodeId, Integer.valueOf(count));
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", null);
	}

    @ApiOperation(value="站点设备树", notes="")
	@GetMapping(value = "/tree")
	public Map<String, Object> tree() {
		List<BusiTreeNodeVo> siteTree = busiSiteTreeService.siteTree("0");
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", siteTree);
	}

	@ApiOperation(value="站点设备树(GIS只取开关及断路器)", notes="")
	@GetMapping(value = "/treeForCalculate")
	public Map<String, Object> treeForCalculate() {
		List<BusiTreeNodeVo> siteTree = busiSiteTreeService.siteTreeForCalculate("0");
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", siteTree);
	}

    @ApiOperation(value="修改节点名称", notes="")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType="query", name = "nodeId", value = "设备节点ID", required = true, dataType = "String"),
        @ApiImplicitParam(paramType="query", name = "newName", value = "节点新名称", required = true, dataType = "String")
    })
	@PostMapping(value = "/updateNode")
	public Map<String, Object> updateNode(@RequestBody BusiSiteTreeDto busiSiteTreeDto) {
		busiSiteTreeService.updateNode(busiSiteTreeDto);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", null);
	}

    @ApiOperation(value="删除节点", notes="")
    @ApiImplicitParam(paramType="query", name = "nodeId", value = "设备节点ID", required = true, dataType = "String")
	@GetMapping(value = "/deleteNode/{nodeId}")
	public Map<String, Object> deleteNode(@PathVariable(value="nodeId",required=true)String  nodeId) {
		busiSiteTreeService.deleteNode(nodeId);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", null);
	}

    @ApiOperation(value = "主设备列表", response = BusiTreeNodeVo.class)
    @ApiImplicitParam(name = "equipmentType", value = "111: 变压器设备; 211：GIS设备; 411：避雷器设备, 611发电机", allowMultiple = true)
    @GetMapping(value = "/mainEquipmentList")
    public Map<String, Object> mainEquipmentList(@RequestParam("equipmentType") String[] equipmentType) {
        List<BusiTreeNodeVo> siteTree = busiSiteTreeService.findMainEquipmentList(equipmentType);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", siteTree);
    }

	/**
	 * 导出sensior设备报表
	 */
	@KerLog(projectName = "PROJECTNAME", modelName = "BUSISITE.MODELNAME", methodName = "BUSISITE.METHODNAME.EXPORTSENSIORDATA")
	@GetMapping(value = "/exportSensiorData/{siteId}", produces = { "application/json;charset=utf-8" })
	public void exportSensiorData(@PathVariable(value = "siteId") String siteId, HttpServletResponse response) {
		try {
			List<SensiorIdExportVo> sensorList = busiSiteTreeService.exportSensiorData(siteId);
			String fileName = "传感器编号报表";
			OutputStream out = response.getOutputStream();
			response.reset();
			response.setHeader("Content-disposition",
					"attachment; filename=" + new String(fileName.getBytes("GB2312"), "8859_1") + ".xls");
			response.setContentType("application/msexcel");
			ExcelExport<SensiorIdExportVo> ex = new ExcelExport<>();
	        HSSFWorkbook wb = ex.exportExcel(fileName, sensorList);
			wb.write(out);
			wb.close();
			out.flush();
			out.close();
		} catch (Exception e) {
			logger.error(String.format("OUTPUT-EXPORT-SENSIOR throws exception=%s", e));
		}
	}
	
	/**
	 * 获取数据输入站点树
	 */
	@ApiOperation(value="站点设备树", notes="")
	@GetMapping(value = "/inputDataTree")
	public Map<String, Object> inputDataTree() {
		List<BusiTreeNodeVo> siteTree = busiSiteTreeService.inputDataTree("0");
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", siteTree);
	}

	@ApiOperation(value="查询主设备名称", notes="")
	@GetMapping(value = "/findAll")
	public Map<String, Object> findAll() {
		List<BusiTreeNodeVo> siteTree = busiSiteTreeService.findAll();
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", siteTree);
	}

	@ApiOperation(value="根据设备类型标识查询设备类型", notes="")
	@PostMapping(value = "/findByEquTypes")
	public Map<String, Object> findByEquTypes(@RequestParam("equType") String equType) {
		List<BusiTreeNodeVo> siteTree = busiSiteTreeService.findByEquTypes(equType);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", siteTree);
	}

	@ApiOperation(value="根据设备类型标识查询设备类型(共三级，例GIS等)", notes="")
	@PostMapping(value = "/findThirdLevelByEquTypes")
	public Map<String, Object> findThirdLevelByEquTypes(@RequestParam("equType") String equType,@RequestParam("equId") String equId) {
		List<BusiTreeNodeVo> siteTree = busiSiteTreeService.findThirdLevelByEquTypes(equType,equId);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", siteTree);
	}
}