findme-backend-merchant-java/src/main/java/com/xjhs/findmemerchant/controller/StoreController.java

238 lines
9.6 KiB
Java
Raw Normal View History

2026-01-09 12:20:24 +08:00
package com.xjhs.findmemerchant.controller;
import com.xjhs.findmemerchant.common.ApiResult;
import com.xjhs.findmemerchant.common.PageData;
import com.xjhs.findmemerchant.common.jpa.query.JpaSpecs;
2026-01-09 12:20:44 +08:00
import com.xjhs.findmemerchant.common.mvc.PageVo;
import com.xjhs.findmemerchant.dto.store.BusinessPeriodDto;
import com.xjhs.findmemerchant.dto.store.StoreBusinessStatusDto;
2026-01-09 12:20:24 +08:00
import com.xjhs.findmemerchant.dto.store.StoreDto;
2026-01-09 12:20:44 +08:00
import com.xjhs.findmemerchant.entity.BusinessPeriod;
2026-01-09 12:20:24 +08:00
import com.xjhs.findmemerchant.entity.Merchant;
import com.xjhs.findmemerchant.entity.Store;
import com.xjhs.findmemerchant.mapper.StoreMapper;
2026-01-09 12:20:44 +08:00
import com.xjhs.findmemerchant.repository.BusinessPeriodRepository;
2026-01-09 12:20:24 +08:00
import com.xjhs.findmemerchant.repository.MerchantRepository;
import com.xjhs.findmemerchant.repository.StoreRepository;
2026-01-09 12:20:44 +08:00
import com.xjhs.findmemerchant.vo.store.BusinessPeriodVo;
import com.xjhs.findmemerchant.vo.store.StoreBusinessStatusUpdateVo;
2026-01-09 12:20:24 +08:00
import com.xjhs.findmemerchant.vo.store.StoreCreateVo;
import com.xjhs.findmemerchant.vo.store.StoreUpdateVo;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.jpa.domain.Specification;
2026-01-09 12:20:44 +08:00
import org.springframework.security.core.annotation.AuthenticationPrincipal;
2026-01-09 12:20:24 +08:00
import org.springframework.transaction.annotation.Transactional;
2026-01-09 12:20:44 +08:00
import org.springframework.validation.annotation.Validated;
2026-01-09 12:20:24 +08:00
import org.springframework.web.bind.annotation.*;
import java.security.Principal;
2026-01-09 12:20:44 +08:00
import java.util.List;
2026-01-09 12:20:24 +08:00
import java.util.Objects;
2026-01-09 12:20:44 +08:00
/**
* 门店管理接口
*/
2026-01-09 12:20:24 +08:00
@Slf4j
@RestController
@RequestMapping("/stores")
@RequiredArgsConstructor
public class StoreController {
private final MerchantRepository merchantRepository;
private final StoreRepository storeRepository;
private final StoreMapper storeMapper;
2026-01-09 12:20:44 +08:00
private final BusinessPeriodRepository businessPeriodRepository;
2026-01-09 12:20:24 +08:00
/**
* 门店信息(分页查询)
*
2026-01-09 12:20:44 +08:00
* @param pageVo 分页参数
* @param merchant @ignore 当前登录商家
2026-01-09 12:20:24 +08:00
* @return 分页数据信息
*/
@GetMapping
@Transactional(readOnly = true)
2026-01-09 12:20:44 +08:00
public ApiResult<PageData<StoreDto>> findPage(@AuthenticationPrincipal Merchant merchant, PageVo pageVo) {
2026-01-09 12:20:24 +08:00
var pageData = this.storeRepository.findAll(Specification.allOf(
JpaSpecs.eq("merchant.id", merchant.getId())
2026-01-09 12:20:44 +08:00
), pageVo.getPageable()).map(this.storeMapper::toDto);
2026-01-09 12:20:24 +08:00
return ApiResult.page(pageData.getTotalElements(), pageData.getContent());
}
/**
2026-01-09 12:20:44 +08:00
* 创建商家的门店
2026-01-09 12:20:24 +08:00
*
* @param vo 门店信息
2026-01-09 12:20:44 +08:00
* @param merchant @ignore 当前登录的商家
2026-01-09 12:20:24 +08:00
* @return 门店信息
*/
@PostMapping
@Transactional(rollbackFor = Exception.class)
2026-01-09 12:20:44 +08:00
public ApiResult<StoreDto> create(@AuthenticationPrincipal Merchant merchant,
@Valid @RequestBody StoreCreateVo vo) {
2026-01-09 12:20:24 +08:00
var store = this.storeMapper.toEntity(vo);
merchant.addStore(store);
this.storeRepository.save(store);
var dto = this.storeMapper.toDto(store);
return ApiResult.data(dto);
}
/**
* 查询门店详情
*
* @param storeId 门店id
2026-01-09 12:20:44 +08:00
* @param merchant @ignore 当前登录的商家
2026-01-09 12:20:24 +08:00
* @return 门店详情
*/
@GetMapping("/{storeId}")
@Transactional(readOnly = true)
2026-01-09 12:20:44 +08:00
public ApiResult<StoreDto> findById(@AuthenticationPrincipal Merchant merchant,
@PathVariable("storeId") String storeId) {
2026-01-09 12:20:24 +08:00
var storeIdLong = Long.parseLong(storeId);
return merchant.getStores().stream()
.filter(x -> Objects.equals(x.getId(), storeIdLong))
.map(this.storeMapper::toDto)
.map(ApiResult::data)
.findFirst()
.orElse(ApiResult.fail("门店信息不存在"));
}
/**
* 更新门店信息
*
* @param storeId 门店id
2026-01-09 12:20:44 +08:00
* @param merchant @ignore 当前登录的商家
2026-01-09 12:20:24 +08:00
* @param vo 更新对象
* @return 门店信息
*/
@PutMapping("/{storeId}")
@Transactional(rollbackFor = Exception.class)
2026-01-09 12:20:44 +08:00
public ApiResult<StoreDto> updateById(@AuthenticationPrincipal Merchant merchant,
@PathVariable("storeId") String storeId,
@Valid @RequestBody StoreUpdateVo vo) {
2026-01-09 12:20:24 +08:00
var storeIdLong = Long.parseLong(storeId);
for (Store store : merchant.getStores()) {
if (Objects.equals(storeIdLong, store.getId())) {
this.storeMapper.updateFromVo(vo, store);
this.storeRepository.save(store);
return ApiResult.data(this.storeMapper.toDto(store));
}
}
return ApiResult.fail("门店信息不存在");
}
/**
* 删除门店
*
2026-01-09 12:20:44 +08:00
* @param storeId 门店id
* @param merchant @ignore 当前登录的商家
2026-01-09 12:20:24 +08:00
*/
@DeleteMapping("/{storeId}")
@Transactional(rollbackFor = Exception.class)
2026-01-09 12:20:44 +08:00
public ApiResult<Void> delteById(@AuthenticationPrincipal Merchant merchant,
@PathVariable("storeId") String storeId) {
2026-01-09 12:20:24 +08:00
var storeIdLong = Long.parseLong(storeId);
merchant.getStores().removeIf(x -> Objects.equals(storeIdLong, x.getId()));
this.merchantRepository.save(merchant);
return ApiResult.success("删除成功");
}
2026-01-09 12:20:44 +08:00
/**
* 获取门店营业状态
*
* @param storeId 门店id
* @param merchant @ignore 当前商户
* @return 门店营业状态信息
*/
@GetMapping("/{storeId}/business-status")
@Transactional(readOnly = true)
public ApiResult<StoreBusinessStatusDto> getStoreBusinessStatus(@AuthenticationPrincipal Merchant merchant,
@PathVariable("storeId") String storeId) {
var storeIdLong = Long.parseLong(storeId);
return merchant.getStores().stream()
.filter(x -> Objects.equals(x.getId(), storeIdLong))
.map(this.storeMapper::toBusinessStatusDto)
.map(ApiResult::data)
.findFirst()
.orElse(ApiResult.fail("门店信息不存在"));
}
/**
* 设置门店营业状态
*
* @param storeId 门店id
* @param merchant @ignore 当前商家
* @param updateVo 更新参数
* @return 门店营业状态信息
*/
@PutMapping("/{storeId}/business-status")
@Transactional(rollbackFor = Exception.class)
public ApiResult<StoreBusinessStatusDto> putStoreBusinessStatus(@PathVariable("storeId") String storeId,
@AuthenticationPrincipal Merchant merchant,
@Validated @RequestBody StoreBusinessStatusUpdateVo updateVo) {
var storeIdLong = Long.parseLong(storeId);
return merchant.getStores().stream()
.filter(x -> Objects.equals(x.getId(), storeIdLong))
.findFirst()
.map(x -> {
this.storeMapper.updateFromBusinessUpdateVo(updateVo, x);
this.storeRepository.save(x);
return this.storeMapper.toBusinessStatusDto(x);
})
.map(ApiResult::data)
.orElse(ApiResult.fail("门店信息不存在"));
}
/**
* 获取门店营业时间段
*
* @param storeId 门店id
* @param merchant @ignore 当前商家
* @return 门店营业时间段 列表
*/
@GetMapping("/{storeId}/business-periods")
public ApiResult<List<BusinessPeriodDto>> getStoreBusinessPeriodList(@AuthenticationPrincipal Merchant merchant,
@PathVariable("storeId") String storeId) {
var storeIdLong = Long.parseLong(storeId);
return merchant.getStores().stream()
.filter(x -> Objects.equals(x.getId(), storeIdLong))
.findFirst()
.map(x -> this.storeMapper.toDtoList(x.getBusinessPeriods()))
.map(ApiResult::data)
.orElse(ApiResult.fail("门店信息不存在"));
}
/**
* 设置门店营业时间段
*
* @param storeId 门店id
* @param merchant @ignore 当前商家
* @param updateVoList 门店营业时间段更新参数列表
* @return 门店营业时间段 列表
*/
@PutMapping("/{storeId}/business-periods")
public ApiResult<List<BusinessPeriodDto>> putStoreBusinessPeriodList(@PathVariable("storeId") String storeId,
@AuthenticationPrincipal Merchant merchant,
@Validated @RequestBody List<BusinessPeriodVo> updateVoList) {
var storeIdLong = Long.parseLong(storeId);
return merchant.getStores().stream()
.filter(x -> Objects.equals(x.getId(), storeIdLong))
.findFirst()
.map(store -> {
store.getBusinessPeriods().clear();
for (BusinessPeriodVo businessPeriodVo : updateVoList) {
var entity = this.storeMapper.toEntity(businessPeriodVo);
store.addBusinessPeriods(entity);
this.businessPeriodRepository.save(entity);
}
return this.storeMapper.toDtoList(store.getBusinessPeriods());
})
.map(ApiResult::data)
.orElse(ApiResult.fail("门店信息不存在"));
}
2026-01-09 12:20:24 +08:00
}