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.TSupervisionUnitEnergyComprehensive;
import com.nantian.cloud.jljc.service.TSupervisionUnitEnergyComprehensiveService;
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.web.bind.annotation.*;

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


/**
 * 节能监察单位产品综合能耗
 * @since 2020-11-20
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tSupervisionUnitEnergyComprehensive")
@Api(value = "tSupervisionUnitEnergyComprehensive", tags = "节能监察单位产品综合能耗")
public class TSupervisionUnitEnergyComprehensiveController {

    private final TSupervisionUnitEnergyComprehensiveService tSupervisionUnitEnergyComprehensiveService;

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

    /**
     * 根据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(tSupervisionUnitEnergyComprehensiveService.getById(id));
    }

    /**
     * 保存/修改节能监察单位产品综合能耗
     */
    @PostMapping("saveOrUpdate")
    @SysLog("保存/修改节能监察单位产品综合能耗")
    @ApiOperation(value = "保存/修改节能监察单位产品综合能耗", notes = "保存/修改节能监察单位产品综合能耗")
    @ApiImplicitParam(name = "TSupervisionUnitEnergyComprehensive", value = "节能监察单位产品综合能耗对象", required = true, dataType = "json", paramType = "path")
    public R saveOrUpdate(@RequestBody TSupervisionUnitEnergyComprehensive tSupervisionUnitEnergyComprehensive) {
        ScUser u = SecurityUtils.getUser();
        tSupervisionUnitEnergyComprehensive.setUserid(u.getId());
        tSupervisionUnitEnergyComprehensive.setUpdateTime(new Date());
        return R.ok(tSupervisionUnitEnergyComprehensiveService.saveOrUpdate(tSupervisionUnitEnergyComprehensive));
    }

    /**
     * 批量保存/修改节能监察单位产品综合能耗
     */
    @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 para = json.getString("data");
        List<TSupervisionUnitEnergyComprehensive> tSupervisionUnitEnergyComprehensiveList = (List<TSupervisionUnitEnergyComprehensive>) JSONArray.parseArray(para, TSupervisionUnitEnergyComprehensive.class);
        List<TSupervisionUnitEnergyComprehensive> newList = new ArrayList<>();
        for (TSupervisionUnitEnergyComprehensive tSupervisionUnitEnergyComprehensive : tSupervisionUnitEnergyComprehensiveList) {
            ScUser u = SecurityUtils.getUser();
            tSupervisionUnitEnergyComprehensive.setUserid(u.getId());
            tSupervisionUnitEnergyComprehensive.setUpdateTime(new Date());
            newList.add(tSupervisionUnitEnergyComprehensive);
        }
        return R.ok(tSupervisionUnitEnergyComprehensiveService.saveOrUpdateBatch(newList));
    }
    

    /**
     * 根据id删除节能监察单位产品综合能耗
     */
    @GetMapping("/delete")
    @SysLog("根据id删除节能监察单位产品综合能耗")
    @ApiOperation(value = "根据id删除节能监察单位产品综合能耗", notes = "根据id删除节能监察单位产品综合能耗")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
    public R delete(Integer id) {
        TSupervisionUnitEnergyComprehensive tSupervisionUnitEnergyComprehensive = tSupervisionUnitEnergyComprehensiveService.getById(id);
        if (tSupervisionUnitEnergyComprehensive == null) {
            return R.failed("id不存在，删除节能监察单位产品综合能耗失败！");
        }
        ScUser u = SecurityUtils.getUser();
        tSupervisionUnitEnergyComprehensive.setUpdateTime(new Date());
        tSupervisionUnitEnergyComprehensive.setDelFlag("4");
        tSupervisionUnitEnergyComprehensive.setUserid(u.getId());
        return R.ok(tSupervisionUnitEnergyComprehensiveService.updateById(tSupervisionUnitEnergyComprehensive));
    }
}
