package com.nantian.cloud.jljc.controller;

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

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

@Slf4j
@CrossOrigin
@AllArgsConstructor
@Api(value = "deleteDataRecord", tags = "删除在线数据删除记录")
@RestController
@RequestMapping("/deleteDataRecord")
public class DeleteDataRecordController {

    @Autowired
    private DeleteDataRecordService  deleteDataRecordService;

    @Autowired
    private MinioTemplate minioTemplate;

    @ApiOperation(value = "分页获取在线数据删除记录", notes = "分页获取在线数据删除记录")
    @SysLog("分页获取在线数据删除记录")
    @GetMapping("/pageInfo")
    @ApiImplicitParam(name = "DeleteDataRecord", value = "分页获取在线数据删除记录", required = true, paramType = "path")
    public R pageInfo(Page page, DeleteDataRecord deleteDataRecord) {
        deleteDataRecord.setDelFlag("1");
        return R.ok(deleteDataRecordService.pageInfo(page,deleteDataRecord));
    }

    /**
     * 新增/修改在线数据删除记录
     */
    @ApiOperation(value = "新增/修改在线数据删除记录", notes = "新增/修改在线数据删除记录")
    @SysLog("新增/修改在线数据删除记录")
    @PostMapping("/addOrUp")
    public R addOrUp(DeleteDataRecord deleteDataRecord, MultipartFile file, HttpServletRequest request) throws Exception {
        log.error("新增/修改删除信息：{}，##文件：{}，##开始时间：{}，##结束时间：{}",
                deleteDataRecord, file);
        ScUser u = SecurityUtils.getUser();
        deleteDataRecord.setUserid(u.getId());
        deleteDataRecord.setUpdateTime(new Date());
        if (deleteDataRecord.getId() == null) { //新增
            deleteDataRecord.setDelFlag("1");
            deleteDataRecord.setDeptId(u.getDeptId());
        }
        if (file != null) {
            if (file.getSize() > 0) {
                deleteDataRecord.setImgUrl(getFileUrl(file));
            }
        }
        deleteDataRecordService.saveOrUpdate(deleteDataRecord);
        return R.ok(deleteDataRecord);
    }

    public String getFileUrl(MultipartFile file) throws Exception {
        String uuid = UUID.randomUUID().toString();
        InputStream inputStream = file.getInputStream(); //得到文件流
        String fileName = file.getOriginalFilename();    //文件名（含后缀）
        minioTemplate.putObject("jljc", uuid, inputStream, inputStream.available(), "application/octet-stream");
        return fileName + "<" + uuid;
    }


    @ApiOperation(value = "删除信息", notes = "删除信息")
    @SysLog("删除信息")
    @DeleteMapping("/delete")
    public R delete(Integer id) {
        DeleteDataRecord deleteDataRecord = deleteDataRecordService.getById(id);
        if (deleteDataRecord == null) {
            return R.failed("删除信息不存在，删除失败！");
        }
        ScUser u = SecurityUtils.getUser();
        deleteDataRecord.setUserid(u.getId());
        deleteDataRecord.setUpdateTime(new Date());
        deleteDataRecord.setDelFlag("4");
        return R.ok(deleteDataRecordService.updateById(deleteDataRecord));
    }
}
