package com.bohuikeji.delivery.backstage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.entity.TCustomer;
import com.bohuikeji.delivery.backstage.entity.TCustomerAddress;
import com.bohuikeji.delivery.backstage.mapper.TCustomerAddressMapper;
import com.bohuikeji.delivery.backstage.service.ITCustomerAddressService;
import com.bohuikeji.frame.module.admin.base.ServiceImplBase;
import com.bohuikeji.frame.module.admin.entity.vo.AddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 客户地址管理表 服务实现类
 * </p>
 *
 * @author 10552
 * @since 2019-08-22
 */
@Service
public class TCustomerAddressServiceImpl extends ServiceImplBase<TCustomerAddressMapper, TCustomerAddress> implements ITCustomerAddressService {
    @Autowired
    ITCustomerAddressService customerAddressService;

    /**
     * 客户修改地址
     * @param custAddId
     * @param name
     * @param phone
     * @param addressJson
     * @param defaultAddress
     * @param locationAddress
     * @return
     */
    @Override
    public TCustomerAddress updateAdress(Integer custAddId,String name, String phone, AddressVo addressJson, String defaultAddress,String locationAddress) {
        TCustomerAddress tCustomerAddress = customerAddressService.getById(custAddId);
        tCustomerAddress.setName(name);
        tCustomerAddress.setPhone(phone);
        tCustomerAddress.setAddressJson(addressJson);
        //tCustomerAddress.setDefaultAddress(defaultAddress);
        tCustomerAddress.setLocationAddress(locationAddress);
        customerAddressService.updateById(tCustomerAddress);
        return tCustomerAddress;
    }

    @Override
    public void setLogoList(String logoUrl, TCustomer customer) {
        List<TCustomerAddress> customerAddresses = customerAddressService.list(new QueryWrapper<TCustomerAddress>().eq("customer_id",customer.getCustomerId()).eq("type","收件地址").isNull("logo_url"));
        if(customerAddresses.size()>0){
            for (TCustomerAddress customerAddress : customerAddresses){
                customerAddressService.updateById(customerAddress.setLogoUrl(logoUrl));
            }
        }
    }
}
