
package com.tmerclub.cloud.multishop.service.impl;

import com.tmerclub.cloud.api.multishop.vo.ShopRefundAddrVO;
import com.tmerclub.cloud.common.cache.constant.CacheNames;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.util.PageUtil;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.util.PrincipalUtil;
import com.tmerclub.cloud.multishop.dto.ShopRefundAddrDTO;
import com.tmerclub.cloud.multishop.mapper.ShopRefundAddrMapper;
import com.tmerclub.cloud.multishop.model.ShopRefundAddr;
import com.tmerclub.cloud.multishop.service.ShopRefundAddrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * 商家退货地址
 *
 * @author tmerclub
 * @date 2021-03-09 15:03:18
 */
@Service
public class ShopRefundAddrServiceImpl implements ShopRefundAddrService {

    @Autowired
    private ShopRefundAddrMapper shopRefundAddrMapper;

    @Override
    public PageVO<ShopRefundAddrVO> page(PageDTO pageDTO, ShopRefundAddrDTO shopRefundAddrDTO) {
        return PageUtil.doPage(pageDTO, () -> shopRefundAddrMapper.list(shopRefundAddrDTO));
    }

    @Override
    public ShopRefundAddrVO getByShopRefundAddrId(Long shopRefundAddrId, Long shopId) {
        return shopRefundAddrMapper.getByShopRefundAddrId(shopRefundAddrId, shopId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(ShopRefundAddr shopRefundAddr) {
        if (Objects.equals(shopRefundAddr.getIsDefault(), 1)) {
            shopRefundAddrMapper.cancelDefaultAddr(shopRefundAddr.getShopId());
        } else {
            // 第一个地址自动为默认地址
            List<ShopRefundAddrVO> refundAddrs = this.listByShopId(shopRefundAddr.getShopId());
            if (CollectionUtils.isEmpty(refundAddrs)) {
                shopRefundAddr.setIsDefault(1);
            }
        }
        shopRefundAddrMapper.save(shopRefundAddr);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ShopRefundAddr shopRefundAddr) {
        ShopRefundAddrVO dbShopRefund = getByShopRefundAddrId(shopRefundAddr.getShopRefundAddrId(), shopRefundAddr.getShopId());
        if (Objects.isNull(dbShopRefund)) {
            throw new LuckException(ResponseEnum.DATA_ERROR);
        } else if (!Objects.equals(shopRefundAddr.getShopId(), dbShopRefund.getShopId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        if (!PrincipalUtil.isDbPhone(shopRefundAddr.getMobile(), dbShopRefund.getMobile(), false)) {
            throw new LuckException("请输入正确的手机号");
        }
        if (shopRefundAddr.getMobile().contains(Constant.ASTERISK)) {
            shopRefundAddr.setMobile(dbShopRefund.getMobile());
        }
        // 取消默认
        if (Objects.equals(shopRefundAddr.getIsDefault(), 0)
                && Objects.equals(dbShopRefund.getIsDefault(), 1)) {
            throw new LuckException("默认地址禁止取消");
        }
        // 设置默认
        if (Objects.equals(shopRefundAddr.getIsDefault(), 1) && Objects.equals(dbShopRefund.getIsDefault(), 0)) {
            shopRefundAddrMapper.cancelDefaultAddr(shopRefundAddr.getShopId());
        }
        shopRefundAddrMapper.update(shopRefundAddr);
    }

    @Override
    public void deleteById(Long shopRefundAddrId, Long shopId) {
        ShopRefundAddrVO dbShopRefund = getByShopRefundAddrId(shopRefundAddrId, shopId);
        if (Objects.isNull(dbShopRefund)) {
            throw new LuckException(ResponseEnum.DATA_ERROR);
        } else if (!Objects.equals(dbShopRefund.getShopId(), shopId)) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        if (Objects.equals(dbShopRefund.getIsDefault(), 1)) {
            throw new LuckException("默认地址禁止删除");
        }
        shopRefundAddrMapper.deleteById(shopRefundAddrId);
    }

    @Override
    @Cacheable(cacheNames = CacheNames.SHOP_REFUND_ADDR, key = "#shopId", sync = true)
    public List<ShopRefundAddrVO> listByShopId(Long shopId) {
        return shopRefundAddrMapper.listByShopId(shopId);
    }

    @Override
    @CacheEvict(cacheNames = CacheNames.SHOP_REFUND_ADDR, key = "#shopId")
    public void removeCacheByShopId(Long shopId) {
    }
}
