Commit 41bd5cd179e45047bfd2b487746b6b1f8c2d7d06

Authored by miaoguoxin
1 parent b3d92023

代码优化

src/main/java/com/diligrp/xtrade/product/dao/ShopDao.java
... ... @@ -6,24 +6,26 @@ import org.apache.ibatis.annotations.Mapper;
6 6  
7 7 import com.diligrp.xtrade.product.domain.dto.ShopQueryDto;
8 8 import com.diligrp.xtrade.product.domain.entity.ShopDo;
  9 +import org.apache.ibatis.annotations.Param;
  10 +
9 11 /**
10   - *
  12 + * @author yangfan
11 13 * @ClassName: ShopDao
12 14 * @Description 店铺dao
13   - * @author yangfan
14 15 * @date 2020年4月21日
15 16 */
16 17 @Mapper
17 18 public interface ShopDao {
18   -
19   - void insert(ShopDo shop);
20   -
21   - void update(ShopDo shop);
22   -
23   - ShopDo selectByShopId(Long shopId);
24   -
25   - List<ShopDo> selectByMerId(Long merId);
26   -
27   - List<ShopDo> selectShop(ShopQueryDto shopQueryDto);
28   -
  19 +
  20 + void insert(ShopDo shop);
  21 +
  22 + void update(ShopDo shop);
  23 +
  24 + ShopDo selectByShopId(Long shopId);
  25 +
  26 + List<ShopDo> selectByMerId(Long merId);
  27 +
  28 + List<ShopDo> selectShop(ShopQueryDto shopQueryDto);
  29 +
  30 + void updateTypeByMerId(@Param("type") Integer type, @Param("merId") Long merId);
29 31 }
... ...
src/main/java/com/diligrp/xtrade/product/domain/builder/MerchantAccountBuilder.java 0 → 100644
  1 +package com.diligrp.xtrade.product.domain.builder;
  2 +
  3 +import com.alibaba.nacos.common.utils.Md5Utils;
  4 +import com.diligrp.xtrade.product.domain.dto.MerchantRequestDto;
  5 +import com.diligrp.xtrade.product.domain.emuns.AccountStatus;
  6 +import com.diligrp.xtrade.product.domain.entity.AccountDo;
  7 +
  8 +import java.time.LocalDateTime;
  9 +
  10 +/**
  11 + * @Auther: miaoguoxin
  12 + * @Date: 2020/4/23 09:23
  13 + */
  14 +public class MerchantAccountBuilder {
  15 +
  16 + public static AccountDo buildDoFromMerchant(MerchantRequestDto requestDto, Long merId) {
  17 + String password = String.format("%s_%s",
  18 + requestDto.getAccountName(), requestDto.getPassword());
  19 + AccountDo newAccountDo = new AccountDo();
  20 + newAccountDo.setName(requestDto.getAccountName());
  21 + newAccountDo.setRealName(requestDto.getRealName());
  22 + newAccountDo.setPassword(Md5Utils.getMD5(password, "UTF-8"));
  23 + newAccountDo.setMobile(requestDto.getMobile());
  24 + newAccountDo.setStatus(AccountStatus.NORMAL.getCode());
  25 + newAccountDo.setIsAdmin(1);
  26 + newAccountDo.setMerId(merId);
  27 + newAccountDo.setCreatedTime(LocalDateTime.now());
  28 + return newAccountDo;
  29 + }
  30 +}
... ...
src/main/java/com/diligrp/xtrade/product/domain/builder/MerchantBuilder.java
... ... @@ -16,7 +16,7 @@ import java.util.Optional;
16 16 */
17 17 public class MerchantBuilder {
18 18  
19   - public static MerchantDo request2Entity(MerchantRequestDto requestDto) {
  19 + public static MerchantDo buildDoFromRequest(MerchantRequestDto requestDto) {
20 20 Integer type = Optional.ofNullable(requestDto.getAccountType())
21 21 .map(AccountType::getCode)
22 22 .orElse(0);
... ... @@ -30,7 +30,7 @@ public class MerchantBuilder {
30 30 return merchantDo;
31 31 }
32 32  
33   - public static MerchantResponseDto entity2Response(MerchantDo merchantDo){
  33 + public static MerchantResponseDto buildResponseFromDo(MerchantDo merchantDo){
34 34 MerchantResponseDto responseDto = new MerchantResponseDto();
35 35 BeanUtils.copyProperties(merchantDo,responseDto);
36 36 responseDto.setType(AccountType.getAccountType(merchantDo.getType()));
... ...
src/main/java/com/diligrp/xtrade/product/repository/AccountRepository.java
1 1 package com.diligrp.xtrade.product.repository;
2 2  
  3 +import com.diligrp.xtrade.product.domain.emuns.IKeyGeneratorKeys;
3 4 import com.diligrp.xtrade.product.domain.entity.AccountDo;
4 5 import com.diligrp.xtrade.product.domain.entity.OperatorDo;
  6 +import com.diligrp.xtrade.shared.sequence.IKeyGenerator;
  7 +import com.diligrp.xtrade.shared.sequence.KeyGeneratorManager;
  8 +import org.springframework.beans.factory.annotation.Autowired;
5 9 import org.springframework.stereotype.Repository;
6 10  
7 11 import java.util.ArrayList;
... ... @@ -15,6 +19,8 @@ import java.util.List;
15 19 @Repository
16 20 public class AccountRepository implements IAccountRepository {
17 21 private final static List<AccountDo> ACCOUNT_DOS = new ArrayList<>();
  22 + @Autowired
  23 + private KeyGeneratorManager keyGeneratorManager;
18 24 @Override
19 25 public OperatorDo getOperator() {
20 26 OperatorDo operatorDo = new OperatorDo();
... ... @@ -35,6 +41,8 @@ public class AccountRepository implements IAccountRepository {
35 41  
36 42 @Override
37 43 public void add(AccountDo accountDo) {
  44 + IKeyGenerator keyGenerator = keyGeneratorManager.getKeyGenerator(IKeyGeneratorKeys.ACCOUNT_SEQUENCE);
  45 + accountDo.setId(keyGenerator.nextId());
38 46 ACCOUNT_DOS.add(accountDo);
39 47 }
40 48 }
... ...
src/main/java/com/diligrp/xtrade/product/repository/MerchantRepository.java
... ... @@ -48,10 +48,6 @@ public class MerchantRepository implements IMerchantRepository {
48 48  
49 49 @Override
50 50 public void update(MerchantDo merchantDo) {
51   - merchantDo.setModifiedTime(LocalDateTime.now());
52   - ShopDo shopDo = new ShopDo();
53   - shopDo.setType(AccountType.PUBLIC.getCode());
54   - shopDo.setMerId(merchantDo.getMerId());
55   - shopDao.update(shopDo);
  51 +
56 52 }
57 53 }
... ...
src/main/java/com/diligrp/xtrade/product/service/AccountService.java
... ... @@ -10,7 +10,5 @@ import com.diligrp.xtrade.product.domain.entity.MerchantDo;
10 10 */
11 11 public interface AccountService {
12 12  
13   - AccountDo build(MerchantRequestDto requestDto, MerchantDo merchantDo) throws Exception;
14   -
15   - void create(MerchantRequestDto requestDto, MerchantDo merchantDo) throws Exception;
  13 + void create(MerchantRequestDto requestDto, MerchantDo merchantDo);
16 14 }
... ...
src/main/java/com/diligrp/xtrade/product/service/ShopService.java
... ... @@ -4,10 +4,11 @@ import java.util.List;
4 4  
5 5 import com.diligrp.xtrade.product.domain.dto.ShopDto;
6 6 import com.diligrp.xtrade.product.domain.dto.ShopQueryDto;
  7 +import com.diligrp.xtrade.product.domain.entity.MerchantDo;
7 8 import com.diligrp.xtrade.product.domain.entity.ShopDo;
8 9  
9 10 public interface ShopService {
10   -
  11 +
11 12 /**
12 13 * @Title insert
13 14 * @Description 保存shop
... ... @@ -15,18 +16,18 @@ public interface ShopService {
15 16 * @throws
16 17 */
17 18 void insert(ShopDto shop);
18   -
  19 +
19 20 /**
20   - *
  21 + *
21 22 * @Title update
22 23 * @Description 修改
23 24 * @param shop
24 25 * @throws
25 26 */
26 27 void update(ShopDto shop);
27   -
  28 +
28 29 /**
29   - *
  30 + *
30 31 * @Title selectByMerId
31 32 * @Description 店铺id查询
32 33 * @param merId
... ... @@ -34,9 +35,9 @@ public interface ShopService {
34 35 * @throws
35 36 */
36 37 ShopDo selectByShopId(Long shopId);
37   -
  38 +
38 39 /**
39   - *
  40 + *
40 41 * @Title selectByMerId
41 42 * @Description 根据商户id获取名下全部商铺
42 43 * @param merId
... ... @@ -44,9 +45,9 @@ public interface ShopService {
44 45 * @throws
45 46 */
46 47 List<ShopDo> selectShopByMerId(Long merId);
47   -
  48 +
48 49 /**
49   - *
  50 + *
50 51 * @Title selectShop
51 52 * @Description 商铺查询
52 53 * @param shopQuery
... ... @@ -54,4 +55,6 @@ public interface ShopService {
54 55 * @throws
55 56 */
56 57 List<ShopDo> selectShop(ShopQueryDto shopQuery);
  58 +
  59 + void updateShopTypeWithMerchant(MerchantDo merchantDo);
57 60 }
... ...
src/main/java/com/diligrp/xtrade/product/service/impl/AccountServiceImpl.java
1 1 package com.diligrp.xtrade.product.service.impl;
2 2  
3   -import com.alibaba.nacos.common.utils.Md5Utils;
  3 +import com.diligrp.xtrade.product.domain.builder.MerchantAccountBuilder;
4 4 import com.diligrp.xtrade.product.domain.dto.MerchantRequestDto;
5   -import com.diligrp.xtrade.product.domain.emuns.AccountStatus;
6   -import com.diligrp.xtrade.product.domain.emuns.IKeyGeneratorKeys;
7 5 import com.diligrp.xtrade.product.domain.entity.AccountDo;
8 6 import com.diligrp.xtrade.product.domain.entity.MerchantDo;
9 7 import com.diligrp.xtrade.product.repository.IAccountRepository;
10 8 import com.diligrp.xtrade.product.service.AccountService;
11   -import com.diligrp.xtrade.shared.sequence.IKeyGenerator;
12   -import com.diligrp.xtrade.shared.sequence.KeyGeneratorManager;
13 9 import org.springframework.beans.factory.annotation.Autowired;
14 10 import org.springframework.stereotype.Service;
15 11  
16   -import java.time.LocalDateTime;
17   -
18 12 /**
19 13 * @Auther: miaoguoxin
20 14 * @Date: 2020/4/22 11:10
... ... @@ -23,30 +17,10 @@ import java.time.LocalDateTime;
23 17 public class AccountServiceImpl implements AccountService {
24 18 @Autowired
25 19 private IAccountRepository accountRepository;
26   - @Autowired
27   - private KeyGeneratorManager keyGeneratorManager;
28   -
29   - @Override
30   - public AccountDo build(MerchantRequestDto requestDto, MerchantDo merchantDo) throws Exception {
31   - String password = String.format("%s_%s",
32   - requestDto.getAccountName(), requestDto.getPassword());
33   - AccountDo newAccountDo = new AccountDo();
34   - newAccountDo.setName(requestDto.getAccountName());
35   - newAccountDo.setRealName(requestDto.getRealName());
36   - newAccountDo.setPassword(Md5Utils.getMD5(password, "UTF-8"));
37   - newAccountDo.setMobile(requestDto.getMobile());
38   - newAccountDo.setStatus(AccountStatus.NORMAL.getCode());
39   - newAccountDo.setIsAdmin(1);
40   - newAccountDo.setMerId(merchantDo.getMerId());
41   - newAccountDo.setCreatedTime(LocalDateTime.now());
42   - IKeyGenerator keyGenerator = keyGeneratorManager.getKeyGenerator(IKeyGeneratorKeys.ACCOUNT_SEQUENCE);
43   - newAccountDo.setId(keyGenerator.nextId());
44   - return newAccountDo;
45   - }
46 20  
47 21 @Override
48   - public void create(MerchantRequestDto requestDto, MerchantDo merchantDo) throws Exception {
49   - AccountDo newAccountDo = this.build(requestDto, merchantDo);
  22 + public void create(MerchantRequestDto requestDto, MerchantDo merchantDo) {
  23 + AccountDo newAccountDo = MerchantAccountBuilder.buildDoFromMerchant(requestDto, merchantDo.getMerId());
50 24 accountRepository.add(newAccountDo);
51 25 }
52 26  
... ...
src/main/java/com/diligrp/xtrade/product/service/impl/MerchantServiceImpl.java
... ... @@ -13,6 +13,7 @@ import com.diligrp.xtrade.product.repository.IAccountRepository;
13 13 import com.diligrp.xtrade.product.repository.IMerchantRepository;
14 14 import com.diligrp.xtrade.product.service.AccountService;
15 15 import com.diligrp.xtrade.product.service.MerchantService;
  16 +import com.diligrp.xtrade.product.service.ShopService;
16 17 import org.springframework.beans.factory.annotation.Autowired;
17 18 import org.springframework.stereotype.Service;
18 19 import org.springframework.transaction.annotation.Transactional;
... ... @@ -25,6 +26,8 @@ public class MerchantServiceImpl implements MerchantService {
25 26 private IMerchantRepository merchantRepository;
26 27 @Autowired
27 28 private AccountService accountService;
  29 + @Autowired
  30 + private ShopService shopService;
28 31  
29 32 @Override
30 33 @Transactional(rollbackFor = Exception.class)
... ... @@ -38,17 +41,17 @@ public class MerchantServiceImpl implements MerchantService {
38 41 }
39 42 MerchantDo merchantDo = merchantRepository.getByAccountId(requestDto.getAccountId());
40 43 if (merchantDo == null){
41   - merchantDo = MerchantBuilder.request2Entity(requestDto);
  44 + merchantDo = MerchantBuilder.buildDoFromRequest(requestDto);
42 45 merchantRepository.add(merchantDo);
43 46 } else {
44 47 if (AccountType.PERSONAL.getCode() != merchantDo.getType()){
45 48 throw new ProductException(ExceptionEnum.CARD_MERCHANT_CREATED);
46 49 }
47   - merchantRepository.update(merchantDo);
  50 + shopService.updateShopTypeWithMerchant(merchantDo);
48 51 }
49 52 //创建市场管理员账户
50 53 accountService.create(requestDto, merchantDo);
51   - return MerchantBuilder.entity2Response(merchantDo);
  54 + return MerchantBuilder.buildResponseFromDo(merchantDo);
52 55 }
53 56  
54 57 }
... ...
src/main/java/com/diligrp/xtrade/product/service/impl/ShopServiceImpl.java
1 1 package com.diligrp.xtrade.product.service.impl;
2 2  
  3 +import java.time.LocalDateTime;
3 4 import java.util.List;
4 5  
  6 +import com.diligrp.xtrade.product.domain.emuns.AccountType;
  7 +import com.diligrp.xtrade.product.domain.entity.MerchantDo;
5 8 import org.springframework.beans.BeanUtils;
6 9 import org.springframework.beans.factory.annotation.Autowired;
7 10 import org.springframework.stereotype.Service;
... ... @@ -68,4 +71,9 @@ public class ShopServiceImpl implements ShopService {
68 71 return shops;
69 72 }
70 73  
  74 + @Override
  75 + public void updateShopTypeWithMerchant(MerchantDo merchantDo) {
  76 + shopDao.updateTypeByMerId(AccountType.PUBLIC.getCode(), merchantDo.getMerId());
  77 + }
  78 +
71 79 }
... ...
src/main/resources/mapping/com/diligrp/xtrade/product/ShopDao.xml
1 1 <?xml version="1.0" encoding="UTF-8"?>
2 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3 3 <mapper namespace="com.diligrp.xtrade.product.dao.ShopDao">
4   -
  4 +
5 5 <sql id="QUERY_COLUMN_LIST">
6 6 <![CDATA[
7 7 `id`, `shop_id` AS shopId, `shop_name` AS shopName, `address`, `mer_id` AS merId, `type`, `status`,
... ... @@ -33,7 +33,7 @@
33 33 <if test="selfShop != null and selfShop != ''"><![CDATA[AND self_shop = #{selfShop}]]></if>
34 34 </where>
35 35 </sql>
36   -
  36 +
37 37 <insert id="insert" parameterType="com.diligrp.xtrade.product.domain.entity.ShopDo">
38 38 <![CDATA[
39 39 INSERT INTO `xt_shop`(`id`, `shop_id`, `shop_name`, `self_shop`, `mer_id`, `main_biz`, `type`, `status`, `address`, `description`, `created_time`, `modified_time`)
... ... @@ -42,21 +42,27 @@
42 42 </insert>
43 43 <select id="selectByShopId" parameterType="long" resultType="com.diligrp.xtrade.product.domain.entity.ShopDo">
44 44 <![CDATA[
45   - SELECT
  45 + SELECT
46 46 ]]>
47 47 <include refid="QUERY_COLUMN_LIST" />
48 48 <![CDATA[
49 49 FROM `xt_shop` WHERE shop_id = #{shopId}
50 50 ]]>
51 51 </select>
52   - <update id="update" parameterType="com.diligrp.xtrade.product.domain.entity.ShopDo">
  52 + <update id="update" parameterType="com.diligrp.xtrade.product.domain.entity.ShopDo">
53 53 <![CDATA[UPDATE `xt_shop`]]>
54 54 <include refid="UPDATE_COLUMN_SET" />
55 55 <![CDATA[WHERE shop_id = #{shopId}]]>
56 56 </update>
57   - <select id="selectByMerId" parameterType="long" resultType="com.diligrp.xtrade.product.domain.entity.ShopDo">
  57 + <update id="updateTypeByMerId">
  58 + UPDATE xt_shop
  59 + SET type = #{type},
  60 + modified_time = now()
  61 + WHERE mer_id = #{merId}
  62 + </update>
  63 + <select id="selectByMerId" parameterType="long" resultType="com.diligrp.xtrade.product.domain.entity.ShopDo">
58 64 <![CDATA[
59   - SELECT
  65 + SELECT
60 66 ]]>
61 67 <include refid="QUERY_COLUMN_LIST" />
62 68 <![CDATA[
... ... @@ -65,12 +71,12 @@
65 71 </select>
66 72 <select id="selectShop" parameterType="com.diligrp.xtrade.product.domain.dto.ShopQueryDto" resultType="com.diligrp.xtrade.product.domain.entity.ShopDo">
67 73 <![CDATA[
68   - SELECT
  74 + SELECT
69 75 ]]>
70 76 <include refid="QUERY_COLUMN_LIST" />
71 77 <![CDATA[
72   - FROM `xt_shop`
  78 + FROM `xt_shop`
73 79 ]]>
74 80 <include refid="QUERY_WHERE_CLAUSE" />
75 81 </select>
76   -</mapper>
77 82 \ No newline at end of file
  83 +</mapper>
... ...