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.TSupervisionInvestigation;
import com.nantian.cloud.jljc.service.TSupervisionInvestigationService;
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("/tSupervisionInvestigation")
@Api(value = "tSupervisionInvestigation", tags = "调查（询问）笔录")
public class TSupervisionInvestigationController {

    private final TSupervisionInvestigationService tSupervisionInvestigationService;

    /**
     * 查询全部调查（询问）笔录
     */
    @GetMapping("/list")
    @SysLog("查询全部调查（询问）笔录")
    @ApiOperation(value = "查询全部调查（询问）笔录", notes = "查询全部调查（询问）笔录")
    @ApiImplicitParam(name = "TSupervisionInvestigation", value = "调查（询问）笔录对象", required = false, dataType = "json", paramType = "path")
    public R selectAll(TSupervisionInvestigation tSupervisionInvestigation) {
        tSupervisionInvestigation.setDelFlag("1"); //筛选未删除项
        return R.ok(tSupervisionInvestigationService.list(Wrappers.query(tSupervisionInvestigation)));
    }

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

    /**
     * 保存/修改调查（询问）笔录
     */
    @PostMapping("saveOrUpdate")
    @SysLog("保存/修改调查（询问）笔录")
    @ApiOperation(value = "保存/修改调查（询问）笔录", notes = "保存/修改调查（询问）笔录")
    @ApiImplicitParam(name = "TSupervisionInvestigation", value = "调查（询问）笔录对象", required = true, dataType = "json", paramType = "path")
    public R saveOrUpdate(@RequestBody TSupervisionInvestigation tSupervisionInvestigation) {
        ScUser u = SecurityUtils.getUser();
        tSupervisionInvestigation.setUserid(u.getId());
        tSupervisionInvestigation.setUpdateTime(new Date());
        hasOld( tSupervisionInvestigation, u);
        return R.ok(tSupervisionInvestigationService.saveOrUpdate(tSupervisionInvestigation));
    }

    /**
     * 批量保存/修改调查（询问）笔录
     */
    @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<TSupervisionInvestigation> tSupervisionInvestigationList = (List<TSupervisionInvestigation>) JSONArray.parseArray(para, TSupervisionInvestigation.class);
        List<TSupervisionInvestigation> newList = new ArrayList<>();
        for (TSupervisionInvestigation tSupervisionInvestigation : tSupervisionInvestigationList) {
            ScUser u = SecurityUtils.getUser();
            tSupervisionInvestigation.setUserid(u.getId());
            tSupervisionInvestigation.setUpdateTime(new Date());
            hasOld( tSupervisionInvestigation, u);
            newList.add(tSupervisionInvestigation);
        }
        return R.ok(tSupervisionInvestigationService.saveOrUpdateBatch(newList));
    }

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