package com.pine.admin.modules.business.controller;

import java.util.*;

import com.pine.common.dto.Page;
import com.pine.admin.modules.base.binder.BaseController;
import com.pine.common.dto.Result;
import com.pine.common.excelutil.ExcelPoiUtil;
import com.pine.common.exception.ApiException;
import com.pine.common.utils.DateTimeTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.pine.admin.modules.business.entity.Order;
import com.pine.admin.modules.business.service.OrderService;

import javax.servlet.http.HttpServletResponse;


/**
 * 订单表
 *
 * @author Pine
 * @email 771190883@qq.com
 * @date 2019-10-11 15:44:20
 */
@Api(value = "订单表接口", tags = {"订单表接口"})
@RestController
@RequestMapping("business/Order")
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;

    /**
     * <p>根据Id。</p>
     */
    @ApiOperation(value = "核销虚拟订单", notes = "核销虚拟订单")
    @PostMapping(value = "/checkWriteOffVirtual")
    public Result checkWriteOffVirtualRequest(Long orderId, String writeOffCode) {
        String s = orderService.checkWriteOffVirtualRequest(orderId, writeOffCode);
        return Result.success(true, s);
    }


    /**
     * <p>根据Id。</p>
     */
    @ApiOperation(value = "获取订单表数据", notes = "根据id来获取订单表详细信息")
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public Result getOrderEntity(@PathVariable("id") String id) {

        Order data = orderService.getById(id);
        return Result.success(true, data);
    }

    /**
     * <p>分页查询。</p>
     */
    @ApiOperation(value = "分页获取订单表数据", notes = "根据条件获取")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public Result page(Page page, Order record) {

        Map<String, Object> result = new HashMap<>();

        List<Order> pp = orderService.getByPage(page, record);

        result.put("page", page);
        result.put("pp", pp);
        return Result.success(true, result);
    }

    /**
     * <p>excel。</p>
     */
    @ApiOperation(value = "分页获取订单表数据", notes = "根据条件获取")
    @RequestMapping(value = "/excel", method = RequestMethod.GET)
    public void excel(HttpServletResponse response) {

        List<Order> allList = orderService.getAllList(null);
        allList.stream().forEach(o -> {
            orderService.FullOrderInfo(o);
            o.setShopName(o.getOrderSkus().get(0).getSkuName());
        });
        try {

            if (!allList.isEmpty()) {
                String excelName = "商城全部订单";
                LinkedHashMap<String, String> fieldMap = new LinkedHashMap<String, String>();
                fieldMap.put("orderCode", "订单号");
                fieldMap.put("billNo", "银联单号");
                fieldMap.put("customerName", "注册账号");
                fieldMap.put("receiptName", "购车人");
                fieldMap.put("receiptMobile", "购车人电话");
                fieldMap.put("receiptMobile", "购车人电话");
                fieldMap.put("address", "收货地址");
                fieldMap.put("shopName", "商品名称");
                fieldMap.put("createTime", "下单时间");
                fieldMap.put("status", "订单状态");
                fieldMap.put("originalPrice", "实付金额");
                fieldMap.put("source", "来源");
                ExcelPoiUtil.export(excelName, allList, fieldMap, response);
            }
        } catch (Exception e) {

            throw new ApiException(e.getMessage());
        }
    }


}
