com.fengduo.bee.web.controller.order.OrderController.java Source code

Java tutorial

Introduction

Here is the source code for com.fengduo.bee.web.controller.order.OrderController.java

Source

/*
 * Copyright 2015-2020 Fengduo.co All right reserved. This software is the confidential and proprietary information of
 * Fengduo.co ("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.co.
 */
package com.fengduo.bee.web.controller.order;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fengduo.bee.commons.cons.ResultCode;
import com.fengduo.bee.commons.core.lang.Argument;
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.commons.result.Result;
import com.fengduo.bee.model.cons.DelFlagEnum;
import com.fengduo.bee.model.cons.OrderStatusEnum;
import com.fengduo.bee.model.cons.OrderTypeEnum;
import com.fengduo.bee.model.cons.PayTypeEnum;
import com.fengduo.bee.model.cons.ProgressEnum;
import com.fengduo.bee.model.cons.SubHandleStatusEnum;
import com.fengduo.bee.model.cons.SubUserTypeEnum;
import com.fengduo.bee.model.cons.VerifyStatusEnum;
import com.fengduo.bee.model.entity.Item;
import com.fengduo.bee.model.entity.ItemFinance;
import com.fengduo.bee.model.entity.ItemFull;
import com.fengduo.bee.model.entity.PayOrder;
import com.fengduo.bee.model.entity.UserSub;
import com.fengduo.bee.web.controller.BaseController;
import com.fengduo.bee.web.shiro.ShiroDbRealm.ShiroUser;

/**
 * ?
 * 
 * @author jie.xu
 * @date 2015617 ?4:53:00
 */
@Controller
public class OrderController extends BaseController {

    /**
     * ????
     */
    @RequestMapping("/sub/check")
    @ResponseBody
    public JsonResult checkSub(Integer subType, Long itemId) {
        SubUserTypeEnum st = SubUserTypeEnum.getEnum(subType);
        if (st == null) {
            return JsonResultUtils.error("??");
        }
        Parameter query = Parameter.newParameter()//
                .pu("itemId", itemId)//
                .pu("userType", SubUserTypeEnum.CORNERSTONE_SUB.getValue())//
                .pu("handleStatus", SubHandleStatusEnum.DEPOSIT_SUCCESS_PAY.getValue());

        UserSub cornerstoneSub = orderService.findUserSub(query);
        Long userId = getCurrentUserId();
        // ????
        boolean flag = userService.isIdentity(userId);
        if (!flag) {
            return JsonResultUtils.buildJsonResult(ResultCode.NEED_IDENTITY, null, "???");
        }
        if (st == SubUserTypeEnum.CORNERSTONE_SUB) {
            // 
            if (cornerstoneSub == null) {
                return JsonResultUtils.success();
            } else {
                return JsonResultUtils.error("");
            }

        } else if (st == SubUserTypeEnum.FOLLOW_SUB) {
            // 
            if (cornerstoneSub == null) {
                return JsonResultUtils.error("");
            } else {
                return JsonResultUtils.success();
            }
        }
        return JsonResultUtils.error("??");
    }

    /**
     * 
     */
    @RequestMapping(value = "/sub/index")
    public String gotoSub(Long itemId, Integer subType, Model model) {
        if (itemId == null || subType == null) {
            return "redirect:/";
        }

        Parameter query = Parameter.newParameter()//
                .pu("itemId", itemId)//
                .pu("delFlag", DelFlagEnum.UN_DELETE.getValue());

        ItemFull itemFull = itemService.findItemFull(query);
        if (itemFull == null) {
            return "redirect:/";
        }

        float subProgress = (itemFull.getRealSub() / itemFull.getAmount()) * 100;
        model.addAttribute("itemFull", itemFull);
        model.addAttribute("subType", subType);
        model.addAttribute("subProgress", subProgress);
        return "sub/index";
    }

    /**
     * 
     * 
     * @param subType :1.2.
     * @param subAmount ?
     * @param advances ???
     * @param handlingCost 
     */
    @RequestMapping(value = "/sub/add", produces = "application/json", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult sub(Long itemId, Integer subType, Float subAmount, Float advances, Float handlingCost) {
        if (itemId == null || subType == null || subAmount == null) {
            return JsonResultUtils.error("????");
        }
        if (SubUserTypeEnum.getEnum(subType) == null) {
            return JsonResultUtils.error("????");
        }
        if (subType == SubUserTypeEnum.FOLLOW_SUB.getValue()) {
            if (advances == null) {
                return JsonResultUtils.error("???");
            }
        }

        ShiroUser currentUser = getCurrentUser();
        // ?????
        if (!currentUser.isIdentity()) {
            return JsonResultUtils.error("????");
        }

        Item item = itemService.getItemById(itemId);
        if (item == null) {
            return JsonResultUtils.error("?!");
        }
        // ??--
        if (!VerifyStatusEnum.isNormal(item.getVerifyStatus())) {
            return JsonResultUtils.error("!");
        }
        if (!ProgressEnum.isLegalSubProgress(item.getProgress(), subType)) {
            return JsonResultUtils.error("??");
        }
        ItemFinance itemFinance = itemService.getItemFinanceByItemId(itemId);
        if (itemFinance != null) {
            // ???
            if (itemFinance.isReachAmount()) {
                return JsonResultUtils.error("??");
            }
        }
        float fold = subAmount / itemFinance.getPerStock();
        if ((fold - Math.floor(fold)) > 0) {
            return JsonResultUtils.error("???");
        }
        if (subAmount >= itemFinance.getAmount()) {
            return JsonResultUtils.error("???");
        }
        if (subType == SubUserTypeEnum.CORNERSTONE_SUB.getValue()) {
            if (subAmount < itemFinance.getAmount() * 0.1) {
                return JsonResultUtils.error("????10%");
            }
        }

        // ?????
        UserSub existSub = orderService.findUnCloseSub(currentUser.getId(), itemId);
        if (existSub != null) {
            return JsonResultUtils.error("??");
        }
        // ?
        Parameter query = Parameter.newParameter()//
                .pu("itemId", itemId)//
                .pu("userType", SubUserTypeEnum.CORNERSTONE_SUB.getValue())//
                .pu("handleStatus", SubHandleStatusEnum.DEPOSIT_SUCCESS_PAY.getValue());

        UserSub cornerstoneSub = orderService.findUserSub(query);
        // ??
        Result subResult = createUserSub(cornerstoneSub, currentUser, subType, subAmount, advances, item,
                itemFinance);
        if (subResult.isFailed()) {
            return JsonResultUtils.error(subResult.getMessage());
        }

        Result orderResult = createOrder(item, currentUser, subAmount, handlingCost, subType);

        UserSub userSub = (UserSub) subResult.getData();
        PayOrder order = (PayOrder) orderResult.getData();
        boolean flag = orderService.insertSubAndOrder(userSub, order);
        if (flag) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("itemId", itemId);
            dataMap.put("orderId", order.getId());
            dataMap.put("subId", userSub.getId());
            return JsonResultUtils.success(dataMap);
        } else {
            return JsonResultUtils.error("");
        }
    }

    /**
     * ?
     */
    @RequestMapping("/sub/cancel")
    @ResponseBody
    public JsonResult cancelSub(Long subId, Integer subType, Long itemId) {
        boolean flag = false;
        UserSub sub = orderService.getUserSubById(subId);
        if (sub == null) {
            return JsonResultUtils.error();
        }
        Item item = itemService.getItemById(itemId);
        if (item == null) {
            return JsonResultUtils.error();
        }
        if (SubUserTypeEnum.isCornerstoneSub(subType)) {
            if (SubHandleStatusEnum.isDepositNotPay(sub.getHandleStatus())) {
                // ??
                flag = orderService.updateSub2Cancel(subId, getCurrentUserId(),
                        OrderStatusEnum.CANCEL_PAY.getValue());

            }
        } else if (SubUserTypeEnum.isFollowSub(subType)) {
            if (SubHandleStatusEnum.isDepositNotPay(sub.getHandleStatus())) {
                // ??
                flag = orderService.updateSub2Cancel(subId, getCurrentUserId(),
                        OrderStatusEnum.CANCEL_PAY.getValue());

            } else if (SubHandleStatusEnum.isAllNotPay(sub.getHandleStatus())) {
                // ??7?
                // 
                if (item.getProgress() == ProgressEnum.PRESELL.getValue()) {
                    if (sub.isInRepentTime()) {
                        List<PayOrder> existList = orderService.listRefundOrder(subId, getCurrentUserId());
                        if (existList == null || existList.isEmpty()) {

                            flag = orderService.updateSub2Cancel(subId, getCurrentUserId(),
                                    OrderStatusEnum.WAIT_REFUND.getValue());
                        }
                    }
                }
            }
        }
        if (flag) {
            return JsonResultUtils.success();
        } else {
            return JsonResultUtils.error();
        }
    }

    /**
     * 
     */
    private Result createUserSub(UserSub cornerstoneSub, ShiroUser currentUser, Integer subType, Float subAmount,
            Float advances, Item item, ItemFinance itemFinance) {
        Long itemId = item.getId();
        UserSub sub = new UserSub();

        if (subType == SubUserTypeEnum.CORNERSTONE_SUB.getValue()) {
            // 
            if (!item.isInCornerstoneTime()) {
                return Result.failed("?");
            }
            if (cornerstoneSub != null) {
                return Result.failed("");
            }
            sub.setAdvances(subAmount);
        } else if (subType == SubUserTypeEnum.FOLLOW_SUB.getValue()) {
            // 
            if (!item.isInPresell()) {
                return Result.failed("?");
            }
            if (cornerstoneSub == null) {
                return Result.failed("");
            }
            sub.setAdvances(advances);

        }
        sub.setItemId(itemId);
        sub.setUserId(currentUser.getId());
        sub.setUserType(subType);
        sub.setRealName(currentUser.getUser().getRealName());
        sub.setAvatar(currentUser.getUser().getAvatar());
        sub.setSubAmount(subAmount);
        sub.setPayStart(item.getPayAllStartDate(subType));
        sub.setHandleStatus(SubHandleStatusEnum.DEPOSIT_NOT_PAY.getValue());
        float percent = (subAmount / itemFinance.getPerStock()) * itemFinance.getPerPercent();
        sub.setPercent(percent);
        return Result.success(sub);
    }

    /**
     * ?
     */
    private Result createOrder(Item item, ShiroUser currentUser, Float subAmount, Float handlingCost,
            Integer subType) {
        PayOrder order = new PayOrder();
        order.setOrderNo(orderService.generateOrderNo());
        order.setItemId(item.getId());
        order.setUserId(currentUser.getId());
        order.setAmount(subAmount);
        order.setHandlingCost(handlingCost);
        order.setStatus(OrderStatusEnum.NON_PAY.getValue());
        order.setItemName(item.getName());
        order.setItemLogo(item.getImg());
        if (subType == SubUserTypeEnum.CORNERSTONE_SUB.getValue()) {
            order.setType(OrderTypeEnum.CASH_ALL.getValue());
            order.setSubUserType(SubUserTypeEnum.CORNERSTONE_SUB.getValue());
        } else if (subType == SubUserTypeEnum.FOLLOW_SUB.getValue()) {
            order.setType(OrderTypeEnum.CASH_DEPOSIT.getValue());
            order.setSubUserType(SubUserTypeEnum.FOLLOW_SUB.getValue());
        }

        return Result.success(order);
    }

    /**
     * ?
     * 
     * @return
     */
    @RequestMapping(value = "/gosub", method = RequestMethod.POST)
    public String sub() {

        // ?itemId??(x)
        // ???????(x)

        // ???,?(x)

        // ???????(x)

        // ????(x)

        // ?(1.2.)

        // 
        // ??(?)(x)
        // (????)(x)
        // 3????(????)
        // ??????(???,)
        // ps:--->?2
        // ??????
        // ????

        // 
        // ?(x)
        // ???,2(x)
        // (?(??)???(??))(x)
        // 3????(????)
        // ???????(???,)
        // ps:--->?2,???
        // ??????
        // ????

        // 
        // 
        // ?
        // ?????
        // ?(???)
        // ???????

        // ??
        // --> ?-->???-->???,??
        // ?????

        // ????Job
        // 60?????????
        //
        // ??--> ???

        // ?--> ???
        return "item/add";
    }

    /**
     * ?
     * 
     * @param itemId id
     * @param orderId ?id
     * @param subId id
     */
    @SuppressWarnings({ "unchecked", "deprecation" })
    @RequestMapping(value = "/order/index")
    public String gotoOrder(Long itemId, Long orderId, Long subId, Model model) {
        Result result = orderInfos(itemId, orderId, subId);
        if (result.isFailed()) {
            return "redirect:/?errMsg=" + URLEncoder.encode(result.getMessage());
        }
        model.addAllAttributes((Map<String, Object>) result.getData());
        return "order/index";

    }

    private Result orderInfos(Long itemId, Long orderId, Long subId) {
        if (Argument.isNotPositive(itemId) || Argument.isNotPositive(orderId) || Argument.isNotPositive(subId)) {
            return Result.failed("???");
        }
        Long userId = getCurrentUserId();
        Item item = itemService.getItemById(itemId);
        if (item == null) {
            return Result.failed("?");
        }
        UserSub sub = orderService.getUserSubById(subId);
        if (sub == null) {
            return Result.failed("?");
        }
        // orderIdsubId ?
        // ??
        // ???
        if (sub.getUserId().longValue() != userId.longValue()) {
            return Result.failed("??");
        }

        PayOrder order = orderService.getOrderById(orderId);
        if (order == null) {
            return Result.failed("??");
        }
        if (order.getUserId().longValue() != userId.longValue()) {
            return Result.failed("??");
        }
        if (!order.isInValidPayTime()) {
            return Result.failed("3");
        }
        if ((order.getStatus() != OrderStatusEnum.NON_PAY.getValue())
                && (order.getStatus() != OrderStatusEnum.FAIL_PAY.getValue())) {
            return Result.failed("??");
        }
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("item", item);
        model.put("sub", sub);
        model.put("order", order);
        return Result.success(model);
    }

    /**
     * 
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/order/offline")
    public String gotoOffline(Long itemId, Long orderId, Long subId, Model model) {
        Result result = orderInfos(itemId, orderId, subId);
        if (result.isFailed()) {
            return "redirect:/";
        }
        model.addAllAttributes((Map<String, Object>) result.getData());
        return "order/offline";
    }

    /**
     * <pre>
     * ??
     * ?
     * </pre>
     */
    @RequestMapping(value = "/order/offline/submit", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult offlineSubmit(Long orderId) {
        PayOrder order = orderService.getOrderById(orderId);
        if (order == null) {
            return JsonResultUtils.error("??");
        }
        if ((order.getStatus() != OrderStatusEnum.NON_PAY.getValue())
                && (order.getStatus() != OrderStatusEnum.FAIL_PAY.getValue())) {
            return JsonResultUtils.error("??");
        }

        PayOrder updateOrder = new PayOrder();
        updateOrder.setId(orderId);
        updateOrder.setPayType(PayTypeEnum.OFFLINE.getValue());
        boolean flag = orderService.updateOrderById(updateOrder);
        if (flag) {
            return JsonResultUtils.success();
        } else {
            return JsonResultUtils.error();
        }
    }

    /**
     * <pre>
     * 
     * 
     * </pre>
     */
    @RequestMapping("/order/payRemain")
    @ResponseBody
    public JsonResult payRemainAmount(Long subId, Long itemId) {
        if (Argument.isNotPositive(subId) || Argument.isNotPositive(itemId)) {
            return JsonResultUtils.error("???");
        }
        Item item = itemService.getItemById(itemId);
        if (item == null) {
            return JsonResultUtils.error("?");
        }
        UserSub sub = orderService.getUserSubById(subId);
        if (sub == null) {
            return JsonResultUtils.error("?");
        }
        if (!sub.isInPayRemainTime()) {
            return JsonResultUtils.error("?");
        }
        float subAmount = sub.getSubAmount() - sub.getAdvances();
        // TODO:jie.xu
        float handlingCost = subAmount * 0.005f;
        PayOrder order = createRemainOrder(item, getCurrentUserId(), subAmount, handlingCost);
        order = orderService.insertOrder(order);
        if (order.getId() > 0) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("itemId", itemId);
            map.put("orderId", order.getId());
            map.put("subId", subId);
            return JsonResultUtils.success(map);
        } else {
            return JsonResultUtils.error();
        }
    }

    private PayOrder createRemainOrder(Item item, Long userId, Float subAmount, Float handlingCost) {
        PayOrder order = new PayOrder();
        order.setOrderNo(orderService.generateOrderNo());
        order.setItemId(item.getId());
        order.setUserId(userId);
        order.setAmount(subAmount);
        order.setHandlingCost(handlingCost);
        order.setStatus(OrderStatusEnum.NON_PAY.getValue());
        order.setItemName(item.getName());
        order.setItemLogo(item.getImg());
        order.setType(OrderTypeEnum.CASH_ALL.getValue());
        order.setSubUserType(SubUserTypeEnum.FOLLOW_SUB.getValue());
        return order;
    }
}