package com.xydl.cac.controller;

import com.alibaba.excel.util.StringUtils;
import com.xydl.cac.adapter.BusiAdapter;
import com.xydl.cac.entity.*;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.Response;
import com.xydl.cac.model.i2sync.Attr;
import com.xydl.cac.model.i2sync.Datanode;
import com.xydl.cac.model.i2sync.Monitordata;
import com.xydl.cac.model.i2sync.Request;
import com.xydl.cac.service.I2syncService;
import com.xydl.cac.service.ModevTypePointService;
import com.xydl.cac.service.ModevTypeService;
import com.xydl.cac.transform.I2DataTransformer;
import com.xydl.cac.util.DateUtil;
import com.xydl.cac.util.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@Api(tags = {"I2同步数据相关接口"})
@RequestMapping("i2sync")
@Slf4j
public class I2syncController extends BasicController {

    @Resource
    I2syncService service;
    @Resource
    ModevTypeService modevTypeService;
    @Resource
    ModevTypePointService modevTypePointService;
    @Resource
    BusiAdapter busiAdapter;
    @Resource
    I2DataTransformer dataTransformer;

    @GetMapping("status")
    @ApiOperation("查询状态")
    public Response<Map<String, Object>> status() {
        Map<String, Object> map = new HashMap<>();
        map.put("enable", busiAdapter.enable);
        map.put("url", busiAdapter.url);
        return Response.success(map);
    }

    @GetMapping("listFieldConfig")
    @ApiOperation("查询字段映射")
    public Response<List<ModevType>> listFieldConfig() {
        List<ModevType> result = service.listFieldConfig();
        return Response.success(result);
    }

    @PostMapping("updateFieldConfig")
    @ApiOperation("修改字段映射")
    public Response<String> updateFieldConfig(@Validated @RequestBody ModevType item) throws Exception {
        if (CollectionUtils.isEmpty(item.getI2syncFields())) {
            throw new BusinessException("i2syncFields不能为空!");
        }
        service.updateFieldConfig(item);
        return Response.success("OK");
    }

    @PostMapping("delFieldConfig")
    @ApiOperation("删除字段映射")
    public Response<String> delFieldConfig(@Validated @RequestBody ModevType item) throws Exception {
        service.delFieldConfig(item.getTablename());
        return Response.success("OK");
    }

    @GetMapping("listConfig")
    @ApiOperation("查询导出类型配置")
    public Response<List<I2syncConfig>> listConfig() {
        List<I2syncConfig> result = service.listConfig();
        return Response.success(result);
    }

    @PostMapping("addConfig")
    @ApiOperation("新增导出类型配置")
    public Response<String> addConfig(@Validated @RequestBody I2syncConfig item) throws Exception {
        service.addConfig(item);
        return Response.success("OK");
    }

    @PostMapping("delConfig")
    @ApiOperation("删除导出类型配置")
    public Response<String> delConfig(@Validated @RequestBody I2syncConfig item) throws Exception {
        if (item.getId() == null) {
            throw new BusinessException("id不能为空!");
        }
        service.delConfig(item.getId());
        return Response.success("OK");
    }

    @GetMapping("listRecord")
    @ApiOperation("查询导出记录")
    public Response<List<I2syncRecord>> listRecord(Integer modevtypeId) {
        List<I2syncRecord> result = service.listRecord(modevtypeId);
        return Response.success(result);
    }

    @GetMapping("prewXml")
    @ApiOperation("预览xml结构")
    public Response<String> prewXml(Integer modevtypeId) throws Exception {
        ModevType modevType = modevTypeService.detail(modevtypeId);
        List<ModevTypePoint> points = modevTypePointService.listAll(modevtypeId);
        List<I2syncField> fieldList = service.listFieldConfig(modevType.getTablename());
        NSensor sensor = NSensor.builder()
                .id(12)
                .typeId(modevtypeId)
                .sensorCode("HNHFT001Q1W000104")
                .equipmentId("HNHFT001Q1W000104")
                .phase("ABC相")
                .build();
        I2syncRecord record = I2syncRecord.builder()
                .build();
        I2syncConfig config = I2syncConfig.builder()
                .typeCode(modevType.getTypeCode())
                .build();
        List<Map<String, Object>> dataList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("acquisitionTime", DateUtil.format(new Date()));
        for (ModevTypePoint point : points) {
            map.put(point.getField(), 1.25);
        }
        dataList.add(map);

        Request request = dataTransformer.transform(sensor, record, config, fieldList, dataList);

        String xml = JSONUtil.object2Xml(request);
        Response<String> resp = Response.success(xml);
        if (StringUtils.isBlank(modevType.getTypeCode())) {
            resp.setWarnMsg("该类型还未配置对应的类型编码将导致type为空");
        }
        return resp;
    }

}
