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.entity.TSupervisionNotification;
import com.nantian.cloud.jljc.service.TSupervisionNotificationService;
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("/tSupervisionNotification")
@Api(value = "tSupervisionNotification", tags = "监察告知书")
public class TSupervisionNotificationController {

    private final TSupervisionNotificationService tSupervisionNotificationService;

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

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

    /**
     * 保存/修改监察告知书
     */
    @PostMapping("saveOrUpdate")
    @SysLog("保存/修改监察告知书")
    @ApiOperation(value = "保存/修改监察告知书", notes = "保存/修改监察告知书")
    @ApiImplicitParam(name = "TSupervisionNotification", value = "监察告知书对象", required = true, dataType = "json", paramType = "path")
    public R saveOrUpdate(@RequestBody TSupervisionNotification tSupervisionNotification) {
        if(tSupervisionNotification.getEpc()==null){
            return R.failed("社会信用编码不能为空，新增监察告知书失败！");
        }
        if(tSupervisionNotification.getMonitorId()==null){
            return R.failed("监察编号不能为空，新增监察告知书失败！");
        }

        ScUser u = SecurityUtils.getUser();
        hasOld(tSupervisionNotification, u);
        tSupervisionNotification.setUserid(u.getId());
        tSupervisionNotification.setUpdateTime(new Date());
        return R.ok(tSupervisionNotificationService.saveOrUpdate(tSupervisionNotification));
    }

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


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