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.TSupervisionInspection;
import com.nantian.cloud.jljc.service.TSupervisionInspectionService;
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("/tSupervisionInspection")
@Api(value = "tSupervisionInspection", tags = "现场检查笔录")
public class TSupervisionInspectionController {

    private final TSupervisionInspectionService tSupervisionInspectionService;

    /**
     * 查询全部现场检查笔录
     */
    @GetMapping("/list")
    @SysLog("查询全部现场检查笔录")
    @ApiOperation(value = "查询全部现场检查笔录", notes = "查询全部现场检查笔录")
    @ApiImplicitParam(name = "TSupervisionInspection", value = "现场检查笔录对象", required = false, dataType = "json", paramType = "path")
    public R selectAll(TSupervisionInspection tSupervisionInspection) {
        tSupervisionInspection.setDelFlag("1"); //筛选未删除项
        return R.ok(tSupervisionInspectionService.list(Wrappers.query(tSupervisionInspection)));
    }

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

    /**
     * 保存/修改现场检查笔录
     */
    @PostMapping("saveOrUpdate")
    @SysLog("保存/修改现场检查笔录")
    @ApiOperation(value = "保存/修改现场检查笔录", notes = "保存/修改现场检查笔录")
    @ApiImplicitParam(name = "TSupervisionInspection", value = "现场检查笔录对象", required = true, dataType = "json", paramType = "path")
    public R saveOrUpdate(@RequestBody TSupervisionInspection tSupervisionInspection) {
        ScUser u = SecurityUtils.getUser();
        tSupervisionInspection.setUserid(u.getId());
        tSupervisionInspection.setUpdateTime(new Date());
        hasOld( tSupervisionInspection, u);
        return R.ok(tSupervisionInspectionService.saveOrUpdate(tSupervisionInspection));
    }

    /**
     * 批量保存/修改现场检查笔录
     */
    @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<TSupervisionInspection> tSupervisionInspectionList = (List<TSupervisionInspection>) JSONArray.parseArray(para, TSupervisionInspection.class);
        List<TSupervisionInspection> newList = new ArrayList<>();
        for (TSupervisionInspection tSupervisionInspection : tSupervisionInspectionList) {
            ScUser u = SecurityUtils.getUser();
            tSupervisionInspection.setUserid(u.getId());
            tSupervisionInspection.setUpdateTime(new Date());
            hasOld( tSupervisionInspection, u);
            newList.add(tSupervisionInspection);
        }
        return R.ok(tSupervisionInspectionService.saveOrUpdateBatch(newList));
    }

    private void hasOld(TSupervisionInspection tSupervisionInspection, ScUser u) {
        TSupervisionInspection old = new TSupervisionInspection();
        old.setEpc(tSupervisionInspection.getEpc());
        old.setMonitorId(tSupervisionInspection.getMonitorId());
        TSupervisionInspection o = tSupervisionInspectionService.getOne(Wrappers.query(old));
        if (o != null) { //有历史数据，属于修改
            tSupervisionInspection.setId(o.getId());
        } else {
            tSupervisionInspection.setDelFlag("1");
            tSupervisionInspection.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) {
        TSupervisionInspection tSupervisionInspection = tSupervisionInspectionService.getById(id);
        if (tSupervisionInspection == null) {
            return R.failed("id不存在，删除现场检查笔录失败！");
        }
        ScUser u = SecurityUtils.getUser();
        tSupervisionInspection.setUpdateTime(new Date());
        tSupervisionInspection.setDelFlag("4");
        tSupervisionInspection.setUserid(u.getId());
        return R.ok(tSupervisionInspectionService.updateById(tSupervisionInspection));
    }
}
