package com.sckw.order.serivce;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sckw.core.common.enums.enums.DictTypeEnum;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.BeanUtils;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.order.dao.KwoTradeOrderAddressMapper;
import com.sckw.order.model.KwoTradeOrderAddress;
import com.sckw.order.model.vo.res.AddressInfoDtoDetailRes;
import com.sckw.system.api.RemoteSystemService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @desc: 交易订单装卸货地址信息service
 * @author: yzc
 * @date: 2023-06-25 14:28
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class KwoTradeOrderAddressService {

    @DubboReference(version = "1.0.0", group = "design", check = false)
    private RemoteSystemService remoteSystemService;

    private final KwoTradeOrderAddressMapper kwoTradeOrderAddressMapper;

    /**
     * @desc: 批量插入
     * @author: yzc
     * @date: 2023-07-13 9:52
     * @Param list:
     * @return: void
     */
    public void insertBatch(List<KwoTradeOrderAddress> list) {
        kwoTradeOrderAddressMapper.insertBatch(list);
    }

    public Long add(KwoTradeOrderAddress address) {
        kwoTradeOrderAddressMapper.insert(address);
        return address.getId();
    }

    /**
     * @desc: 根据orderId删除
     * @author: yzc
     * @date: 2023-07-14 14:49
     * @Param orderId:
     * @return: void
     */
    public void delByOrderId(Long orderId) {
        LambdaUpdateWrapper<KwoTradeOrderAddress> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(KwoTradeOrderAddress::getTOrderId, orderId).eq(KwoTradeOrderAddress::getDelFlag, Global.NO)
                .set(KwoTradeOrderAddress::getDelFlag, Global.YES);
        kwoTradeOrderAddressMapper.update(null, wrapper);
    }

    /**
     * @desc: 根据orderId获取
     * @author: yzc
     * @date: 2023-07-18 11:24
     * @Param torderId:
     * @return: java.util.List<com.sckw.order.model.KwoTradeOrderAddress>
     */
    public List<KwoTradeOrderAddress> getByOrderId(Long torderId) {
        LambdaQueryWrapper<KwoTradeOrderAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwoTradeOrderAddress::getTOrderId, torderId)
                .eq(KwoTradeOrderAddress::getDelFlag, Global.NO)
                .orderByAsc(KwoTradeOrderAddress::getSort);
        List<KwoTradeOrderAddress> list = kwoTradeOrderAddressMapper.selectList(wrapper);
        return CollUtil.emptyIfNull(list);
    }

    /**
     * @param torderId
     * @param type
     * @return java.util.List<com.sckw.order.model.KwoTradeOrderAddress>
     * @desc 根据贸易订单id和地址类型查询
     * @author yzc
     * @date 2024/3/12 11:51
     */
    public List<KwoTradeOrderAddress> getByOrderIdAndType(Long torderId, String type) {
        LambdaQueryWrapper<KwoTradeOrderAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwoTradeOrderAddress::getTOrderId, torderId)
                .eq(KwoTradeOrderAddress::getAddressType, type)
                .eq(KwoTradeOrderAddress::getDelFlag, Global.NO)
                .orderByAsc(KwoTradeOrderAddress::getSort);
        List<KwoTradeOrderAddress> list = kwoTradeOrderAddressMapper.selectList(wrapper);
        return CollUtil.emptyIfNull(list);
    }

    /**
     * @desc: 根据orderId获取
     * @author: yzc
     * @date: 2023-07-18 11:24
     * @Param torderId:
     * @return: java.util.List<com.sckw.order.model.KwoTradeOrderAddress>
     */
    public AddressInfoDtoDetailRes getByOrderIdAndAddressId(Long tOrderId, Long tAddressId) {
        Map<String, Map<String, String>> dict = remoteSystemService.queryDictByType(
                List.of(DictTypeEnum.TRADE_TYPE.getType(), DictTypeEnum.PICKUP_TYPE.getType(),
                        DictTypeEnum.DELIVERY_TYPE.getType(), DictTypeEnum.TORDER_SOURCE.getType(), DictTypeEnum.TORDER_STATUS.getType(),
                        DictTypeEnum.PRODUCT_NAME_TYPE.getType(), DictTypeEnum.UNIT_TYPE.getType(), DictTypeEnum.TAX_RATE.getType(),
                        DictTypeEnum.TORDER_UNIT_TYPE.getType(), DictTypeEnum.TORDER_ADDRESS_TYPE.getType(), DictTypeEnum.ADDRESS_TYPE.getType()));
        Map<String, String> orderAddressMap, addressMap;
        if (CollectionUtils.isNotEmpty(dict)) {
            orderAddressMap = dict.get(DictTypeEnum.TORDER_ADDRESS_TYPE.getType());
            addressMap = dict.get(DictTypeEnum.ADDRESS_TYPE.getType());
        } else {
            orderAddressMap = new HashMap<>(Global.NUMERICAL_SIXTEEN);
            addressMap = new HashMap<>(Global.NUMERICAL_SIXTEEN);
        }
        LambdaQueryWrapper<KwoTradeOrderAddress> wrapper = new LambdaQueryWrapper<>();
        if (tAddressId != null) {
            wrapper.eq(KwoTradeOrderAddress::getId, tAddressId);
        }
        wrapper.eq(KwoTradeOrderAddress::getTOrderId, tOrderId).eq(KwoTradeOrderAddress::getDelFlag, Global.NO);
        KwoTradeOrderAddress address = kwoTradeOrderAddressMapper.selectOne(wrapper);
        AddressInfoDtoDetailRes addressInfoDetailRes = new AddressInfoDtoDetailRes();
        if (address != null) {
            BeanUtils.copyProperties(address, addressInfoDetailRes);
            addressInfoDetailRes.setAddressTypeLabel(CollectionUtils.isNotEmpty(orderAddressMap) ? orderAddressMap.get(address.getAddressType()) : null);
            addressInfoDetailRes.setTypeLabel(CollectionUtils.isNotEmpty(addressMap) ? addressMap.get(address.getType()) : null);
        }
        return addressInfoDetailRes;
    }

    /**
     * @desc: 根据orderIds删除
     * @author: yzc
     * @date: 2023-07-18 16:50
     * @Param orderIds:
     * @return: void
     */
    public void delByOrderIds(List<Long> orderIds) {
        LambdaUpdateWrapper<KwoTradeOrderAddress> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(KwoTradeOrderAddress::getTOrderId, orderIds).eq(KwoTradeOrderAddress::getDelFlag, Global.NO)
                .set(KwoTradeOrderAddress::getDelFlag, Global.YES);
        kwoTradeOrderAddressMapper.update(null, wrapper);
    }

    /**
     * @param ids
     * @return void
     * @desc 根据ids删除
     * @author yzc
     * @date 2024/3/12 11:54
     */
    public void delByIds(List<Long> ids) {
        LambdaUpdateWrapper<KwoTradeOrderAddress> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(KwoTradeOrderAddress::getId, ids)
                .eq(KwoTradeOrderAddress::getDelFlag, Global.NO)
                .set(KwoTradeOrderAddress::getDelFlag, Global.YES);
        kwoTradeOrderAddressMapper.update(null, wrapper);
    }

    /**
     * @desc: 根据orderIds查询
     * @author: yzc
     * @date: 2023-08-02 18:11
     * @Param tOrderIds:
     * @return: java.util.List<com.sckw.order.model.KwoTradeOrderAddress>
     */
    public List<KwoTradeOrderAddress> getByOrderIds(Collection<Long> tOrderIds) {
        LambdaQueryWrapper<KwoTradeOrderAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(KwoTradeOrderAddress::getTOrderId, tOrderIds)
                .eq(KwoTradeOrderAddress::getDelFlag, Global.NO)
                .orderByAsc(KwoTradeOrderAddress::getTOrderId)
                .orderByAsc(KwoTradeOrderAddress::getSort);
        List<KwoTradeOrderAddress> list = kwoTradeOrderAddressMapper.selectList(wrapper);
        return CollUtil.emptyIfNull(list);
    }

    /**
     * @param ids
     * @param tOrderId
     * @return java.util.Map<java.lang.String, java.lang.Long>
     * @desc 根据id集合及贸易订单id查询
     * @author yzc
     * @date 2024/3/13 9:25
     */
    public Map<String, Long> getByIdsAndTOrderId(Collection<Long> ids, Long tOrderId) {
        LambdaQueryWrapper<KwoTradeOrderAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(KwoTradeOrderAddress::getId, ids)
                .eq(KwoTradeOrderAddress::getTOrderId, tOrderId)
                .eq(KwoTradeOrderAddress::getDelFlag, Global.NO);
        List<KwoTradeOrderAddress> list = kwoTradeOrderAddressMapper.selectList(wrapper);
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyMap();
        }
        return list.stream().collect(Collectors.toMap(KwoTradeOrderAddress::getAddressType, KwoTradeOrderAddress::getId, (k1, k2) -> k1));
    }

    /**
     * @param type 地址类型
     * @param tOrderId 贸易订单ID
     * @return List
     * @desc 查询地址信息
     * @author zk
     * @date 2024/4/2
     */
    public List<KwoTradeOrderAddress> findByAddress(Long tOrderId, Integer type) {
        return kwoTradeOrderAddressMapper.findByAddress(tOrderId, type);
    }
}
