package com.nantian.cloud.dbcapp.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nantian.cloud.dbcapp.entity.ConfigureDataCollectNew;
import com.nantian.cloud.dbcapp.entity.ConfigureProcess;
import com.nantian.cloud.dbcapp.entity.TConfigEndtime;
import com.nantian.cloud.dbcapp.entity.TConfigureEnergykinds;
import com.nantian.cloud.dbcapp.service.ConfigureDataCollectNewService;
import com.nantian.cloud.dbcapp.service.ConfigureProcessService;
import com.nantian.cloud.dbcapp.service.TConfigEndtimeService;
import com.nantian.cloud.dbcapp.service.TConfigureEnergykindsService;
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;

@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/configureEnergykindsInfo")
public class TConfigureEnergykindsController {
    @Autowired
    private  TConfigureEnergykindsService tConfigureEnergykindsService;

    @Autowired
    private  ConfigureProcessService configureProcessService;

    @Autowired
    private TConfigEndtimeService tConfigEndtimeService;

    @Autowired
    private ConfigureDataCollectNewService configureDataCollectNewService;


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

    @GetMapping("/getKindsList")
    @SysLog("获取采集数据项配置")
    @ApiOperation(value = "获取采集数据项配置", notes = "获取采集数据项配置")
    @ApiImplicitParam(name = "epc", value = "企业信用编码", required = true, dataType = "string", paramType = "path")
    public R getKindsList(String epc){
        if (epc == null) {
            return R.failed("企业信用编码为空，查询失败！");
        }
        return R.ok(tConfigureEnergykindsService.getKindsList(epc));
    }

    @GetMapping("/getNewKindsList")
    @SysLog("获取采集数据项配置")
    @ApiOperation(value = "获取采集数据项配置", notes = "获取采集数据项配置")
    @ApiImplicitParam(name = "epc", value = "企业信用编码", required = true, dataType = "string", paramType = "path")
    public R getNewKindsList(String epc){
        if (epc == null) {
            return R.failed("企业信用编码为空，查询失败！");
        }
        return R.ok(tConfigureEnergykindsService.getNewKindsList(epc));
    }

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

    @GetMapping("/getCollectList")
    @SysLog("根据条件获取工序单元能源种类")
    @ApiOperation(value = "获取企业工序单元", notes = "获取企业工序单元")
    @ApiImplicitParam(name = "epc", value = "企业信用编码", required = true, dataType = "string", paramType = "path")
    public R getEnergyKindsInfoCollectList(String epc,String baseCode){
        if (epc == null) {
            return R.failed("企业信用编码为空，查询失败！");
        }
        if (baseCode == null) {
            return R.failed("工序单元为空，查询失败！");
        }
        return R.ok(tConfigureEnergykindsService.getCollectList(epc,baseCode));
    }

    @GetMapping("/getNoCollectList")
    @SysLog("获取无采集配置项的能源种类")
    @ApiOperation(value = "获取企业工序单元", notes = "获取企业工序单元")
    @ApiImplicitParam(name = "epc", value = "企业信用编码", required = true, dataType = "string", paramType = "path")
    public R getNoCollectList(String epc,String baseCode){
        if (epc == null) {
            return R.failed("企业信用编码为空，查询失败！");
        }
        if (baseCode == null) {
            return R.failed("工序单元为空，查询失败！");
        }
        return R.ok(tConfigureEnergykindsService.getNoCollectList(epc,baseCode));
    }


    @DeleteMapping("/{collectId}")
    @SysLog("根据id删除企业用能种类")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
    public R removeById(@PathVariable Integer collectId){
        return R.ok(tConfigureEnergykindsService.removeById(collectId));
    }


    @ApiOperation(value = "修改采集项", notes = "修改采集项")
    @SysLog("根据id更新企业用能种类")
    @PutMapping("/update")
    public R updateById(@RequestBody TConfigureEnergykinds tConfigureEnergykinds){
         return R.ok(tConfigureEnergykindsService.updateById(tConfigureEnergykinds));
    }

    @PostMapping("/addCollect")
    @SysLog("新增采集计算")
    @ApiOperation(value = "新增采集计算", notes = "新增采集计算")
    @ApiImplicitParam(name = "tConfigureEnergykinds", value = "新增采集计算", required = true, dataType = "json", paramType = "path")
    public R save(@RequestBody String list) {
        JSONObject json = JSONObject.parseObject(list);
        String data = json.getString("data");
        List<TConfigureEnergykinds> datas = (List<TConfigureEnergykinds>) JSONArray.parseArray(data, TConfigureEnergykinds.class);
        TConfigureEnergykinds t = new TConfigureEnergykinds();
        if(datas != null && datas.size() >= 1){
            return R.ok(tConfigureEnergykindsService.saveOrUpdateBatch(datas));
        }else {
            return R.failed("数据为空！");
        }
    }

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

    @PostMapping("/addProcess")
    @SysLog("新增工序")
    @ApiOperation(value = "新增工序", notes = "新增工序")
    @ApiImplicitParam(name = "configureProcess", value = "新增工序", required = true, dataType = "json", paramType = "path")
    public R save(@RequestBody ConfigureProcess configureProcess) {
        ConfigureProcess t = new ConfigureProcess();
        t.setProcessCode(configureProcess.getProcessCode());
        if(configureProcessService.getOne(Wrappers.query(configureProcess))!=null){
            return R.failed("记录已经存在不能重复添加！");
        }
        Boolean state=configureProcessService.save(configureProcess);
        return state ? R.ok() : R.failed();
    }
    @PostMapping("/addConfig")
    @SysLog("新增新老数据截止时间")
    @ApiOperation(value = "新增新老数据截止时间", notes = "新增新老数据截止时间")
    @ApiImplicitParam(name = "tConfigEndtime", value = "新增新老数据截止时间", required = true, dataType = "json", paramType = "path")
    public R saveConfig(@RequestBody TConfigEndtime tConfigEndtime) {
        TConfigEndtime t = new TConfigEndtime();
        t.setEnterpriseCode(tConfigEndtime.getEnterpriseCode());
        if(tConfigEndtimeService.getOne(Wrappers.query(t))!=null){
            return R.failed("记录已经存在不能重复添加！");
        }
        Boolean state=tConfigEndtimeService.save(tConfigEndtime);
        return state ? R.ok() : R.failed();
    }

    @ApiOperation(value = "修改截止时间", notes = "修改截止时间")
    @SysLog("修改截止时间")
    @PutMapping("/updateEndTime")
    public R updateEndTime(@RequestBody TConfigEndtime tConfigEndtime){
        return R.ok(tConfigEndtimeService.updateEndTime(tConfigEndtime));
    }

    @ApiOperation(value = "修改映射编码", notes = "修改映射编码")
    @SysLog("修改映射编码")
    @PutMapping("/updateDataCodeOld")
    public R updateBaseCode(@RequestBody ConfigureDataCollectNew configureDataCollectNew){
        return R.ok(tConfigureEnergykindsService.updateDataCodeOld(configureDataCollectNew));
    }

    @ApiOperation(value = "批量修改映射编码", notes = "批量修改映射编码")
    @SysLog("批量修改映射编码")
    @PutMapping("/updateDataCodeOldList")
    public R updateBaseCodeList(@RequestBody String list){
        JSONObject json = JSONObject.parseObject(list);
        String data = json.getString("data");
        List<ConfigureDataCollectNew> configureData = (List<ConfigureDataCollectNew>) JSONArray.parseArray(data, ConfigureDataCollectNew.class);
        if(configureData != null && configureData.size() >= 1){
            return R.ok(configureDataCollectNewService.saveOrUpdateBatch(configureData));
        }else {
            return R.failed("数据为空！");
        }
    }

    @PostMapping("/delNewKindsList")
    @SysLog("批量删除企业能源消费情况")
    @ApiOperation(value = "批量删除企业能源消费情况", notes = "批量删除企业能源消费情况")
    public R delNewKindsList(@RequestBody String list){
        JSONObject json = JSONObject.parseObject(list);
        String data = json.getString("data");
        List<TConfigureEnergykinds> delData = (List<TConfigureEnergykinds>) JSONArray.parseArray(data, TConfigureEnergykinds.class);
        Boolean status = false;
        if(delData != null && delData.size() >= 1){
            for(int i=0;i<delData.size();i++){
               status = tConfigureEnergykindsService.removeById(delData.get(i).getId());
            }
            return status ? R.ok() : R.failed();
        }else {
            return R.failed("数据为空！");
        }
    }
}
