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.TDualcontrolIndex;
import com.nantian.cloud.jljc.service.TDualcontrolIndexService;
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.*;

/**
 * 能耗双控-双控指标
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tDualcontrolIndex")
@Api(value="tDualcontrolIndex",tags = " 能耗双控-双控指标表")
public class TDualcontrolIndexController {

    @Autowired
    private TDualcontrolIndexService indexService;

    @GetMapping("/treeData")
    @SysLog("获取双控指标树形数据")
    @ApiOperation(value = "获取双控指标树形数据", notes = "获取双控指标树形数据")
    public R getIndexTree(String type, String plan, String year){
        return R.ok(indexService.getIndexTree(type,plan,year));
    }

    @GetMapping("/indexByVersion")
    @SysLog("根据版本和年度获取双控指标数据")
    @ApiOperation(value = "根据版本和年度获取双控指标数据", notes = "根据版本和年度获取双控指标数据")
    public R getIndexByVersion(long versionId, String year){
        return R.ok(indexService.getIndexByVersion(versionId,year));
    }

    @GetMapping("/saveIndex")
    @SysLog("下发双控指标")
    @ApiOperation(value = "下发双控指标", notes = "下发双控指标")
    public R saveIndexByVersion(long versionId, String year){
        return indexService.saveIndexByVersion(versionId,year);
    }


    @GetMapping("/taskList")
    @SysLog("获取双控指标分解任务列表")
    @ApiOperation(value = "获取双控指标分解任务列表", notes = "获取双控指标分解任务列表")
    public R taskList(TDualcontrolIndex index){
        ScUser u = SecurityUtils.getUser();
        index.setDeptId(u.getDeptId());
        index.setDelFlag("1");
        index.setIndexType("1");
        return R.ok(indexService.list(Wrappers.query(index)));
    }

    @PostMapping("/renewTask")
    @SysLog("双控指标分解任务更新")
    @ApiOperation(value = "双控指标分解任务更新", notes = "双控指标分解任务更新")
    public R renewTask(@RequestBody TDualcontrolIndex index){
        ScUser u = SecurityUtils.getUser();
        QueryWrapper<TDualcontrolIndex> q = new QueryWrapper<>();
        q.eq("del_flag","1");
        q.eq("year",index.getYear());
        q.eq("index_type","1"); //目标分解任务
        q.eq("dept_id",u.getDeptId()); //获取该部门创建的分解任务
        TDualcontrolIndex old = indexService.getOne(q);
        if(index.getId()==null && old!=null){ //更新数据id为空，且存在老数据
            return R.failed(index.getYear()+"年任务已存在，不可重复新增！");
        }
        index.setUpdateTime(new Date());
        index.setUserid(u.getId());
        index.setDeptId(u.getDeptId());
        return R.ok(indexService.saveOrUpdate(index));
    }

    @GetMapping("/timeList")
    @SysLog("可下发双控指标的年份")
    @ApiOperation(value = "可下发双控指标的年份", notes = "可下发双控指标的年份")
    public R getTimeList(String planYear) {

        List<String> timeStr = new ArrayList<>();
        String[] tmp = planYear.split("-");
        timeStr.add(planYear);
        for(Integer i=Integer.parseInt(tmp[0]); i<=Integer.parseInt(tmp[1]);i++){
           timeStr.add(i.toString());
        }

        List<Map<String,Object>> times = new ArrayList<>();
        ScUser u = SecurityUtils.getUser();
        TDualcontrolIndex q = new TDualcontrolIndex();
        q.setIndexType("2");
        q.setDelFlag("1");
        q.setDeptId(u.getDeptId()); //只查询本部门数据
        for (String year : timeStr) {
            q.setYear(year);
            List<TDualcontrolIndex> old = indexService.list(Wrappers.query(q));
            Map<String,Object> map = new HashMap();
            map.put("year",year);
            if(old!=null && old.size()!=0){ //双控指标已下达，不可重复下达
                map.put("status",2);
                map.put("type","success");
            }else{ //可下达双控指标
                map.put("status",1);
                map.put("type","primary");
            }
            times.add(map);
        }
        return R.ok(times);
    }
}
