package com.hzya.frame.sys.file.upload.controller;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.page.PageAttribute;
import com.hzya.frame.sys.file.upload.entity.FileResultEntity;
import com.hzya.frame.sys.file.upload.entity.FileUploadEntity;
import com.hzya.frame.sys.file.upload.service.IFileUploadService;
import com.hzya.frame.web.action.DefaultController;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/**
 * @description: 系统文件上传  controller
 * @tableName: sys_file
 * @entityName: FileUploadEntity
 * 包含：
 * 1、分页查询(queryPaged)
 * 2、index页面(goIndexPage)
 * 3、新增页面(goAddPage)
 * 4、新增信息保存(save)
 * 5、修改页面(goEditPage)
 * 6、修改信息保存(update)
 * 7、信息查看(goShowPage)
 * 8、删除信息(delete)
 * @author: gjh
 * @history: 1.0
 */

@Controller("sys_file_fileUpload")
@RequestMapping("/sys/file/FileUploadController")
public class FileUploadController extends DefaultController {
    private final Logger log;
    @Autowired
    protected IFileUploadService fileuploadService;

    public FileUploadController() {
        this.log = LogManager.getLogger(this.getClass());
    }

    /**
     * 系统文件上传分页信息
     **/
    @RequestMapping(value = "queryPaged")
    @ResponseBody
    public PageAttribute<FileUploadEntity> queryPaged(FileUploadEntity entity) throws Exception {
        PageAttribute<FileUploadEntity> page = null;
        try {
            page = fileuploadService.queryPaged(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;
    }

    /**
     * 跳转到系统文件上传的index页面
     **/
    @RequestMapping(value = "goIndexPage", method = RequestMethod.GET)
    public ModelAndView goIndexPage() {
        ModelAndView view = new ModelAndView();
        /**这里根据实际的需要填写需要传到前台的对象**/
        //view.addObject(attributeName, attributeValue);
        /**这里是返回对应的页面路径**/
        view.setViewName("/sys/file/upload/upload/list");
        return view;
    }

    /**
     * 跳转到系统文件上传信息新增页面
     **/
    @RequestMapping(value = "goAddPage")
    public ModelAndView goAddPage() {
        ModelAndView view = new ModelAndView();
        /**这里根据实际的需要填写需要传到前台的对象**/
        //view.addObject(attributeName, attributeValue);
        /**这里是返回对应的页面路径**/
        view.setViewName("/sys/file/upload/upload/add");
        return view;
    }

    /**
     * 系统文件上传新增信息保存
     **/
     @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity save(FileUploadEntity entity) {
        try {
            fileuploadService.save(entity);
            return getSuccessMessageEntity("保存成功", "save");
        } catch (Exception e) {
            logger.error("save", e);
            e.printStackTrace();
            return getFailureMessageEntity("保存失败！" + e.getMessage(), "save");
        }
    }

    /**
     * 跳转到系统文件上传信息修改页面
     **/
    @RequestMapping(value = "edit/{id}")
    public ModelAndView goEditPage(@PathVariable("id") Long id) {
        ModelAndView view = new ModelAndView();
        /**这里根据实际的需要填写需要传到前台的对象**/
        FileUploadEntity entity = fileuploadService.get(id);
        view.addObject("entity", entity);
        /**这里是返回对应的页面路径**/
        view.setViewName("/sys/file/upload/upload/edit");
        return view;
    }

    /**
     * 系统文件上传修改信息保存
     **/
     @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity update(FileUploadEntity entity) {
        try {
            fileuploadService.update(entity);
            return getSuccessMessageEntity("更新成功！", "update");
        } catch (Exception e) {
            logger.error("update", e);
            e.printStackTrace();
            return getFailureMessageEntity("更新失败！" + e.getMessage(), "update");
        }
    }

    /**
     * 跳转到系统文件上传信息查看
     **/
    @RequestMapping(value = "show/{id}")
    public ModelAndView goShowPage(@PathVariable("id") Long id) {
        ModelAndView view = new ModelAndView();
        /**这里根据实际的需要填写需要传到前台的对象**/
        FileUploadEntity entity = fileuploadService.get(id);
        view.addObject("entity", entity);
        /**这里是返回对应的页面路径**/
        view.setViewName("/sys/file/upload/upload/show");
        return view;
    }

    /**
     * 删除系统文件上传信息
     **/
     @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity delete(Long id) {
        try {
            fileuploadService.removeById(id);
            return getSuccessMessageEntity("删除成功！", "delete");
        } catch (Exception e) {
            logger.error("delete", e);
            e.printStackTrace();
            return getFailureMessageEntity("删除失败！" + e.getMessage(), "delete");
        }
    }

    /**
     * 打开文件上传页面
     **/
    @RequestMapping(value = "goFileUploadIndexPage", method = RequestMethod.GET)
    public ModelAndView goFileUploadIndexPage(Long ticket_id, Long business_id, String business_type, boolean fileFlag) {
        ModelAndView view = new ModelAndView();
        /**这里根据实际的需要填写需要传到前台的对象**/
        view.addObject("ticket_id", ticket_id);
        view.addObject("business_id", business_id);
        view.addObject("business_type", business_type);
        view.addObject("fileFlag", fileFlag);
        /**这里是返回对应的页面路径**/
        view.setViewName("/sys/file/upload/upload/uploadIndex");
        return view;
    }


    /**
     * 打开单票的上传页面
     **/
    @RequestMapping(value = "goTicketFileUploadIndexPage", method = RequestMethod.GET)
    public ModelAndView goTicketFileUploadIndexPage(Long ticket_id, Long business_id, String business_type, boolean fileFlag) {
        ModelAndView view = new ModelAndView();
        /**这里根据实际的需要填写需要传到前台的对象**/
        view.addObject("ticket_id", ticket_id);
        view.addObject("business_id", business_id);
        view.addObject("business_type", business_type);
        view.addObject("fileFlag", fileFlag);
        /**这里是返回对应的页面路径**/
        view.setViewName("/sys/file/upload/upload/uploadIndex");
        return view;
    }

    /***
     * 文件上传接口 sys/file/FileUploadController/fileUpload
     * @param
     * @return@@
     */
    @RequestMapping(value = "fileUpload", method = RequestMethod.POST)
    @ResponseBody
    public FileResultEntity fileUpload(@RequestParam("file") MultipartFile file, FileResultEntity entity) {


        log.info("-------------------开始调用上传文件upload接口-------------------");
        try {
            entity = fileuploadService.fileUpload(file, entity);
            entity.setSuccess(true);
        } catch (Exception e) {
            entity.setFileFlag(false);
            e.printStackTrace();
        }
        log.info("-------------------结束调用上传文件upload接口-------------------");

        return entity;
    }

    /***
     * 根据业务信息，把以前上传的附件放入到单票目录下面
     * @param
     * @return@@
     */
     @RequestMapping(value = "copyFileToBusiness", method = RequestMethod.POST)
    @ResponseBody
    public FileResultEntity copyFileToBusiness(FileResultEntity entity) {


        log.info("-------------------开始调用文件复制接口-------------------");
        try {
            entity = fileuploadService.copyFileToBusiness(entity);
        } catch (Exception e) {

        }
        log.info("-------------------结束调用件复制接口-------------------");

        return entity;
    }


    /**
     * 根据fileSaveName删除附件
     *
     * @param businessFileId 业务文件的ID
     * @param businessType   删除类型   1，业务删除（业务删除时候，需要把文件备份一份到指定的文件夹），2，非业务删除（直接删除文件）
     * @param fileSaveName   附件的唯一名称
     * @return
     */
     @RequestMapping(value = "deleteFileByFileSaveName", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity deleteFileByFileSaveName(Long businessFileId, String businessType, String fileSaveName) {
        try {
            boolean flag = fileuploadService.deleteFileByFileSaveName(businessFileId, businessType, fileSaveName);
            return getSuccessMessageEntity("删除成功！", "delete");
        } catch (Exception e) {
            logger.error("delete", e);
            e.printStackTrace();
            return getFailureMessageEntity("删除失败！" + e.getMessage(), "delete");
        }
    }

}

