package com.nantian.cloud.dbcapp.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nantian.cloud.dbcapp.entity.TEnergyIndexInfo;
import com.nantian.cloud.dbcapp.entity.TStandard;
import com.nantian.cloud.dbcapp.entity.TUnitAssist;
import com.nantian.cloud.dbcapp.service.TEnergyIndexInfoService;
import com.nantian.cloud.dbcapp.service.TStandardService;
import com.nantian.cloud.dbcapp.service.TUnitAssistService;
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("configure")
public class TUnitAssistController {
    @Autowired
    private TUnitAssistService tUnitAssistService;

    @Autowired
    private TEnergyIndexInfoService tEnergyIndexInfoService;

    @Autowired
    private TStandardService tStandardService;

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

    @PostMapping("addAssist")
    @SysLog("新增单位产品采集计算")
    @ApiOperation(value = "新增单位产品采集计算", notes = "新增单位产品采集计算")
    @ApiImplicitParam(name = "TUnitAssist", value = "新增单位产品采集计算", required = true, dataType = "json", paramType = "path")
    public R save(@RequestBody TUnitAssist tUnitAssist) {
        TUnitAssist t = new TUnitAssist();
        if(tUnitAssistService.getOne(Wrappers.query(t))!=null){
            return R.failed("记录已经存在不能重复添加！");
        }
        return R.ok(tUnitAssistService.save(tUnitAssist));
    }

    @GetMapping("getEnergyIndexInfoList")
    @SysLog("获取企业单位产品能耗计算参照表")
    @ApiOperation(value = "获取企业单位产品能耗计算参照表", notes = "获取企业单位产品能耗计算参照表")
    @ApiImplicitParam(name = "epc", value = "企业信用编码", required = true, dataType = "string", paramType = "path")
    public R getEnergyIndexInfo(String epc){
        if (epc == null) {
            return R.failed("企业信用编码为空，查询失败！");
        }
        return R.ok(tEnergyIndexInfoService.getEnergyIndexInfoList(epc));
    }

    @ApiOperation(value = "修改企业单位产品能耗计算参照表", notes = "修改企业单位产品能耗计算参照表")
    @PutMapping("/updateEnergyIndexInfo")
    @SysLog("修改企业单位产品能耗计算参照表")
    public R updateEnergyIndexInfo(@RequestBody TEnergyIndexInfo tEnergyIndexInfo){
        return R.ok(tEnergyIndexInfoService.updateById(tEnergyIndexInfo));
    }

    @PostMapping("addEnergyIndexInfo")
    @SysLog("新增企业单位产品能耗计算参照表")
    @ApiOperation(value = "新增企业单位产品能耗计算参照表", notes = "新增企业单位产品能耗计算参照表")
    @ApiImplicitParam(name = "TEnergyIndexInfo", value = "新增企业单位产品能耗计算参照表", required = true, dataType = "json", paramType = "path")
    public R saveEnergy(@RequestBody TEnergyIndexInfo tEnergyIndexInfo) {
        TEnergyIndexInfo t = new TEnergyIndexInfo();
        if(tEnergyIndexInfoService.getOne(Wrappers.query(tEnergyIndexInfo))!=null){
            return R.failed("记录已经存在不能重复添加！");
        }
        return R.ok(tEnergyIndexInfoService.save(tEnergyIndexInfo));
    }

    @PostMapping("addStandard")
    @SysLog("配置行业标准表")
    @ApiOperation(value = "配置行业标准表", notes = "配置行业标准表")
    @ApiImplicitParam(name = "TStandard", value = "配置行业标准表", required = true, dataType = "json", paramType = "path")
    public R saveStandard(@RequestBody TStandard tStandard) {
        TStandard t = new TStandard();
        t.setCode(tStandard.getCode());
        t.setQuota_name(tStandard.getQuota_name());
        List<TStandard> list = tStandardService.list(Wrappers.query(t));
        if (list.size() > 1) {
            return R.failed("记录已经存在不能重复添加！");
        }else if(list.size() == 1){
            TStandard standard = list.get(0);
            if (standard.getType().equals(tStandard.getType())) {
                return R.failed("记录已经存在不能重复添加！");
            }
        }
        float n=Float.valueOf(tStandard.getAdvanced_value());
        float n1=Float.valueOf(tStandard.getLimit_value());
        float m= (float) (n-n*0.2);//先进值(最小值)
        float m1= (float) (n1+n1*0.2);//限定值(最大值)
        tStandard.setMax_value(String.valueOf(m1));
        tStandard.setMin_value(String.valueOf(m));
        return R.ok(tStandardService.save(tStandard));
    }
    /*public R saveStandard(@RequestBody TStandard tStandard) {
        TStandard t = new TStandard();
        t.setQuota_name(tStandard.getQuota_name());
        List<TStandard> list = tStandardService.list(Wrappers.query(t));
        for(int i=0;i<list.size();i++){
            if(tStandard.getType()==list.get(i).getType()){
                return R.failed("相同标准不能重复添加！");
            }
        }
        if(list.size()>1){
            return R.failed("标准已经存在不能重复添加！");
        }else{
            if(tStandardService.getOne(Wrappers.query(t))!=null){
                return R.failed("标准已经存在不能重复添加！");
            }
        }
        return R.ok(tStandardService.save(tStandard));
    }*/

    @ApiOperation(value = "修改行业标准表", notes = "修改行业标准表")
    @SysLog("修改行业标准表")
    @PutMapping("/updateStandard")
    public R updateById(@RequestBody TStandard tStandard){
        return R.ok(tStandardService.updateById(tStandard));
    }

    @ApiOperation(value = "修改行业标准表", notes = "修改行业标准表")
    @SysLog("删除行业标准表")
    @DeleteMapping("/delStandard/{standardId}")
    public R delById(@PathVariable String standardId){
        return R.ok(tStandardService.removeById(standardId));
    }


    @GetMapping("getStandard")
    @SysLog("获取配置行业标准表")
    @ApiOperation(value = "配置行业标准表", notes = "配置行业标准表")
    @ApiImplicitParam(name = "TStandard", value = "配置行业标准表", required = true, dataType = "json", paramType = "path")
    public R getStandard(String code,String name,String quotaName) {
        return R.ok(tStandardService.getStandardList(code,name,quotaName));
    }
}
