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.TSupervisionUnitItem;
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.web.bind.annotation.*;

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


/**
 * 节能监察单位产品项目
 * @since 2020-10-29
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tSupervisionUnitItem")
@Api(value = "tSupervisionUnitItem", tags = "节能监察单位产品项目")
public class TSupervisionUnitItemController {

    private final TSupervisionUnitItemService tSupervisionUnitItemService;

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

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

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

    /**
     * 保存/修改节能监察单位产品项目
     */
    @SysLog("保存/修改节能监察单位产品项目")
    @PostMapping("saveOrUpdate")
    @ApiOperation(value = "保存/修改节能监察单位产品项目", notes = "保存/修改节能监察单位产品项目")
    @ApiImplicitParam(name = "TSupervisionUnitItem", value = "节能监察单位产品项目对象", required = true, dataType = "json", paramType = "path")
    public R saveOrUpdate(@RequestBody TSupervisionUnitItem tSupervisionUnitItem) {
        ScUser u = SecurityUtils.getUser();
        tSupervisionUnitItem.setUserid(u.getId());
        tSupervisionUnitItem.setUpdateTime(new Date());
        if (tSupervisionUnitItem.getId() != null) {
            tSupervisionUnitItem.setDelFlag("1");
            tSupervisionUnitItem.setDeptId(u.getDeptId());
        }
        return R.ok(tSupervisionUnitItemService.saveOrUpdate(tSupervisionUnitItem));
    }

    /**
     * 批量保存/修改节能监察单位产品项目
     */
    @SysLog("批量保存/修改节能监察单位产品项目")
    @PostMapping("saveOrUpdateBatch")
    @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<TSupervisionUnitItem> tSupervisionUnitItemList = (List<TSupervisionUnitItem>) JSONArray.parseArray(para, TSupervisionUnitItem.class);
        List<TSupervisionUnitItem> newList = new ArrayList<>();
        for (TSupervisionUnitItem tSupervisionUnitItem : tSupervisionUnitItemList) {
            ScUser u = SecurityUtils.getUser();
            tSupervisionUnitItem.setUserid(u.getId());
            tSupervisionUnitItem.setUpdateTime(new Date());
            if (tSupervisionUnitItem.getId() != null) {
                tSupervisionUnitItem.setDelFlag("1");
                tSupervisionUnitItem.setDeptId(u.getDeptId());
            }
            newList.add(tSupervisionUnitItem);
        }
        return R.ok(tSupervisionUnitItemService.saveOrUpdateBatch(newList));
    }

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