package com.nantian.cloud.dbcapp.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.dbcapp.entity.ConfigureDataCollect;
import com.nantian.cloud.dbcapp.service.ConfigureDataCollectService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/configureDataCollect")
public class ConfigureDataCollectController {

    @Autowired
    private ConfigureDataCollectService configureDataCollectService;

    @GetMapping("/getList")
    @SysLog("获取企业工序单元")
    @ApiOperation(value = "获取企业工序单元", notes = "获取企业工序单元")
    @ApiImplicitParam(name = "epc", value = "企业信用编码", required = true, dataType = "string", paramType = "path")
    public R getConfigureDataCollect(Page page,String epc, String collectItemName){
        if (epc == null) {
            return R.failed("企业信用编码为空，查询失败！");
        }
        return R.ok(configureDataCollectService.getList(page,epc,collectItemName));
    }

    @GetMapping("/getListData")
    @SysLog("获取企业工序单元")
    @ApiOperation(value = "获取企业工序单元", notes = "获取企业工序单元")
    @ApiImplicitParam(name = "epc", value = "企业信用编码", required = true, dataType = "string", paramType = "path")
    public R getConfigureDataCollectData(String epc, String collectItemName){
        if (epc == null) {
            return R.failed("企业信用编码为空，查询失败！");
        }
        return R.ok(configureDataCollectService.getListData(epc,collectItemName));
    }

    @GetMapping("/getListDataNew")
    @SysLog("获取企业工序单元")
    @ApiOperation(value = "获取企业工序单元", notes = "获取企业工序单元")
    @ApiImplicitParam(name = "epc", value = "企业信用编码", required = true, dataType = "string", paramType = "path")
    public R getConfigureDataCollectDataNew(String epc, String collectItemName){
        if (epc == null) {
            return R.failed("企业信用编码为空，查询失败！");
        }
        return R.ok(configureDataCollectService.getListDataNew(epc,collectItemName));
    }

    /*新增采集数据项配置*/
    @PostMapping("/addCollect")
    @SysLog("新增采集数据项配置")
    @ApiOperation(value = "采集数据项配置", notes = "采集数据项配置")
    @ApiImplicitParam(name = "epc", value = "企业信用编码", required = true, dataType = "string", paramType = "path")
    public R addCollect(@RequestBody ConfigureDataCollect configureDataCollect){
        return R.ok(configureDataCollectService.save(configureDataCollect));
    }


    /*新增采集数据项配置到新表*/
    @PostMapping("/addCollectData")
    @SysLog("新增采集数据项配置到新表")
    @ApiOperation(value = "采集数据项配置", notes = "采集数据项配置")
    @ApiImplicitParam(name = "epc", value = "企业信用编码", required = true, dataType = "string", paramType = "path")
    public R addCollectData(@RequestBody String s){
        JSONObject json = JSONObject.parseObject(s);
        String data = json.getString("data");
        boolean flag=false;
        List<ConfigureDataCollect> configureDataCollectList = JSONArray.parseArray(data, ConfigureDataCollect.class);
        for (ConfigureDataCollect configureDataCollect : configureDataCollectList) {
            List<Map<String, Object>> list=configureDataCollectService.getListDataNew(configureDataCollect.getEnterpriseCode(),configureDataCollect.getCollectItemName());
            if(list.size()==0){
                flag=configureDataCollectService.saveData(configureDataCollect);
            }
        }
        return R.ok(flag);
    }

    @PutMapping("/updateCollect")
    @SysLog("修改采集数据项配置")
    @ApiOperation(value = "修改采集数据项配置", notes = "修改采集数据项配置")
    @ApiImplicitParam(name = "configureDataCollect", value = "企业采集配置项对象", required = true, dataType = "json", paramType = "path")
    public R updateById(@RequestBody ConfigureDataCollect configureDataCollect) {
        return R.ok(configureDataCollectService.updateById(configureDataCollect));
    }
}
