package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.PageResponse;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.core.utils.FtpUtils;
import com.ssm.manage.entity.ImgMaintain;
import com.ssm.manage.entity.StaffWorks;
import com.ssm.manage.query.ImgMaintainRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmImgMaintainService;
import com.ssm.manage.utils.Constant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Api(value="平台维护项目相关资源图片controller",tags={"平台维护项目相关资源图片相关接口"})
@RestController
@RequestMapping("/demay/ssm/img")
@Slf4j
public class SsmImgMaintainController {
    @Autowired
    private SsmImgMaintainService ssmImgMaintainService;

    @ApiOperation(value = "项目相关资源图片记录分页查询  平台端-项目相关资源图片记录分页查询",httpMethod = "POST",notes = "",response = PageResponse.class)
    @RequestMapping("/page")
    public PageResponse page(@RequestBody ImgMaintainRequest imgMaintainRequest){
        PageResponse pageResponse = new PageResponse();
        PageInfo<ImgMaintain> pageInfo = new PageInfo();
        try {
            pageInfo = ssmImgMaintainService.page(imgMaintainRequest);
        } catch (Exception e) {
            log.error("SsmImgMaintainController.page:", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }


    @ApiOperation(value = "项目相关资源图片上传  平台端-项目相关资源图片上传",httpMethod = "POST",notes = "",response = GenericResponse.class)
    @RequestMapping("/upload")
    public GenericResponse upload(List<MultipartFile> fileList, ImgMaintainRequest imgMaintainRequest, HttpSession session){
        GenericResponse genericResponse = new GenericResponse();
        if (null == fileList || fileList.size() == 0){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"图片");
            return genericResponse;
        }
        //图片上传
        FtpUtils ftp = new FtpUtils();
        boolean b = true;
        //文件信息list
        List<ImgMaintain> uploadFilesInfo = new ArrayList<>();
        for (MultipartFile multipartFile : fileList) {
            ImgMaintain img = new ImgMaintain();
            String fileName = multipartFile.getOriginalFilename();//获取文件名
//            String fileName = multipartFile.getOriginalFilename();//获取文件名
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());//获取文件后缀
            String saveName = UUID.randomUUID() + "." + suffix;
            String path = Constant.filePath + "/" ;//文件上传路径
            img.setFileName(saveName);
            img.setFilePath(path);
            img.setType(imgMaintainRequest.getType());
            uploadFilesInfo.add(img);
            InputStream inputStream = null;
            try {
                inputStream = multipartFile.getInputStream();
                b = ftp.uploadFile(path, saveName, inputStream);
            } catch (IOException e) {
                log.error("SsmImgMaintainController.upload:", e);
                genericResponse.setCodeAndMsg(ResponseCode.FILE_UPLOAD_FAILED);
                return genericResponse;
            }
        }
        if (b) {
            String userNum = SessionHelp.getLoginAccount(session);
            int i = 0;
            try {
                i = ssmImgMaintainService.saveInfo(uploadFilesInfo,userNum);
            } catch (Exception e) {
                log.error("SsmImgMaintainController.upload:", e);
                genericResponse.setCodeAndMsg(ResponseCode.FAILED);
                return genericResponse;
            }
            if (i > 0) {
                genericResponse.setCodeAndMsg(ResponseCode.OK);
            }
        }
        return genericResponse;
    }

    @ApiOperation(value = "单个删除项目相关资源图片   平台端-单个删除项目相关资源图片",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/delete")
    public GenericResponse delete(@RequestBody ImgMaintainRequest imgMaintainRequest){
        GenericResponse<StaffWorks> genericResponse = new GenericResponse<>();
        if (imgMaintainRequest.getId() == null){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"图片Id");
            return genericResponse;
        }
        ImgMaintain imgMaintain = ssmImgMaintainService.selectByPrimaryKey(imgMaintainRequest.getId());
        String path = imgMaintain.getFilePath();
        String name = imgMaintain.getFileName();
        try {
            new FtpUtils().deleteFile(path,name);
        } catch (IOException e) {
            log.error("SsmImgMaintainController.delete:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FILE_DELETE_FAILED);
            return genericResponse;
        }

        int i = 0;
        try {
            i = ssmImgMaintainService.deleteByPrimaryKey(imgMaintainRequest.getId());
        }catch (Exception e){
            log.error("SsmImgMaintainController.delete:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }


    @ApiOperation(value = "批量删除项目相关资源图片   平台端-批量删除项目相关资源图片",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/deleteBatch")
    public GenericResponse deleteBatch(@RequestBody List<ImgMaintain> list){
        GenericResponse<StaffWorks> genericResponse = new GenericResponse<>();

        //删除图片
        for (ImgMaintain imgMaintain : list) {
            String path = imgMaintain.getFilePath();
            String name = imgMaintain.getFileName();
            try {
                new FtpUtils().deleteFile(path, name);
            } catch (IOException e) {
                log.error("SsmImgMaintainController.deleteBatch:", e);
                genericResponse.setCodeAndMsg(ResponseCode.FILE_DELETE_FAILED);
                return genericResponse;
            }
        }
        //删除表记录
        int i = 0;
        try {
            i = ssmImgMaintainService.delete(list);
        }catch (Exception e){
            log.error("SsmImgMaintainController.deleteBatch:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

}
