package com.ruoyi.web.modules.account.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.account.constant.MemberConstants;
import com.ruoyi.web.modules.account.domain.BizMemberAddress;
import com.ruoyi.web.modules.account.mapper.BizMemberAddressMapper;
import com.ruoyi.web.modules.account.service.IMemberAddressService;
import com.ruoyi.web.modules.account.vo.MemberAddressVO;

/**
 * 会员地址 Service 实现
 */
@Service
public class MemberAddressServiceImpl implements IMemberAddressService
{
    @Autowired
    private BizMemberAddressMapper addressMapper;

    @Override
    public List<MemberAddressVO> listByMember(Long memberId)
    {
        List<MemberAddressVO> list = new ArrayList<>();
        for (BizMemberAddress addr : addressMapper.selectByMemberId(memberId))
        {
            list.add(toVo(addr));
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long addAddress(Long memberId, BizMemberAddress address)
    {
        validateAddress(address);
        address.setMemberId(memberId);
        if (MemberConstants.DEFAULT_YES.equals(address.getIsDefault()))
        {
            addressMapper.clearDefault(memberId);
        }
        else
        {
            address.setIsDefault(MemberConstants.DEFAULT_NO);
        }
        addressMapper.insert(address);
        return address.getAddressId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAddress(Long memberId, BizMemberAddress address)
    {
        if (address.getAddressId() == null)
        {
            throw new ServiceException("地址ID不能为空");
        }
        BizMemberAddress existing = addressMapper.selectById(address.getAddressId(), memberId);
        if (existing == null)
        {
            throw new ServiceException("地址不存在");
        }
        validateAddress(address);
        address.setMemberId(memberId);
        if (MemberConstants.DEFAULT_YES.equals(address.getIsDefault()))
        {
            addressMapper.clearDefault(memberId);
        }
        addressMapper.update(address);
    }

    @Override
    public void deleteAddress(Long memberId, Long addressId)
    {
        if (addressMapper.logicDelete(addressId, memberId) == 0)
        {
            throw new ServiceException("地址不存在");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setDefaultAddress(Long memberId, Long addressId)
    {
        BizMemberAddress existing = addressMapper.selectById(addressId, memberId);
        if (existing == null)
        {
            throw new ServiceException("地址不存在");
        }
        addressMapper.clearDefault(memberId);
        addressMapper.setDefault(addressId, memberId);
    }

    private void validateAddress(BizMemberAddress address)
    {
        if (StringUtils.isEmpty(address.getConsigneeName()))
        {
            throw new ServiceException("请输入收货人");
        }
        if (StringUtils.isEmpty(address.getMobile()))
        {
            throw new ServiceException("请输入联系电话");
        }
        if (StringUtils.isEmpty(address.getRegionCode()) || StringUtils.isEmpty(address.getRegionName()))
        {
            throw new ServiceException("请选择所在地区");
        }
        if (StringUtils.isEmpty(address.getDetailAddress()))
        {
            throw new ServiceException("请输入详细地址");
        }
    }

    private MemberAddressVO toVo(BizMemberAddress addr)
    {
        MemberAddressVO vo = new MemberAddressVO();
        vo.setAddressId(addr.getAddressId());
        vo.setConsigneeName(addr.getConsigneeName());
        vo.setMobile(addr.getMobile());
        vo.setRegionCode(addr.getRegionCode());
        vo.setRegionName(addr.getRegionName());
        vo.setDetailAddress(addr.getDetailAddress());
        vo.setIsDefault(addr.getIsDefault());
        vo.setFullAddress(buildFullAddress(addr.getRegionName(), addr.getDetailAddress()));
        return vo;
    }

    private String buildFullAddress(String regionName, String detailAddress)
    {
        String region = StringUtils.isEmpty(regionName) ? "" : regionName.replace("/", "");
        return region + (detailAddress != null ? detailAddress : "");
    }
}
