package com.gree.mall.manager.controller.esOrder;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.gree.mall.manager.bean.esOrder.EsOrderAddBean;
import com.gree.mall.manager.bean.esOrder.EsOrderRefundBean;
import com.gree.mall.manager.bean.material.manage.PartsApplyBean;
import com.gree.mall.manager.bean.order.BatchRemarkBean;
import com.gree.mall.manager.bean.order.OrderDetailBean;
import com.gree.mall.manager.bean.order.SendOrderBody;
import com.gree.mall.manager.bean.order.WorkerOrderInfo;
import com.gree.mall.manager.constant.Constant;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.EsOrder.EsOrderLogic;
import com.gree.mall.manager.logic.ExpressLogic;
import com.gree.mall.manager.logic.order.OrderLogic;
import com.gree.mall.manager.plus.entity.*;
import com.gree.mall.manager.utils.CommonUtils;
import com.gree.mall.manager.utils.excel.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "二手订单管理", tags = {"二手订单管理"})
@RequestMapping(value = "/esOrder", produces = "application/json; charset=utf-8")
public class EsOrderController {

    @Autowired
    EsOrderLogic esOrderLogic;

    @Autowired
    ExpressLogic expressLogic;



    @PostMapping("/list")
    @ApiOperation("列表")
    public ResponseHelper<Page<EsOrderInfo>> list(
            @ApiParam(value = "订单号", required = false) @RequestParam(required = false) String orderId,
            @ApiParam(value = "产品名称", required = false) @RequestParam(required = false) String esGoodsName,
            @ApiParam(value = "买家名称", required = false) @RequestParam(required = false) String userName,
            @ApiParam(value = "卖家名称", required = false) @RequestParam(required = false) String sellName,
            @ApiParam(value = "买家手机", required = false) @RequestParam(required = false) String userMobile,
            @ApiParam(value = "卖家手机", required = false) @RequestParam(required = false) String sellMobile,
            @ApiParam(value = "订单状态 SAVE 买家下单 WAIT 待确认  PAID 待发货 SEND 卖家发货  OVER 交易完成 TIME_OUT 超时 CANCEL 取消  WAIT_REFUND 售后中 REFUND 已退", required = false) @RequestParam(required = false) List<String> status,
            @ApiParam(value = "开始创建时间", required = false) @RequestParam(required = false) String startCreateTime,
            @ApiParam(value = "结束创建时间", required = false) @RequestParam(required = false) String endCreateTime,
            @ApiParam(value = "开始支付时间", required = false) @RequestParam(required = false) String startPayTime,
            @ApiParam(value = "结束支付时间", required = false) @RequestParam(required = false) String endPayTime,
            @ApiParam(value = "页号", required = true) @RequestParam(required = true) Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam(required = true) Integer pageSize,

            HttpServletRequest request
    ) throws RemoteServiceException {
        IPage<EsOrderInfo> list = esOrderLogic
                .list(request,esGoodsName,orderId, sellName, userName, userMobile,sellMobile, status, startCreateTime,
                        endCreateTime, startPayTime, endPayTime,pageNum, pageSize);
        return ResponseHelper.success(list);
    }


    @GetMapping("/listExcel")
    @ApiOperation("订单导出")
    public void downloadFile (HttpServletResponse response,
            @ApiParam(value = "订单号", required = false) @RequestParam(required = false) String orderId,
            @ApiParam(value = "产品名称", required = false) @RequestParam(required = false) String esGoodsName,
            @ApiParam(value = "买家名称", required = false) @RequestParam(required = false) String userName,
            @ApiParam(value = "卖家名称", required = false) @RequestParam(required = false) String sellName,
            @ApiParam(value = "买家手机", required = false) @RequestParam(required = false) String userMobile,
            @ApiParam(value = "卖家手机", required = false) @RequestParam(required = false) String sellMobile,
            @ApiParam(value = "订单状态 SAVE 买家下单 WAIT 待确认  PAID 待发货 SEND 卖家发货  OVER 交易完成 TIME_OUT 超时 CANCEL 取消  WAIT_REFUND 售后中 REFUND 已退", required = false) @RequestParam(required = false) List<String> status,
            @ApiParam(value = "开始创建时间", required = false) @RequestParam(required = false) String startCreateTime,
            @ApiParam(value = "结束创建时间", required = false) @RequestParam(required = false) String endCreateTime,
            @ApiParam(value = "开始支付时间", required = false) @RequestParam(required = false) String startPayTime,
            @ApiParam(value = "结束支付时间", required = false) @RequestParam(required = false) String endPayTime,


            HttpServletRequest request
    ) throws Exception {
        esOrderLogic.export2( request,  response,esGoodsName,orderId, sellName, userName, userMobile,sellMobile, status, startCreateTime,
                endCreateTime, startPayTime, endPayTime);
    }



    @PostMapping("/detail")
    @ApiOperation("订单详情")
    public ResponseHelper<EsOrderInfo> detail(
            @ApiParam(value = "订单号", required = true) @RequestParam(required = true) String esOrderId
    ) {
        EsOrderInfo detail = esOrderLogic.detail(esOrderId);
        return ResponseHelper.success(detail);
    }

    @GetMapping("/express")
    @ApiOperation(value = "查看物流")
    public ResponseHelper<List<ExpressInfo>> express(
            @ApiParam(required = true, value = "物流单号") @RequestParam(required = true) String logisticsNo,
            @ApiParam(required = true, value = "物流公司code") @RequestParam(required = true) String companyCode
    ) throws IOException, RemoteServiceException {
        List<ExpressInfo> expressInfos = expressLogic.queryExpress(logisticsNo,companyCode);
        return ResponseHelper.success(expressInfos);
    }


    @PostMapping("/add")
    @ApiOperation("新增订单")
    public ResponseHelper add(
            @RequestBody EsOrderAddBean esOrderAddBean
    ) {
        esOrderLogic.add(esOrderAddBean);
        return ResponseHelper.success();
    }


    @PostMapping("/payPrice")
    @ApiOperation("线上单确认收款")
    public ResponseHelper payPrice(
            @ApiParam(value = "订单号", required = true) @RequestParam(required = true) String esOrderId,
            @ApiParam(value = "金额", required = false) @RequestParam(required = false) BigDecimal price,
            @ApiParam(value = "收款凭证", required = false) @RequestParam(required = false) String fileUrl,
            @ApiParam(value = "备注", required = false) @RequestParam(required = false) String remark
    ) {
        esOrderLogic.payPrice(esOrderId,price,fileUrl,remark);
        return ResponseHelper.success();
    }


    @PostMapping("/payOk")
    @ApiOperation("线下单确认订单")
    public ResponseHelper payPrice(
            @ApiParam(value = "订单号", required = true) @RequestParam(required = true) String esOrderId
    ) {
        esOrderLogic.payOk(esOrderId);
        return ResponseHelper.success();
    }




    @PostMapping("/cancel")
    @ApiOperation("取消订单")
    public ResponseHelper cancel(
            @ApiParam(value = "订单号", required = true) @RequestParam(required = true) String esOrderId

    ) {
        esOrderLogic.cancel(esOrderId);
        return ResponseHelper.success();
    }


    @PostMapping("/payPriceList")
    @ApiOperation("线下单确认收款记录")
    public ResponseHelper<List<EsOrderPrice>> payPriceList(
            @ApiParam(value = "订单号", required = true) @RequestParam(required = true) String esOrderId
    ) {
        List<EsOrderPrice> esOrderPrices = esOrderLogic.payPriceList(esOrderId);
        return ResponseHelper.success(esOrderPrices);
    }


    @PostMapping("/send")
    @ApiOperation("发货")
    public ResponseHelper send(
            @ApiParam(value = "订单号", required = true) @RequestParam(required = true) String esOrderId,
            @ApiParam(value = "是否自提 YES是 NO否", required = true) @RequestParam(required = true) String pickType,
            @ApiParam(value = "快递单号", required = false) @RequestParam(required = false) String logisticsNo,
            @ApiParam(value = "快递公司code", required = false) @RequestParam(required = false) String logisticsCode,
            @ApiParam(value = "快递公司名称", required = false) @RequestParam(required = false) String logisticsName
    ) {
        esOrderLogic.send(esOrderId,pickType,logisticsNo,logisticsCode,logisticsName);
        return ResponseHelper.success();
    }


    @PostMapping("/refund")
    @ApiOperation("发起退款")
    public ResponseHelper refund(
            @ApiParam(value = "订单号", required = true) @RequestParam(required = true) String esOrderId,
            @ApiParam(value = "处理方式 REFUND 退款  GOODS_REFUND 退货退款  FAIL 驳回 GOODS 退货", required = true) @RequestParam(required = true) String refundType,
            @ApiParam(value = "退货数量", required = false) @RequestParam(required = false) Integer refundNum,
            @ApiParam(value = "退货备注", required = false) @RequestParam(required = false) String refundRemark,
            @ApiParam(value = "退款金额", required = false) @RequestParam(required = false) BigDecimal refundPrice
    ) {
        esOrderLogic.refund(esOrderId,refundType,refundNum,refundRemark,refundPrice);
        return ResponseHelper.success();
    }


    @PostMapping("/listRefund")
    @ApiOperation("退货列表")
    public ResponseHelper<Page<EsOrderRefund>> listRefund(
            @ApiParam(value = "订单号", required = false) @RequestParam(required = false) String orderId,
            @ApiParam(value = "售后编号", required = false) @RequestParam(required = false) String esOrderRefundId,
            @ApiParam(value = "产品名称", required = false) @RequestParam(required = false) String esGoodsName,
            @ApiParam(value = "买家名称", required = false) @RequestParam(required = false) String userName,
            @ApiParam(value = "卖家名称", required = false) @RequestParam(required = false) String sellName,
            @ApiParam(value = "买家手机", required = false) @RequestParam(required = false) String userMobile,
            @ApiParam(value = "卖家手机", required = false) @RequestParam(required = false) String sellMobile,
            @ApiParam(value = "处理方式 REFUND 退款  GOODS_REFUND 退货退款  FAIL 驳回 GOODS 退货", required = false) @RequestParam(required = false) List<String> status,
            @ApiParam(value = "开始创建时间", required = false) @RequestParam(required = false) String startCreateTime,
            @ApiParam(value = "结束创建时间", required = false) @RequestParam(required = false) String endCreateTime,
            @ApiParam(value = "开始支付时间", required = false) @RequestParam(required = false) String startPayTime,
            @ApiParam(value = "结束支付时间", required = false) @RequestParam(required = false) String endPayTime,
            @ApiParam(value = "页号", required = true) @RequestParam(required = true) Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam(required = true) Integer pageSize,

            HttpServletRequest request
    ) throws RemoteServiceException {
        IPage<EsOrderRefund> list = esOrderLogic
                .listRefund(request,esOrderRefundId,esGoodsName,orderId, sellName, userName, userMobile,sellMobile, status, startCreateTime,
                        endCreateTime, startPayTime, endPayTime,pageNum, pageSize);
        return ResponseHelper.success(list);
    }


    @PostMapping("/detailRefund")
    @ApiOperation("退货订单详情")
    public ResponseHelper<EsOrderRefundBean> detailRefund(
            @ApiParam(value = "退货单号", required = true) @RequestParam(required = true) String esOrderRefundId
    ) {
        EsOrderRefundBean detail = esOrderLogic.detailRefund(esOrderRefundId);
        return ResponseHelper.success(detail);
    }



    @PostMapping("/refundPrice")
    @ApiOperation("退款确认")
    public ResponseHelper refundPrice(
            @ApiParam(value = "订单号", required = true) @RequestParam(required = true) String esOrderRefundId,
            @ApiParam(value = "处理方式 REFUND 退款  GOODS_REFUND 退货退款  FAIL 驳回 GOODS 退货", required = true) @RequestParam(required = true) String refundType,
            @ApiParam(value = "退货数量", required = false) @RequestParam(required = false) Integer refundNum,
            @ApiParam(value = "退货备注", required = false) @RequestParam(required = false) String refundRemark,
            @ApiParam(value = "退款金额", required = false) @RequestParam(required = false) BigDecimal refundPrice
    ) {
        esOrderLogic.refundPrice(esOrderRefundId,refundType,refundNum,refundRemark,refundPrice);
        return ResponseHelper.success();
    }


    @GetMapping("/listExcelRefund")
    @ApiOperation("维权订单导出")
    public void downloadFileRefund (HttpServletResponse response,
                                    @ApiParam(value = "订单号", required = false) @RequestParam(required = false) String orderId,
                                    @ApiParam(value = "产品名称", required = false) @RequestParam(required = false) String esGoodsName,
                                    @ApiParam(value = "买家名称", required = false) @RequestParam(required = false) String userName,
                                    @ApiParam(value = "卖家名称", required = false) @RequestParam(required = false) String sellName,
                                    @ApiParam(value = "买家手机", required = false) @RequestParam(required = false) String userMobile,
                                    @ApiParam(value = "卖家手机", required = false) @RequestParam(required = false) String sellMobile,
                                    @ApiParam(value = "处理方式 REFUND 退款  GOODS_REFUND 退货退款  FAIL 驳回 GOODS 退货", required = false) @RequestParam(required = false) List<String> status,
                                    @ApiParam(value = "开始创建时间", required = false) @RequestParam(required = false) String startCreateTime,
                                    @ApiParam(value = "结束创建时间", required = false) @RequestParam(required = false) String endCreateTime,
                                    @ApiParam(value = "开始支付时间", required = false) @RequestParam(required = false) String startPayTime,
                                    @ApiParam(value = "结束支付时间", required = false) @RequestParam(required = false) String endPayTime,


                              HttpServletRequest request
    ) throws Exception {
        esOrderLogic.downloadFileRefund( request,  response,esGoodsName,orderId, sellName, userName, userMobile,sellMobile, status, startCreateTime,
                endCreateTime, startPayTime, endPayTime);
    }

}
