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.TSupervisionNote;
import com.nantian.cloud.jljc.service.TSupervisionNoteService;
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("/tSupervisionNote")
@Api(value = "tSupervisionNote", tags = "节能监察通知书")
public class TSupervisionNoteController {

    private final TSupervisionNoteService tSupervisionNoteService;

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

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

    /**
     * 保存/修改节能监察通知书
     */
    @PostMapping("saveOrUpdate")
    @SysLog("保存/修改节能监察通知书")
    @ApiOperation(value = "保存/修改节能监察通知书", notes = "保存/修改节能监察通知书")
    @ApiImplicitParam(name = "TSupervisionNote", value = "节能监察通知书对象", required = true, dataType = "json", paramType = "path")
    public R saveOrUpdate(@RequestBody TSupervisionNote tSupervisionNote) {
        if(tSupervisionNote.getEpc()==null){
            return R.failed("社会信用编码不能为空，新增节能监察通知书失败！");
        }
        if(tSupervisionNote.getMonitorId()==null){
            return R.failed("监察编号不能为空，新增节能监察通知书失败！");
        }
        ScUser u = SecurityUtils.getUser();
        hasOld(tSupervisionNote, u);
        tSupervisionNote.setUserid(u.getId());
        tSupervisionNote.setUpdateTime(new Date());
        return R.ok(tSupervisionNoteService.saveOrUpdate(tSupervisionNote));
    }

    /**
     * 批量保存/修改节能监察通知书
     */
    @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<TSupervisionNote> tSupervisionNoteList = (List<TSupervisionNote>) JSONArray.parseArray(para, TSupervisionNote.class);
        List<TSupervisionNote> newList = new ArrayList<>();
        for (TSupervisionNote tSupervisionNote : tSupervisionNoteList) {
            ScUser u = SecurityUtils.getUser();
            tSupervisionNote.setUserid(u.getId());
            tSupervisionNote.setUpdateTime(new Date());
            hasOld(tSupervisionNote, u);
            newList.add(tSupervisionNote);
        }
        return R.ok(tSupervisionNoteService.saveOrUpdateBatch(newList));
    }

    private void hasOld(TSupervisionNote tSupervisionNote, ScUser u) {
        TSupervisionNote old = new TSupervisionNote();
        old.setEpc(tSupervisionNote.getEpc());
        old.setMonitorId(tSupervisionNote.getMonitorId());
        TSupervisionNote o = tSupervisionNoteService.getOne(Wrappers.query(old));
        if (o != null) { //有历史数据，属于修改
            tSupervisionNote.setId(o.getId());
        } else {
            tSupervisionNote.setDelFlag("1");
            tSupervisionNote.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) {
        TSupervisionNote tSupervisionNote = tSupervisionNoteService.getById(id);
        if (tSupervisionNote == null) {
            return R.failed("id不存在，删除节能监察通知书失败！");
        }
        ScUser u = SecurityUtils.getUser();
        tSupervisionNote.setUpdateTime(new Date());
        tSupervisionNote.setDelFlag("4");
        tSupervisionNote.setUserid(u.getId());
        return R.ok(tSupervisionNoteService.updateById(tSupervisionNote));
    }
}
