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

import com.tmerclub.cloud.api.supplier.vo.SupplierApiRefundAddrVO;
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.supplier.dto.SupplierRefundAddrDTO;
import com.tmerclub.cloud.supplier.mapper.SupplierRefundAddrMapper;
import com.tmerclub.cloud.supplier.model.SupplierRefundAddr;
import com.tmerclub.cloud.supplier.service.SupplierRefundAddrService;
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 lhd
 * @date 2022-03-14 17:04:32
 */
@Service
public class SupplierRefundAddrServiceImpl implements SupplierRefundAddrService {

    @Autowired
    private SupplierRefundAddrMapper supplierRefundAddrMapper;

    @Override
    public PageVO<SupplierApiRefundAddrVO> page(PageDTO pageDTO, SupplierRefundAddrDTO supplierRefundAddrDTO) {
        return PageUtil.doPage(pageDTO, () -> supplierRefundAddrMapper.list(supplierRefundAddrDTO));
    }

    @Override
    public SupplierApiRefundAddrVO getBySupplierRefundAddrId(Long supplierRefundAddrId, Long supplierId) {
        return supplierRefundAddrMapper.getBySupplierRefundAddrId(supplierRefundAddrId, supplierId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SupplierRefundAddr supplierRefundAddr) {
        if (Objects.equals(supplierRefundAddr.getIsDefault(), 1)) {
            supplierRefundAddrMapper.cancelDefaultAddr(supplierRefundAddr.getSupplierId());
        } else {
            // 第一个地址自动为默认地址
            List<SupplierApiRefundAddrVO> refundAddrs = this.listBySupplierId(supplierRefundAddr.getSupplierId());
            if (CollectionUtils.isEmpty(refundAddrs)) {
                supplierRefundAddr.setIsDefault(1);
            }
        }
        supplierRefundAddrMapper.save(supplierRefundAddr);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SupplierRefundAddr supplierRefundAddr) {
        SupplierApiRefundAddrVO dbSupplierRefund = getBySupplierRefundAddrId(supplierRefundAddr.getSupplierRefundAddrId(), supplierRefundAddr.getSupplierId());
        if (Objects.isNull(dbSupplierRefund)) {
            throw new LuckException(ResponseEnum.DATA_ERROR);
        } else if (!Objects.equals(supplierRefundAddr.getSupplierId(), dbSupplierRefund.getSupplierId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        if (!PrincipalUtil.isDbPhone(supplierRefundAddr.getMobile(), dbSupplierRefund.getMobile(), false)) {
            throw new LuckException("请输入正确的手机号");
        }
        if (supplierRefundAddr.getMobile().contains(Constant.ASTERISK)) {
            supplierRefundAddr.setMobile(dbSupplierRefund.getMobile());
        }
        // 取消默认
        if (Objects.equals(supplierRefundAddr.getIsDefault(), 0)
                && Objects.equals(dbSupplierRefund.getIsDefault(), 1)) {
            throw new LuckException("默认地址禁止取消");
        }
        // 设置默认
        if (Objects.equals(supplierRefundAddr.getIsDefault(), 1) && Objects.equals(dbSupplierRefund.getIsDefault(), 0)) {
            supplierRefundAddrMapper.cancelDefaultAddr(supplierRefundAddr.getSupplierId());
        }
        supplierRefundAddrMapper.update(supplierRefundAddr);
    }

    @Override
    public void deleteById(Long supplierRefundAddrId, Long supplierId) {
        SupplierApiRefundAddrVO dbSupplierRefund = getBySupplierRefundAddrId(supplierRefundAddrId, supplierId);
        if (Objects.isNull(dbSupplierRefund)) {
            throw new LuckException(ResponseEnum.DATA_ERROR);
        } else if (!Objects.equals(dbSupplierRefund.getSupplierId(), supplierId)) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        if (Objects.equals(dbSupplierRefund.getIsDefault(), 1)) {
            throw new LuckException("默认地址禁止删除");
        }
        supplierRefundAddrMapper.deleteById(supplierRefundAddrId);
    }

    @Override
    @Cacheable(cacheNames = CacheNames.SUPPLIER_REFUND_ADDR, key = "#supplierId", sync = true)
    public List<SupplierApiRefundAddrVO> listBySupplierId(Long supplierId) {
        return supplierRefundAddrMapper.listBySupplierId(supplierId);
    }

    @Override
    @CacheEvict(cacheNames = CacheNames.SUPPLIER_REFUND_ADDR, key = "#supplierId")
    public void removeCacheBySupplierId(Long supplierId) {
    }
}
