package com.ruoyi.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.biz.domain.bo.ArStoreBo;
import com.ruoyi.biz.domain.vo.ArStoreVo;
import com.ruoyi.biz.domain.ArStore;
import com.ruoyi.biz.mapper.ArStoreMapper;
import com.ruoyi.biz.service.IArStoreService;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 门店Service业务层处理
 *
 * @author gwd
 * @date 2023-07-11
 */
@RequiredArgsConstructor
@Service
public class ArStoreServiceImpl implements IArStoreService {

    private final ArStoreMapper baseMapper;

    /**
     * 查询门店
     */
    @Override
    public ArStoreVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询门店列表
     */
    @Override
    public TableDataInfo<ArStoreVo> queryPageList(ArStoreBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ArStore> lqw = buildQueryWrapper(bo);
        Page<ArStoreVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询门店列表
     */
    @Override
    public List<ArStoreVo> queryList(ArStoreBo bo) {
        LambdaQueryWrapper<ArStore> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ArStore> buildQueryWrapper(ArStoreBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ArStore> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), ArStore::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), ArStore::getAddress, bo.getAddress());
        lqw.eq(bo.getLatitude() != null, ArStore::getLatitude, bo.getLatitude());
        lqw.eq(bo.getLongitude() != null, ArStore::getLongitude, bo.getLongitude());
        return lqw;
    }

    /**
     * 新增门店
     */
    @Override
    public Boolean insertByBo(ArStoreBo bo) {
        ArStore add = BeanUtil.toBean(bo, ArStore.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改门店
     */
    @Override
    public Boolean updateByBo(ArStoreBo bo) {
        ArStore update = BeanUtil.toBean(bo, ArStore.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ArStore entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除门店
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<ArStoreVo> getStoreList(BigDecimal latitude, BigDecimal longitude) {
        List<ArStoreVo> list = baseMapper.getStoreList(latitude,longitude);
        list.forEach(arStoreVo -> {
            if(arStoreVo.getDistance().compareTo(new BigDecimal(1000)) >= 0){
                arStoreVo.setDistance(arStoreVo.getDistance().divide(new BigDecimal(1000),2,BigDecimal.ROUND_HALF_UP));
                arStoreVo.setUnit("km");
            }else{
                arStoreVo.setDistance(arStoreVo.getDistance().divide(new BigDecimal(1),2,BigDecimal.ROUND_HALF_UP));
                arStoreVo.setUnit("m");
            }
        });
        return list;
    }
}
