/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.user.platform.controller;

import cn.hutool.core.io.IORuntimeException;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.enums.OrderStatus;
import com.yami.shop.bean.enums.OrderType;
import com.yami.shop.bean.enums.RefundStatusEnum;
import com.yami.shop.bean.model.Order;
import com.yami.shop.bean.model.OrderItem;
import com.yami.shop.bean.model.UserAddrOrder;
import com.yami.shop.bean.param.DeliveryOrderParam;
import com.yami.shop.bean.param.OrderParam;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.*;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 积分订单管理实现
 *
 * @author lgh
 * @version 1.0.0
 * @since 2018-09-15
 */
@Controller
@RequestMapping("/score/order")
public class ScoreOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private UserAddrOrderService userAddrOrderService;

    @Autowired
    private ProductService productService;

    @Autowired
    private SkuService skuService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    public ResponseEntity<IPage<Order>> page(OrderParam orderParam,PageParam<Order> page) {
        orderParam.setShopId(Constant.PLATFORM_SHOP_ID);
        orderParam.setOrderType(OrderType.SCORE.value());
        IPage<Order> orderIPage = orderService.pageOrdersDetialByOrderParam(page, orderParam);
        for (Order order : orderIPage.getRecords()) {
            order.setShopName("官方店");
        }
        return ResponseEntity.ok(orderIPage);
    }

    /**
     * 获取信息
     */
    @GetMapping("/orderInfo/{orderNumber}")
    public ResponseEntity<Order> info(@PathVariable("orderNumber") String orderNumber) {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        Order order = orderService.getOrderByOrderNumberAndShopId(orderNumber,shopId,true);
        List<OrderItem> orderItems = orderItemService.getOrderItemsByOrderNumber(orderNumber);
        order.setOrderItems(orderItems);
        UserAddrOrder userAddrOrder = userAddrOrderService.getById(order.getAddrOrderId());
        order.setUserAddrOrder(userAddrOrder);
        return ResponseEntity.ok(order);
    }

    /**
     * 发货
     */
    @PutMapping("/delivery")
    public ResponseEntity<Void> delivery(@RequestBody DeliveryOrderParam deliveryOrderParam) {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        Order order = orderService.getOrderByOrderNumberAndShopId(deliveryOrderParam.getOrderNumber(),shopId,true);

        if (Objects.equals(order.getRefundStatus(), RefundStatusEnum.APPLY.value())) {
            throw new GlobalException("该订单有退款信息正在处理当中，请处理完毕之后再进行发货的操作！");
        }
        if (!Objects.equals(order.getHbOrderStatus(), OrderStatus.PADYED.value())){
            throw new GlobalException("订单不处于待发货状态，无法进行发货");
        }

        Order orderParam = new Order();
        orderParam.setOrderId(order.getOrderId());
        orderParam.setDvyId(deliveryOrderParam.getDvyId());
        orderParam.setDvyFlowId(deliveryOrderParam.getDvyFlowId());
        orderParam.setDvyTime(new Date());
        orderParam.setUpdateTime(new Date());
        orderParam.setHbOrderStatus(OrderStatus.CONSIGNMENT.value());
        orderParam.setUserId(order.getUserId());

        orderService.delivery(orderParam);

        List<OrderItem> orderItems = orderItemService.getOrderItemsByOrderNumber(deliveryOrderParam.getOrderNumber());
        for (OrderItem orderItem : orderItems) {
            productService.removeProductCacheByProdId(orderItem.getProdId());
            skuService.removeSkuCacheBySkuId(orderItem.getSkuId(),orderItem.getProdId());
        }
        return ResponseEntity.ok().build();
    }

    /**
     * 已销售订单
     *
     * @param orderParam
     */
    @GetMapping("/soldExcel")
    public void soldExcel(OrderParam orderParam, HttpServletResponse response) {
//        orderParam.setIsPayed(1);
        orderParam.setOrderType(OrderType.SCORE.value());
        orderService.soldExcel(orderParam,response);
//        Long shopId = Constant.PLATFORM_SHOP_ID;
//        order.setShopId(shopId);
//        order.setIsPayed(1);
//        List<Order> orders = orderService.listOrdersDetialByOrder(order, startTime, endTime);
//
//        //通过工具类创建writer
//        ExcelWriter writer = ExcelUtil.getWriter();
//        // 待发货
//        String[] hearder = {"订单编号", "下单时间", "收件人", "手机", "收货地址", "商品名称", "数量", "订单应付", "订单运费", "订单实付"};
//        Sheet sheet = writer.getSheet();
//        sheet.setColumnWidth(0, 20 * 256);
//        sheet.setColumnWidth(1, 20 * 256);
//        sheet.setColumnWidth(3, 20 * 256);
//        sheet.setColumnWidth(4, 60 * 256);
//        sheet.setColumnWidth(5, 60 * 256);
//
//        writer.merge(hearder.length - 1, "销售信息整理");
//        writer.writeRow(Arrays.asList(hearder));
//
//        int row = 1;
//        for (Order dbOrder : orders) {
//            UserAddrOrder addr = dbOrder.getUserAddrOrder();
//            String addrInfo = addr.getProvince() + addr.getCity() + addr.getArea() + addr.getAddr();
//            List<OrderItem> orderItems = dbOrder.getOrderItems();
//            int firstRow = row + 1;
//            int lastRow = row + orderItems.size();
//            int col = -1;
//            // 订单编号
//            mergeIfNeed(writer, firstRow, lastRow, ++col, col, dbOrder.getOrderNumber());
//            // 下单时间
//            mergeIfNeed(writer, firstRow, lastRow, ++col, col, dbOrder.getCreateTime());
//            // 收件人
//            mergeIfNeed(writer, firstRow, lastRow, ++col, col, addr.getReceiver());
//            // "手机"
//            mergeIfNeed(writer, firstRow, lastRow, ++col, col, addr.getMobile());
//            // "收货地址"
//            mergeIfNeed(writer, firstRow, lastRow, ++col, col, addrInfo);
//            int prodNameCol = ++col;
//            int prodCountCol = ++col;
//            for (OrderItem orderItem : orderItems) {
//                row++;
//                // 商品名称
//                writer.writeCellValue(prodNameCol, row, orderItem.getProdName());
//                // 数量
//                writer.writeCellValue(prodCountCol, row, orderItem.getProdCount());
//            }
//            // 订单应付
//            mergeIfNeed(writer, firstRow, lastRow, ++col, col, dbOrder.getTotal());
//            // 订单运费
//            mergeIfNeed(writer, firstRow, lastRow, ++col, col, dbOrder.getFreightAmount());
//            // 订单实付
//            mergeIfNeed(writer, firstRow, lastRow, ++col, col, dbOrder.getActualTotal());
//
//        }
//        writeExcel(response, writer);
    }
//
//    /**
//     * 如果需要合并的话，就合并
//     */
//    private void mergeIfNeed(ExcelWriter writer, int firstRow, int lastRow, int firstColumn, int lastColumn, Object content) {
//        if (lastRow - firstRow > 0 || lastColumn - firstColumn > 0) {
//            writer.merge(firstRow, lastRow, firstColumn, lastColumn, content, false);
//        } else {
//            writer.writeCellValue(firstColumn, firstRow, content);
//        }
//
//    }
//
//    private void writeExcel(HttpServletResponse response, ExcelWriter writer) {
//        //response为HttpServletResponse对象
//        response.setContentType("application/vnd.ms-excel;charset=utf-8");
//        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
//        response.setHeader("Content-Disposition", "attachment;filename=1.xls");
//
//        ServletOutputStream servletOutputStream = null;
//        try {
//            servletOutputStream = response.getOutputStream();
//            writer.flush(servletOutputStream);
//            servletOutputStream.flush();
//        } catch (IORuntimeException | IOException e) {
//            e.printStackTrace();
//        } finally {
//            writer.close();
//            try {
//                if (servletOutputStream != null) {
//                    servletOutputStream.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }
}
