package org.dromara.zhishu.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pdd.pop.sdk.common.util.JsonUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.zhishu.domain.CourierLog;
import org.dromara.zhishu.util.InterfaceUtils;
import org.dromara.zhishu.util.UrlUtil;
import org.springframework.stereotype.Service;
import org.dromara.zhishu.domain.bo.CourierLogBo;
import org.dromara.zhishu.domain.vo.CourierLogVo;
import org.dromara.zhishu.service.ICourierLogService;

import java.util.HashMap;
import java.util.List;
import java.util.Collection;
import java.util.Map;

/**
 * 快递日志Service业务层处理
 *
 * @author yxy
 * @date 2026-03-18
 */
@RequiredArgsConstructor
@Service
public class CourierLogServiceImpl implements ICourierLogService {

    /**
     * 查询快递日志
     *
     * @param id 主键
     * @return 快递日志
     */
    @Override
    public CourierLogVo queryById(Long id){
        String courierLogStr = InterfaceUtils.getInterfaceGetWithParams(UrlUtil.getOrderServiceUrl(),"/api/courierLog/"+id,new HashMap<>());
        CourierLogVo courierLog = JsonUtil.transferToObj(courierLogStr, CourierLogVo.class);
        return courierLog;
    }

    /**
     * 分页查询快递日志列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 快递日志分页列表
     */
    @Override
    public TableDataInfo<CourierLog> queryPageList(CourierLogBo bo, PageQuery pageQuery) {

        Map<String, Object> params = new HashMap<>();
        params.put("pageNum", pageQuery.getPageNum());
        params.put("pageSize", pageQuery.getPageSize());

        // 添加查询条件
        if (bo.getErpOrderId() != null) {
            params.put("erpOrderId", bo.getErpOrderId());
        }
        if (bo.getOrderSn() != null && !bo.getOrderSn().isEmpty()) {
            params.put("orderSn", bo.getOrderSn());
        }
        if (bo.getPartnerId() != null && !bo.getPartnerId().isEmpty()) {
            params.put("partnerId", bo.getPartnerId());
        }
        if (bo.getOrderSerialNo() != null && !bo.getOrderSerialNo().isEmpty()) {
            params.put("orderSerialNo", bo.getOrderSerialNo());
        }

        String courierLogListStr = InterfaceUtils.getInterfaceGetWithParams(UrlUtil.getOrderServiceUrl(), "/api/courierLog/getList", params);
        Map<String, Object> dataMap = JsonUtil.transferToObj(courierLogListStr, Map.class);
        List<CourierLog> courierLogList = (List<CourierLog>) dataMap.get("data");
        int total = (int) dataMap.get("total");

        Page<CourierLog> result = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize(), total);
        result.setRecords(courierLogList);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的快递日志列表
     *
     * @param bo 查询条件
     * @return 快递日志列表
     */
    @Override
    public List<CourierLogVo> queryList(CourierLogBo bo) {
        // 导出功能可能需要，暂时返回null，如有需要可实现
        return null;
    }



    /**
     * 校验并批量删除快递日志信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        for (Long id : ids) {
            InterfaceUtils.postForm(UrlUtil.getOrderServiceUrl(), "/api/courierLog/deleteById/" + id, new HashMap<>());
        }
        return true;
    }
}