首次提交
This commit is contained in:
commit
1101681331
131 changed files with 7017 additions and 0 deletions
|
|
@ -0,0 +1,139 @@
|
|||
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.dto.store.StoreDto;
|
||||
import com.xjhs.findmemerchant.entity.Merchant;
|
||||
import com.xjhs.findmemerchant.entity.Store;
|
||||
import com.xjhs.findmemerchant.mapper.StoreMapper;
|
||||
import com.xjhs.findmemerchant.repository.MerchantRepository;
|
||||
import com.xjhs.findmemerchant.repository.StoreRepository;
|
||||
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.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.Objects;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/stores")
|
||||
@RequiredArgsConstructor
|
||||
public class StoreController {
|
||||
private final MerchantRepository merchantRepository;
|
||||
private final StoreRepository storeRepository;
|
||||
private final StoreMapper storeMapper;
|
||||
|
||||
/**
|
||||
* 门店信息(分页查询)
|
||||
*
|
||||
* @param pageable 分页参数
|
||||
* @param principal @ignore 当前登录门店
|
||||
* @return 分页数据信息
|
||||
*/
|
||||
@GetMapping
|
||||
@Transactional(readOnly = true)
|
||||
public ApiResult<PageData<StoreDto>> findPage(Pageable pageable, Principal principal) {
|
||||
var merchant = (Merchant) principal;
|
||||
var pageData = this.storeRepository.findAll(Specification.allOf(
|
||||
JpaSpecs.eq("merchant.id", merchant.getId())
|
||||
), pageable).map(this.storeMapper::toDto);
|
||||
return ApiResult.page(pageData.getTotalElements(), pageData.getContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建商户的门店
|
||||
*
|
||||
* @param vo 门店信息
|
||||
* @param principal @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("商家信息错误");
|
||||
}
|
||||
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
|
||||
* @param principal @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("商家信息错误");
|
||||
}
|
||||
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
|
||||
* @param principal @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("商家信息错误");
|
||||
}
|
||||
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("门店信息不存在");
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除门店
|
||||
*
|
||||
* @param storeId 门店id
|
||||
* @param principal @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("商家信息错误");
|
||||
}
|
||||
var storeIdLong = Long.parseLong(storeId);
|
||||
merchant.getStores().removeIf(x -> Objects.equals(storeIdLong, x.getId()));
|
||||
this.merchantRepository.save(merchant);
|
||||
return ApiResult.success("删除成功");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue