package com.gxwebsoft.shop.controller;

import cn.binarywang.wx.miniapp.api.WxMaOrderShippingService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaOrderShippingServiceImpl;
import cn.binarywang.wx.miniapp.bean.shop.request.shipping.*;
import cn.binarywang.wx.miniapp.bean.shop.response.WxMaOrderShippingInfoBaseResponse;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.gxwebsoft.common.core.utils.JSONUtil;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.entity.*;
import com.gxwebsoft.shop.service.ShopExpressService;
import com.gxwebsoft.shop.service.ShopOrderDeliveryService;
import com.gxwebsoft.shop.param.ShopOrderDeliveryParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.service.impl.KuaiDi100Impl;
import com.kuaidi100.sdk.pojo.HttpResult;
import com.kuaidi100.sdk.request.BOrderReq;
import com.kuaidi100.sdk.response.SubscribeResp;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发货单控制器
 *
 * @author 科技小王子
 * @since 2025-01-11 10:45:12
 */
@Tag(name = "发货单管理")
@RestController
@RequestMapping("/api/shop/shop-order-delivery")
public class ShopOrderDeliveryController extends BaseController {
    @Resource
    private ShopOrderDeliveryService shopOrderDeliveryService;


    @Operation(summary = "发货回调")
    @PostMapping("/notify")
    @GetMapping("/notify")
    public SubscribeResp notify(@RequestBody Map<String, Object> data) {
        System.out.println("快递100回调:" + data);
        SubscribeResp subscribeResp = new SubscribeResp();
        subscribeResp.setResult(Boolean.TRUE);
        subscribeResp.setReturnCode("200");
        subscribeResp.setMessage("成功");
        return subscribeResp;
    }

    @Operation(summary = "分页查询发货单")
    @GetMapping("/page")
    public ApiResult<PageResult<ShopOrderDelivery>> page(ShopOrderDeliveryParam param) {
        // 使用关联查询
        return success(shopOrderDeliveryService.pageRel(param));
    }

    @Operation(summary = "查询全部发货单")
    @GetMapping()
    public ApiResult<List<ShopOrderDelivery>> list(ShopOrderDeliveryParam param) {
        // 使用关联查询
        return success(shopOrderDeliveryService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:shopOrderDelivery:list')")
    @Operation(summary = "根据id查询发货单")
    @GetMapping("/{id}")
    public ApiResult<ShopOrderDelivery> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(shopOrderDeliveryService.getByIdRel(id));
    }

    @Operation(summary = "添加发货单")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ShopOrderDelivery shopOrderDelivery) {
        if (shopOrderDeliveryService.save(shopOrderDelivery)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "修改发货单")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ShopOrderDelivery shopOrderDelivery) {
        if (shopOrderDeliveryService.updateById(shopOrderDelivery)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "删除发货单")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (shopOrderDeliveryService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @Operation(summary = "批量添加发货单")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ShopOrderDelivery> list) {
        if (shopOrderDeliveryService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "批量修改发货单")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ShopOrderDelivery> batchParam) {
        if (batchParam.update(shopOrderDeliveryService, "delivery_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "批量删除发货单")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (shopOrderDeliveryService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
