/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.base.entity.Annex;
import com.tofly.base.service.AnnexService;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.ftp.config.FtpUtil;
import com.tofly.common.oauth.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;


/**
 *
 */
@RestController
@RequestMapping("/annex")
@Api(tags="文件接口")
public class AnnexController {
    @Autowired
    private AnnexService annexService;
    @Value("${user.avatar}")
    private String avatarPath;

    /**
     * 分页查询
     * @param annex 分页对象
     * @return IPage
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/list")
    public ResultRespone<List<Annex>> getFormVisibleConfigPage(Page page, @RequestBody Annex annex) {
        return ResultRespone.success(annexService.page(annex));
    }

    /**
     * 通过id查询
     * @param id id
     * @return Result
     */
    @ApiOperation(value = "通过id获取", notes = "通过id获取")
    @GetMapping("/{id}")
    public ResultRespone<Annex> getById(@PathVariable Integer id) {
        return ResultRespone.success(annexService.getById(id));
    }

    /**
     * 新增
     * @param annex
     * @return Result
     */
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping
    public ResultRespone save(@RequestBody Annex annex) {
        annex.setReportUser(SecurityUtils.getUserId());
        annex.setReportTime(new Date());
        return annexService.save(annex) ? ResultRespone.success("添加成功") : ResultRespone.failed("添加失败");

    }

    /**
     * 修改
     * @param annex
     * @return Result
     */
    @ApiOperation(value = "修改", notes = "修改")
    @PutMapping
    public ResultRespone updateById(@RequestBody Annex annex) {
        return annexService.updateById(annex) ? ResultRespone.success("修改成功") : ResultRespone.failed("修改失败");

    }

    /**
     * 修改
     * @param annex
     * @return Result
     */
    @ApiOperation(value = "审核", notes = "审核")
    @PostMapping("/reviewed")
    public ResultRespone reviewed(@RequestBody Annex annex) {
        annex.setReviewedUser(SecurityUtils.getUserId());
        annex.setReviewedDate(new Date());
        return annexService.reviewed(annex) ? ResultRespone.success("审核成功") : ResultRespone.failed("审核失败");
    }

    /**
     * 通过id删除
     * @param id id
     * @return Result
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @DeleteMapping("/{id}")
    public ResultRespone removeById(@PathVariable Integer id) {
        return annexService.removeById(id) ? ResultRespone.success("删除成功") : ResultRespone.failed("删除失败");
    }

    /**
     * 上传
     * @param file
     * @return Result
     */
    @ApiOperation(value = "上传", notes = "上传")
    @PostMapping("/saveFile")
    public ResultRespone saveFile(MultipartFile file) {
        String filePath = avatarPath + IdWorker.getIdStr() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        boolean b = FtpUtil.uploadFile2Ftp(file, filePath);
        if(b){
            return ResultRespone.success(filePath);
        }
        return ResultRespone.failed("上传文件失败");
    }
}
