package com.miaxis.app.controller.report;

import com.miaxis.app.file.vo.FileVo;
import com.miaxis.app.report.domain.ReportInfo;
import com.miaxis.app.report.dto.ReportInfoDTO;
import com.miaxis.app.report.service.IReportImagesService;
import com.miaxis.app.report.service.IReportInfoService;
import com.miaxis.common.aliyunOSS.AliyunConfig;
import com.miaxis.common.aliyunOSS.AliyunUpload;
import com.miaxis.common.annotation.Log;
import com.miaxis.common.constant.Constants;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.core.page.ResponsePageInfo;
import com.miaxis.common.enums.BusinessTypeEnum;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 【举报信息】Controller
 *
 * @author miaxis
 * @date 2021-01-06
 */
@RestController
@RequestMapping(Constants.OPEN_PREFIX+"/report/info")
@Api(tags={"【app-举报信息】"})
public class ReportInfoController extends BaseController{
    @Autowired
    private IReportInfoService reportInfoService;

    @Autowired
    private IReportImagesService reportImagesService;


    /**
     * 查询举报信息列表
     */
    //@PreAuthorize("@ss.hasPermi('report:info:list')")
    @GetMapping("/list")
    @ApiOperation("查询举报信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码" ,dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize",value = "每页数据量" , dataType = "int", paramType = "query", required = false),
    })
    public ResponsePageInfo<ReportInfo> list(@ModelAttribute ReportInfo reportInfo){
        startPage();
        List<ReportInfo> list = reportInfoService.selectReportInfoList(reportInfo);
        return toResponsePageInfo(list);
    }


    /**
     * 获取举报信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('report:info:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取举报信息详细信息")
    public Response<ReportInfo> getInfo(
            @ApiParam(name = "id", value = "举报信息参数", required = true)
            @PathVariable("id") Long id
    ){
        return Response.success(reportInfoService.selectReportInfoById(id));
    }

    /**
     * 新增举报信息
     */
    //@PreAuthorize("@ss.hasPermi('report:info:add')")
    @Log(title = "举报信息", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    @ApiOperation("新增举报信息")
    public Response add(@RequestBody ReportInfoDTO reportInfoDTO){
        reportInfoService.insertReportInfo(reportInfoDTO);
        return Response.success();
    }

    /**
     * 修改举报信息
     */
    //@PreAuthorize("@ss.hasPermi('report:info:edit')")
    @Log(title = "举报信息", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    @ApiOperation("修改举报信息")
    public Response<Integer> edit(@RequestBody ReportInfo reportInfo){
        return toResponse(reportInfoService.updateReportInfo(reportInfo));
    }

    /**
     * 删除举报信息
     */
    //@PreAuthorize("@ss.hasPermi('report:info:remove')")
    @Log(title = "举报信息", businessType = BusinessTypeEnum.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除举报信息")
    public  Response<Integer> remove(
            @ApiParam(name = "ids", value = "举报信息ids参数", required = true)
            @PathVariable Long[] ids
    ){
        return toResponse(reportInfoService.deleteReportInfoByIds(ids));
    }



    /**
     * 上传举报照片
     */
    @Log(title = "上传举报照片", businessType = BusinessTypeEnum.INSERT)
    @PostMapping("/image")
    @ApiOperation("上传举报信息")
    public Response<FileVo> uploadImage(MultipartFile multFile) throws IOException {
        String savePath = AliyunConfig.GZPT_REPORT
                + new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/report/";
        String fileName = System.currentTimeMillis() + ".jpg";//文件名
        String fileUrl = AliyunUpload.uploadForStream(multFile.getInputStream(), savePath, fileName);
        FileVo fileVo = new FileVo();
        fileVo.setFileName(fileName);
        fileVo.setFilePath(savePath+fileName);
        fileVo.setFileUrl(fileUrl);
        return Response.success(fileVo);
    }
}
