package com.neusoft.smart.pos.address.service;

import com.neusoft.smart.pos.address.constants.AddressErrorCode;
import com.neusoft.smart.pos.address.dto.PostDeleteUserCargoAddressRequest;
import com.neusoft.smart.pos.address.dto.PostUpdateUserCargoAddressRequest;
import com.neusoft.smart.pos.address.dto.PostUserCargoAddressRequest;
import com.neusoft.smart.pos.address.persistent.domain.UserCargoAddress;
import com.neusoft.smart.pos.address.repository.UserCargoAddressRepository;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

@Service
public class UserCargoAddressService {

    @Resource
    private UserCargoAddressRepository userCargoAddressRepository;

    public void insertUserCargoAddress(Integer userId, List<PostUserCargoAddressRequest.UserCargoAddressInfo> addressInfoList) {
        for (PostUserCargoAddressRequest.UserCargoAddressInfo userCargoAddressInfo : addressInfoList ) {
            UserCargoAddress address = new UserCargoAddress();
            BeanUtils.copyProperties(userCargoAddressInfo, address);
            address.setUserId(userId);
            verifyPriority(userId, address);
            userCargoAddressRepository.insertSelective(address);
        }
    }

    private void verifyPriority(Integer userId, UserCargoAddress address) {
        List<UserCargoAddress> defaultAddressList = userCargoAddressRepository.selectAddress(userId, address.getAddressType(), 0);
        if(defaultAddressList == null || defaultAddressList.size() == 0) {  // 没有默认地址则设置为默认地址
            address.setPriority(0);
        } else {
            if(address.getPriority() == 0) {    // 先把原来的默认地址降低优先级
                for (UserCargoAddress defaultAddress: defaultAddressList) {
                    defaultAddress.setPriority(1);
                    userCargoAddressRepository.updateByPrimaryKeySelective(defaultAddress);
                }
            }
        }
    }

    public List<UserCargoAddress> getUserCargoAddressByUserId(Integer userId) {
        return userCargoAddressRepository.selectByUserId(userId);
    }

    public List<UserCargoAddress> getUserCargoAddressByUserId(Integer userId, Integer addressType, Integer priority) {
        if(priority == -1) { // all priority
            if(addressType == -1) { // all type
                return userCargoAddressRepository.selectByUserId(userId);
            } else {
                return userCargoAddressRepository.selectByUserIdAndAddressType(userId, addressType);
            }
        } else {
            if(addressType == -1) { // all type
                return userCargoAddressRepository.selectByUserIdAndPriority(userId,priority);
            } else {
                return userCargoAddressRepository.selectAddress(userId, addressType, priority);
            }
        }
    }

    public UserCargoAddress getUserCargoAddressById(Integer addressId) {
        return userCargoAddressRepository.selectById(addressId);
    }

    public void updateUserAddressById(PostUpdateUserCargoAddressRequest request) {
        if(request.getAddressId() == null) {
            throw new BusinessCommonException(AddressErrorCode.ADDRESS_ID_EMPTY);
        }
        UserCargoAddress address = new UserCargoAddress();
        BeanUtils.copyProperties(request, address);
        verifyPriorityForUpdate(address);
        userCargoAddressRepository.updateByPrimaryKeySelective(address);
    }

    private void verifyPriorityForUpdate(UserCargoAddress address) {
        UserCargoAddress userCargoAddress = userCargoAddressRepository.selectById(address.getAddressId());
        if(userCargoAddress == null || address.getUserId().intValue() != userCargoAddress.getUserId().intValue()) {
            throw new BusinessCommonException(AddressErrorCode.NOT_YOUR_ADDRESS);
        }
        if(userCargoAddress.getPriority() == 0) {   // 不能单独取消默认地址
            address.setPriority(0);
        } else {
            if(address.getPriority() != null && address.getPriority() == 0) { // 非默认地址变更为默认地址，需要先把默认地址降低优先级
                List<UserCargoAddress> defaultAddressList = userCargoAddressRepository.selectAddress(userCargoAddress.getUserId(), userCargoAddress.getAddressType(), 0);
                if(defaultAddressList != null) {
                    for (UserCargoAddress defaultAddress: defaultAddressList) {
                        defaultAddress.setPriority(1);
                        userCargoAddressRepository.updateByPrimaryKeySelective(defaultAddress);
                    }
                }
            }
        }
    }

    public Integer deleteUserAddressById(PostDeleteUserCargoAddressRequest request) {
        if(request.getAddressId() == null) {
            throw new BusinessCommonException(AddressErrorCode.ADDRESS_ID_EMPTY);
        }
        if(request.getUserId() == null) {
            throw new BusinessCommonException(AddressErrorCode.USER_ID_EMPTY);
        }
        UserCargoAddress userCargoAddress = userCargoAddressRepository.selectById(request.getAddressId());
        if(userCargoAddress == null || userCargoAddress.getUserId().intValue() != request.getUserId().intValue()) {
            throw new BusinessCommonException(AddressErrorCode.NOT_YOUR_ADDRESS);
        }
        return userCargoAddressRepository.deleteCargoAddress(request.getAddressId());
    }

}
