package com.haha.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.haha.admin.annotation.RequirePermission;
import com.haha.common.annotation.Log;
import com.haha.common.enums.OperationType;
import com.haha.common.vo.PageResult;
import com.haha.common.vo.Result;
import com.haha.entity.ErpGoods;
import com.haha.service.ErpGoodsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * ERP 商品管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/erp-goods")
@RequiredArgsConstructor
public class ErpGoodsController {

    private final ErpGoodsService erpGoodsService;

    /**
     * 从 ERP 同步系统商品数据
     */
    @RequirePermission("erp:goods:sync")
    @Log(module = "ERP商品管理", operation = OperationType.SYNC, summary = "同步ERP商品数据")
    @PostMapping("/sync")
    public Result<Void> syncFromErp() {
        erpGoodsService.syncFromErp();
        return Result.success("同步任务已启动，正在后台执行，请稍后刷新页面查看结果");
    }

    /**
     * 分页查询已缓存的 ERP 商品列表
     */
    @RequirePermission("erp:goods:read")
    @GetMapping
    public Result<PageResult<ErpGoods>> list(@RequestParam(required = false) String keyword,
                                             @RequestParam(defaultValue = "1") int page,
                                             @RequestParam(defaultValue = "10") int pageSize) {
        IPage<ErpGoods> pageResult = erpGoodsService.listAll(keyword, page, pageSize);
        return Result.success("查询成功", PageResult.of(pageResult));
    }

    /**
     * 关联本地商品与 ERP 商品
     */
    @RequirePermission("erp:goods:bind")
    @Log(module = "ERP商品管理", operation = OperationType.UPDATE, summary = "关联ERP商品")
    @PostMapping("/bind")
    public Result<Void> bind(@RequestBody Map<String, Long> params) {
        Long productId = params.get("productId");
        Long erpGoodsId = params.get("erpGoodsId");
        if (productId == null || erpGoodsId == null) {
            return Result.error("productId 和 erpGoodsId 不能为空");
        }
        erpGoodsService.bindGoods(productId, erpGoodsId);
        return Result.success("关联成功");
    }

    /**
     * 解除关联
     */
    @RequirePermission("erp:goods:bind")
    @Log(module = "ERP商品管理", operation = OperationType.UPDATE, summary = "解除ERP商品关联")
    @PostMapping("/unbind")
    public Result<Void> unbind(@RequestBody Map<String, Long> params) {
        Long productId = params.get("productId");
        if (productId == null) {
            return Result.error("productId 不能为空");
        }
        erpGoodsService.unbindGoods(productId);
        return Result.success("解除关联成功");
    }

    /**
     * 一键自动关联：按 ERP goodsCode 匹配本地商品名称，唯一匹配则自动绑定
     */
    @RequirePermission("erp:goods:bind")
    @Log(module = "ERP商品管理", operation = OperationType.UPDATE, summary = "一键自动关联ERP商品")
    @PostMapping("/auto-bind")
    public Result<Map<String, Integer>> autoBind() {
        Map<String, Integer> result = erpGoodsService.autoBind();
        return Result.success(String.format("关联 %d 条，跳过 %d 条",
                result.get("bound"), result.get("skipped")), result);
    }
}
