package com.nantian.cloud.jly.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jly.entity.Memorandum;
import com.nantian.cloud.jly.service.MemorandumService;
import com.nantian.cloud.jly.vo.MemorandumVo;
import com.nantian.cloud.jly.vo.util.PageUtils;
import com.nantian.cloud.jly.vo.util.Pages;
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.ApiOperation;
import lombok.AllArgsConstructor;
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.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * 备忘录表
 *
 * @author wuyuahao
 * @date 2021-05-06
 */
@RestController
@AllArgsConstructor
@RequestMapping("/memorandum")
@Api(value = "memorandum", tags = "备忘录表")
public class MemorandumController {

    @Autowired
    private final MemorandumService memorandumService;
    @Autowired
    private MinioTemplate minioTemplate;


    /**
     * 分页查询
     *
     * @param page      分页对象
     * @return
     */
    @SysLog("备忘录表-分页查询")
    @GetMapping("/page")
    @ApiOperation(value = "查询备忘录表", notes = "条件查询备忘录表")
    public R getcheckPlanPage(Page page, MemorandumVo enterpriseInfo) {
        Pages pages=new Pages();
        pages.setSize(page.getSize());
        pages.setCurrent(page.getCurrent());
        List<Integer> num = memorandumService.count(enterpriseInfo);
        PageUtils pageUtils=new PageUtils(memorandumService.selectPageAll(pages,enterpriseInfo),num==null ? 0 : num.size());
        pageUtils.setSize(page.getSize());
        return R.ok(pageUtils);
    }



    /**
     * 保存
     *
     * @param memorandum
     * @param file
     * @return R
     */
    @SysLog("备忘录表-保存")
    @PostMapping
    @ApiOperation(value = "新增企业备忘录",notes = "新增企业备忘录")
    @ApiImplicitParam(name = "EnterpriseInfo", value = "企业备忘录", required = true, dataType = "json", paramType = "path")
    synchronized public R save(Memorandum memorandum, @RequestParam("file") MultipartFile file) {
        ScUser u= SecurityUtils.getUser();
        String uuid="";
        if (uuid == null || uuid == "") {            uuid = UUID.randomUUID().toString();
        }

        try {
            InputStream inputStream = file.getInputStream(); //得到文件流
            String fileName = file.getOriginalFilename(); //文件名
            minioTemplate.putObject("jly", uuid, inputStream,inputStream.available(),"application/octet-stream");
            memorandum.setFileName(fileName + "<" + uuid);
        } catch (Exception e) {
            e.printStackTrace();
            return R.ok(e);
        }
        if(u.getId()!=null){
            memorandum.setUserid(u.getId());
        }
        if (u.getDeptId() != null) {
            memorandum.setDeptId(u.getDeptId());
        }

        memorandum.setDelFlag("1");
        memorandum.setUpdateTime(new Date());
        return R.ok(memorandumService.saveFile(memorandum));
    }


    /**
     * 修改
     *
     * @param memorandum
     * @return R
     */
    @SysLog("备忘录表-修改")
    @PutMapping
    @ApiOperation(value = "修改备忘录表", notes = "修改备忘录表")
    public R updateById(@RequestBody Memorandum memorandum) {
        ScUser u= SecurityUtils.getUser();
        if(u.getId()!=null){
            memorandum.setUserid(u.getId());
        }
        memorandum.setUpdateTime(new Date());
        return R.ok(memorandumService.updateById(memorandum));
    }
}
