package com.slodon.b2b2c.controller.business.front;

import cn.hutool.core.date.DateUtil;
import com.slodon.b2b2c.business.example.*;
import com.slodon.b2b2c.business.pojo.*;
import com.slodon.b2b2c.core.constant.*;
import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PageVO;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.*;
import com.slodon.b2b2c.goods.pojo.Goods;
import com.slodon.b2b2c.member.pojo.Member;
import com.slodon.b2b2c.model.business.*;
import com.slodon.b2b2c.model.goods.GoodsModel;
import com.slodon.b2b2c.model.promotion.*;
import com.slodon.b2b2c.model.seller.StoreModel;
import com.slodon.b2b2c.promotion.example.LadderGroupOrderExtendExample;
import com.slodon.b2b2c.promotion.example.PresellOrderExtendExample;
import com.slodon.b2b2c.promotion.example.SpellTeamMemberExample;
import com.slodon.b2b2c.promotion.pojo.*;
import com.slodon.b2b2c.seller.pojo.Store;
import com.slodon.b2b2c.vo.business.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author slodon
 */
@Api(tags = "front-订单管理")
@RestController
@RequestMapping("v3/business/front/orderInfo")
public class FrontOrderInfoController extends BaseController {

    @Resource
    private OrderModel orderModel;
    @Resource
    private OrderExtendModel orderExtendModel;
    @Resource
    private OrderProductModel orderProductModel;
    @Resource
    private OrderLogModel orderLogModel;
    @Resource
    private OrderPayModel orderPayModel;
    @Resource
    private OrderReturnModel orderReturnModel;
    @Resource
    private OrderReserveModel orderReserveModel;
    @Resource
    private StoreModel storeModel;
    @Resource
    private PresellOrderExtendModel presellOrderExtendModel;
    @Resource
    private LadderGroupOrderExtendModel ladderGroupOrderExtendModel;
    @Resource
    private LadderGroupModel ladderGroupModel;
    @Resource
    private GoodsModel goodsModel;
    @Resource
    private SpellTeamModel spellTeamModel;
    @Resource
    private SpellTeamMemberModel spellTeamMemberModel;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation("时间列表")
    @GetMapping("timeList")
    public JsonResult<List<TimeVO>> getTimeList() {
        List<TimeVO> list = new ArrayList<>();
        list.add(new TimeVO(0, "全部订单"));
        list.add(new TimeVO(1, "近3个月订单"));
        list.add(new TimeVO(2, "今年内订单"));
        //获取当前年份
        int year = DateUtil.year(new Date());
        list.add(new TimeVO(year - 1, year - 1 + "年订单"));
        list.add(new TimeVO(year - 2, year - 2 + "年订单"));
        list.add(new TimeVO(year - 3, year - 3 + "年订单"));
        list.add(new TimeVO(3, year - 3 + "年以前订单"));
        return SldResponse.success(list);
    }

    @ApiOperation("订单列表相关接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "订单号/商品名称/店铺名称", paramType = "query"),
            @ApiImplicitParam(name = "orderState", value = "订单状态：0-已取消；10-待付款订单；20-代发货订单；30-待收货订单；40-已完成;50-已关闭", paramType = "query"),
            @ApiImplicitParam(name = "evaluateState", value = "订单评价状态：1.未评价", paramType = "query"),
            @ApiImplicitParam(name = "time", value = "时间：1-近3个月订单；2-今年内订单；3-xx年以前订单", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("list")
    public JsonResult<PageVO<MemberOrderListVO>> getList(HttpServletRequest request, String keyword, Integer orderState, Integer evaluateState, Integer time) {
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        Member member = UserUtil.getUser(request, Member.class);

        List<MemberOrderListVO> vos = new ArrayList<>();
        //根据父订单分组
        String fields = "parent_sn";
        OrderExample orderExample = new OrderExample();
        orderExample.setOrderState(orderState);
        orderExample.setKeyword(keyword);
        orderExample.setEvaluateState(evaluateState);
        orderExample.setMemberId(member.getMemberId());
        orderExample.setGroupBy(fields);
        orderExample.setOrderBy(fields + " desc");
        orderExample.setDeleteState(OrderConst.DELETE_STATE_0);
        if (!StringUtil.isNullOrZero(time)) {
            if (time.equals(OrderConst.TIME_1)) {
                //近3个月订单
                orderExample.setCreateTimeAfter(DateUtil.offsetMonth(new Date(), -3));
            } else if (time.equals(OrderConst.TIME_2)) {
                //今年内订单
                orderExample.setCreateTimeAfter(DateUtil.beginOfYear(new Date()));
            } else if (time.equals(OrderConst.TIME_3)) {
                //获取当前年份
                int year = DateUtil.year(new Date());
                orderExample.setCreateTimeBefore(TimeUtil.getYearStartTime(year - 3));
            } else {
                //当年订单
                orderExample.setCreateTimeAfter(TimeUtil.getYearStartTime(time));
                orderExample.setCreateTimeBefore(TimeUtil.getYearEndTime(time));
            }
        }
        List<Order> orderListParent = orderModel.getOrderFieldList(fields, orderExample, pager);
        if (CollectionUtils.isEmpty(orderListParent)) {
            return SldResponse.success(new PageVO<>(vos, pager));
        }

        //遍历父订单列表
        for (Order orderParent : orderListParent) {
            OrderExample orderExample1 = new OrderExample();
            orderExample1.setParentSn(orderParent.getParentSn());
            orderExample1.setOrderState(orderState);
            //获取子订单列表
            List<Order> orderListChild = orderModel.getOrderList(orderExample1, null);
            if (!orderListChild.get(0).getOrderState().equals(OrderConst.ORDER_STATE_10) && !StringUtils.isEmpty(keyword)) {
                //不是待支付的，按照条件过滤
                orderExample1.setKeyword(keyword);
                orderListChild = orderModel.getOrderList(orderExample1, null);
            }

            List<Order> showList = new ArrayList<>();
            //合计金额
            BigDecimal totalMoney = BigDecimal.ZERO;
            //活动价
            BigDecimal activityPrice = BigDecimal.ZERO;
            //未付款未拆单共计商品数
            Integer goodsNum = 0;
            //订单子状态
            Integer orderSubState = 0;
            //定金剩余时间
            Date depositRemainTime = null;
            //尾款开始时间
            Date remainTime = null;
            //预售发货时间
            Date deliverTime = null;
            //是否有定金
            boolean isHasDeposit = false;
            //是否退还定金
            boolean isRefundDeposit = false;
            //遍历子订单
            for (Order orderChild : orderListChild) {
                if (!orderChild.getOrderState().equals(OrderConst.ORDER_STATE_10)) {
                    //已付款订单共计商品数
                    Integer productNum = 0;
                    //查询预售订单扩展信息
                    if (orderChild.getOrderType() == PromotionConst.PROMOTION_TYPE_103) {
                        PresellOrderExtendExample presellOrderExtendExample = new PresellOrderExtendExample();
                        presellOrderExtendExample.setOrderSn(orderChild.getOrderSn());
                        List<PresellOrderExtend> preSellOrderExtendList = presellOrderExtendModel.getPresellOrderExtendList(presellOrderExtendExample, null);
                        AssertUtil.notEmpty(preSellOrderExtendList, "获取预售订单扩展信息为空");
                        PresellOrderExtend presellOrderExtend = preSellOrderExtendList.get(0);
                        orderSubState = presellOrderExtend.getOrderSubState();
                        if (presellOrderExtend.getIsAllPay() == OrderConst.IS_ALL_PAY_0) {
                            depositRemainTime = presellOrderExtend.getDepositEndTime();
                            remainTime = presellOrderExtend.getRemainStartTime();
                            isHasDeposit = true;
                        }
                        deliverTime = presellOrderExtend.getDeliverTime();
                        activityPrice = presellOrderExtend.getPresellPrice();
                    }
                    //查询阶梯团订单扩展信息
                    if (orderChild.getOrderType() == PromotionConst.PROMOTION_TYPE_105) {
                        LadderGroupOrderExtendExample groupOrderExtendExample = new LadderGroupOrderExtendExample();
                        groupOrderExtendExample.setOrderSn(orderChild.getOrderSn());
                        List<LadderGroupOrderExtend> groupOrderExtendList = ladderGroupOrderExtendModel.getLadderGroupOrderExtendList(groupOrderExtendExample, null);
                        AssertUtil.notEmpty(groupOrderExtendList, "获取阶梯团订单扩展信息为空");
                        LadderGroupOrderExtend groupOrderExtend = groupOrderExtendList.get(0);
                        orderSubState = groupOrderExtend.getOrderSubState();
                        remainTime = groupOrderExtend.getRemainStartTime();
                        isHasDeposit = true;
                        //查询是否退还定金
                        LadderGroup ladderGroup = ladderGroupModel.getLadderGroupByGroupId(groupOrderExtend.getGroupId());
                        AssertUtil.notNull(ladderGroup, "获取阶梯团活动信息为空，请重试！");
                        if (ladderGroup.getIsRefundDeposit() == LadderGroupConst.IS_REFUND_DEPOSIT_1) {
                            isRefundDeposit = true;
                        }
                        activityPrice = groupOrderExtend.getProductPrice();
                    }
                    MemberOrderListVO vo = new MemberOrderListVO(orderChild, orderSubState, remainTime, isHasDeposit);
                    vo.setIsRefundDeposit(isRefundDeposit);
                    //拼团订单
                    if (orderChild.getOrderType() == PromotionConst.PROMOTION_TYPE_102) {
                        SpellTeamMemberExample teamMemberExample = new SpellTeamMemberExample();
                        teamMemberExample.setOrderSn(orderChild.getOrderSn());
                        List<SpellTeamMember> teamMemberList = spellTeamMemberModel.getSpellTeamMemberList(teamMemberExample, null);
                        AssertUtil.notEmpty(teamMemberList, "获取拼团成员信息为空");
                        //查询拼团团队状态
                        SpellTeam spellTeam = spellTeamModel.getSpellTeamBySpellTeamId(teamMemberList.get(0).getSpellTeamId());
                        AssertUtil.notNull(spellTeam, "获取拼团团队信息为空，请重试");
                        //拼团没有成功就不显示待发货
                        if (spellTeam.getState() == SpellConst.SPELL_GROUP_STATE_1 && orderChild.getOrderState() == OrderConst.ORDER_STATE_20) {
                            vo.setOrderStateValue("已付款,待成团");
                        }
                    }
                    if (orderChild.getOrderType() == PromotionConst.PROMOTION_TYPE_103) {
                        if (depositRemainTime != null) {
                            long time1 = depositRemainTime.getTime();
                            long time2 = System.currentTimeMillis();
                            long differTime = (time1 - time2) / 1000;
                            vo.setDepositRemainTime(differTime < 0 ? 0 : differTime);
                            long time3 = remainTime.getTime();
                            long remainEndTime = (time3 - time2) / 1000;
                            vo.setRemainEndTime(remainEndTime < 0 ? 0 : remainEndTime);
                        }
                        if (!isHasDeposit && orderChild.getOrderState() == OrderConst.ORDER_STATE_10) {
                            //全款预售订单状态展示
                            vo.setOrderStateValue("待付款");
                        }
                    }
                    vo.setDeliverTime(TimeUtil.getZDDay(deliverTime));
                    vo.setOrderSubState(orderSubState);
                    //获取商家名称
                    vo.setStoreId(orderChild.getStoreId());
                    vo.setStoreName(orderChild.getStoreName());
                    Store store = storeModel.getStoreByStoreId(orderChild.getStoreId());
                    //默认店铺logo
                    if (StringUtils.isEmpty(store.getStoreLogo())) {
                        store.setStoreLogo(stringRedisTemplate.opsForValue().get("default_image_store_logo"));
                    }
                    vo.setStoreLogo(FileUrlUtil.getFileUrl(store.getStoreLogo(), null));
                    vo.setTotalMoney(orderChild.getOrderAmount());
                    //获取订单货品列表信息
                    OrderProductExample orderProductExample = new OrderProductExample();
                    orderProductExample.setOrderSn(orderChild.getOrderSn());
                    orderProductExample.setOrderBy("order_product_id ASC");
                    List<OrderProduct> orderProductList = orderProductModel.getOrderProductList(orderProductExample, null);
                    List<OrderProductListVO> productListVos = new ArrayList<>();
                    if (!CollectionUtils.isEmpty(orderProductList)) {
                        for (OrderProduct orderProduct : orderProductList) {
                            //有退货,查询退货是否已完成
                            OrderReturnExample returnExample = new OrderReturnExample();
                            returnExample.setOrderSn(orderProduct.getOrderSn());
                            returnExample.setOrderProductId(orderProduct.getOrderProductId());
                            List<OrderReturn> returnList = orderReturnModel.getOrderReturnList(returnExample, null);
                            if (!CollectionUtils.isEmpty(returnList)) {
                                OrderReturn orderReturn = returnList.get(0);
                                orderProduct.setAfsSn(orderReturn.getAfsSn());
                                orderProduct.setAfsState(orderReturn.getState());
                                if (orderReturn.getState().equals(OrdersAfsConst.RETURN_STATE_300)) {
                                    //退款完成
                                    orderProduct.setAfsButton(OrderProductConst.AFS_BUTTON_402);
                                } else {
                                    //没有退款完成的，展示退款中
                                    orderProduct.setAfsButton(OrderProductConst.AFS_BUTTON_401);
                                }
                            }
                            OrderProductListVO orderProductListVO = new OrderProductListVO(orderProduct);
                            if (!StringUtil.isNullOrZero(activityPrice)) {
                                orderProductListVO.setProductShowPrice(activityPrice);
                            }
                            productListVos.add(orderProductListVO);
                            //合计商品数
                            productNum += orderProductListVO.getProductNum();
                        }
                    }
                    vo.setGoodsNum(productNum);
                    vo.setOrderProductListVOList(productListVos);
                    vos.add(vo);
                } else {
                    //获取订单货品列表信息
                    OrderProductExample orderProductExample = new OrderProductExample();
                    orderProductExample.setOrderSn(orderChild.getOrderSn());
                    orderProductExample.setOrderBy("order_product_id ASC");
                    List<OrderProduct> orderProductList = orderProductModel.getOrderProductList(orderProductExample, null);
                    orderChild.setOrderProductList(orderProductList);
                    showList.add(orderChild);
                }
            }
            if (!CollectionUtils.isEmpty(showList)) {
                Order orderDb = showList.get(0);
                //查询预售订单扩展信息
                if (orderDb.getOrderType() == PromotionConst.PROMOTION_TYPE_103) {
                    PresellOrderExtendExample presellOrderExtendExample = new PresellOrderExtendExample();
                    presellOrderExtendExample.setOrderSn(orderDb.getOrderSn());
                    List<PresellOrderExtend> preSellOrderExtendList = presellOrderExtendModel.getPresellOrderExtendList(presellOrderExtendExample, null);
                    AssertUtil.notEmpty(preSellOrderExtendList, "获取预售订单扩展信息为空");
                    PresellOrderExtend presellOrderExtend = preSellOrderExtendList.get(0);
                    orderSubState = presellOrderExtend.getOrderSubState();
                    if (presellOrderExtend.getIsAllPay() == OrderConst.IS_ALL_PAY_0) {
                        depositRemainTime = presellOrderExtend.getDepositEndTime();
                        remainTime = presellOrderExtend.getRemainStartTime();
                        isHasDeposit = true;
                    }
                    deliverTime = presellOrderExtend.getDeliverTime();
                    activityPrice = presellOrderExtend.getPresellPrice();
                }
                //查询阶梯团订单扩展信息
                if (orderDb.getOrderType() == PromotionConst.PROMOTION_TYPE_105) {
                    LadderGroupOrderExtendExample groupOrderExtendExample = new LadderGroupOrderExtendExample();
                    groupOrderExtendExample.setOrderSn(orderDb.getOrderSn());
                    List<LadderGroupOrderExtend> groupOrderExtendList = ladderGroupOrderExtendModel.getLadderGroupOrderExtendList(groupOrderExtendExample, null);
                    AssertUtil.notEmpty(groupOrderExtendList, "获取阶梯团订单扩展信息为空");
                    LadderGroupOrderExtend groupOrderExtend = groupOrderExtendList.get(0);
                    orderSubState = groupOrderExtend.getOrderSubState();
                    remainTime = groupOrderExtend.getRemainStartTime();
                    isHasDeposit = true;
                    //查询是否退还定金
                    LadderGroup ladderGroup = ladderGroupModel.getLadderGroupByGroupId(groupOrderExtend.getGroupId());
                    AssertUtil.notNull(ladderGroup, "获取阶梯团活动信息为空，请重试！");
                    if (ladderGroup.getIsRefundDeposit() == LadderGroupConst.IS_REFUND_DEPOSIT_1) {
                        isRefundDeposit = true;
                    }
                    activityPrice = groupOrderExtend.getProductPrice();
                }
                MemberOrderListVO vo = new MemberOrderListVO(orderDb, orderSubState, remainTime, isHasDeposit);
                vo.setIsRefundDeposit(isRefundDeposit);
                //拼团订单
                if (orderDb.getOrderType() == PromotionConst.PROMOTION_TYPE_102) {
                    SpellTeamMemberExample teamMemberExample = new SpellTeamMemberExample();
                    teamMemberExample.setOrderSn(orderDb.getOrderSn());
                    List<SpellTeamMember> teamMemberList = spellTeamMemberModel.getSpellTeamMemberList(teamMemberExample, null);
                    AssertUtil.notEmpty(teamMemberList, "获取拼团成员信息为空");
                    //查询拼团团队状态
                    SpellTeam spellTeam = spellTeamModel.getSpellTeamBySpellTeamId(teamMemberList.get(0).getSpellTeamId());
                    AssertUtil.notNull(spellTeam, "获取拼团团队信息为空，请重试");
                    //拼团没有成功就不显示待发货
                    if (spellTeam.getState() == SpellConst.SPELL_GROUP_STATE_1 && orderDb.getOrderState() == OrderConst.ORDER_STATE_20) {
                        vo.setOrderStateValue("已付款,待成团");
                    }
                }
                if (orderDb.getOrderType() == PromotionConst.PROMOTION_TYPE_103) {
                    if (depositRemainTime != null) {
                        long time1 = depositRemainTime.getTime();
                        long time2 = System.currentTimeMillis();
                        long differTime = (time1 - time2) / 1000;
                        vo.setDepositRemainTime(differTime < 0 ? 0 : differTime);
                        long time3 = remainTime.getTime();
                        long remainEndTime = (time3 - time2) / 1000;
                        vo.setRemainEndTime(remainEndTime < 0 ? 0 : remainEndTime);
                    }
                }
                vo.setDeliverTime(TimeUtil.getZDDay(deliverTime));
                vo.setOrderSubState(orderSubState);
                List<OrderProductListVO> productListVos = new ArrayList<>();

                if (showList.size() > 1) {
                    //获取商家名称
                    vo.setStoreId(0L);
                    vo.setStoreName(stringRedisTemplate.opsForValue().get("basic_site_name"));

                    ArrayList<OrderProduct> orderProducts = new ArrayList<>();
                    for (Order order : showList) {
                        orderProducts.addAll(order.getOrderProductList());
                        //获取合计金额
                        totalMoney = totalMoney.add(order.getOrderAmount());
                    }
                    //合计金额
                    vo.setTotalMoney(totalMoney);
                    //获取订单货品列表信息
                    for (OrderProduct orderProduct : orderProducts) {
                        OrderProductListVO orderProductListVO = new OrderProductListVO(orderProduct);
                        if (!StringUtil.isNullOrZero(activityPrice)) {
                            orderProductListVO.setProductShowPrice(activityPrice);
                        }
                        productListVos.add(orderProductListVO);
                        //获取共计商品数
                        goodsNum += orderProduct.getProductNum();
                    }
                    //共计商品数
                    vo.setGoodsNum(goodsNum);
                    //订单货品列表信息
                    vo.setOrderProductListVOList(productListVos);
                    vos.add(vo);
                } else {
                    vo.setStoreId(orderDb.getStoreId());
                    vo.setStoreName(orderDb.getStoreName());
                    Store store = storeModel.getStoreByStoreId(orderDb.getStoreId());
                    //默认店铺logo
                    if (StringUtils.isEmpty(store.getStoreLogo())) {
                        store.setStoreLogo(stringRedisTemplate.opsForValue().get("default_image_store_logo"));
                    }
                    vo.setStoreLogo(FileUrlUtil.getFileUrl(store.getStoreLogo(), null));
                    //合计金额
                    vo.setTotalMoney(orderDb.getOrderAmount());
                    //获取订单货品列表信息
                    List<OrderProduct> orderProductList = orderDb.getOrderProductList();
                    for (OrderProduct orderProduct : orderProductList) {
                        OrderProductListVO orderProductListVO = new OrderProductListVO(orderProduct);
                        if (!StringUtil.isNullOrZero(activityPrice)) {
                            orderProductListVO.setProductShowPrice(activityPrice);
                        }
                        productListVos.add(orderProductListVO);
                        //共计商品数
                        goodsNum += orderProduct.getProductNum();
                    }
                    vo.setGoodsNum(goodsNum);
                    vo.setOrderProductListVOList(productListVos);
                    vos.add(vo);
                }
            }
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("获取订单详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderSn", value = "订单号", required = true)
    })
    @PostMapping("detail")
    public JsonResult<MemberOrderDetailVO> getOrderDetail(HttpServletRequest request, @RequestParam("orderSn") String orderSn) {
        Member member = UserUtil.getUser(request, Member.class);

        Order order = orderModel.getOrdersWithOpByOrderSn(orderSn);
        AssertUtil.notNull(order, "无此订单");
        AssertUtil.isTrue(!member.getMemberId().equals(order.getMemberId()), "您无权操作此订单");

        //获取订单扩展表信息
        OrderExtendExample orderExtendExample = new OrderExtendExample();
        orderExtendExample.setOrderSn(order.getOrderSn());
        OrderExtend orderExtend = orderExtendModel.getOrderExtendList(orderExtendExample, null).get(0);

        //获取订单支付信息
        OrderPayExample orderPayExample = new OrderPayExample();
        orderPayExample.setPaySn(order.getPaySn());
        OrderPay orderPay = orderPayModel.getOrderPayList(orderPayExample, null).get(0);

        MemberOrderDetailVO orderVO = new MemberOrderDetailVO(order, orderExtend, orderPay);
        orderVO.setOrderTypeValue(MemberOrderListVO.dealOrderTypeValue(order.getOrderType(), false));
        //获取会员邮箱信息
        orderVO.setMemberEmail(member.getMemberEmail());

        //获取订单日志信息
        OrderLogExample orderLogExample = new OrderLogExample();
        orderLogExample.setOrderSn(order.getOrderSn());
        orderLogExample.setOrderStateLogNotEquals(-1);
        orderLogExample.setOrderBy("log_time asc");
        List<OrderLog> orderLogList = orderLogModel.getOrderLogList(orderLogExample, null);
        orderVO.setOrderLogs(orderLogList);

        //虚拟商品查询预留信息
        if (order.getIsVirtualGoods() == GoodsConst.IS_VIRTUAL_GOODS_2) {
            OrderReserveExample example = new OrderReserveExample();
            example.setOrderSn(orderSn);
            example.setOrderBy("reserve_id asc");
            List<OrderReserve> orderReserveList = orderReserveModel.getOrderReserveList(example, null);
            orderVO.setOrderReserveList(orderReserveList);
        }

        //总运费
        BigDecimal totalExpress = BigDecimal.ZERO;
        //实付款(含运费)
        BigDecimal actualPayment = BigDecimal.ZERO;

        ArrayList<ChildOrdersVO> childOrdersVos = new ArrayList<>();
        //判断该订单的状态
        if (!order.getOrderState().equals(OrderConst.ORDER_STATE_10)) {
            //售后按钮展示
            dealAfsButton(order);

            //查询店铺logo
            Store store = storeModel.getStoreByStoreId(order.getStoreId());
            AssertUtil.notNull(store, "店铺不存在");
            //默认店铺logo
            if (StringUtil.isEmpty(store.getStoreLogo())) {
                store.setStoreLogo(stringRedisTemplate.opsForValue().get("default_image_store_logo"));
            }
            ChildOrdersVO childOrdersVO = new ChildOrdersVO(order, orderExtend, store.getStoreLogo());
            childOrdersVO.setServicePhone(store.getServicePhone());
            //获取货品信息
            List<OrderProductListVO> productListVos = new ArrayList<>();
            if (!CollectionUtils.isEmpty(order.getOrderProductList())) {
                for (OrderProduct orderProduct : order.getOrderProductList()) {
                    OrderProductListVO orderProductListVO = new OrderProductListVO(orderProduct);
                    productListVos.add(orderProductListVO);
                }
            }
            childOrdersVO.setOrderProductListVOList(productListVos);

            //实付款(含运费)
            actualPayment = actualPayment.add(order.getOrderAmount());
            orderVO.setActualPayment(actualPayment);
            //总运费
            totalExpress = totalExpress.add(order.getExpressFee());
            orderVO.setTotalExpress(totalExpress);
            //商品总额
            orderVO.setTotalMoney(order.getGoodsAmount());

            childOrdersVos.add(childOrdersVO);
            orderVO.setChildOrdersVOS(childOrdersVos);
        } else {
            //获得该订单号的父订单号下的所有子订单
            OrderExample orderExample = new OrderExample();
            orderExample.setParentSn(order.getParentSn());
            List<Order> orderList = orderModel.getOrderList(orderExample, null);
            //订单列表大于1说明是多个店铺
            if (orderList.size() > 1) {
                orderVO.setIsManyStore(true);
            }

            //商品总金额
            BigDecimal goodsAmount = BigDecimal.ZERO;
            //店铺优惠券优惠金额
            BigDecimal storeVoucherAmount = orderExtend.getStoreVoucherAmount();
            //平台优惠券优惠金额
            BigDecimal platformVoucherAmount = orderExtend.getPlatformVoucherAmount();
            //满优惠金额
            BigDecimal fullDiscountAmount = orderVO.dealFullDiscountAmount(orderExtend.getPromotionInfo());
            //积分抵扣金额
            BigDecimal integralCashAmount = order.getIntegralCashAmount();
            //遍历所有子订单
            for (Order orderChild : orderList) {
                if (!orderChild.getOrderSn().equals(orderSn)) {
                    //获取订单扩展表信息
                    OrderExtendExample extendExample = new OrderExtendExample();
                    extendExample.setOrderSn(orderChild.getOrderSn());
                    OrderExtend extend = orderExtendModel.getOrderExtendList(extendExample, null).get(0);

                    storeVoucherAmount = storeVoucherAmount.add(extend.getStoreVoucherAmount());
                    platformVoucherAmount = platformVoucherAmount.add(extend.getPlatformVoucherAmount());
                    fullDiscountAmount = fullDiscountAmount.add(orderVO.dealFullDiscountAmount(extend.getPromotionInfo()));
                    integralCashAmount = integralCashAmount.add(orderChild.getIntegralCashAmount());
                }
                //查询店铺logo
                Store store = storeModel.getStoreByStoreId(orderChild.getStoreId());
                AssertUtil.notNull(store, "店铺不存在");
                //默认店铺logo
                if (StringUtil.isEmpty(store.getStoreLogo())) {
                    store.setStoreLogo(stringRedisTemplate.opsForValue().get("default_image_store_logo"));
                }
                ChildOrdersVO childOrdersVO = new ChildOrdersVO(orderChild, orderExtend, store.getStoreLogo());
                childOrdersVO.setServicePhone(store.getServicePhone());

                //获取货品信息
                OrderProductExample orderProductExample = new OrderProductExample();
                orderProductExample.setOrderSn(orderChild.getOrderSn());
                orderProductExample.setOrderBy("order_product_id ASC");
                List<OrderProduct> orderProductList = orderProductModel.getOrderProductList(orderProductExample, null);
                List<OrderProductListVO> productListVOS = new ArrayList<>();
                if (!CollectionUtils.isEmpty(orderProductList)) {
                    orderChild.setOrderProductList(orderProductList);

                    //售后按钮展示
                    dealAfsButton(orderChild);

                    for (OrderProduct orderProduct : orderProductList) {
                        OrderProductListVO orderProductListVO = new OrderProductListVO(orderProduct);
                        productListVOS.add(orderProductListVO);

                    }
                }
                childOrdersVO.setOrderProductListVOList(productListVOS);

                //实付款(含运费)
                actualPayment = actualPayment.add(orderChild.getOrderAmount());

                //总运费
                totalExpress = totalExpress.add(orderChild.getExpressFee());

                //商品总额
                goodsAmount = goodsAmount.add(orderChild.getGoodsAmount());

                childOrdersVos.add(childOrdersVO);
            }
            orderVO.setActualPayment(actualPayment);
            orderVO.setTotalExpress(totalExpress);
            orderVO.setTotalMoney(goodsAmount);
            orderVO.setChildOrdersVOS(childOrdersVos);
            orderVO.setStoreVoucherAmount(storeVoucherAmount);
            orderVO.setPlatformVoucherAmount(platformVoucherAmount);
            orderVO.setFullDiscountAmount(fullDiscountAmount);
            orderVO.setIntegralCashAmount(integralCashAmount);
        }
        if (order.getOrderState() == OrderConst.ORDER_STATE_30) {
            //待收货状态，计算自动收货时间
            String autoReceive = stringRedisTemplate.opsForValue().get("time_limit_of_auto_receive");
            int autoReceiveDay = StringUtil.isEmpty(autoReceive) ? 7 : Integer.parseInt(autoReceive);
            //计算时间
            Date autoReceiveTime = TimeUtil.getDayAgoDate(orderExtend.getDeliverTime(), autoReceiveDay);
            orderVO.setAutoReceiveTime(autoReceiveTime);
        }
        //拼团订单倒计时（秒）
        if (order.getOrderType() == PromotionConst.PROMOTION_TYPE_102 && order.getOrderState() == OrderConst.ORDER_STATE_10) {
            orderVO.setRemainTime(dealRemainTime("spell_order_auto_cancel_time", order.getCreateTime(), 30));
            orderVO.setCancelTime(dealCancelTime("spell_order_auto_cancel_time", order.getCreateTime(), 30));
        }
        //秒杀订单倒计时（秒）
        if (order.getOrderType() == PromotionConst.PROMOTION_TYPE_104 && order.getOrderState() == OrderConst.ORDER_STATE_10) {
            orderVO.setRemainTime(dealRemainTime("seckill_order_cancle", order.getCreateTime(), 5));
            orderVO.setCancelTime(dealCancelTime("seckill_order_cancle", order.getCreateTime(), 5));
        }
        //预售订单
        if (order.getOrderType() == PromotionConst.PROMOTION_TYPE_103) {
            PresellOrderExtendExample presellOrderExtendExample = new PresellOrderExtendExample();
            presellOrderExtendExample.setOrderSn(orderSn);
            List<PresellOrderExtend> preSellOrderExtendList = presellOrderExtendModel.getPresellOrderExtendList(presellOrderExtendExample, null);
            AssertUtil.notEmpty(preSellOrderExtendList, "获取预售订单扩展信息为空");
            PresellOrderExtend presellOrderExtend = preSellOrderExtendList.get(0);
            orderVO.setPresellInfo(new MemberOrderDetailVO.PresellDetailInfo(presellOrderExtend));
            if (order.getOrderState() == OrderConst.ORDER_STATE_10) {
                orderVO.setOrderSubState(presellOrderExtend.getOrderSubState());
                if (presellOrderExtend.getIsAllPay() == OrderConst.IS_ALL_PAY_1) {
                    orderVO.setOrderStateValue(MemberOrderListVO.getRealOrderStateValue(order.getOrderState(),
                            0, 0L, order.getOrderType()));
                    orderVO.setRemainTime(dealRemainTime("deposit_order_auto_cancel_time", order.getCreateTime(), 30));
                    orderVO.setCancelTime(dealCancelTime("deposit_order_auto_cancel_time", order.getCreateTime(), 30));
                } else {
                    long time1 = presellOrderExtend.getRemainStartTime().getTime();
                    long time2 = System.currentTimeMillis();
                    long depositRemainTime = (time1 - time2) / 1000;
                    depositRemainTime = depositRemainTime < 0 ? 0 : depositRemainTime;

                    orderVO.setOrderStateValue(MemberOrderListVO.getRealOrderStateValue(order.getOrderState(),
                            presellOrderExtend.getOrderSubState(), depositRemainTime, order.getOrderType()));
                    //预售订单倒计时（秒）
                    if (presellOrderExtend.getOrderSubState() == OrderConst.ORDER_SUB_STATE_101) {
                        //付定金倒计时
                        orderVO.setRemainTime(dealRemainTime("deposit_order_auto_cancel_time", order.getCreateTime(), 30));
                        orderVO.setCancelTime(dealCancelTime("deposit_order_auto_cancel_time", order.getCreateTime(), 30));
                    } else if (presellOrderExtend.getOrderSubState() == OrderConst.ORDER_SUB_STATE_102) {
                        //付尾款倒计时
                        long time3 = presellOrderExtend.getRemainEndTime().getTime();
                        long endRemainTime = (time3 - time2) / 1000;
                        orderVO.setRemainTime(endRemainTime < 0 ? 0 : endRemainTime);
                        orderVO.setCancelTime(presellOrderExtend.getRemainEndTime());
                        orderVO.setActualPayment(orderVO.getActualPayment().subtract(presellOrderExtend.getDepositAmount()
                                .multiply(new BigDecimal(presellOrderExtend.getProductNum()))));
                    }
                    orderVO.setOrderTypeValue(MemberOrderListVO.dealOrderTypeValue(order.getOrderType(), true));
                }
            } else {
                if (presellOrderExtend.getIsAllPay() == OrderConst.IS_ALL_PAY_0) {
                    orderVO.setOrderTypeValue(MemberOrderListVO.dealOrderTypeValue(order.getOrderType(), true));
                }
            }
            orderVO.getChildOrdersVOS().forEach(childOrdersVO -> {
                childOrdersVO.getOrderProductListVOList().forEach(orderProductListVO -> {
                    orderProductListVO.setProductShowPrice(presellOrderExtend.getPresellPrice());
                });
            });
        }
        //阶梯团订单
        if (order.getOrderType() == PromotionConst.PROMOTION_TYPE_105) {
            LadderGroupOrderExtendExample groupOrderExtendExample = new LadderGroupOrderExtendExample();
            groupOrderExtendExample.setOrderSn(orderSn);
            List<LadderGroupOrderExtend> groupOrderExtendList = ladderGroupOrderExtendModel.getLadderGroupOrderExtendList(groupOrderExtendExample, null);
            AssertUtil.notEmpty(groupOrderExtendList, "获取阶梯团订单扩展信息为空");
            LadderGroupOrderExtend groupOrderExtend = groupOrderExtendList.get(0);
            MemberOrderDetailVO.LadderGroupDetailInfo groupDetailInfo = new MemberOrderDetailVO.LadderGroupDetailInfo(groupOrderExtend, order.getActivityDiscountAmount());
            //查询是否退还定金
            LadderGroup ladderGroup = ladderGroupModel.getLadderGroupByGroupId(groupOrderExtend.getGroupId());
            AssertUtil.notNull(ladderGroup, "获取阶梯团活动信息为空，请重试！");
            if (ladderGroup.getIsRefundDeposit() == LadderGroupConst.IS_REFUND_DEPOSIT_1) {
                groupDetailInfo.setIsRefundDeposit(true);
            }
            orderVO.setLadderGroupDetailInfo(groupDetailInfo);
            if (order.getOrderState() == OrderConst.ORDER_STATE_10) {
                long time1 = groupOrderExtend.getRemainStartTime().getTime();
                long time2 = System.currentTimeMillis();
                long depositRemainTime = (time1 - time2) / 1000;
                depositRemainTime = depositRemainTime < 0 ? 0 : depositRemainTime;

                orderVO.setOrderSubState(groupOrderExtend.getOrderSubState());
                orderVO.setOrderStateValue(MemberOrderListVO.getRealOrderStateValue(order.getOrderState(),
                        groupOrderExtend.getOrderSubState(), depositRemainTime, order.getOrderType()));
                //阶梯团订单倒计时（秒）
                if (groupOrderExtend.getOrderSubState() == LadderGroupConst.ORDER_SUB_STATE_1) {
                    //付定金倒计时
                    orderVO.setRemainTime(dealRemainTime("ladder_group_deposit_order_auto_cancel_time", order.getCreateTime(), 30));
                    orderVO.setCancelTime(dealCancelTime("ladder_group_deposit_order_auto_cancel_time", order.getCreateTime(), 30));
                } else if (groupOrderExtend.getOrderSubState() == LadderGroupConst.ORDER_SUB_STATE_2) {
                    if (depositRemainTime > 0) {
                        //生成尾款倒计时
                        orderVO.setRemainTime(depositRemainTime);
                    } else {
                        //付尾款倒计时
                        long time3 = groupOrderExtend.getRemainEndTime().getTime();
                        long endRemainTime = (time3 - time2) / 1000;
                        orderVO.setRemainTime(endRemainTime < 0 ? 0 : endRemainTime);
                        orderVO.setCancelTime(groupOrderExtend.getRemainEndTime());
                        orderVO.setActualPayment(orderVO.getActualPayment().subtract(groupOrderExtend.getAdvanceDeposit()
                                .multiply(new BigDecimal(groupOrderExtend.getProductNum()))));
                    }
                }
            }
            orderVO.getChildOrdersVOS().forEach(childOrdersVO -> {
                childOrdersVO.getOrderProductListVOList().forEach(orderProductListVO -> {
                    orderProductListVO.setProductShowPrice(groupOrderExtend.getProductPrice());
                });
            });
        }
        return SldResponse.success(orderVO);
    }

    /**
     * 处理订单支付剩余时间
     *
     * @param key          redis存储的key
     * @param createTime   订单创建时间
     * @param defaultValue 默认值
     * @return 剩余时间数
     */
    private long dealRemainTime(String key, Date createTime, Integer defaultValue) {
        //买家几分钟未支付订单，订单取消
        String value = stringRedisTemplate.opsForValue().get(key);
        int limitMinute = value == null ? defaultValue : Integer.parseInt(value);
        // 获取当前时间limitHour小时之前的时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, -limitMinute);
        Date cancelTime = calendar.getTime();

        long time1 = createTime.getTime();
        long time2 = cancelTime.getTime();
        long remainTime = (time1 - time2) / 1000;
        return remainTime < 0 ? 0 : remainTime;
    }

    /**
     * 处理订单未支付取消时间
     *
     * @param key          redis存储的key
     * @param createTime   订单创建时间
     * @param defaultValue 默认值
     * @return 取消时间
     */
    private Date dealCancelTime(String key, Date createTime, Integer defaultValue) {
        //买家几分钟未支付订单，订单取消时间
        String value = stringRedisTemplate.opsForValue().get(key);
        int limitMinute = value == null ? defaultValue : Integer.parseInt(value);
        return DateUtil.offsetMinute(createTime, limitMinute);
    }

    /**
     * 处理订单货品售后展示按钮
     *
     * @param order 订单信息
     */
    private void dealAfsButton(Order order) {
        Integer orderState = order.getOrderState();
        if (orderState.equals(OrderConst.ORDER_STATE_20)
                || orderState.equals(OrderConst.ORDER_STATE_30)
                || orderState.equals(OrderConst.ORDER_STATE_40)) {
            //可以售后的状态，遍历订单货品，设置售后按钮
            for (OrderProduct orderProduct : order.getOrderProductList()) {
                if (orderProduct.getIsGift() == OrderConst.IS_GIFT_YES) {
                    continue;
                }
                Goods goods = goodsModel.getGoodsByGoodsId(orderProduct.getGoodsId());
                AssertUtil.notNull(goods, "商品不存在");
                //虚拟商品检测是否可以退款
                if (goods.getIsVirtualGoods() == GoodsConst.IS_VIRTUAL_GOODS_2) {
                    //不支持就结束本次循环
                    if (goods.getAfterSaleService() == GoodsConst.AFTER_SALE_SERVICE_NO) {
                        continue;
                    }
                }
                switch (orderState) {
                    //待发货，只能申请仅退款
                    case OrderConst.ORDER_STATE_20:
                        dealReturnInfo(orderProduct, OrderProductConst.AFS_BUTTON_100);
                        break;
                    //待买家收货，显示退款
                    case OrderConst.ORDER_STATE_30:
                        dealReturnInfo(orderProduct, OrderProductConst.AFS_BUTTON_200);
                        break;
                    case OrderConst.ORDER_STATE_40:
                        dealReturnInfo(orderProduct, OrderProductConst.AFS_BUTTON_300);
                        break;
                    default:
                        break;
                }
            }
        }
    }

    /**
     * 处理退货信息
     *
     * @param orderProduct 订单货品
     * @param button       没有申请过退换货时展示的状态
     */
    private void dealReturnInfo(OrderProduct orderProduct, Integer button) {
        //有退货,查询退货是否已完成
        OrderReturnExample returnExample = new OrderReturnExample();
        returnExample.setOrderSn(orderProduct.getOrderSn());
        returnExample.setOrderProductId(orderProduct.getOrderProductId());
        List<OrderReturn> returnList = orderReturnModel.getOrderReturnList(returnExample, null);
        if (CollectionUtils.isEmpty(returnList)) {
            orderProduct.setAfsButton(button);
            return;
        }
        OrderReturn orderReturn = returnList.get(0);
        orderProduct.setAfsSn(orderReturn.getAfsSn());
        orderProduct.setAfsState(orderReturn.getState());
        if (orderReturn.getState().equals(OrdersAfsConst.RETURN_STATE_300)) {
            //退款完成
            orderProduct.setAfsButton(OrderProductConst.AFS_BUTTON_402);
        } else if (orderReturn.getState().equals(OrdersAfsConst.RETURN_STATE_202)) {
            //退款失败
            orderProduct.setAfsButton(OrderProductConst.AFS_BUTTON_301);
        } else {
            //没有退款完成的，展示退款中
            orderProduct.setAfsButton(OrderProductConst.AFS_BUTTON_401);
        }
    }

    @ApiOperation("获取聊天界面我的订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "店铺id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("myOrders")
    public JsonResult<PageVO<ChatOrdersVO>> myOrders(HttpServletRequest request, Long storeId) {
        Member member = UserUtil.getUser(request, Member.class);

        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        OrderExample example = new OrderExample();
        if (!StringUtil.isNullOrZero(storeId)) {
            example.setStoreId(storeId);
        }
        example.setMemberId(member.getMemberId());
        List<Order> list = orderModel.getOrderList(example, pager);
        ArrayList<ChatOrdersVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(order -> {
                //查询货品列表
                OrderProductExample productExample = new OrderProductExample();
                productExample.setOrderSn(order.getOrderSn());
                productExample.setOrderBy("order_product_id ASC");
                List<OrderProduct> productList = orderProductModel.getOrderProductList(productExample, null);
                vos.add(new ChatOrdersVO(order, productList));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }
}
