|
|
@@ -1,17 +1,151 @@
|
|
|
package com.izouma.dingdong.service.merchant;
|
|
|
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.izouma.dingdong.domain.User;
|
|
|
import com.izouma.dingdong.domain.merchant.Merchant;
|
|
|
+import com.izouma.dingdong.domain.merchant.MerchantSettings;
|
|
|
+import com.izouma.dingdong.dto.MerchantDTO;
|
|
|
+import com.izouma.dingdong.enums.ApplyStatus;
|
|
|
+import com.izouma.dingdong.enums.Identity;
|
|
|
+import com.izouma.dingdong.exception.BusinessException;
|
|
|
+import com.izouma.dingdong.repo.UserRepo;
|
|
|
import com.izouma.dingdong.repo.merchant.MerchantRepo;
|
|
|
+import com.izouma.dingdong.repo.merchant.MerchantSettingsRepo;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.PageImpl;
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
@Service
|
|
|
@AllArgsConstructor
|
|
|
public class MerchantService {
|
|
|
|
|
|
private MerchantRepo merchantRepo;
|
|
|
|
|
|
+ private MerchantSettingsRepo merchantSettingsRepo;
|
|
|
+
|
|
|
+ private UserRepo userRepo;
|
|
|
+
|
|
|
+ /*
|
|
|
+ 商户注册申请
|
|
|
+ */
|
|
|
+ public MerchantDTO registerApply(MerchantDTO merchantDTO) {
|
|
|
+ //用于商家登录
|
|
|
+ User user = userRepo.findByPhone(merchantDTO.getPhone());
|
|
|
+ if (ObjectUtil.isNull(user)) {
|
|
|
+ user = User.builder().username(merchantDTO.getPhone())
|
|
|
+ .password(merchantDTO.getPassword())
|
|
|
+ .blacklist(false)
|
|
|
+ .enabled(true)
|
|
|
+ .identity(Identity.MERCHANT)
|
|
|
+ .phone(merchantDTO.getPhone())
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+ user.setPassword(merchantDTO.getPassword());
|
|
|
+
|
|
|
+ //查看商家是否已存在
|
|
|
+ Merchant merchant1 = merchantRepo.findByPhone(merchantDTO.getPhone());
|
|
|
+
|
|
|
+ if (ObjectUtil.isNotNull(merchant1)) {
|
|
|
+ if (merchant1.getStatus().equals(ApplyStatus.PENDING)) {
|
|
|
+ throw new BusinessException("申请中");
|
|
|
+ }
|
|
|
+ if (merchant1.getStatus().equals(ApplyStatus.PASS)) {
|
|
|
+ throw new BusinessException("已申请");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+/* if (ObjectUtil.isEmpty(merchantDTO.getBusinessNature())) {
|
|
|
+ throw new BusinessException("商家性质未填");
|
|
|
+ }*/
|
|
|
+
|
|
|
+ //新建商家
|
|
|
+ Merchant merchant = new Merchant();
|
|
|
+ MerchantSettings merchantSettings = new MerchantSettings();
|
|
|
+
|
|
|
+ //dto转实体
|
|
|
+ BeanUtil.copyProperties(merchantDTO, merchant);
|
|
|
+ merchant.setUserId(user.getId());
|
|
|
+ merchant.setStatus(ApplyStatus.PENDING);
|
|
|
+
|
|
|
+ //dto转实体
|
|
|
+ BeanUtil.copyProperties(merchantDTO, merchantSettings);
|
|
|
+ merchantSettings.setMerchantId(merchant.getId());
|
|
|
+
|
|
|
+ userRepo.save(user);
|
|
|
+ merchantRepo.save(merchant);
|
|
|
+ merchantSettingsRepo.save(merchantSettings);
|
|
|
+ return merchantDTO;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ 商户修改
|
|
|
+ */
|
|
|
+ public MerchantDTO change(MerchantDTO merchantDTO) {
|
|
|
+ Merchant merchant = merchantRepo.findById(merchantDTO.getId()).orElseThrow(new BusinessException("商户不存在"));
|
|
|
+ MerchantSettings merchantSettings = merchantSettingsRepo.findByMerchantId(merchantDTO.getId());
|
|
|
+ if (ObjectUtil.isNull(merchantSettings)) {
|
|
|
+ throw new BusinessException("商户不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ //dto转实体
|
|
|
+ BeanUtil.copyProperties(merchantDTO, merchant);
|
|
|
+ BeanUtil.copyProperties(merchantDTO, merchantSettings);
|
|
|
+ merchantRepo.save(merchant);
|
|
|
+ merchantSettingsRepo.save(merchantSettings);
|
|
|
+ return merchantDTO;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ 商户删除
|
|
|
+ */
|
|
|
+ public void del(Long id) {
|
|
|
+ merchantRepo.deleteById(id);
|
|
|
+ merchantSettingsRepo.deleteByMerchantId(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ 商户过审
|
|
|
+ */
|
|
|
+ public void audit(Long id, Boolean pass) {
|
|
|
+ Merchant merchant = merchantRepo.findById(id).orElseThrow(new BusinessException("商户不存在"));
|
|
|
+ if (pass) {
|
|
|
+ merchant.setStatus(ApplyStatus.PASS);
|
|
|
+ merchant.setIsPass(true);
|
|
|
+ } else {
|
|
|
+ merchant.setStatus(ApplyStatus.DENY);
|
|
|
+ merchant.setIsPass(false);
|
|
|
+ }
|
|
|
+ merchantRepo.save(merchant);
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
+ /*
|
|
|
+ 显示所有
|
|
|
+ */
|
|
|
+ public Page<MerchantDTO> showAll(Pageable pageable) {
|
|
|
+ List<MerchantDTO> merchantDTOS = CollUtil.newArrayList();
|
|
|
+ List<Merchant> merchants = merchantRepo.findAll();
|
|
|
+ List<MerchantSettings> merchantSettings = merchantSettingsRepo.findAll();
|
|
|
+ for (int i = 0; i < merchants.size(); i++) {
|
|
|
+ merchantDTOS.add(new MerchantDTO(merchants.get(i), merchantSettings.get(i)));
|
|
|
+ }
|
|
|
+ return new PageImpl<>(merchantDTOS, pageable, merchantDTOS.size());
|
|
|
+ }
|
|
|
|
|
|
+ /*
|
|
|
+ 显示个人
|
|
|
+ */
|
|
|
+ public MerchantDTO my(Long id) {
|
|
|
+ Merchant merchant = merchantRepo.findById(id).orElseThrow(new BusinessException("商户不存在"));
|
|
|
+ MerchantSettings merchantSettings = merchantSettingsRepo.findByMerchantId(id);
|
|
|
+ return new MerchantDTO(merchant, merchantSettings);
|
|
|
+ }
|
|
|
|
|
|
}
|