package com.dgtly.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.dgtly.common.annotation.Log;
import com.dgtly.common.core.domain.AjaxResult;
import com.dgtly.common.enums.BusinessType;
import com.dgtly.common.exception.base.BaseException;
import com.dgtly.common.utils.MapDataUtil;
import com.dgtly.common.utils.poi.ExcelUtil;
import com.dgtly.order.domain.MetaHanaSalesOrder;
import com.dgtly.order.domain.OrderDetailModel;
import com.dgtly.order.service.IMetaHanaSalesOrderService;
import com.dgtly.system.domain.SysBatchSignInfo;
import com.dgtly.system.service.ISysBatchSignForService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.dgtly.common.core.controller.BaseController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 订单基础 订单的基础信息Controller
 * 
 * @author qxp
 * @date 2020-07-10
 */
@Controller
@RequestMapping("/order/orderbase")
public class OrderBaseController extends BaseController {
    private String prefix = "order/orderbase";

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private IMetaHanaSalesOrderService metaHanaSalesOrderService;

    @Value(value = "${ruoyi.cloudPath}")
    private String cloudPath;
    @Autowired
    private ISysBatchSignForService sysBatchSignForService;


    @RequiresPermissions("order:orderbase:view")
    @GetMapping()
    public String orderbase() {
        return prefix + "/orderbase";
    }

    /**
     * 查询订单基础 订单的基础信息列表
     */
    @RequiresPermissions("order:orderbase:list")
    @PostMapping("/list")
    @ResponseBody
    public Object list(MetaHanaSalesOrder order) {
        startPage();
        List<MetaHanaSalesOrder> list = metaHanaSalesOrderService.selectDistinctMetaHanaSalesOrder(order);

        return getDataTable(list);
    }


    /**
     * 修改订单基础 订单的基础信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        String url = cloudPath + "order" + "/order/meta-hana-sales-order/orderLineDetail?orderNumber=" + id;
        //String url = "http://10.32.2.212:8000/" + "order"+"/order/meta-hana-sales-order/orderLineDetail?orderNumber=" + id;
        String str = "";
        try {
            str = restTemplate.getForObject(url, String.class);
            JSONObject json = JSONObject.parseObject(str);
            if (json.getInteger("code") == 200) {
                OrderDetailModel order = json.getObject("data", OrderDetailModel.class);
                mmap.put("orderBase", order);
            } else {
                throw new BaseException("order", json.getString("message"));
            }
        } catch (Exception e) {
            logger.error("查询基础订单详情报错" + url, e);
            throw e;
        }

        return prefix + "/edit";
    }

    @RequiresPermissions("order:orderbase:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MetaHanaSalesOrder order) {
        List<MetaHanaSalesOrder> list = metaHanaSalesOrderService.selectDistinctMetaHanaSalesOrder(order);
        if (list.size() >= 10000) {
            return new AjaxResult(301, "数据大于 10000 条Excel将无法打开，请过滤查询条件");
        }
        ExcelUtil<MetaHanaSalesOrder> util = new ExcelUtil<MetaHanaSalesOrder>(MetaHanaSalesOrder.class);
        return util.exportExcel(list, "订单数据");
    }

    /**
     * 批量刷新确认期望到货时间
     *
     * @return
     */
    @RequiresPermissions("order:orderbase:refreshArrivalTime")
    @GetMapping("/refreshArrivalTime")
    @ResponseBody
    public AjaxResult refreshArrivalTime() {
        AjaxResult rest = new AjaxResult();
        String url = cloudPath + "order" + "/order/meta-hana-sales-order/dealHistoryData";
        String str = "";
        try {
            str = restTemplate.getForObject(url, String.class);
            JSONObject json = JSONObject.parseObject(str);
            if (json.getInteger("code") == 200) {
                rest.setCode(0);
                rest.setMsg("刷新成功");
            } else {
                rest.setCode(-1);
                rest.setMsg("刷新失败");
            }
        } catch (Exception e) {
            logger.error("批量刷新确认期望到货时间" + url, e);
            throw e;
        }
        return rest;
    }

    /**
     * 批量导入
     */
    @Log(title = "批量签收", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<SysBatchSignInfo> util = new ExcelUtil<>(SysBatchSignInfo.class);
        List<SysBatchSignInfo> list = util.importExcel(file.getInputStream());
        if (list != null && list.size() > 0) {
            int num = sysBatchSignForService.updateData(list);
            if (num == 1) {
                return AjaxResult.success("导入成功");
            } else {
                return AjaxResult.error("导入失败");
            }
        } else {
            return AjaxResult.success("Excel没有数据");
        }

    }

}
