package com.younike.system.service.impl;

import com.younike.common.core.domain.AjaxResult;
import com.younike.common.utils.geo.DistanceUtil;
import com.younike.system.domain.MemberAddress;
import com.younike.system.domain.Merchant;
import com.younike.system.domain.vo.MemberAddressVo;
import com.younike.system.mapper.MemberAddressMapper;
import com.younike.system.param.MemberAddressParam;
import com.younike.system.service.IMemberAddressService;
import com.younike.common.utils.StringUtils;
import com.younike.system.service.IMerchantService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 会员服务地址Service业务层处理
 *
 * @author elev
 * @date 2022-01-04
 */
@Service
public class MemberAddressServiceImpl implements IMemberAddressService {
    @Autowired
    private MemberAddressMapper memberAddressMapper;

    @Autowired
   private IMerchantService merchantService;

    /**
     * 查询会员服务地址
     *
     * @param addressId 会员服务地址主键
     * @return 会员服务地址
     */
    @Override
    public MemberAddress selectMemberAddressByAddressId(Long addressId) {
        return memberAddressMapper.selectMemberAddressByAddressId(addressId);
    }

    /**
     * 查询会员服务地址列表
     *
     * @param memberAddress 会员服务地址
     * @return 会员服务地址
     */
    @Override
    public List<MemberAddress> selectMemberAddressList(MemberAddress memberAddress) {
        return memberAddressMapper.selectMemberAddressList(memberAddress);
    }

    /**
     * 新增会员服务地址
     *
     * @param memberAddress 会员服务地址
     * @return 结果
     */
    @Override
    public int insertMemberAddress(MemberAddress memberAddress) {
        memberAddress.setCreateTime(LocalDateTime.now());
        return memberAddressMapper.insertMemberAddress(memberAddress);
    }

    /**
     * 修改会员服务地址
     *
     * @param memberAddress 会员服务地址
     * @return 结果
     */
    @Override
    public int updateMemberAddress(MemberAddress memberAddress) {
        memberAddress.setUpdateTime(LocalDateTime.now());
        return memberAddressMapper.updateMemberAddress(memberAddress);
    }

    /**
     * 批量删除会员服务地址
     *
     * @param addressIds 需要删除的会员服务地址主键
     * @return 结果
     */
    @Override
    public int deleteMemberAddressByAddressIds(Long[] addressIds) {
        return memberAddressMapper.deleteMemberAddressByAddressIds(addressIds);
    }

    /**
     * 删除会员服务地址信息
     *
     * @param addressId 会员服务地址主键
     * @return 结果
     */
    @Override
    public int deleteMemberAddressByAddressId(Long addressId) {
        MemberAddress memberAddress = new MemberAddress();
        memberAddress.setUpdateTime(LocalDateTime.now());
        memberAddress.setAddressId(addressId);
        memberAddress.setDelFlag("2");
        return memberAddressMapper.updateMemberAddress(memberAddress);
//        return memberAddressMapper.deleteMemberAddressByAddressId(addressId);
    }

    /**
     * 获取会员地址列表
     *
     * @param param
     * @return
     */
    @Override
    public AjaxResult getAddressList(MemberAddressParam param) {
        //要返回的数据
        MemberAddressVo memberAddressVo = new MemberAddressVo();
        List<MemberAddress> addressList = memberAddressMapper.getAddressList(param.getMemberId());
        //查询为空
        if(ObjectUtils.isEmpty(addressList)){
            return AjaxResult.success(memberAddressVo);
        }

        //验证商家id是否为空
        if (StringUtils.isNotNull(param.getMerchantId())) {
            //根据商家id查询商家坐标以及服务范围
            Merchant merchant = merchantService.selectMerchantByMerchantId(param.getMerchantId());
            if(ObjectUtils.isEmpty(merchant)){
                memberAddressVo.setAddressList(addressList);
                return AjaxResult.success(memberAddressVo);
            }
            //验证服务范围是否为空
            if(!StringUtils.isNotNull(merchant.getServiceDistance())){
                memberAddressVo.setAddressList(addressList);
                return AjaxResult.success(memberAddressVo);
            }
            //会员地址集合
            List<MemberAddress> addressS = new ArrayList<MemberAddress>();
            //超出范围的会员地址集合
            List<MemberAddress> outAddressS = new ArrayList<MemberAddress>();

            //配送范围
            BigDecimal maxDeliverDistance = merchant.getServiceDistance();

            addressList.forEach(memberAddress -> {
                //商家与会员距离
                BigDecimal shortestDistance = null;
                // 计算距离
                shortestDistance = DistanceUtil.getShortestDistance(merchant.getLatitude(),
                        merchant.getLongitude(), memberAddress.getLatitude(), memberAddress.getLongitude());

                //验证坐标是否超出范围
             if(shortestDistance.compareTo(maxDeliverDistance)>0){
                 //超出配送范围
                 outAddressS.add(memberAddress);
                }else {
                 addressS.add(memberAddress);
                }

            });

            memberAddressVo.setAddressList(addressS);
            memberAddressVo.setOutAddressList(outAddressS);


        }

        return AjaxResult.success(memberAddressVo);
    }


}
