package org.dromara.zhishu.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.zhishu.domain.Logistics;
import org.dromara.zhishu.domain.bo.LogisticsBo;
import org.dromara.zhishu.mapper.LogisticsMapper;
import org.dromara.zhishu.service.LogisticsService;
import org.dromara.zhishu.util.JsonObjUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class LogisticsServiceImpl implements LogisticsService {
    @Autowired
    private LogisticsMapper logisticsMapper;
    ;

    @Override
    public Integer insert(LogisticsBo bo) {
        Logistics add = new Logistics();
        String are = logisticsMapper.selectByArea(bo.getDelivery_area(), bo.getDelivery_city());
        String city = logisticsMapper.selectByCity(bo.getDelivery_city(), bo.getDelivery_province());
        String province = logisticsMapper.selectByProvince(bo.getDelivery_province());
        add.setDeliveryProvince(province);
        add.setDeliveryCity(city);
        add.setDeliveryArea(are);

        add.setPricingMethod(bo.getPricing_method().toString());
        add.setShipping(bo.getShipping());
        add.setStatus("0");
        add.setWarehouseId(bo.getWarehouse_id());

        if (city == null) {
            add.setDeliveryAddress(province + "/" + are);
        } else {
            add.setDeliveryAddress(province + "/" + city + "/" + are);
        }
        add.setTemplateName(bo.getTemplate_name());
        log.info("打印数据-{}", JSONObject.toJSONString(bo));
        add.setShippingRange(JSONObject.toJSONString(bo.getShippingRange()));
        add.setRemark(bo.getRemark());
        logisticsMapper.insert(add);
        System.out.println(add);
        return 0;
    }

    @Override
    public List<Logistics> selectLogistics(String warehouseId) {
        return logisticsMapper.selectLogistice(warehouseId);
    }

    @Override
    public Logistics queryById(Long id) {
        Logistics logistics = logisticsMapper.selectByWareId(id);
        if(logistics!=null) {
            String[] name = logistics.getDeliveryAddress().split("/");
            Long proId =logisticsMapper.selectByid(name[0],null,null,null,null);
            Long cityId =logisticsMapper.selectByid(null,name[1],null,proId,null);
            Long areaId =logisticsMapper.selectByid(null,null,name[2],proId,cityId);
            logistics.setDeliveryProvince(proId.toString());
            logistics.setDeliveryCity(cityId.toString());
            logistics.setDeliveryArea(areaId.toString());
        }
        return logistics;
//        return  null;
    }

    @Override
    public Boolean setTemplate(LogisticsBo bo) {
        Integer message = 0;
        if (bo.getId() != null) {
            Logistics add = new Logistics();
            add.setId(bo.getId());
            String are = logisticsMapper.selectByArea(bo.getDelivery_area(), bo.getDelivery_city());
            String city = logisticsMapper.selectByCity(bo.getDelivery_city(), bo.getDelivery_province());
            String province = logisticsMapper.selectByProvince(bo.getDelivery_province());
            add.setDeliveryProvince(province);
            add.setDeliveryCity(city);
            add.setDeliveryArea(are);
            add.setContact(bo.getContact());
            add.setPhoneNumber(bo.getPhoneNumber());
            add.setFullAddress(bo.getFullAddress());
            add.setPricingMethod(bo.getPricing_method().toString());
            add.setShipping(bo.getShipping());
            add.setStatus("0");
            add.setWarehouseId(bo.getWarehouse_id());
            add.setRemark(bo.getRemark());

            if (city == null) {
                add.setDeliveryAddress(province + "/" + are);
            } else {
                add.setDeliveryAddress(province + "/" + city + "/" + are);
            }
            add.setTemplateName(bo.getTemplate_name());
            log.info("打印数据-{}", JSONObject.toJSONString(bo));
            String jsonString = JSONObject.toJSONString(bo.getShippingRange());
            add.setShippingRange(jsonString);

            // 赋值最低价格
            ObjectMapper objectMapper = new ObjectMapper();

            // 将 JSON 转换为 Map<String, List<String>>
            Map<String, List<String>> regionMap = null;
            try {
                regionMap = objectMapper.readValue(jsonString, Map.class);
            } catch (JsonProcessingException e) {
                throw new ServiceException("解析模板运送范围失败", 555);
            }
            if (ObjectUtil.isNotEmpty(regionMap)) {
                List<BigDecimal> priceList = new ArrayList<>();
                // 遍历 Map 并修改每个列表中的第二个元素
                for (Map.Entry<String, List<String>> entry : regionMap.entrySet()) {
                    List<String> list = entry.getValue();
                    String strValue = String.valueOf(list.get(1));
                    priceList.add(new BigDecimal(strValue));
                }
                BigDecimal minPrice = Collections.min(priceList);
                add.setFirPrice(minPrice);
            }
            message = logisticsMapper.updateByBo(add);
        }

        return message > 0;
    }
}
