package com.hzya.frame.sys.businessFile.controller;

import com.github.pagehelper.PageInfo;
import com.hzya.frame.page.PageAttribute;
import com.hzya.frame.sys.businessFile.entity.BusinessFileEntity;
import com.hzya.frame.sys.businessFile.service.IBusinessFileService;
import com.hzya.frame.sys.file.download.service.IFileDownloadService;
import com.hzya.frame.web.action.DefaultController;
import com.hzya.frame.web.action.SessionContext;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.zip.ZipOutputStream;

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

@Controller("sys_business_file")
@RequestMapping("/sys/business/file/BusinessFileController")
public class BusinessFileController extends DefaultController {
    @Autowired
    protected IBusinessFileService businessfileService;
    @Autowired
    private IFileDownloadService fileDownloadService;

    /**
     * 文件业务关系分页信息
     **/
     @RequestMapping(value = "queryPaged")
    @ResponseBody
    public PageAttribute<BusinessFileEntity> queryPaged(BusinessFileEntity entity) throws Exception {
         PageAttribute<BusinessFileEntity> page = null;
        try {
            page = businessfileService.queryPaged(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;
    }

     @RequestMapping(value = "queryList")
    @ResponseBody
    public List<BusinessFileEntity> queryList(BusinessFileEntity entity) throws Exception {
        List<BusinessFileEntity> list = null;
        try {
             list = businessfileService.query(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

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

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

    /**
     * 文件业务关系新增信息保存
     **/
     @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity save(BusinessFileEntity entity) {
        try {
            businessfileService.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();
        /**这里根据实际的需要填写需要传到前台的对象**/
        BusinessFileEntity entity = businessfileService.get(id);
        view.addObject("entity", entity);
        /**这里是返回对应的页面路径**/
        view.setViewName("/sys/businessFile/businessFile/edit");
        return view;
    }

    /**
     * 文件业务关系修改信息保存
     **/
     @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity update(BusinessFileEntity entity) {
        try {
            businessfileService.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();
        /**这里根据实际的需要填写需要传到前台的对象**/
        BusinessFileEntity entity = businessfileService.get(id);
        view.addObject("entity", entity);
        /**这里是返回对应的页面路径**/
        view.setViewName("/sys/businessFile/businessFile/show");
        return view;
    }

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

    /**
     * 业务附件批量保存,通过JSON字符串传入保存参数
     **/
     @RequestMapping(value = "saveBusinessFile", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity saveBusinessFile(BusinessFileEntity entity) {
        try {
            entity = businessfileService.saveBatchBusinessFile(entity);
            return getSuccessMessageEntity("保存成功！" );
        } catch (Exception e) {
            logger.error("save", e);
            e.printStackTrace();
            return getFailureMessageEntity("保存失败！" + e.getMessage(), "delete");
        }
    }

    /**
     * 业务附件批量保存
     **/
     @RequestMapping(value = "saveBatchBusinessFile", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity saveBatchBusinessFile(BusinessFileEntity entity) {
        try {
            entity = businessfileService.saveBusinessFile(entity);
            return getSuccessMessageEntity("保存成功！" );
        } catch (Exception e) {
            logger.error("save", e);
            e.printStackTrace();
            return getFailureMessageEntity("保存失败！" + e.getMessage(), "delete");
        }
    }


    /**
     * 业务附件批量保存
     **/
     @RequestMapping(value = "validateTicketFiles", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity validateTicketFiles(BusinessFileEntity entity) {
        try {
            entity = businessfileService.validateTicketFiles(entity);
            return getSuccessMessageEntity("验证成功！" );
        } catch (Exception e) {
            logger.error("save", e);
            e.printStackTrace();
            return getFailureMessageEntity(e.getMessage(), "validate");
        }
    }

     @RequestMapping(value = "listQueryBusinessFilesByGroup", method = RequestMethod.POST)
    @ResponseBody
    public List<BusinessFileEntity> listQueryBusinessFilesByGroup(BusinessFileEntity entity) {
        List<BusinessFileEntity> listQueryBusinessFilesByGroup = businessfileService.listQueryBusinessFilesByGroup(entity);
        return listQueryBusinessFilesByGroup;
    }


    /***
     * 根据单票ID下载所有的附件
     * @param entity
     * @param response
     */
     @RequestMapping(value = "ticketFilesDownloadToZip")
    public void ticketFilesDownloadToZip(BusinessFileEntity entity, HttpServletResponse response) {
        ZipOutputStream out = null;
        response.setContentType("APPLICATION/OCTET-STREAM");
        String zipName = entity.getZipName() + ".zip";
        try {
            zipName = new String(zipName.getBytes(StandardCharsets.UTF_8), "iso8859-1");
            response.setHeader("Content-Disposition", "attachment; filename=" + zipName);
            out = new ZipOutputStream(response.getOutputStream());

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 文件业务关系分页信息
     **/
     @RequestMapping(value = "queryBusinessFileByServiceType")
    @ResponseBody
    public List<BusinessFileEntity> queryBusinessFileByServiceType(BusinessFileEntity entity) throws Exception {
        List<BusinessFileEntity> businessFileEntityList = null;
        try {
            businessFileEntityList = businessfileService.queryBusinessFileByServiceType(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return businessFileEntityList;
    }



    /**
     * 跳转到国内清关文件文件上传页面
     **/
    @RequestMapping(value = "goDomesticUploadPage/{id}")
    public ModelAndView goDomesticUploadPage(@PathVariable("id") Long id) {
        ModelAndView view = new ModelAndView();
        /** 传入整个单票对象*/

        /** 获取当前登陆人的服务项*/

        /**这里是返回对应的页面路径**/
        view.setViewName("/sys/businessFile/businessFile/add");
        return view;
    }


    /**
     * 跳转制作保函列表页面
     **/
    @RequestMapping(value = "goTicketFileDocumentsIndexPage", method = RequestMethod.GET)
    public ModelAndView goTicketFileDocumentsIndexPage(Long ticket_id, Long business_id, String business_type, boolean fileFlag) {
        ModelAndView view = new ModelAndView();
        /**这里根据实际的需要填写需要传到前台的对象**/
         view.addObject("business_type", "guarantee");
        /**这里是返回对应的页面路径**/

        view.setViewName("/sys/businessFile/businessFile/documents");
        return view;
    }


    /**
     * 业务附件批量保存
     **/
     @RequestMapping(value = "saveBusinessFileToGuarantee", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity saveBusinessFileToGuarantee(BusinessFileEntity entity) {
        try {
            entity = businessfileService.saveBusinessFileToGuarantee(entity);
            return getSuccessMessageEntity("保存成功！" );
        } catch (Exception e) {
            logger.error("save", e);
            e.printStackTrace();
            return getFailureMessageEntity("保存失败！" + e.getMessage(), "delete");
        }
    }

}

