package com.ruoyi.web.controller.jarvis;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jarvis.config.JarvisGoofishProperties;
import com.ruoyi.jarvis.domain.ErpGoofishOrder;
import com.ruoyi.jarvis.domain.ErpGoofishOrderEventLog;
import com.ruoyi.jarvis.domain.ErpGoofishOrderEventLogQuery;
import com.ruoyi.jarvis.dto.GoofishShipPreviewVo;
import com.ruoyi.jarvis.service.IErpGoofishOrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/jarvis/erpGoofishOrder")
public class ErpGoofishOrderController extends BaseController {

    @Resource
    private IErpGoofishOrderService erpGoofishOrderService;

    @Resource
    private JarvisGoofishProperties goofishProperties;

    /**
     * 订单变更日志全表检索（跨订单排查），须配置在 /{id} 之前避免歧义；对应日志标记 [goofish-order-event]
     */
    @PreAuthorize("@ss.hasPermi('jarvis:erpGoofishOrder:list')")
    @GetMapping("/eventLog/list")
    public TableDataInfo eventLogList(ErpGoofishOrderEventLogQuery query) {
        startPage();
        List<ErpGoofishOrderEventLog> list = erpGoofishOrderService.selectEventLogList(query);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('jarvis:erpGoofishOrder:list')")
    @GetMapping("/stats/rrsLogistics")
    public AjaxResult rrsLogisticsStats() {
        return AjaxResult.success(erpGoofishOrderService.selectRrsJdLinkedLogisticsStats());
    }

    @PreAuthorize("@ss.hasPermi('jarvis:erpGoofishOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(ErpGoofishOrder query) {
        startPage();
        List<ErpGoofishOrder> list = erpGoofishOrderService.selectList(query);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('jarvis:erpGoofishOrder:query')")
    @GetMapping("/shipPreview/{id}")
    public AjaxResult shipPreview(@PathVariable Long id) {
        GoofishShipPreviewVo vo = erpGoofishOrderService.shipPreview(id);
        return AjaxResult.success(vo);
    }

    @PreAuthorize("@ss.hasPermi('jarvis:erpGoofishOrder:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(erpGoofishOrderService.selectById(id));
    }

    @PreAuthorize("@ss.hasPermi('jarvis:erpGoofishOrder:query')")
    @GetMapping("/{id}/eventLogs")
    public AjaxResult eventLogs(@PathVariable Long id) {
        List<ErpGoofishOrderEventLog> list = erpGoofishOrderService.listEventLogsByOrderId(id);
        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('jarvis:erpGoofishOrder:edit')")
    @Log(title = "闲管家拉单", businessType = BusinessType.OTHER)
    @PostMapping("/pull/{appKey}")
    public AjaxResult pullOne(@PathVariable String appKey, @RequestParam(value = "hours", required = false) Integer hours) {
        int h = hours == null ? goofishProperties.getPullLookbackHours() : hours;
        int n = erpGoofishOrderService.pullOrdersForAppKey(appKey, h);
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("processedItems", n);
        data.put("lookbackHours", h);
        return AjaxResult.success(data);
    }

    @PreAuthorize("@ss.hasPermi('jarvis:erpGoofishOrder:edit')")
    @Log(title = "闲管家全量拉单", businessType = BusinessType.OTHER)
    @PostMapping("/pullAll")
    public AjaxResult pullAll(@RequestParam(value = "hours", required = false) Integer hours) {
        int h = hours == null ? goofishProperties.getPullLookbackHours() : hours;
        int n = erpGoofishOrderService.pullAllEnabled(h);
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("processedItems", n);
        data.put("lookbackHours", h);
        return AjaxResult.success(data);
    }

    @PreAuthorize("@ss.hasPermi('jarvis:erpGoofishOrder:edit')")
    @Log(title = "闲管家历史全量拉单", businessType = BusinessType.OTHER)
    @PostMapping("/pull/{appKey}/full")
    public AjaxResult pullOneFull(@PathVariable String appKey) {
        int n = erpGoofishOrderService.pullOrdersForAppKeyFullHistory(appKey);
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("processedItems", n);
        data.put("pullFullHistoryDays", goofishProperties.getPullFullHistoryDays());
        data.put("pullTimeChunkSeconds", goofishProperties.getPullTimeChunkSeconds());
        return AjaxResult.success(data);
    }

    @PreAuthorize("@ss.hasPermi('jarvis:erpGoofishOrder:edit')")
    @Log(title = "闲管家全账号历史全量拉单", businessType = BusinessType.OTHER)
    @PostMapping("/pullAll/full")
    public AjaxResult pullAllFull() {
        int n = erpGoofishOrderService.pullAllEnabledFullHistory();
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("processedItems", n);
        data.put("pullFullHistoryDays", goofishProperties.getPullFullHistoryDays());
        data.put("pullTimeChunkSeconds", goofishProperties.getPullTimeChunkSeconds());
        return AjaxResult.success(data);
    }

    @PreAuthorize("@ss.hasPermi('jarvis:erpGoofishOrder:edit')")
    @Log(title = "闲管家订单详情刷新", businessType = BusinessType.UPDATE)
    @PostMapping("/refreshDetail/{id}")
    public AjaxResult refreshDetail(@PathVariable Long id) {
        erpGoofishOrderService.refreshDetail(id);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('jarvis:erpGoofishOrder:edit')")
    @Log(title = "闲管家重试发货", businessType = BusinessType.UPDATE)
    @PostMapping("/retryShip/{id}")
    public AjaxResult retryShip(@PathVariable Long id) {
        erpGoofishOrderService.retryShip(id);
        return AjaxResult.success();
    }
}
