ExtStoreServiceImpl.java 3.22 KB
package com.diligrp.rider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.diligrp.rider.common.exception.BizException;
import com.diligrp.rider.entity.ExtStore;
import com.diligrp.rider.mapper.ExtStoreMapper;
import com.diligrp.rider.service.ExtStoreService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ExtStoreServiceImpl implements ExtStoreService {

    private final ExtStoreMapper extStoreMapper;

    @Override
    public ExtStore syncStore(String appKey, ExtStore store) {
        store.setAppKey(appKey);
        long now = System.currentTimeMillis() / 1000;

        // 根据 appKey + outStoreId 判断是新增还是更新
        ExtStore existing = extStoreMapper.selectOne(new LambdaQueryWrapper<ExtStore>()
                .eq(ExtStore::getAppKey, appKey)
                .eq(ExtStore::getOutStoreId, store.getOutStoreId())
                .last("LIMIT 1"));

        if (existing == null) {
            store.setStatus(store.getStatus() != null ? store.getStatus() : 1);
            store.setCreateTime(now);
            store.setUpdateTime(now);
            extStoreMapper.insert(store);
        } else {
            store.setId(existing.getId());
            store.setUpdateTime(now);
            extStoreMapper.updateById(store);
        }
        return extStoreMapper.selectById(store.getId());
    }

    @Override
    public List<ExtStore> listByApp(String appKey) {
        return extStoreMapper.selectList(new LambdaQueryWrapper<ExtStore>()
                .eq(ExtStore::getAppKey, appKey)
                .eq(ExtStore::getStatus, 1)
                .orderByDesc(ExtStore::getId));
    }

    @Override
    public ExtStore getById(Long id, String appKey) {
        ExtStore store = extStoreMapper.selectById(id);
        if (store == null) throw new BizException("门店不存在");
        if (appKey != null && !appKey.equals(store.getAppKey())) throw new BizException("无权访问该门店");
        return store;
    }

    @Override
    public void setStatus(Long id, String appKey, int status) {
        ExtStore store = getById(id, appKey);
        extStoreMapper.update(null, new LambdaUpdateWrapper<ExtStore>()
                .eq(ExtStore::getId, id)
                .set(ExtStore::getStatus, status)
                .set(ExtStore::getUpdateTime, System.currentTimeMillis() / 1000));
    }

    @Override
    public void delete(Long id, String appKey) {
        getById(id, appKey);
        extStoreMapper.deleteById(id);
    }

    // 平台管理端:不限 appKey 查看所有门店
    @Override
    public List<ExtStore> listAll(String appKey, Long cityId, int page) {
        LambdaQueryWrapper<ExtStore> wrapper = new LambdaQueryWrapper<ExtStore>()
                .orderByDesc(ExtStore::getId);
        if (appKey != null && !appKey.isBlank()) wrapper.eq(ExtStore::getAppKey, appKey);
        if (cityId != null && cityId > 0) wrapper.eq(ExtStore::getCityId, cityId);
        int offset = (page - 1) * 20;
        wrapper.last("LIMIT " + offset + ",20");
        return extStoreMapper.selectList(wrapper);
    }
}