package com.huatek.busi.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.dto.BusiVideoDto;
import com.huatek.busi.dto.CapacitanceEquipmentImCacheDto;
import com.huatek.busi.dto.DataOutputEntityDto;
import com.huatek.busi.dto.SimulateResultDto;
import com.huatek.busi.dto.cablecurrent.CableCurrentPageDto;
import com.huatek.busi.dto.cablecurrent.CableCurrentResultDto;
import com.huatek.busi.entity.BusiCableCurrentResult;
import com.huatek.busi.service.cablecurrent.ICableCurrentResultService;
import com.huatek.busi.service.dataoutput.DataOutputEntityService;
import com.huatek.busi.vo.BusiCapacitanceMonitorVo;
import com.huatek.busi.vo.BusiVideoVo;
import com.huatek.busi.vo.SimulateResultVo;
import com.huatek.busi.vo.cablecurrent.CableCurrentPageVo;
import com.huatek.busi.vo.cablecurrent.CableCurrentPageXmlVo;
import com.huatek.busi.vo.dataoutput.DataOutputEntityVo;
import com.huatek.busi.vo.warning.BusiCapaVo;
import com.huatek.torch.frame.exception.OwlException;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.stream.XMLStreamException;
import java.io.*;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

import java.nio.charset.Charset;
import java.util.Map;

/**
 * @author Vent_yan
 * 2023-03-27
 */
@Slf4j
@Api(tags={"电缆载流管理接口"})
@RestController
@RequestMapping("/api/cableCurrent")
public class CableCurrentResultController {

    @Reference(check = false)
    ICableCurrentResultService cableCurrentResultService;

    @Reference(check = false)
    DataOutputEntityService dataOutputEntityService;

    @ApiOperation(value = "根据输入数据项计算仿真结果数据",httpMethod="POST", notes= "根据输入数据项计算仿真结果数据", produces = "application/json;charset=utf-8", response= Map.class)
    @PostMapping(value = "/calculateByInputItemDate", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> calculateByInputItemDate(@RequestHeader String token, @RequestBody CableCurrentResultDto cableCurrentResultDto){
        cableCurrentResultDto.setType("1");
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "",cableCurrentResultService.calculateByInputItemDate(token, cableCurrentResultDto));
    }

    @ApiOperation(value = "电缆载流管理详情")
    @GetMapping(value = "/calculateByInfo")
    public Map<String, Object> calculateByInfo(@RequestHeader String token,@RequestParam(value = "id") String id) {
        BusiCableCurrentResult busiCableCurrentResult = cableCurrentResultService.calculateByInfo(id);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "",busiCableCurrentResult);
    }

    @ApiOperation(value = "列表分页查询", response = BusiVideoVo.class)
    @PostMapping(value = "/queryList", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> queryList(@RequestBody CableCurrentPageDto cableCurrentPageDto) {
        PageInfo<CableCurrentPageVo> cableCurrentPageVos = cableCurrentResultService.queryList(cableCurrentPageDto);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, cableCurrentPageVos.getTotal(), "",
                cableCurrentPageVos.getList());
    }

    @ApiOperation(value = "文件仿真下载")
    @ApiImplicitParam(name = "id", value = "记录ID")
    @GetMapping("/downloadReal")
    public void downloadReal(HttpServletResponse response, HttpServletRequest request,
                             @RequestParam(value = "id") String id ) throws XMLStreamException {
        BusiCableCurrentResult busiCableCurrentResult = cableCurrentResultService.calculateByInfo(id);
        CableCurrentPageXmlVo cableCurrentPageVo=new CableCurrentPageXmlVo();
        cableCurrentPageVo.setId(busiCableCurrentResult.getId());
        cableCurrentPageVo.setCreateTime(busiCableCurrentResult.getCreateTime());
        if("1".equals(busiCableCurrentResult)){
            cableCurrentPageVo.setFlag("抽水");
            cableCurrentPageVo.setPumpingCurrent(busiCableCurrentResult.getPumpingCurrent());
            cableCurrentPageVo.setElectricPowerDuration(busiCableCurrentResult.getPumpingDuration());
        }else{
            cableCurrentPageVo.setFlag("发电");
            cableCurrentPageVo.setPumpingCurrent(busiCableCurrentResult.getElectricPowerCurrent());
            cableCurrentPageVo.setElectricPowerDuration(busiCableCurrentResult.getElectricPowerDuration());
        }

        String objtoxml = objtoxml(cableCurrentPageVo);
        Document doc = DocumentHelper.createDocument();
        try{
            //4、创建一个文件输出流
            FileOutputStream fos = new FileOutputStream("emps.xml");
            //装饰者模式 写XML文档的输出流
            XMLWriter writer = new XMLWriter(fos);
            writer.write(objtoxml);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }



    }

    /**
     * 将对象转为流程XML
     */
    public static String objtoxml(Object obj) {
        // 创建输出流
        StringWriter sw = new StringWriter();
        try {
            // 利用jdk中自带的转换类实现
            JAXBContext context = JAXBContext.newInstance(obj.getClass());

            Marshaller marshaller = context.createMarshaller();
            // 格式化xml输出的格式
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                    Boolean.TRUE);
            // 将对象转换成输出流形式的xml
            marshaller.marshal(obj, sw);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return sw.toString();
    }
}
