package com.ruoyi.logistics.service.impl;

import java.io.UnsupportedEncodingException;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.datascope.annotation.DataScope;
import com.ruoyi.common.redis.service.RedisIdGenerator;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.logistics.enums.OrderStatusEnum;
import com.ruoyi.logistics.service.LogisticsOrderService;
import com.ruoyi.system.api.model.LoginUser;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.logistics.mapper.BizWaybillOrderMapper;
import com.ruoyi.logistics.domain.BizWaybillOrder;
import com.ruoyi.logistics.service.IBizWaybillOrderService;

/**
 * 运单管理Service业务层处理
 * 
 * @author RuiJing
 * @date 2026-01-29
 */
@Service
public class BizWaybillOrderServiceImpl implements IBizWaybillOrderService 
{
    @Autowired
    private BizWaybillOrderMapper bizWaybillOrderMapper;
    @Autowired
    LogisticsOrderService logisticsOrderService;

    @Autowired
    private RedisIdGenerator redisIdGenerator;

    /**
     * 查询运单管理
     * 
     * @param waybillId 运单管理主键
     * @return 运单管理
     */
    @Override
    public BizWaybillOrder selectBizWaybillOrderByWaybillId(Long waybillId)
    {

        return bizWaybillOrderMapper.selectBizWaybillOrderByWaybillId(waybillId);
    }

    /**
     * 查询运单管理列表
     * 
     * @param bizWaybillOrder 运单管理
     * @return 运单管理
     */
    @DataScope(deptAlias = "d", userAlias = "u")
    @Override
    public List<BizWaybillOrder> selectBizWaybillOrderList(BizWaybillOrder bizWaybillOrder)
    {
        return bizWaybillOrderMapper.selectBizWaybillOrderList(bizWaybillOrder);
    }

    /**
     * 新增运单管理
     * 
     * @param bizWaybillOrder 运单管理
     * @return 结果
     */
    @Override
    public int insertBizWaybillOrder(BizWaybillOrder bizWaybillOrder) throws UnsupportedEncodingException, JsonProcessingException {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        String orderId = redisIdGenerator.generateUniqueId("RJSD");
        bizWaybillOrder.setWaybillNo(orderId);
        bizWaybillOrder.setOrderStatus(OrderStatusEnum.ORDER_STATUS_1.getCode());
        //前置校验
        JSONObject jsonObject= logisticsOrderService.precheckOrder(bizWaybillOrder);
        if(!jsonObject.getBooleanValue("success")){
            throw new ServiceException("下单前置校验未通过！ 原因:"+jsonObject.getString("msg"));
        }
        JSONObject orderObject= logisticsOrderService.createOrder(bizWaybillOrder);
        if(!orderObject.getBooleanValue("success")){
            throw new ServiceException("下单失败！ 原因:"+orderObject.getString("msg"));
        }
        if(2==bizWaybillOrder.getOrderType()){
            //顺丰物流返回的运单号
            JSONArray waybillNoArray=orderObject.getJSONArray("data");
            for (int i = 0; i < waybillNoArray.size(); i++) {
                JSONObject waybillNoJSONObject = waybillNoArray.getJSONObject(i);
                int waybillType = waybillNoJSONObject.getIntValue("waybillType");
                String  waybillNo = waybillNoJSONObject.getString("waybillNo");
                if(waybillType==1){
                    bizWaybillOrder.setExternalWaybillNo(waybillNo) ;
                }
            }
        }
        bizWaybillOrder.setCreateTime(DateUtils.getNowDate());
        bizWaybillOrder.setUserId(loginUser.getUserid());
        bizWaybillOrder.setDeptId(loginUser.getSysUser().getDeptId());
        return bizWaybillOrderMapper.insertBizWaybillOrder(bizWaybillOrder);
    }

    /**
     * 修改运单管理
     * 
     * @param bizWaybillOrder 运单管理
     * @return 结果
     */
    @Override
    public int updateBizWaybillOrder(BizWaybillOrder bizWaybillOrder){
        bizWaybillOrder.setUpdateTime(DateUtils.getNowDate());
        return  bizWaybillOrderMapper.updateBizWaybillOrder(bizWaybillOrder);
    }

    /**
     * 批量删除运单管理
     * 
     * @param waybillIds 需要删除的运单管理主键
     * @return 结果
     */
    @Override
    public int deleteBizWaybillOrderByWaybillIds(Long[] waybillIds)
    {
        return bizWaybillOrderMapper.deleteBizWaybillOrderByWaybillIds(waybillIds);
    }

    /**
     * 删除运单管理信息
     * 
     * @param waybillId 运单管理主键
     * @return 结果
     */
    @Override
    public int deleteBizWaybillOrderByWaybillId(Long waybillId)
    {
        return bizWaybillOrderMapper.deleteBizWaybillOrderByWaybillId(waybillId);
    }

    @Override
    public BizWaybillOrder selectBizWaybillOrderByQuery(BizWaybillOrder query) {
        return bizWaybillOrderMapper.selectBizWaybillOrderByQuery(query);
    }

    @Override
    public void updateBizWaybillOrderState(BizWaybillOrder bizWaybillOrder) {
         bizWaybillOrderMapper.updateBizWaybillOrderState(bizWaybillOrder);

    }

    @Override
    public int canceleBizWaybillOrder(BizWaybillOrder bizWaybillOrder) throws UnsupportedEncodingException {
        JSONObject orderObject= logisticsOrderService.cancelOrder(bizWaybillOrder);
        if(!orderObject.getBooleanValue("success")){
            throw new ServiceException(orderObject.getString("msg"));
        }
        bizWaybillOrder.setOrderStatus(6);
        return bizWaybillOrderMapper.canceleBizWaybillOrder(bizWaybillOrder);

    }
}
