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.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") @RequiredArgsConstructor public class StoreController { private final MerchantRepository merchantRepository; private final StoreRepository storeRepository; private final StoreMapper storeMapper; private final BusinessPeriodRepository businessPeriodRepository; /** * 门店信息(分页查询) * * @param pageVo 分页参数 * @param merchant @ignore 当前登录商家 * @return 分页数据信息 */ @GetMapping @Transactional(readOnly = true) public ApiResult> findPage(@AuthenticationPrincipal Merchant merchant, PageVo pageVo) { var pageData = this.storeRepository.findAll(Specification.allOf( JpaSpecs.eq("merchant.id", merchant.getId()) ), pageVo.getPageable()).map(this.storeMapper::toDto); return ApiResult.page(pageData.getTotalElements(), pageData.getContent()); } /** * 创建商家的门店 * * @param vo 门店信息 * @param merchant @ignore 当前登录的商家 * @return 门店信息 */ @PostMapping @Transactional(rollbackFor = Exception.class) public ApiResult create(@AuthenticationPrincipal Merchant merchant, @Valid @RequestBody StoreCreateVo vo) { 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 merchant @ignore 当前登录的商家 * @return 门店详情 */ @GetMapping("/{storeId}") @Transactional(readOnly = true) public ApiResult findById(@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::toDto) .map(ApiResult::data) .findFirst() .orElse(ApiResult.fail("门店信息不存在")); } /** * 更新门店信息 * * @param storeId 门店id * @param merchant @ignore 当前登录的商家 * @param vo 更新对象 * @return 门店信息 */ @PutMapping("/{storeId}") @Transactional(rollbackFor = Exception.class) public ApiResult 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())) { this.storeMapper.updateFromVo(vo, store); this.storeRepository.save(store); return ApiResult.data(this.storeMapper.toDto(store)); } } return ApiResult.fail("门店信息不存在"); } /** * 删除门店 * * @param storeId 门店id * @param merchant @ignore 当前登录的商家 */ @DeleteMapping("/{storeId}") @Transactional(rollbackFor = Exception.class) public ApiResult 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 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 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> 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> putStoreBusinessPeriodList(@PathVariable("storeId") String storeId, @AuthenticationPrincipal Merchant merchant, @Validated @RequestBody List 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("门店信息不存在")); } }