package com.jeesharp.business.modules.mall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.entity.MallMemberAddress;
import com.jeesharp.business.modules.mall.mapper.MallMemberAddressMapper;
import com.jeesharp.business.modules.mall.service.MallMemberAddressService;
import com.jeesharp.business.modules.mall.vo.MallMemberAddressQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员地址 Service 实现类
 */
@Service
@Transactional(readOnly = true)
public class MallMemberAddressServiceImpl extends CrudService<MallMemberAddressMapper, MallMemberAddress, MallMemberAddressQueryVO> implements MallMemberAddressService {

    private static final int DEFAULT_ADDRESS = 1;
    private static final int NOT_DEFAULT_ADDRESS = 0;
    private static final String IDS_SEPARATOR = ",";

    @Override
    @Transactional(readOnly = false)
    public void createAddress(MallMemberAddress entity) {
        if (entity.getIsDefault() != null && entity.getIsDefault() == DEFAULT_ADDRESS) {
            cancelDefaultAddress(entity.getMemberId());
        }
        save(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateAddress(MallMemberAddress entity) {
        if (entity.getIsDefault() != null && entity.getIsDefault() == DEFAULT_ADDRESS) {
            cancelDefaultAddress(entity.getMemberId());
        }
        update(entity);
    }

    @Override
    public List<MallMemberAddress> getMemberAddresses(String memberId) {
        MallMemberAddressQueryVO query = new MallMemberAddressQueryVO();
        query.setMemberId(memberId);
        return findList(query);
    }

    @Override
    public MallMemberAddress getDefaultAddress(String memberId) {
        MallMemberAddressQueryVO query = new MallMemberAddressQueryVO();
        query.setMemberId(memberId);
        query.setIsDefault(DEFAULT_ADDRESS);
        List<MallMemberAddress> addresses = findList(query);
        return addresses.isEmpty() ? null : addresses.get(0);
    }

    @Override
    @Transactional(readOnly = false)
    public void setDefaultAddress(String addressId, String memberId) {
        cancelDefaultAddress(memberId);
        MallMemberAddress address = get(addressId);
        if (address != null && address.getMemberId().equals(memberId)) {
            address.setIsDefault(DEFAULT_ADDRESS);
            update(address);
        }
    }

    @Override
    @Transactional(readOnly = false)
    public void cancelDefaultAddress(String memberId) {
        MallMemberAddressQueryVO query = new MallMemberAddressQueryVO();
        query.setMemberId(memberId);
        query.setIsDefault(DEFAULT_ADDRESS);
        List<MallMemberAddress> defaultAddresses = findList(query);
        for (MallMemberAddress address : defaultAddresses) {
            address.setIsDefault(NOT_DEFAULT_ADDRESS);
            update(address);
        }
    }

    @Override
    public String getFullAddress(MallMemberAddress address) {
        if (address == null) {
            return "";
        }
        return String.format("%s%s%s%s",
                address.getProvince() != null ? address.getProvince() : "",
                address.getCity() != null ? address.getCity() : "",
                address.getDistrict() != null ? address.getDistrict() : "",
                address.getDetailAddress() != null ? address.getDetailAddress() : "");
    }
}
