package com.nantian.cloud.jljc.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nantian.cloud.jljc.entity.TSuperviseTask;
import com.nantian.cloud.jljc.entity.TSupervisionComprehensiveEnergy;
import com.nantian.cloud.jljc.entity.TSupervisionUnitEnergyComprehensive;
import com.nantian.cloud.jljc.service.TSuperviseTaskService;
import com.nantian.cloud.jljc.service.TSupervisionComprehensiveEnergyService;
import com.nantian.cloud.jljc.service.TSupervisionUnitEnergyService;
import com.nantian.cloud.jljc.service.TSupervisionUnitItemService;
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.ApiImplicitParam;
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.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 节能监察综合能耗
 * @since 2020-11-13
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/TSupervisionComprehensiveEnergy")
@Api(value = "TSupervisionComprehensiveEnergy", tags = "节能监察综合能耗")
public class TSupervisionComprehensiveEnergyController {

    @Autowired
    private TSupervisionComprehensiveEnergyService tSupervisionComprehensiveEnergyService;
    @Autowired
    private TSuperviseTaskService tSuperviseTaskService;
    @Autowired
    private TSupervisionUnitEnergyService tSupervisionUnitEnergyService;
    @Autowired
    private TSupervisionUnitItemService tSupervisionUnitItemService;

    /**
     * 查询全部节能监察综合能耗
     */
    @GetMapping("/list")
    @SysLog("查询全部节能监察综合能耗")
    @ApiOperation(value = "查询全部节能监察综合能耗", notes = "查询全部节能监察综合能耗")
    @ApiImplicitParam(name = "TSupervisionComprehensiveEnergy", value = "节能监察综合能耗对象", required = false, dataType = "json", paramType = "path")
    public R selectAll(TSupervisionComprehensiveEnergy tSupervisionComprehensiveEnergy) {
        tSupervisionComprehensiveEnergy.setDelFlag("1"); //筛选未删除项
        return R.ok(tSupervisionComprehensiveEnergyService.list(Wrappers.query(tSupervisionComprehensiveEnergy)));
    }

    /**
     * 根据id获取节能监察综合能耗
     */
    @GetMapping("/getById")
    @SysLog("根据id获取节能监察综合能耗")
    @ApiOperation(value = "根据id获取节能监察综合能耗", notes = "根据id获取节能监察综合能耗")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
    public R getById(Integer id) {
        return R.ok(tSupervisionComprehensiveEnergyService.getById(id));
    }


    @GetMapping("/getByUnitEnergyId")
    @SysLog("根据id获取节能监察单位产品项目")
    @ApiOperation(value = "根据id获取节能监察单位产品项目", notes = "根据id获取节能监察单位产品项目")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
    public R getByUnitEnergyId(TSupervisionComprehensiveEnergy tSupervisionComprehensiveEnergy) {
        return R.ok(tSupervisionComprehensiveEnergyService.getByUnitEnergyId(tSupervisionComprehensiveEnergy));
    }

    /**
     * 批量保存/修改节能监察综合能耗
     */
    @Transactional
    @PostMapping("saveOrUpdateBatch")
    @SysLog("批量保存/修改节能监察综合能耗")
    @ApiOperation(value = "批量保存/修改节能监察综合能耗", notes = "批量保存/修改节能监察综合能耗")
    @ApiImplicitParam(name = "data", value = "节能监察综合能耗对象", required = true, dataType = "json", paramType = "path")
    public R saveOrUpdateBatch(@RequestBody String s) {
        JSONObject json = JSONObject.parseObject(s);

        String task = json.getString("task");
        TSuperviseTask tSuperviseTask = (TSuperviseTask) JSONObject.parseObject(task, TSuperviseTask.class);
        if(tSuperviseTask.getId()==null){
            return R.failed("监察计划id不能为空！");
        }
        if(tSuperviseTask.getTce()==null){
            return R.failed("综合能耗tce不能为空！");
        }

        TSuperviseTask q = new TSuperviseTask();
        q.setDelFlag("1");
        q.setId(tSuperviseTask.getId());
        TSuperviseTask old = tSuperviseTaskService.getOne(Wrappers.query(q));
        if(old==null){
            return R.failed("审查计划为空，保存节能监察综合能耗失败！");
        }
        ScUser u = SecurityUtils.getUser();
        old.setUserid(u.getId());
        old.setUpdateTime(new Date());
        old.setTce(tSuperviseTask.getTce());
        tSuperviseTaskService.updateById(old);

        String para = json.getString("data");
        TSupervisionComprehensiveEnergy t=new TSupervisionComprehensiveEnergy();
        t.setMonitorId(tSuperviseTask.getMonitorId());
        t.setYear(tSuperviseTask.getYear());
        t.setDelFlag("1");
        List<TSupervisionComprehensiveEnergy> lists=tSupervisionComprehensiveEnergyService.list(Wrappers.query(t));
        if(lists.size()>1){
            for(int i=0;i<lists.size();i++){
                tSupervisionComprehensiveEnergyService.removeById(lists.get(i).getId());
            }
        }
        List<TSupervisionComprehensiveEnergy> comprehensiveEnergyList = (List<TSupervisionComprehensiveEnergy>) JSONArray.parseArray(para, TSupervisionComprehensiveEnergy.class);
        List<TSupervisionComprehensiveEnergy> newList = new ArrayList<>();
        for (TSupervisionComprehensiveEnergy comprehensiveEnergy : comprehensiveEnergyList) {
            comprehensiveEnergy.setUserid(u.getId());
            comprehensiveEnergy.setUpdateTime(new Date());
            newList.add(comprehensiveEnergy);
        }
        tSupervisionComprehensiveEnergyService.saveOrUpdateBatch(newList);

        /*String unitEnergy = json.getString("unitEnergy");
        TSupervisionUnitEnergy tSupervisionUnitEnergy = (TSupervisionUnitEnergy) JSONObject.parseObject(unitEnergy, TSupervisionUnitEnergy.class);
        tSupervisionUnitEnergy.setDelFlag("1");
        tSupervisionUnitEnergy.setUserid(u.getId());
        tSupervisionUnitEnergy.setDeptId(u.getDeptId());
        tSupervisionUnitEnergy.setUpdateTime(new Date());
        boolean b = tSupervisionUnitEnergyService.saveOrUpdate(tSupervisionUnitEnergy);

        String unitItem = json.getString("unitItem");
        List<TSupervisionUnitItem> tSupervisionUnitItemList = (List<TSupervisionUnitItem>) JSONObject.parseArray(unitItem, TSupervisionUnitItem.class);
        List<TSupervisionUnitItem> newItemList = new ArrayList<>();
        for (TSupervisionUnitItem item : tSupervisionUnitItemList) {
            item.setDelFlag("1");
            item.setUserid(u.getId());
            item.setDeptId(u.getDeptId());
            item.setUpdateTime(new Date());
            if(b){
                item.setUnitEnergyId(tSupervisionUnitEnergy.getId());
            }
            newItemList.add(item);
        }
        tSupervisionUnitItemService.saveOrUpdateBatch(newItemList);*/

        return R.ok();
    }

}
