package com.nantian.cloud.dbcapp.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nantian.cloud.dbcapp.entity.ConfigureDataCollectGenerate;
import com.nantian.cloud.dbcapp.service.ConfigureDataCollectGenerateService;
import com.nantian.cloud.dbcapp.unit.MD5Util;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/configureDataCollectGenerate")
public class ConfigureDataCollectGenerateController {

    @Autowired
    private ConfigureDataCollectGenerateService configureDataCollectGenerateService;


    @PostMapping("/addCollectDataGenerate")
    @SysLog("采集配置项配置工具-采集数据项配置生成")
    @ApiOperation(value = "采集数据项配置生成", notes = "采集数据项配置生成")
    @ApiImplicitParam(name = "epc", value = "企业信用编码", required = true, dataType = "string", paramType = "path")
    public R addCollectDataGenerate(@RequestBody String s){
        JSONObject json = JSONObject.parseObject(s);
        String data = json.getString("data");
        boolean flag=false;
        List<ConfigureDataCollectGenerate> configureDataCollectGenerateList = JSONArray.parseArray(data, ConfigureDataCollectGenerate.class);
        List<String> digestList = configureDataCollectGenerateService.getDigestList(configureDataCollectGenerateList.get(0).getEnterpriseCode());
        List<Long> idList = configureDataCollectGenerateService.getIdList(configureDataCollectGenerateList.get(0).getEnterpriseCode());
        for (ConfigureDataCollectGenerate configureDataCollectGenerate : configureDataCollectGenerateList) {
            R result = check(configureDataCollectGenerate);
            if (result != null) {
                return result;
            }
            configureDataCollectGenerate.setDigest(getDigest(configureDataCollectGenerate));
            configureDataCollectGenerate.setMemorandumDate(LocalDateTime.now());
            if (idList.contains(configureDataCollectGenerate.getId())){
                ConfigureDataCollectGenerate configureDataOld = configureDataCollectGenerateService.getById(configureDataCollectGenerate.getId());
                if (!configureDataCollectGenerate.equals(configureDataOld)){
                    configureDataCollectGenerateService.backUpGenerateData(configureDataOld);
                    flag=configureDataCollectGenerateService.updateById(configureDataCollectGenerate);
                }
            }else {
                if(digestList.contains(configureDataCollectGenerate.getDigest())){
                    return R.failed(configureDataCollectGenerate,"采集配置项重复！");
                }
                flag=configureDataCollectGenerateService.save(configureDataCollectGenerate);
            }
        }
        return R.ok(flag,"保存成功！");
    }

    @DeleteMapping("/removeCollectDataGenerate")
    @SysLog("采集配置项配置工具-删除生成的采集数据配置项")
    @ApiOperation(value = "删除生成的采集数据配置项",notes = "删除生成的采集配置项")
    @ApiImplicitParam(name = "epc", value = "企业信用代码",required = true,dataType = "string",paramType = "path")
    public R removeById(@RequestBody ConfigureDataCollectGenerate configureDataCollectGenerate){
        configureDataCollectGenerateService.backUpGenerateData(configureDataCollectGenerateService.getById(configureDataCollectGenerate.getId()));
        return R.ok(configureDataCollectGenerateService.removeById(configureDataCollectGenerate.getId()));
    }

    @PutMapping("/updateCollectDataGenerate")
    @SysLog("采集配置项配置工具-修改生成的采集数据项配置")
    @ApiOperation(value = "修改生成的采集数据项配置", notes = "修改生成的采集数据项配置")
    @ApiImplicitParam(name = "configureDataCollectGenerate", value = "企业采集配置项生成对象", required = true, dataType = "json", paramType = "path")
    public R updateById(@RequestBody ConfigureDataCollectGenerate configureDataCollectGenerate) {
        R result = check(configureDataCollectGenerate);
        if (result != null) {
            return result;
        }
        configureDataCollectGenerateService.backUpGenerateData(configureDataCollectGenerateService.getById(configureDataCollectGenerate.getId()));
        return R.ok(configureDataCollectGenerateService.updateById(configureDataCollectGenerate),"修改成功！");
    }

    @GetMapping("/getListCollectDataGenerate")
    @SysLog("采集配置项配置工具-查询生成的采集数据项配置")
    @ApiOperation(value = "查询生成的采集数据项配置", notes = "查询生成的采集数据项配置")
    @ApiImplicitParam(name = "epc", value = "企业信用编码", required = true, dataType = "string", paramType = "path")
    public R getListCollectDataGenerate(String epc){
        if (epc == null || "".equals(epc)) {
            return R.failed("企业信用编码为空，查询失败！");
        }
        List<Map<String, Object>> generateList = configureDataCollectGenerateService.getGenerateList(epc);
        return R.ok(generateList,"查询成功");
    }

    private String getDigest(ConfigureDataCollectGenerate configureDataCollectGenerate) {
        return getMark(configureDataCollectGenerate.getEnterpriseCode(), configureDataCollectGenerate.getDataCode());
    }

    protected String getMark(String... strings) {
        return MD5Util.getDigest(strings);
    }

    private R check(ConfigureDataCollectGenerate configureDataCollectGenerate) {
        if (StringUtils.isBlank(configureDataCollectGenerate.getEnterpriseCode())){
            return R.failed(false,"企业社会信用代码不能为空！");
        }else if (StringUtils.isBlank(configureDataCollectGenerate.getDataCode())){
            return R.failed(false,"采集数据项编码不能为空！");
        }else if (StringUtils.isBlank(configureDataCollectGenerate.getProcessCode())){
            return R.failed(false,"生产工序编码不能为空！");
        }else if(StringUtils.isBlank(configureDataCollectGenerate.getProcessUnitCode())){
            return R.failed(false,"工序单元编码不能为空！");
        }else if (StringUtils.isBlank(configureDataCollectGenerate.getEquipmentUnitCode())){
            return R.failed(false,"重点用能设备编码不能为空！");
        }else if (StringUtils.isBlank(configureDataCollectGenerate.getEnergyClassCode())){
            return R.failed(false,"采集数据类型编码不能为空！");
        }else if (StringUtils.isBlank(configureDataCollectGenerate.getEnergyTypeCode())){
            return R.failed(false,"能源分类分项编码不能为空！");
        }else if (StringUtils.isBlank(configureDataCollectGenerate.getEnergyTypeCode())){
            return R.failed(false,"能源分类分项编码不能为空！");
        }else if (StringUtils.isBlank(configureDataCollectGenerate.getDataUsageCode())){
            return R.failed(false,"用途编码不能为空！");
        }else if (StringUtils.isBlank(configureDataCollectGenerate.getInputType())){
            return R.failed(false,"数据采集来源不能为空！");
        }else if (StringUtils.isBlank(configureDataCollectGenerate.getStatType())){
            return R.failed(false,"采集频率不能为空！");
        }else if (StringUtils.isBlank(configureDataCollectGenerate.getUnit())){
            return R.failed(false,"单位不能为空！");
        }else if (StringUtils.isBlank(configureDataCollectGenerate.getDataCodeOld())){
            return R.failed(false,"采集配置项映射编码不能为空！");
        }else {
            return null;
        }
    }
}
