首次提交
This commit is contained in:
parent
1101681331
commit
ed171eda88
107 changed files with 1370 additions and 524 deletions
|
|
@ -3,25 +3,37 @@ 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;
|
||||
import com.xjhs.findmemerchant.common.mvc.PageVo;
|
||||
import com.xjhs.findmemerchant.dto.store.BusinessPeriodDto;
|
||||
import com.xjhs.findmemerchant.dto.store.StoreBusinessStatusDto;
|
||||
import com.xjhs.findmemerchant.dto.store.StoreDto;
|
||||
import com.xjhs.findmemerchant.entity.BusinessPeriod;
|
||||
import com.xjhs.findmemerchant.entity.Merchant;
|
||||
import com.xjhs.findmemerchant.entity.Store;
|
||||
import com.xjhs.findmemerchant.mapper.StoreMapper;
|
||||
import com.xjhs.findmemerchant.repository.BusinessPeriodRepository;
|
||||
import com.xjhs.findmemerchant.repository.MerchantRepository;
|
||||
import com.xjhs.findmemerchant.repository.StoreRepository;
|
||||
import com.xjhs.findmemerchant.vo.store.BusinessPeriodVo;
|
||||
import com.xjhs.findmemerchant.vo.store.StoreBusinessStatusUpdateVo;
|
||||
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.domain.Pageable;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 门店管理接口
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/stores")
|
||||
|
|
@ -30,38 +42,36 @@ public class StoreController {
|
|||
private final MerchantRepository merchantRepository;
|
||||
private final StoreRepository storeRepository;
|
||||
private final StoreMapper storeMapper;
|
||||
private final BusinessPeriodRepository businessPeriodRepository;
|
||||
|
||||
|
||||
/**
|
||||
* 门店信息(分页查询)
|
||||
*
|
||||
* @param pageable 分页参数
|
||||
* @param principal @ignore 当前登录门店
|
||||
* @param pageVo 分页参数
|
||||
* @param merchant @ignore 当前登录商家
|
||||
* @return 分页数据信息
|
||||
*/
|
||||
@GetMapping
|
||||
@Transactional(readOnly = true)
|
||||
public ApiResult<PageData<StoreDto>> findPage(Pageable pageable, Principal principal) {
|
||||
var merchant = (Merchant) principal;
|
||||
public ApiResult<PageData<StoreDto>> findPage(@AuthenticationPrincipal Merchant merchant, PageVo pageVo) {
|
||||
var pageData = this.storeRepository.findAll(Specification.allOf(
|
||||
JpaSpecs.eq("merchant.id", merchant.getId())
|
||||
), pageable).map(this.storeMapper::toDto);
|
||||
), pageVo.getPageable()).map(this.storeMapper::toDto);
|
||||
return ApiResult.page(pageData.getTotalElements(), pageData.getContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建商户的门店
|
||||
* 创建商家的门店
|
||||
*
|
||||
* @param vo 门店信息
|
||||
* @param principal @ignore 当前登录的商户
|
||||
* @param merchant @ignore 当前登录的商家
|
||||
* @return 门店信息
|
||||
*/
|
||||
@PostMapping
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ApiResult<StoreDto> create(@Valid @RequestBody StoreCreateVo vo, Principal principal) {
|
||||
var merchant = (Merchant) principal;
|
||||
if (Objects.isNull(merchant)) {
|
||||
return ApiResult.fail("商家信息错误");
|
||||
}
|
||||
public ApiResult<StoreDto> create(@AuthenticationPrincipal Merchant merchant,
|
||||
@Valid @RequestBody StoreCreateVo vo) {
|
||||
var store = this.storeMapper.toEntity(vo);
|
||||
merchant.addStore(store);
|
||||
this.storeRepository.save(store);
|
||||
|
|
@ -73,16 +83,13 @@ public class StoreController {
|
|||
* 查询门店详情
|
||||
*
|
||||
* @param storeId 门店id
|
||||
* @param principal @ignore 当前登录的商户
|
||||
* @param merchant @ignore 当前登录的商家
|
||||
* @return 门店详情
|
||||
*/
|
||||
@GetMapping("/{storeId}")
|
||||
@Transactional(readOnly = true)
|
||||
public ApiResult<StoreDto> findById(@PathVariable("storeId") String storeId, Principal principal) {
|
||||
var merchant = (Merchant) principal;
|
||||
if (Objects.isNull(merchant)) {
|
||||
return ApiResult.fail("商家信息错误");
|
||||
}
|
||||
public ApiResult<StoreDto> findById(@AuthenticationPrincipal Merchant merchant,
|
||||
@PathVariable("storeId") String storeId) {
|
||||
var storeIdLong = Long.parseLong(storeId);
|
||||
return merchant.getStores().stream()
|
||||
.filter(x -> Objects.equals(x.getId(), storeIdLong))
|
||||
|
|
@ -96,17 +103,15 @@ public class StoreController {
|
|||
* 更新门店信息
|
||||
*
|
||||
* @param storeId 门店id
|
||||
* @param principal @ignore 当前登录的商户
|
||||
* @param merchant @ignore 当前登录的商家
|
||||
* @param vo 更新对象
|
||||
* @return 门店信息
|
||||
*/
|
||||
@PutMapping("/{storeId}")
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ApiResult<StoreDto> updateById(@PathVariable("storeId") String storeId, Principal principal, @Valid @RequestBody StoreUpdateVo vo) {
|
||||
var merchant = (Merchant) principal;
|
||||
if (Objects.isNull(merchant)) {
|
||||
return ApiResult.fail("商家信息错误");
|
||||
}
|
||||
public ApiResult<StoreDto> updateById(@AuthenticationPrincipal Merchant merchant,
|
||||
@PathVariable("storeId") String storeId,
|
||||
@Valid @RequestBody StoreUpdateVo vo) {
|
||||
var storeIdLong = Long.parseLong(storeId);
|
||||
for (Store store : merchant.getStores()) {
|
||||
if (Objects.equals(storeIdLong, store.getId())) {
|
||||
|
|
@ -121,19 +126,112 @@ public class StoreController {
|
|||
/**
|
||||
* 删除门店
|
||||
*
|
||||
* @param storeId 门店id
|
||||
* @param principal @ignore 当前登录的商户
|
||||
* @param storeId 门店id
|
||||
* @param merchant @ignore 当前登录的商家
|
||||
*/
|
||||
@DeleteMapping("/{storeId}")
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ApiResult<Void> delteById(@PathVariable("storeId") String storeId, Principal principal) {
|
||||
var merchant = (Merchant) principal;
|
||||
if (Objects.isNull(merchant)) {
|
||||
return ApiResult.fail("商家信息错误");
|
||||
}
|
||||
public ApiResult<Void> delteById(@AuthenticationPrincipal Merchant merchant,
|
||||
@PathVariable("storeId") String storeId) {
|
||||
var storeIdLong = Long.parseLong(storeId);
|
||||
merchant.getStores().removeIf(x -> Objects.equals(storeIdLong, x.getId()));
|
||||
this.merchantRepository.save(merchant);
|
||||
return ApiResult.success("删除成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取门店营业状态
|
||||
*
|
||||
* @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("门店信息不存在"));
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue