package com.nantian.cloud.jljc.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nantian.cloud.jljc.entity.TDualcontrolParam;
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.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 能耗双控-参数设置
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tDualcontrolParam")
@Api(value="tDualcontrolParam",tags = " 能耗双控-参数设置")
public class TDualcontrolParamController {

    @Autowired
    private TDualcontrolParamService paramService;

    @GetMapping("/versionList")
    @SysLog("查询版本信息（双控）")
    @ApiOperation(value = "查询版本信息",notes = "查询版本信息")
    public R versionList(TDualcontrolParam param){
        ScUser u = SecurityUtils.getUser();
        param.setType("0"); //版本
        param.setDelFlag("1");
        param.setDeptId(u.getDeptId()); //查询本部门版本信息
        List<TDualcontrolParam> list = paramService.list(Wrappers.query(param));
        return R.ok(list);
    }

    @PostMapping("/renewVersion")
    @SysLog("更新版本信息（双控）")
    @ApiOperation(value = "更新版本信息", notes = "更新版本信息")
    public R renewVersion(@RequestBody TDualcontrolParam param) {
        ScUser u = SecurityUtils.getUser();
        if(param.getId()==null){
            param.setAddTime(new Date());
            param.setAddUserid(u.getId());
            param.setDeptId(u.getDeptId());
            param.setAddUserName(u.getUserRealName());
        }
        param.setUpdateTime(new Date());
        param.setUpdateUserid(u.getId());
        param.setUpdateUserName(u.getUserRealName());
        boolean b = paramService.saveOrUpdate(param);
        return R.ok();
    }

    @GetMapping("/paramList")
    @SysLog("查询参数（双控）")
    @ApiOperation(value = "查询参数", notes = "查询参数")
    public R getParamList(TDualcontrolParam param){
        ScUser u = SecurityUtils.getUser();
        param.setDelFlag("1");
        param.setDeptId(u.getDeptId());
        //查询档位设置
        param.setType("1"); //档位
        List<TDualcontrolParam> gear = paramService.list(Wrappers.query(param));
        //查询差额值
        param.setType("2"); //差额
        TDualcontrolParam balance = paramService.getOne(Wrappers.query(param));

        //获取历史版本信息
        QueryWrapper<TDualcontrolParam> q = new QueryWrapper<>();
        q.eq("dept_id",u.getDeptId());
        //q.eq("year",param.getYear()); //查询之前设置的档位和差额值，就不带参数年份
        q.eq("del_flag","1");
        q.eq("type","0");
        q.orderByDesc("update_time");
        List<TDualcontrolParam> version = paramService.list(q);
        //当前版本没有数据，需取历史版本的数据
        if(gear==null || gear.size()==0){
            param.setType("1");
            a:for (int i = version.size() - 1; i >= 0; i--) {
                param.setVersionId(version.get(i).getId());
                List<TDualcontrolParam> tmp = paramService.list(Wrappers.query(param));
                if(tmp!=null&&tmp.size()>0){
                    for (TDualcontrolParam tDualcontrolParam : tmp) {
                        tDualcontrolParam.setId(null);
                        tDualcontrolParam.setVersionId(null);
                    }
                    gear = tmp;
                    break a;
                }
            }
        }
        if(balance==null){
            param.setType("2");
            a:for (int i = version.size() - 1; i >= 0; i--) {
                param.setVersionId(version.get(i).getId());
                TDualcontrolParam tmp = paramService.getOne(Wrappers.query(param));
                if(tmp!=null){
                    balance = tmp;
                    balance.setId(null);
                    balance.setVersionId(null);
                    break a;
                }
            }
        }

        Map<String,Object> map = new HashMap<>();
        map.put("gear",gear);
        map.put("balance",balance);
        return  R.ok(map);
    }

    @Transactional
    @PostMapping("/renewParamBatch")
    @SysLog("更新参数（双控）")
    @ApiOperation(value = "更新参数", notes = "更新参数")
    public R saveOrUpdateParamBatch(@RequestBody String str) {
        paramService.saveOrUpdateParamBatch(str);
        return  R.ok();
    }

    @GetMapping("/initializePre")
    @SysLog("查询上个五年计划最后一年数据（双控）")
    @ApiOperation(value = "查询上个五年计划最后一年数据", notes = "查询上个五年计划最后一年数据")
    public R initializePreData(String planYear,Long versionId){
        return R.ok(paramService.initializePreData(planYear,versionId));
    }
}
