package co.yixiang.yshop.module.shop.service.shop;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.util.distancecalculator.DistanceCalculatorUtil;
import co.yixiang.yshop.module.shop.controller.admin.shop.vo.*;
import co.yixiang.yshop.module.shop.controller.app.vo.ShopDTO;
import co.yixiang.yshop.module.shop.convert.shop.ShopConvert;
import co.yixiang.yshop.module.shop.dal.dataobject.shop.ShopDO;
import co.yixiang.yshop.module.shop.dal.mysql.shop.ShopMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.shop.enums.ErrorCodeConstants.SHOP_NOT_EXISTS;


/**
 * 门店 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class ShopServiceImpl implements ShopService {

    @Resource
    private ShopMapper shopMapper;

    @Override
    public Long createShop(ShopCreateReqVO createReqVO) {
        // 插入
        ShopDO shop = ShopConvert.INSTANCE.convert(createReqVO);
        shopMapper.insert(shop);
        // 返回
        return shop.getId();
    }

    @Override
    public void updateShop(ShopUpdateReqVO updateReqVO) {
        // 校验存在
        validateShopExists(updateReqVO.getId());
        // 更新
        ShopDO updateObj = ShopConvert.INSTANCE.convert(updateReqVO);
        shopMapper.updateById(updateObj);
    }

    @Override
    public void deleteShop(Long id) {
        // 校验存在
        validateShopExists(id);
        // 删除
        shopMapper.deleteById(id);
    }

    private void validateShopExists(Long id) {
        if (shopMapper.selectById(id) == null) {
            throw exception(SHOP_NOT_EXISTS);
        }
    }

    @Override
    public ShopDO getShop(Long id) {
        return shopMapper.selectById(id);
    }

    @Override
    public List<ShopDO> getShopList(Collection<Long> ids) {
        return shopMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ShopDO> getShopPage(ShopPageReqVO pageReqVO) {
        return shopMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopDO> getShopList(ShopExportReqVO exportReqVO) {
        return shopMapper.selectList(exportReqVO);
    }

    @Override
    public List<ShopRespVO> getShopList4App(ShopDTO dto) {
        //处理核销时间到期的门店
        handlerExpireShop();

        Double lat = dto.getLat();
        Double lon = dto.getLon();
        //查询启用的门店
        ShopExportReqVO reqVO = new ShopExportReqVO();
        reqVO.setStatus(Boolean.TRUE);
        List<ShopDO> shopList = this.getShopList(reqVO);
        List<ShopRespVO> shopRespVOS = ShopConvert.INSTANCE.convertList(shopList);
        return shopRespVOS.stream()
                .peek(item -> {
                    double distance = DistanceCalculatorUtil.calculateDistance(lat, lon, item.getLat(), item.getLon());
                    item.setDistance((int) distance);
                })
                .sorted(Comparator.comparing(ShopRespVO::getDistance))
                .collect(Collectors.toList());

    }

    /**
     * 更改核销时间到期门店的状态
     */
    private void handlerExpireShop() {
        shopMapper.handlerExpireShop();
    }

}
