package com.fzm.mall.server.admin.distribution.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.distribution.entity.dto.AddressDTO;
import com.fzm.mall.server.admin.distribution.entity.dto.AddressSaveDTO;
import com.fzm.mall.server.admin.distribution.entity.dto.AddressUpdateDTO;
import com.fzm.mall.server.admin.distribution.mapper.AddressMapper;
import com.fzm.mall.server.admin.distribution.model.Address;
import com.fzm.mall.server.admin.distribution.service.IAddressService;
import com.fzm.mall.server.admin.exception.MyException;
import com.fzm.mall.server.admin.util.CopyUtil;
import com.fzm.mall.server.admin.util.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 物流中心-地址 服务实现类
 * </p>
 *
 * @author wulixian
 * @since 2021-03-03
 */
@Service
@Transactional
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    @Resource
    private AddressMapper addressMapper;

    @Override
    public void save(AddressSaveDTO addressDTO) {
        Address address = CopyUtil.to(addressDTO, Address.class);
        address.setAddressId(UUIDUtil.getUUID());
        if(address.getIsDefault()){
            updateOtherNotDefault(address.getMerchantId());
        }
        addressMapper.insert(address);
    }

    /**
     * 将其他的重置为非默认
     * @param merchantId
     */
    private void updateOtherNotDefault(String merchantId) {
        UpdateWrapper<Address> wrapper = new UpdateWrapper<>();
        wrapper.eq("merchant_id", merchantId);
        Address address = new Address();
        address.setMerchantId(merchantId);
        address.setIsDefault(false);
        addressMapper.update(address, wrapper);
    }

    @Override
    public void update(AddressUpdateDTO addressUpdateDTO) {
        Address address = CopyUtil.to(addressUpdateDTO, Address.class);
        if(address.getIsDefault()){
            updateOtherNotDefault(address.getMerchantId());
        }
        UpdateWrapper<Address> wrapper = new UpdateWrapper<>();
        wrapper.eq("address_id", address.getAddressId())
                .eq("merchant_id", address.getMerchantId())
                .eq("update_time", address.getUpdateTime());
        int update = addressMapper.update(address, wrapper);
        if(update == 0){
            throw new MyException(MallResponseError.UPDATE_FAIL);
        }
    }

    @Override
    public void delete(String addressId, String merchantId) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("address_id", addressId).eq("merchant_id", merchantId);
        addressMapper.delete(queryWrapper);
    }

    @Override
    public AddressDTO read(String addressId, String merchantId) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("address_id", addressId).eq("merchant_id", merchantId);
        Address address = addressMapper.selectOne(queryWrapper);
        return CopyUtil.to(address, AddressDTO.class);
    }

    @Override
    public List<AddressDTO> list(String merchantId) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("merchant_id", merchantId);
        List<Address> list = addressMapper.selectList(queryWrapper);
        return CopyUtil.toList(list, AddressDTO.class);
    }
}
