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

import com.slodon.b2b2c.core.constant.GoodsConst;
import com.slodon.b2b2c.core.constant.OrderConst;
import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.express.TracesResult;
import com.slodon.b2b2c.core.express.TrackUtil;
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.integral.example.*;
import com.slodon.b2b2c.integral.pojo.*;
import com.slodon.b2b2c.member.pojo.Member;
import com.slodon.b2b2c.model.integral.*;
import com.slodon.b2b2c.model.seller.StoreExpressSettingModel;
import com.slodon.b2b2c.model.seller.StoreModel;
import com.slodon.b2b2c.seller.example.StoreExpressSettingExample;
import com.slodon.b2b2c.seller.pojo.Store;
import com.slodon.b2b2c.seller.pojo.StoreExpressSetting;
import com.slodon.b2b2c.vo.integral.IntegralOrderDetailVO;
import com.slodon.b2b2c.vo.integral.IntegralOrderProductVO;
import com.slodon.b2b2c.vo.integral.IntegralOrderVO;
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Api(tags = "front-订单管理")
@RestController
@RequestMapping("v3/integral/front/integral/order")
public class FrontIntegralOrderController extends BaseController {

    @Resource
    private IntegralOrderModel integralOrderModel;
    @Resource
    private IntegralOrderProductModel integralOrderProductModel;
    @Resource
    private IntegralOrderLogModel integralOrderLogModel;
    @Resource
    private IntegralOrderPayModel integralOrderPayModel;
    @Resource
    private IntegralOrderReserveModel integralOrderReserveModel;
    @Resource
    private StoreModel storeModel;
    @Resource
    private StoreExpressSettingModel storeExpressSettingModel;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation("订单数量(我的模块展示使用)")
    @GetMapping("orderCount")
    public JsonResult orderCount(HttpServletRequest request) {
        Member member = UserUtil.getUser(request, Member.class);

        IntegralOrderExample example = new IntegralOrderExample();
        example.setMemberId(member.getMemberId());
        example.setOrderStateIn(OrderConst.ORDER_STATE_10 + "," + OrderConst.ORDER_STATE_20 + "," + OrderConst.ORDER_STATE_30);
        example.setDeleteState(OrderConst.DELETE_STATE_0);
        Integer orderCount = integralOrderModel.getIntegralOrderCount(example);
        return SldResponse.success(orderCount);
    }

    @ApiOperation("订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderState", value = "订单状态：0-已取消；10-待付款订单；20-待发货订单；30-待收货订单；40-已完成;50-已关闭", paramType = "query"),
            @ApiImplicitParam(name = "orderSn", value = "订单号", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("list")
    public JsonResult<PageVO<IntegralOrderVO>> list(HttpServletRequest request, Integer orderState, String orderSn) {
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        Member member = UserUtil.getUser(request, Member.class);

        IntegralOrderExample example = new IntegralOrderExample();
        example.setOrderState(orderState);
        example.setOrderSnOrGoodsNameLike(orderSn);
        example.setMemberId(member.getMemberId());
        example.setDeleteState(OrderConst.DELETE_STATE_0);
        List<IntegralOrder> list = integralOrderModel.getIntegralOrderList(example, pager);
        List<IntegralOrderVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (IntegralOrder integralOrder : list) {
                IntegralOrderVO vo = new IntegralOrderVO(integralOrder);
                //获取订单货品列表信息
                IntegralOrderProductExample orderProductExample = new IntegralOrderProductExample();
                orderProductExample.setOrderSn(integralOrder.getOrderSn());
                List<IntegralOrderProduct> orderProductList = integralOrderProductModel.getIntegralOrderProductList(orderProductExample, null);
                AssertUtil.notEmpty(orderProductList, "订单货品不存在");
                List<IntegralOrderProductVO> productListVOS = new ArrayList<>();
                orderProductList.forEach(orderProduct -> {
                    productListVOS.add(new IntegralOrderProductVO(orderProduct));
                });
                vo.setOrderProductList(productListVOS);
                vos.add(vo);
            }
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

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

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

        //获取订单支付信息
        IntegralOrderPayExample orderPayExample = new IntegralOrderPayExample();
        orderPayExample.setPaySn(order.getPaySn());
        IntegralOrderPay orderPay = integralOrderPayModel.getIntegralOrderPayList(orderPayExample, null).get(0);

        IntegralOrderDetailVO detailVO = new IntegralOrderDetailVO(order, orderPay);
        List<IntegralOrderProductVO> productVOS = new ArrayList<>();
        //获取货品信息
        order.getOrderProductList().forEach(orderProduct -> {
            productVOS.add(new IntegralOrderProductVO(orderProduct));
        });
        detailVO.setOrderProductList(productVOS);
        //获取订单日志信息
        IntegralOrderLogExample orderLogExample = new IntegralOrderLogExample();
        orderLogExample.setOrderSn(order.getOrderSn());
        orderLogExample.setOrderBy("log_time asc");
        List<IntegralOrderLog> orderLogList = integralOrderLogModel.getIntegralOrderLogList(orderLogExample, null);
        detailVO.setOrderLogList(orderLogList);
        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(order.getDeliverTime(), autoReceiveDay);
            detailVO.setAutoReceiveTime(autoReceiveTime);
        }
        //店铺客服电话
        Store store = storeModel.getStoreByStoreId(order.getStoreId());
        AssertUtil.notNull(store, "店铺不存在");
        detailVO.setServicePhone(store.getServicePhone());
        //虚拟商品查询预留信息
        if (order.getIsVirtualGoods() == GoodsConst.IS_VIRTUAL_GOODS_2) {
            IntegralOrderReserveExample example = new IntegralOrderReserveExample();
            example.setOrderSn(orderSn);
            example.setOrderBy("reserve_id asc");
            List<IntegralOrderReserve> orderReserveList = integralOrderReserveModel.getIntegralOrderReserveList(example, null);
            detailVO.setOrderReserveList(orderReserveList);
        }
        return SldResponse.success(detailVO);
    }

    @ApiOperation("查看物流")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderSn", value = "订单号", required = true, paramType = "query")
    })
    @GetMapping("getTrace")
    public JsonResult<TracesResult> getTrace(HttpServletRequest request, String orderSn) {
        Member member = UserUtil.getUser(request, Member.class);
        //查询订单信息
        IntegralOrder order = integralOrderModel.getOrdersWithOpByOrderSn(orderSn);
        AssertUtil.notNull(order, "订单信息为空");
        AssertUtil.isTrue(!member.getMemberId().equals(order.getMemberId()), "您无权查看物流");
        AssertUtil.isTrue(order.getIsVirtualGoods() == GoodsConst.IS_VIRTUAL_GOODS_2, "该订单为虚拟商品订单，没有物流信息");

        IntegralOrderProduct orderProduct = order.getOrderProductList().get(0);
        if (order.getOrderState() > OrderConst.ORDER_STATE_20 && StringUtil.isEmpty(order.getExpressCode())) {
            TracesResult tracesResult = new TracesResult();
            tracesResult.setGoodsImage(FileUrlUtil.getFileUrl(orderProduct.getProductImage(), null));
            tracesResult.setExpressName(order.getDeliverName());
            tracesResult.setExpressNumber(order.getDeliverMobile());
            tracesResult.setType("1");
            return SldResponse.success(tracesResult);
        } else {
            AssertUtil.isTrue(StringUtil.isEmpty(order.getExpressCode()), "快递公司编号信息为空");

            TracesResult tracesResult;
            String expressType = stringRedisTemplate.opsForValue().get("express_type");
            if ("1".equals(expressType)) {
                //快递鸟
                tracesResult = this.getExpressTrack1(order.getOrderSn(), order.getExpressCode(), order.getExpressName(),
                        order.getExpressNumber(), order.getStoreId(), order.getReceiverMobile().substring(7, 11));
            } else {
                //快递100
                tracesResult = this.getExpressTrack2(order.getReceiverMobile(), order.getExpressCode(), order.getExpressName(),
                        order.getExpressNumber());
            }
            tracesResult.setGoodsImage(FileUrlUtil.getFileUrl(orderProduct.getProductImage(), null));
            return SldResponse.success(tracesResult);
        }
    }

    /**
     * 获取快递鸟物流轨迹
     *
     * @param orderSn        订单号
     * @param expressCode    快递编码
     * @param expressName    快递公司
     * @param expressNumber  快递单号
     * @param storeId        店铺id
     * @param receiverMobile 收货人手机号
     * @return 物流信息轨迹
     */
    public TracesResult getExpressTrack1(String orderSn, String expressCode, String expressName, String expressNumber, Long storeId, String receiverMobile) {
        //快递鸟电商ID
        String eBusinessId;
        //快递鸟电商加密私钥
        String appKey;
        String accountSetting = stringRedisTemplate.opsForValue().get("express_account_setting");
        if (StringUtil.isEmpty(accountSetting) || "1".equals(accountSetting)) {
            eBusinessId = stringRedisTemplate.opsForValue().get("express_ebusinessid");
            AssertUtil.notNull(eBusinessId, "请完善快递鸟配置信息：");
            appKey = stringRedisTemplate.opsForValue().get("express_apikey");
            AssertUtil.notNull(appKey, "请完善快递鸟配置信息：");
        } else {
            StoreExpressSettingExample example = new StoreExpressSettingExample();
            example.setStoreId(storeId);
            List<StoreExpressSetting> list = storeExpressSettingModel.getStoreExpressSettingList(example, null);
            AssertUtil.notEmpty(list, "请完善快递鸟配置信息：");

            eBusinessId = list.get(0).getBusinessId();
            AssertUtil.notNull(eBusinessId, "请完善快递鸟配置信息：");
            appKey = list.get(0).getAppKey();
            AssertUtil.notNull(appKey, "请完善快递鸟配置信息：");
        }
        return TrackUtil.getKdniaoTrack(orderSn, expressCode, expressName, expressNumber, eBusinessId, appKey, receiverMobile);
    }

    /**
     * 获取快递100物流轨迹
     *
     * @param phone         手机号
     * @param expressCode   快递编码
     * @param expressName   快递公司
     * @param expressNumber 快递单号
     * @return 物流信息轨迹
     */
    public TracesResult getExpressTrack2(String phone, String expressCode, String expressName, String expressNumber) {
        String key = stringRedisTemplate.opsForValue().get("express_100_key");
        AssertUtil.notNull(key, "请设置快递100 授权key");
        String customer = stringRedisTemplate.opsForValue().get("express_100_customer");
        AssertUtil.notNull(customer, "请设置快递100 授权码");
        return TrackUtil.getKd100Track(phone, expressCode, expressName, expressNumber, key, customer);
    }
}
