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.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.TSupervisionEnergy;
import com.nantian.cloud.jljc.service.TSupervisionEnergyService;
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.ApiImplicitParams;
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-10-27
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tSupervisionEnergy")
@Api(value = "tSupervisionEnergy", tags = "单位产品能耗监测表")
public class TSupervisionEnergyController {
    private final TSupervisionEnergyService tSupervisionEnergyService;


    /**
     * 分页查询
     */
    @GetMapping("/page")
    @SysLog("分页获取单位产品能耗监测表")
    @ApiOperation(value = "分页获取单位产品能耗监测表", notes = "分页获取单位产品能耗监测表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "TSupervisionEnergy", value = "单位产品能耗监测表对象", required = false, dataType = "json", paramType = "path")
    })
    public R gettSupervisionEnergyPage(Page page, TSupervisionEnergy tSupervisionEnergy) {
        tSupervisionEnergy.setDelFlag("1"); //查询未删除数据
        return R.ok(tSupervisionEnergyService.page(page, Wrappers.query(tSupervisionEnergy)));
    }


    /**
     * 获取所有单位产品能耗监测表
     */
    @GetMapping("/list")
    @SysLog("获取所有单位产品能耗监测表")
    @ApiOperation(value = "获取所有单位产品能耗监测表", notes = "获取所有单位产品能耗监测表")
    @ApiImplicitParam(name = "TSupervisionEnergy", value = "单位产品能耗监测表对象", required = false, dataType = "json", paramType = "path")
    public R gettSupervisionEnergyPage(TSupervisionEnergy tSupervisionEnergy) {
        tSupervisionEnergy.setDelFlag("1"); //查询未删除数据
        return R.ok(tSupervisionEnergyService.list(Wrappers.query(tSupervisionEnergy)));
    }

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

    /**
     * 新增/修改单位产品能耗监测表
     */
    @PostMapping("/saveOrUp")
    @SysLog("新增/修改单位产品能耗监测表")
    @ApiOperation(value = "新增/修改单位产品能耗监测表", notes = "新增/修改单位产品能耗监测表")
    @ApiImplicitParam(name = "TSupervisionEnergy", value = "单位产品能耗监测表对象", required = true, dataType = "json", paramType = "path")
    public R saveOrUp(@RequestBody TSupervisionEnergy tSupervisionEnergy) {
        ScUser u = SecurityUtils.getUser();
        tSupervisionEnergy.setUserid(u.getId());
        tSupervisionEnergy.setUpdateTime(new Date());
        if (tSupervisionEnergy.getId() == null) { //新增
            tSupervisionEnergy.setDeptId(u.getDeptId());
            tSupervisionEnergy.setDelFlag("1");
        }
        return R.ok(tSupervisionEnergyService.saveOrUpdate(tSupervisionEnergy));
    }

    /**
     * 批量新增/修改单位产品能耗监测表
     */
    @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 energys = json.getString("data");
        List<TSupervisionEnergy> tSupervisionEnergyList = (List<TSupervisionEnergy>) JSONArray.parseArray(energys, TSupervisionEnergy.class);
        List<TSupervisionEnergy> newList = new ArrayList<>();
        for (TSupervisionEnergy tSupervisionEnergy : tSupervisionEnergyList) {
            if (tSupervisionEnergy.getYear() == null) {
                return R.failed("年度不能为空，请检查！");
            }
            if (tSupervisionEnergy.getEnterpriseCode() == null) {
                return R.failed("企业社会信用编码不能为空，请检查！");
            }
            ScUser u = SecurityUtils.getUser();
            tSupervisionEnergy.setUserid(u.getId());
            tSupervisionEnergy.setUpdateTime(new Date());
            if (tSupervisionEnergy.getId() == null) { //新增
                tSupervisionEnergy.setDeptId(u.getDeptId());
                tSupervisionEnergy.setDelFlag("1");
            }
            newList.add(tSupervisionEnergy);
        }
        return R.ok(tSupervisionEnergyService.saveOrUpdateBatch(newList));
    }

    /**
     * 删除单位产品能耗监测表
     */
    @DeleteMapping("/delete")
    @SysLog("删除单位产品能耗监测表")
    @ApiOperation(value = "删除单位产品能耗监测表", notes = "删除单位产品能耗监测表")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
    public R removeById(Integer id) {
        TSupervisionEnergy tSupervisionEnergy = tSupervisionEnergyService.getById(id);
        ScUser u = SecurityUtils.getUser();
        tSupervisionEnergy.setUserid(u.getId());
        tSupervisionEnergy.setDelFlag("4");
        tSupervisionEnergy.setUpdateTime(new Date());
        return R.ok(tSupervisionEnergyService.updateById(tSupervisionEnergy));
    }

}
