com.fengduo.bee.web.controller.product.ProductController.java Source code

Java tutorial

Introduction

Here is the source code for com.fengduo.bee.web.controller.product.ProductController.java

Source

/*
 * Copyright 2015-2020 Fengduo.com All right reserved. This software is the confidential and proprietary information of
 * Fengduo.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with Fengduo.com.
 */
package com.fengduo.bee.web.controller.product;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fengduo.bee.commons.cons.ResultCode;
import com.fengduo.bee.commons.core.lang.Argument;
import com.fengduo.bee.commons.pagination.PaginationList;
import com.fengduo.bee.commons.pagination.PaginationParser.IPageUrl;
import com.fengduo.bee.commons.persistence.Parameter;
import com.fengduo.bee.commons.result.JsonResultUtils;
import com.fengduo.bee.commons.result.JsonResultUtils.JsonResult;
import com.fengduo.bee.model.cons.DelFlagEnum;
import com.fengduo.bee.model.cons.VerifyStatusEnum;
import com.fengduo.bee.model.dto.ItemDTO;
import com.fengduo.bee.model.entity.Item;
import com.fengduo.bee.model.entity.ItemComment;
import com.fengduo.bee.model.entity.ItemFinance;
import com.fengduo.bee.model.entity.ItemFull;
import com.fengduo.bee.model.entity.ItemMember;
import com.fengduo.bee.model.entity.User;
import com.fengduo.bee.model.entity.UserSub;
import com.fengduo.bee.web.controller.BaseController;
import com.fengduo.bee.web.shiro.ShiroDbRealm.ShiroUser;

/**
 * ?(),?
 * 
 * @author zxc May 28, 2015 11:54:13 PM
 */
@Controller
public class ProductController extends BaseController {

    /**
     * ?pdf ??
     */
    @RequestMapping("/download/check")
    @ResponseBody
    public JsonResult checkDownload(Long itemId) {
        if (Argument.isNotPositive(itemId)) {
            return JsonResultUtils.error("??,??!");
        }
        ShiroUser user = getCurrentUser();
        if (user == null) {
            return JsonResultUtils.buildJsonResult(ResultCode.NEED_LOGIN, null, "?");
        }
        if (!user.isIdentity()) {
            return JsonResultUtils.buildJsonResult(ResultCode.NEED_IDENTITY, null, "???");
        }

        Parameter query = Parameter.newParameter()//
                .pu("itemId", itemId)//
                .pu("verifyStatus", VerifyStatusEnum.NORMAL.getValue())//
                .pu("delFlag", DelFlagEnum.UN_DELETE.getValue());
        Item findItem = itemService.findItem(query);
        if (findItem == null) {
            return JsonResultUtils.error("??,??!");
        }
        return JsonResultUtils.success();
    }

    /**
     * ?pdf?
     * 
     * @return
     */
    @RequestMapping(value = "/item/{id}/downloadPdf")
    @ResponseBody
    public JsonResult downloadpdf(@PathVariable("id") Long id) {
        if (Argument.isNotPositive(id)) {
            return JsonResultUtils.error("??,??!");
        }
        ItemFinance itemFinance = itemService.getItemFinanceByItemId(id);
        if (itemFinance == null) {
            return JsonResultUtils.error("??,??!");
        }
        String url = itemFinance.getPdfUrl();
        return JsonResultUtils.success(url);
    }

    /**
     * pdf?
     * 
     * @param id
     * @return
     * @throws IOException
     */
    @RequestMapping("/item/{id}/download")
    public ResponseEntity<byte[]> download(@PathVariable("id") Long id) throws IOException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", ".pdf");
        String name = "";
        name = new String(name.getBytes(), "ISO8859-1");
        headers.set("content-disposition", "attachment;filename=" + name + ".pdf");

        if (Argument.isNotPositive(id)) {
            return new ResponseEntity<byte[]>(null, headers, HttpStatus.CREATED);
        }
        ItemFinance itemFinance = itemService.getItemFinanceByItemId(id);
        if (itemFinance == null) {
            return new ResponseEntity<byte[]>(null, headers, HttpStatus.CREATED);
        }
        String url = itemFinance.getPdfUrl();
        File file = fileService.getFile(url);
        if (file == null) {
            return new ResponseEntity<byte[]>(null, headers, HttpStatus.CREATED);
        }

        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
    }

    /**
     * ?(?)
     * 
     * @return
     */
    @RequestMapping(value = "/projects")
    public ModelAndView list(ModelAndView mav, final ItemDTO itemDTO) {
        mav.setViewName("product/list");

        Parameter query = Parameter.newParameter()// 
                .pu("stage", itemDTO.getStage())// ?
                .pu("progress", itemDTO.getProgress())// 
                .pu("tags", itemDTO.getTag())// ?
                .pu("verifyStatus", VerifyStatusEnum.NORMAL.getValue())// ?
                .pu("delFlag", DelFlagEnum.UN_DELETE.getValue())// ?
                .pu("page", itemDTO.getPage());

        PaginationList<ItemFull> listPaginationItemFull = itemService.listPaginationItemFull(query, new IPageUrl() {

            @Override
            public String parsePageUrl(Object... objs) {
                StringBuffer sf = new StringBuffer("/projects?page=" + (Integer) objs[1]);
                if (itemDTO.getTag() != null) {
                    sf.append("&tag=" + itemDTO.getTag());
                }
                if (itemDTO.getProgress() != null) {
                    sf.append("&progress=" + itemDTO.getProgress());
                }
                if (itemDTO.getStage() != null) {
                    sf.append("&stage=" + itemDTO.getStage());
                }
                return sf.toString();
            }
        });
        mav.addObject("itemFullList", listPaginationItemFull);
        mav.addObject("tag", itemDTO.getTag());
        mav.addObject("progress", itemDTO.getProgress());
        mav.addObject("stage", itemDTO.getStage());
        return mav;
    }

    /**
     * ?
     * 
     * @return
     */
    @RequestMapping(value = "/{id}/project")
    public ModelAndView detail(@PathVariable("id") Long id, ModelAndView mav) {
        mav.setViewName("product/detail");
        if (Argument.isNotPositive(id)) {
            return mav;
        }

        Parameter query = Parameter.newParameter().pu("itemId", id);
        // ????
        ItemFull itemFull = itemService.findItemFull(query);
        if (itemFull == null) {
            return mav;
        }
        // ??
        List<ItemMember> itemMemberList = itemService.getItemMemberByItemId(id);
        // ??
        User itemUser = userService.getUserById(itemFull.getUserId());

        mav.addObject("itemFull", itemFull);
        mav.addObject("itemMemberList", itemMemberList);
        mav.addObject("itemUser", itemUser);
        mav.addObject("id", id);
        return mav;
    }

    /**
     * ?
     * 
     * @return
     */
    @RequestMapping(value = "/{id}/topic")
    public ModelAndView topic(@PathVariable("id") Long id, ModelAndView mav) {
        mav.setViewName("product/topic");
        if (Argument.isNotPositive(id)) {
            return mav;
        }

        Parameter query = Parameter.newParameter().pu("itemId", id);
        List<ItemComment> listItemComment = itemService.listItemComment(query);
        ItemFull itemFull = itemService.findItemFull(query);
        if (itemFull == null) {
            return mav;
        }
        User itemUser = userService.getUserById(itemFull.getUserId());

        mav.addObject("itemFull", itemFull);
        mav.addObject("itemUser", itemUser);
        mav.addObject("listItemComment", listItemComment);
        mav.addObject("id", id);
        return mav;
    }

    /**
     * ?
     * 
     * @return
     */
    @RequestMapping(value = "/{id}/investor")
    public ModelAndView investorList(@PathVariable("id") Long id, ModelAndView mav) {
        mav.setViewName("product/investor");
        if (Argument.isNotPositive(id)) {
            return mav;
        }

        Parameter query = Parameter.newParameter().pu("itemId", id);
        ItemFull itemFull = itemService.findItemFull(query);
        if (itemFull == null) {
            return mav;
        }
        List<UserSub> listUserSub = orderService.listUserSub(query);
        User itemUser = userService.getUserById(itemFull.getUserId());

        mav.addObject("itemFull", itemFull);
        mav.addObject("itemUser", itemUser);
        mav.addObject("listUserSub", listUserSub);
        mav.addObject("id", id);
        return mav;
    }
}