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.TSupervisionEnergyTypes;
import com.nantian.cloud.jljc.service.TSupervisionEnergyTypesService;
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-10-29
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tSupervisionEnergyTypes")
@Api(value = "tSupervisionEnergyTypes", tags = "节能监察用能种类")
public class TSupervisionEnergyTypesController {

    private final TSupervisionEnergyTypesService tSupervisionEnergyTypesService;

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

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

    /**
     * 保存/修改节能监察用能种类
     */
    @PostMapping("saveOrUpdate")
    @SysLog("保存/修改节能监察用能种类")
    @ApiOperation(value = "保存/修改节能监察用能种类", notes = "保存/修改节能监察用能种类")
    @ApiImplicitParam(name = "TSupervisionEnergyTypes", value = "节能监察用能种类对象", required = true, dataType = "json", paramType = "path")
    public R saveOrUpdate(@RequestBody TSupervisionEnergyTypes tSupervisionEnergyTypes) {
        ScUser u = SecurityUtils.getUser();
        tSupervisionEnergyTypes.setUserid(u.getId());
        tSupervisionEnergyTypes.setUpdateTime(new Date());
        hasOld( tSupervisionEnergyTypes, u);
        return R.ok(tSupervisionEnergyTypesService.saveOrUpdate(tSupervisionEnergyTypes));
    }

    /**
     * 批量保存/修改节能监察用能种类
     */
    @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<TSupervisionEnergyTypes> tSupervisionEnergyTypesList = (List<TSupervisionEnergyTypes>) JSONArray.parseArray(para, TSupervisionEnergyTypes.class);
        List<TSupervisionEnergyTypes> newList = new ArrayList<>();
        for (TSupervisionEnergyTypes tSupervisionEnergyTypes : tSupervisionEnergyTypesList) {
            ScUser u = SecurityUtils.getUser();
            tSupervisionEnergyTypes.setUserid(u.getId());
            tSupervisionEnergyTypes.setUpdateTime(new Date());
            hasOld( tSupervisionEnergyTypes, u);
            newList.add(tSupervisionEnergyTypes);
        }
        return R.ok(tSupervisionEnergyTypesService.saveOrUpdateBatch(newList));
    }

    private void hasOld(TSupervisionEnergyTypes tSupervisionEnergyTypes, ScUser u) {
        TSupervisionEnergyTypes old = new TSupervisionEnergyTypes();
        //old.setEpc(tSupervisionEnergyTypes.getEpc());
        old.setMonitorId(tSupervisionEnergyTypes.getMonitorId());
        TSupervisionEnergyTypes o = tSupervisionEnergyTypesService.getOne(Wrappers.query(old));
        if (o != null) { //有历史数据，属于修改
            tSupervisionEnergyTypes.setId(o.getId());
        } else {
            tSupervisionEnergyTypes.setDelFlag("1");
            tSupervisionEnergyTypes.setDeptId(u.getDeptId());
        }
    }

    /**
     * 根据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) {
        TSupervisionEnergyTypes tSupervisionEnergyTypes = tSupervisionEnergyTypesService.getById(id);
        if (tSupervisionEnergyTypes == null) {
            return R.failed("id不存在，删除节能监察用能种类失败！");
        }
        ScUser u = SecurityUtils.getUser();
        tSupervisionEnergyTypes.setUpdateTime(new Date());
        tSupervisionEnergyTypes.setDelFlag("4");
        tSupervisionEnergyTypes.setUserid(u.getId());
        return R.ok(tSupervisionEnergyTypesService.updateById(tSupervisionEnergyTypes));
    }
}
