package com.nantian.cloud.jljc.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nantian.cloud.jljc.entity.TDualcontrolIndexresolve;
import com.nantian.cloud.jljc.entity.TDualcontrolParam;
import com.nantian.cloud.jljc.service.TDualcontrolIndexresolveService;
import com.nantian.cloud.jljc.service.TDualcontrolParamService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 能耗双控-双控指标分解计算
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tDualcontrolIndexresolve")
@Api(value="tDualcontrolIndexresolve",tags = "能耗双控-双控指标分解计算表")
public class TDualcontrolIndexresolveController {

    @Autowired
    private TDualcontrolIndexresolveService indexresolveService;
    @Autowired
    private TDualcontrolParamService paramService;

    @GetMapping("/initializeIndex")
    @SysLog("初始化分档计算界面")
    @ApiOperation(value = "初始化分档计算界面", notes = "初始化分档计算界面")
    public R initializeIndexData(String type, String planYear, Long versionId) {
        return R.ok(indexresolveService.initializeIndexData(type,planYear,versionId));
    }

    @PostMapping("/calculateIndex")
    @SysLog("双控指标分解计算")
    @ApiOperation(value = "双控指标分解计算", notes = "双控指标分解计算")
    public R calculateIndex(@RequestBody String str) {
        List<TDualcontrolIndexresolve> list = indexresolveService.calculateIndex(str);
        return R.ok(list);
    }

    @PostMapping("/renewBatch")
    @SysLog("更新双控指标计算数据")
    @ApiOperation(value = "更新计算数据", notes = "更新计算数据")
    public R saveOrUpdateBatch(@RequestBody String str) {
        JSONObject json = JSONObject.parseObject(str);
        List<TDualcontrolIndexresolve> dataList = (List<TDualcontrolIndexresolve>) JSONArray.parseArray(json.getString("data"), TDualcontrolIndexresolve.class);
        TDualcontrolParam version = JSONArray.parseObject(json.getString("version"), TDualcontrolParam.class);
        ScUser u = SecurityUtils.getUser();
        for (TDualcontrolIndexresolve data : dataList) {
            if (!"-1".equals(data.getGearCode())&&!"差额".equals(data.getItemName())&&!"求和验算".equals(data.getItemName())){
                if(data.getItemName().contains("（合计值）")){
                    data.setItemName(data.getItemName().split("（合计值）")[0]);
                }
                data.setDeptId(u.getDeptId());
                if(data.getId()==null){
                    indexresolveService.save(data);
                }else{
                    indexresolveService.updateById(data);
                }
            }
        }
        version.setUpdateTime(new Date());
        version.setUpdateUserid(u.getId());
        paramService.updateById(version); //更新版本数据
        return  R.ok();
    }

}
