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.TSupervisionUnitEnergy;
import com.nantian.cloud.jljc.entity.TSupervisionUnitEnergyType;
import com.nantian.cloud.jljc.service.TSupervisionUnitEnergyService;
import com.nantian.cloud.jljc.service.TSupervisionUnitEnergyTypeService;
import com.nantian.cloud.jljc.vo.Query;
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-02
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tSupervisionUnitEnergyType")
@Api(value = "tSupervisionUnitEnergyType", tags = "节能监察单位产品种类")
public class TSupervisionUnitEnergyTypeController {

    private final TSupervisionUnitEnergyTypeService tSupervisionUnitEnergyTypeService;

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

    /**
     * 获取节能监察单位产品种类树形结构
     */
    @GetMapping("/tree")
    @SysLog("获取节能监察单位产品种类树形结构")
    @ApiOperation(value = "获取节能监察单位产品种类树形结构", notes = "获取节能监察单位产品种类树形结构")
    @ApiImplicitParam(name = "Query", value = "查询条件（名称name）", required = false, dataType = "json", paramType = "path")
    public R tree(Query query) {
        return R.ok(tSupervisionUnitEnergyTypeService.getTree(query));
    }

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

    /**
     * 批量保存/修改节能监察单位产品
     */
    @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<TSupervisionUnitEnergyType> typeList = (List<TSupervisionUnitEnergyType>) JSONArray.parseArray(para, TSupervisionUnitEnergyType.class);
        List<TSupervisionUnitEnergyType> newList = new ArrayList<>();
        for (TSupervisionUnitEnergyType type : typeList) {
            ScUser u = SecurityUtils.getUser();
            type.setUserid(u.getId());
            type.setUpdateTime(new Date());
            if (type.getId() == null) { //新增
                type.setDelFlag("1");
                type.setDeptId(u.getDeptId());
            }
            newList.add(type);
        }
        return R.ok(tSupervisionUnitEnergyTypeService.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) {
        TSupervisionUnitEnergyType type = tSupervisionUnitEnergyTypeService.getById(id);
        if (type == null) {
            return R.failed("id不存在，删除节能监察单位产品种类！");
        }
        ScUser u = SecurityUtils.getUser();
        type.setUpdateTime(new Date());
        type.setDelFlag("4");
        type.setUserid(u.getId());
        return R.ok(tSupervisionUnitEnergyTypeService.updateById(type));
    }
}
