MerchantServiceImpl.java 6.72 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.dto.MerchantStoreDTO;
import com.diligrp.rider.entity.MerchantStore;
import com.diligrp.rider.entity.MerchantUsers;
import com.diligrp.rider.mapper.MerchantStoreMapper;
import com.diligrp.rider.mapper.MerchantUsersMapper;
import com.diligrp.rider.service.CityService;
import com.diligrp.rider.service.MerchantService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
public class MerchantServiceImpl implements MerchantService {

    private final MerchantStoreMapper storeMapper;
    private final MerchantUsersMapper usersMapper;
    private final CityService cityService;

    private static final int PAGE_SIZE = 20;

    @Override
    @Transactional
    public Long addStore(MerchantStoreDTO dto) {
        var city = cityService.getById(dto.getCityId());
        if (city == null) throw new BizException("城市不存在");

        MerchantStore store = toEntity(dto);
        store.setAddTime(System.currentTimeMillis() / 1000);
        store.setIsDel(0);
        storeMapper.insert(store);

        if (dto.getAccountMobile() != null && !dto.getAccountMobile().isBlank()) {
            Long exists = usersMapper.selectCount(new LambdaQueryWrapper<MerchantUsers>()
                    .eq(MerchantUsers::getMobile, dto.getAccountMobile()));
            if (exists > 0) throw new BizException("该手机号已有账号");
            MerchantUsers user = new MerchantUsers();
            user.setStoreId(store.getId());
            user.setMobile(dto.getAccountMobile());
            user.setUserNickname(dto.getName());
            user.setUserStatus(1);
            user.setType(1);
            user.setCreateTime(System.currentTimeMillis() / 1000);
            usersMapper.insert(user);
        }
        return store.getId();
    }

    @Override
    public void editStore(MerchantStoreDTO dto) {
        MerchantStore existing = storeMapper.selectById(dto.getId());
        if (existing == null) throw new BizException("店铺不存在");
        MerchantStore store = toEntity(dto);
        store.setId(dto.getId());
        storeMapper.updateById(store);
    }

    @Override
    public List<MerchantStore> storeList(Long cityId, String keyword, int page) {
        LambdaQueryWrapper<MerchantStore> wrapper = new LambdaQueryWrapper<MerchantStore>()
                .eq(MerchantStore::getIsDel, 0)
                .orderByAsc(MerchantStore::getListOrder)
                .orderByDesc(MerchantStore::getId);
        if (cityId != null && cityId > 0) wrapper.eq(MerchantStore::getCityId, cityId);
        if (keyword != null && !keyword.isBlank()) wrapper.like(MerchantStore::getName, keyword);
        int offset = (page - 1) * PAGE_SIZE;
        wrapper.last("LIMIT " + offset + "," + PAGE_SIZE);
        return storeMapper.selectList(wrapper);
    }

    @Override
    public MerchantStore getStore(Long storeId) {
        return storeMapper.selectById(storeId);
    }

    @Override
    public void setOperatingState(Long storeId, int state) {
        storeMapper.update(null, new LambdaUpdateWrapper<MerchantStore>()
                .eq(MerchantStore::getId, storeId)
                .set(MerchantStore::getOperatingState, state));
    }

    @Override
    public void setAutoOrder(Long storeId, int auto) {
        storeMapper.update(null, new LambdaUpdateWrapper<MerchantStore>()
                .eq(MerchantStore::getId, storeId)
                .set(MerchantStore::getAutomaticOrder, auto));
    }

    @Override
    public void updateFeeConfig(Long storeId, BigDecimal freeShipping, BigDecimal upToSend) {
        storeMapper.update(null, new LambdaUpdateWrapper<MerchantStore>()
                .eq(MerchantStore::getId, storeId)
                .set(MerchantStore::getFreeShipping, freeShipping)
                .set(MerchantStore::getUpToSend, upToSend));
    }

    @Override
    public void delStore(Long storeId) {
        storeMapper.update(null, new LambdaUpdateWrapper<MerchantStore>()
                .eq(MerchantStore::getId, storeId)
                .set(MerchantStore::getIsDel, 1));
    }

    @Override
    @Transactional
    public MerchantStore syncStore(String appKey, MerchantStoreDTO dto) {
        if (appKey == null || appKey.isBlank()) throw new BizException("AppKey不能为空");
        if (dto.getOutStoreId() == null || dto.getOutStoreId().isBlank()) throw new BizException("外部门店编号不能为空");

        long now = System.currentTimeMillis() / 1000;
        MerchantStore existing = getByOutStoreId(appKey, dto.getOutStoreId());

        if (existing == null) {
            MerchantStore store = toEntity(dto);
            store.setAppKey(appKey);
            store.setAddTime(now);
            store.setIsDel(0);
            storeMapper.insert(store);
            return store;
        } else {
            MerchantStore store = toEntity(dto);
            store.setId(existing.getId());
            store.setAppKey(appKey);
            storeMapper.updateById(store);
            return storeMapper.selectById(existing.getId());
        }
    }

    @Override
    public MerchantStore getByOutStoreId(String appKey, String outStoreId) {
        return storeMapper.selectOne(new LambdaQueryWrapper<MerchantStore>()
                .eq(MerchantStore::getAppKey, appKey)
                .eq(MerchantStore::getOutStoreId, outStoreId)
                .eq(MerchantStore::getIsDel, 0)
                .last("LIMIT 1"));
    }

    private MerchantStore toEntity(MerchantStoreDTO dto) {
        MerchantStore store = new MerchantStore();
        store.setName(dto.getName());
        store.setCityId(dto.getCityId());
        store.setThumb(dto.getThumb());
        store.setAddress(dto.getAddress());
        store.setLng(dto.getLng());
        store.setLat(dto.getLat());
        store.setOperatingState(dto.getOperatingState() != null ? dto.getOperatingState() : 1);
        store.setAutomaticOrder(dto.getAutomaticOrder() != null ? dto.getAutomaticOrder() : 0);
        store.setShippingType(dto.getShippingType() != null ? dto.getShippingType() : 1);
        store.setFreeShipping(dto.getFreeShipping() != null ? dto.getFreeShipping() : BigDecimal.ZERO);
        store.setUpToSend(dto.getUpToSend() != null ? dto.getUpToSend() : BigDecimal.ZERO);
        store.setOpenDate(dto.getOpenDate());
        store.setOpenTime(dto.getOpenTime());
        store.setAbout(dto.getAbout());
        store.setOutStoreId(dto.getOutStoreId());
        return store;
    }
}