package com.nantian.cloud.jljc.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.TNoticeLog;
import com.nantian.cloud.jljc.service.TNoticeLogService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.minio.service.MinioTemplate;
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.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


/**
 * 通知表日志
 *
 * @date 2020-09-23
 */
@Slf4j
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tNoticeLog")
@Api(value = "tNoticeLog", tags = "通知表日志")
public class TNoticeLogController {
    @Autowired
    private TNoticeLogService tNoticeLogService;

    /**
     * 分页获取通知表日志
     */
    @GetMapping("/page")
    @SysLog("分页获取通知表日志")
    @ApiOperation(value = "分页获取通知表日志", notes = "分页获取通知表日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "tNoticeLog", value = "通知表日志对象", required = false, dataType = "json", paramType = "path")
    })
    public R gettNoticeLogPage(Page page, TNoticeLog tNoticeLog) {
        tNoticeLog.setDelFlag("1"); //筛选未删除项
        return R.ok(tNoticeLogService.page(page, Wrappers.query(tNoticeLog)));
    }

    /**
     * 根据id获取通知表日志
     */
    @GetMapping("/getById")
    @SysLog("根据id获取通知表日志")
    @ApiOperation(value = "根据id获取通知表日志", notes = "根据id获取通知表日志")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
    public R getById(int id) {
        return R.ok(tNoticeLogService.getById(id));
    }

    /**
     * 已读通知更新日志（企业端）
     */
    @GetMapping("/read")
    @SysLog("已读通知更新日志（企业端）")
    @ApiOperation(value = "已读通知更新日志（企业端）", notes = "已读通知更新日志（企业端）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeId", value = "通知表id", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "epc", value = "企业社会信用编码", required = true, dataType = "int", paramType = "path")
    })
    public R Read(Integer noticeId, String epc) {
        if (epc == null || "".equals(epc)) {
            return R.failed("已读通知失败，企业信用编码不能为空！");
        }
        if (noticeId == null) {
            return R.failed("已读通知失败，通知表id不能为空！");
        }

        TNoticeLog q = new TNoticeLog();
        q.setNoticeId(noticeId);
        q.setEpc(epc);
        q.setDelFlag("1");
        TNoticeLog old = tNoticeLogService.getOne(Wrappers.query(q));

        TNoticeLog tNoticeLog = new TNoticeLog();
        if(old!=null){ //有历史数据属于修改
            tNoticeLog.setId(old.getId());
        }
        //企业查看通知了
        tNoticeLog.setNoticeId(noticeId);
        tNoticeLog.setEpc(epc);
        tNoticeLog.setDelFlag("1");
        tNoticeLog.setState("T");

        ScUser u = SecurityUtils.getUser();
        tNoticeLog.setUserid(u.getId());
        tNoticeLog.setDeptId(u.getDeptId());
        tNoticeLog.setUpdateTime(new Date());
        return R.ok(tNoticeLogService.saveOrUpdate(tNoticeLog));
    }

    /**
     * 已读通知更新日志（政府端）
     */
    @GetMapping("/readGov")
    @SysLog("已读通知更新日志（政府端）")
    @ApiOperation(value = "已读通知更新日志（政府端）", notes = "已读通知更新日志（政府端）")
    @ApiImplicitParam(name = "noticeId", value = "通知表id", required = true, dataType = "int", paramType = "path")
    public R ReadGov(Integer noticeId) {
        if (noticeId == null) {
            return R.failed("政府端已读通知失败，通知表id不能为空！");
        }

        TNoticeLog q = new TNoticeLog();
        ScUser u = SecurityUtils.getUser();
        q.setNoticeId(noticeId);
        q.setZfUserid(u.getId());
        q.setZfDeptId(u.getDeptId());
        q.setDelFlag("1");
        TNoticeLog old = tNoticeLogService.getOne(Wrappers.query(q));

        TNoticeLog tNoticeLog = new TNoticeLog();
        if(old!=null){ //有历史数据属于修改
            tNoticeLog.setId(old.getId());
        }
        //政府端查看通知
        tNoticeLog.setNoticeId(noticeId);
        tNoticeLog.setZfUserid(u.getId());
        tNoticeLog.setZfDeptId(u.getDeptId());
        tNoticeLog.setState("T");
        tNoticeLog.setDelFlag("1");

        tNoticeLog.setUserid(u.getId());
        tNoticeLog.setDeptId(u.getDeptId());
        tNoticeLog.setUpdateTime(new Date());
        return R.ok(tNoticeLogService.saveOrUpdate(tNoticeLog));
    }

}
