mirror of https://github.com/jeecgboot/jeecg-boot
Merge pull request #174 from LQYBill/feat/create-user-init-balance
feat: add user client support and init balance on creationpull/8547/head
commit
dc08cdb4f2
|
@ -4,22 +4,20 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.jeecg.common.aspect.annotation.AutoLog;
|
||||
import org.jeecg.common.system.query.QueryGenerator;
|
||||
import org.jeecg.common.system.vo.LoginUser;
|
||||
import org.jeecg.common.util.SpringContextUtils;
|
||||
import org.jeecg.common.util.oConvertUtils;
|
||||
import org.jeecg.modules.business.entity.Balance;
|
||||
import org.jeecg.modules.business.entity.Client;
|
||||
import org.jeecg.modules.business.entity.ClientSku;
|
||||
import org.jeecg.modules.business.entity.Shop;
|
||||
import org.jeecg.modules.business.service.IClientService;
|
||||
import org.jeecg.modules.business.service.IClientSkuService;
|
||||
import org.jeecg.modules.business.service.IShopService;
|
||||
import org.jeecg.modules.business.service.*;
|
||||
import org.jeecg.modules.business.vo.ClientPage;
|
||||
import org.jeecg.modules.business.vo.PlatformOrderOption;
|
||||
import org.jeecg.modules.online.cgform.mapper.OnlCgformFieldMapper;
|
||||
import org.jeecgframework.poi.excel.ExcelImportUtil;
|
||||
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
|
||||
|
@ -56,11 +54,17 @@ public class ClientController {
|
|||
|
||||
private final IClientSkuService clientSkuService;
|
||||
|
||||
private final IBalanceService balanceService;
|
||||
|
||||
private final IPlatformOrderService platformOrderService;
|
||||
|
||||
@Autowired
|
||||
public ClientController(IClientService clientService, IShopService shopService, IClientSkuService clientSkuService) {
|
||||
public ClientController(IClientService clientService, IShopService shopService, IClientSkuService clientSkuService, IBalanceService balanceService, IPlatformOrderService platformOrderService) {
|
||||
this.clientService = clientService;
|
||||
this.shopService = shopService;
|
||||
this.clientSkuService = clientSkuService;
|
||||
this.balanceService = balanceService;
|
||||
this.platformOrderService = platformOrderService;
|
||||
}
|
||||
|
||||
|
||||
|
@ -73,8 +77,6 @@ public class ClientController {
|
|||
* @param req
|
||||
* @return
|
||||
*/
|
||||
@AutoLog(value = "客户-分页列表查询")
|
||||
@ApiOperation(value = "客户-分页列表查询", notes = "客户-分页列表查询")
|
||||
@GetMapping(value = "/list")
|
||||
public Result<?> queryPageList(Client client,
|
||||
@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
|
||||
|
@ -92,13 +94,16 @@ public class ClientController {
|
|||
* @param clientPage
|
||||
* @return
|
||||
*/
|
||||
@AutoLog(value = "客户-添加")
|
||||
@ApiOperation(value = "客户-添加", notes = "客户-添加")
|
||||
@PostMapping(value = "/add")
|
||||
public Result<?> add(@RequestBody ClientPage clientPage) {
|
||||
Client client = new Client();
|
||||
BeanUtils.copyProperties(clientPage, client);
|
||||
clientService.saveMain(client, clientPage.getShopList(), clientPage.getClientSkuList());
|
||||
String useBalance = clientPage.getUseBalance();
|
||||
log.info("useBalance:{}", useBalance);
|
||||
if ("1".equals(useBalance)) {
|
||||
balanceService.initBalance(client.getId());
|
||||
}
|
||||
return Result.OK("添加成功!");
|
||||
}
|
||||
|
||||
|
@ -121,9 +126,7 @@ public class ClientController {
|
|||
* @param clientPage
|
||||
* @return
|
||||
*/
|
||||
@AutoLog(value = "客户-编辑")
|
||||
@ApiOperation(value = "客户-编辑", notes = "客户-编辑")
|
||||
@PutMapping(value = "/edit")
|
||||
@PostMapping(value = "/edit")
|
||||
public Result<?> edit(@RequestBody ClientPage clientPage) {
|
||||
Client client = new Client();
|
||||
BeanUtils.copyProperties(clientPage, client);
|
||||
|
@ -131,8 +134,13 @@ public class ClientController {
|
|||
if (clientEntity == null) {
|
||||
return Result.error("未找到对应数据");
|
||||
}
|
||||
if (client.getUseBalance() != null && "1".equals(clientPage.getUseBalance())){
|
||||
// If useBalance is set to 1, initialize balance for the client
|
||||
balanceService.initBalance(client.getId());
|
||||
}
|
||||
clientService.updateMain(client, clientPage.getShopList(), clientPage.getClientSkuList());
|
||||
updateShopId();
|
||||
log.info("useBalance from clientPage: {}, useBalance updated for client: {}", clientPage.getUseBalance(), client.getUseBalance());
|
||||
log.info("Shop names replaced by new created shop IDs");
|
||||
return Result.OK("编辑成功!");
|
||||
}
|
||||
|
@ -155,11 +163,32 @@ public class ClientController {
|
|||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@AutoLog(value = "客户-通过id删除")
|
||||
@ApiOperation(value = "客户-通过id删除", notes = "客户-通过id删除")
|
||||
@DeleteMapping(value = "/delete")
|
||||
public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
|
||||
// check if the client has any shops with orders
|
||||
List<String> deletedShopIds = new ArrayList<>();
|
||||
List<String> deletedBalanceIds;
|
||||
List<Shop> shopList = shopService.listByClient(id);
|
||||
if (shopList != null && !shopList.isEmpty()) {
|
||||
for (Shop shop : shopList) {
|
||||
Integer ordersAmount = platformOrderService.countOrdersByShop(shop.getId());
|
||||
log.info("checking shop: {}, ordersAmount: {}", shop.getName(), ordersAmount);
|
||||
if (ordersAmount != 0) {
|
||||
return Result.error("客户的店铺" + shop.getName() + "存在订单,无法删除");
|
||||
}
|
||||
}
|
||||
deletedShopIds = shopList.stream().map(Shop::getId).collect(Collectors.toList());
|
||||
shopService.removeByIds(deletedShopIds);
|
||||
}
|
||||
// delete client balance
|
||||
List<Balance> balances = balanceService.list(
|
||||
new QueryWrapper<Balance>().eq("client_id", id)
|
||||
);
|
||||
deletedBalanceIds = balances.stream().map(Balance::getId).collect(Collectors.toList());
|
||||
balanceService.deleteBalanceByClientId(id);
|
||||
clientService.delMain(id);
|
||||
log.info("Deleted Client: {}, Deleted Shops: {}, Deleted Balances: {}",
|
||||
id, deletedShopIds, deletedBalanceIds);
|
||||
return Result.OK("删除成功!");
|
||||
}
|
||||
|
||||
|
@ -169,11 +198,41 @@ public class ClientController {
|
|||
* @param ids
|
||||
* @return
|
||||
*/
|
||||
@AutoLog(value = "客户-批量删除")
|
||||
@ApiOperation(value = "客户-批量删除", notes = "客户-批量删除")
|
||||
@DeleteMapping(value = "/deleteBatch")
|
||||
public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
|
||||
this.clientService.delBatchMain(Arrays.asList(ids.split(",")));
|
||||
List<String> clientIds = Arrays.asList(ids.split(","));
|
||||
List<String> deletedClientIds = new ArrayList<>();
|
||||
|
||||
for (String clientId : clientIds) {
|
||||
Client client = clientService.getById(clientId);
|
||||
if (client == null) {
|
||||
continue;
|
||||
}
|
||||
// check if the client has any shops with orders
|
||||
List<String> deletedShopIds = new ArrayList<>();
|
||||
List<String> deletedBalanceIds;
|
||||
List<Shop> shopList = shopService.listByClient(clientId);
|
||||
if (shopList != null && !shopList.isEmpty()) {
|
||||
for (Shop shop : shopList) {
|
||||
Integer ordersAmount = platformOrderService.countOrdersByShop(shop.getId());
|
||||
log.info("checking shop: {}, ordersAmount: {}", shop.getName(), ordersAmount);
|
||||
if (ordersAmount > 0) {
|
||||
return Result.error("客户的店铺" + shop.getName() + "存在订单,无法删除");
|
||||
}
|
||||
}
|
||||
deletedShopIds = shopList.stream().map(Shop::getId).collect(Collectors.toList());
|
||||
shopService.removeByIds(deletedShopIds);
|
||||
}
|
||||
// delete client balance
|
||||
List<Balance> balances = balanceService.list(
|
||||
new QueryWrapper<Balance>().eq("client_id", clientId)
|
||||
);
|
||||
deletedBalanceIds = balances.stream().map(Balance::getId).collect(Collectors.toList());
|
||||
balanceService.deleteBalanceByClientId(clientId);
|
||||
log.info("Deleted Client: {}, Deleted Shops: {}, Deleted Balances: {}",
|
||||
clientId, deletedShopIds, deletedBalanceIds);
|
||||
}
|
||||
this.clientService.delBatchMain(clientIds);
|
||||
return Result.OK("批量删除成功!");
|
||||
}
|
||||
|
||||
|
@ -183,8 +242,6 @@ public class ClientController {
|
|||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@AutoLog(value = "客户-通过id查询")
|
||||
@ApiOperation(value = "客户-通过id查询", notes = "客户-通过id查询")
|
||||
@GetMapping(value = "/queryById")
|
||||
public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
|
||||
Client client = clientService.getById(id);
|
||||
|
@ -201,8 +258,6 @@ public class ClientController {
|
|||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@AutoLog(value = "店铺-通过主表ID查询")
|
||||
@ApiOperation(value = "店铺-通过主表ID查询", notes = "店铺-通过主表ID查询")
|
||||
@GetMapping(value = "/queryShopByMainId")
|
||||
public Result<?> queryShopListByMainId(@RequestParam(name = "id", required = true) String id) {
|
||||
List<Shop> shopList = shopService.selectByMainId(id);
|
||||
|
@ -218,8 +273,6 @@ public class ClientController {
|
|||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@AutoLog(value = "客户名下SKU-通过主表ID查询")
|
||||
@ApiOperation(value = "客户名下SKU-通过主表ID查询", notes = "客户名下SKU-通过主表ID查询")
|
||||
@GetMapping(value = "/queryClientSkuByMainId")
|
||||
public Result<?> queryClientSkuListByMainId(@RequestParam(name = "id", required = true) String id) {
|
||||
log.info(id);
|
||||
|
|
|
@ -200,6 +200,14 @@ public class Client implements Serializable {
|
|||
@ApiModelProperty(value = "invoice in chronological order or first can invoice")
|
||||
private java.lang.String isChronologicalOrder;
|
||||
|
||||
/**
|
||||
* 是否使用余额
|
||||
*/
|
||||
@Excel(name = "是否使用余额", width = 15, dicCode = "yn")
|
||||
@Dict(dicCode = "yn")
|
||||
@ApiModelProperty(value = "是否使用余额")
|
||||
private String useBalance;
|
||||
|
||||
public String fullName() {
|
||||
return firstName + " " + surname;
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ public interface ClientMapper extends BaseMapper<Client> {
|
|||
String getActiveClientIdByCode(@Param("code") String code);
|
||||
List<Client> getClientByType(@Param("type") String type);
|
||||
Client getClientByCode(@Param("code") String internalCode);
|
||||
Client getClientById(@Param("id") String clientId);
|
||||
Client getClientFromOrder(@Param("orderId")String orderId);
|
||||
|
||||
Client getClientFromPurchase(@Param("purchaseId") String purchaseId);
|
||||
|
|
|
@ -263,4 +263,5 @@ public interface PlatformOrderMapper extends BaseMapper<PlatformOrder> {
|
|||
PlatformOrder selectForUpdateSkipLock(@Param("id") String orderId);
|
||||
|
||||
List<String> fetchPlatformOrderIdsByShopifyNote(String shopifyNote);
|
||||
Integer countOrdersByShop(@Param("shopId") String shopId);
|
||||
}
|
||||
|
|
|
@ -107,6 +107,11 @@
|
|||
JOIN credit ON c.id = credit.client_id
|
||||
WHERE credit.invoice_number = #{invoiceNumber};
|
||||
</select>
|
||||
<select id="getClientById" resultType="org.jeecg.modules.business.entity.Client">
|
||||
SELECT *
|
||||
FROM client
|
||||
WHERE id = #{id};
|
||||
</select>
|
||||
<select id="getClientsByCode" resultType="java.lang.String">
|
||||
SELECT c.id
|
||||
FROM client c
|
||||
|
|
|
@ -1412,4 +1412,9 @@
|
|||
WHERE shopify_note LIKE #{shopifyNote}
|
||||
AND erp_status in (1,2);
|
||||
</select>
|
||||
<select id="countOrdersByShop" resultType="java.lang.Integer">
|
||||
SELECT COUNT(*)
|
||||
FROM platform_order
|
||||
WHERE shop_id = #{shopId}
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
|
@ -17,6 +17,9 @@ import java.util.List;
|
|||
public interface IBalanceService extends IService<Balance> {
|
||||
|
||||
BigDecimal getBalanceByClientIdAndCurrency(String clientId, String currency);
|
||||
|
||||
void initBalance(String clientId);
|
||||
|
||||
void updateBalance(String clientId, String invoiceCode, String invoiceType);
|
||||
void updateBalance(String clientId, String CreditId, BigDecimal amount, String currencyId);
|
||||
|
||||
|
@ -27,6 +30,8 @@ public interface IBalanceService extends IService<Balance> {
|
|||
*/
|
||||
void deleteBalance(String operationId, String operationType);
|
||||
|
||||
void deleteBalanceByClientId(String clientId);
|
||||
|
||||
/**
|
||||
* Edit balance record
|
||||
* @param operationId operation id : invoice id or credit id
|
||||
|
|
|
@ -298,4 +298,6 @@ public interface IPlatformOrderService extends IService<PlatformOrder> {
|
|||
PlatformOrder selectForUpdateSkipLock(String orderId);
|
||||
|
||||
List<String> fetchPlatformOrderIdsByShopifyNote(String shopifyNote);
|
||||
|
||||
Integer countOrdersByShop(String shopId);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package org.jeecg.modules.business.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.modules.business.entity.*;
|
||||
import org.jeecg.modules.business.mapper.BalanceMapper;
|
||||
|
@ -45,6 +46,30 @@ public class BalanceServiceImpl extends ServiceImpl<BalanceMapper, Balance> impl
|
|||
public BigDecimal getBalanceByClientIdAndCurrency(String clientId, String currency) {
|
||||
return balanceMapper.getBalanceByClientIdAndCurrency(clientId, currency);
|
||||
}
|
||||
@Override
|
||||
public void initBalance(String clientId) {
|
||||
// TODO : check if the balance already exists for this client
|
||||
Client client = clientMapper.getClientById(clientId);
|
||||
if(client == null) {
|
||||
throw new RuntimeException("Client not found for id: " + clientId);
|
||||
}
|
||||
String currency = client.getCurrency();
|
||||
if(currency == null) {
|
||||
throw new RuntimeException("Client currency is not set for client id: " + clientId);
|
||||
}
|
||||
String currencyId = currencyService.getIdByCode(currency);
|
||||
BigDecimal previousBalance = balanceMapper.getBalanceByClientIdAndCurrency(clientId, currency);
|
||||
if(previousBalance != null) {
|
||||
log.info("Balance already exists for client: {}, currency: {}, balance: {}", clientId, currency, previousBalance);
|
||||
return;
|
||||
}
|
||||
// initialize balance to zero
|
||||
SysUser sysUser = new SysUser();
|
||||
Balance balance = Balance.of(sysUser.getUsername(), clientId, currencyId, Balance.OperationType.Init.name(), null, BigDecimal.ZERO)
|
||||
.setCreateBy(sysUser.getUsername());
|
||||
balanceMapper.insert(balance);
|
||||
log.info("Initialized balance for client: {}, currency: {}, , currencyId: {},balance: {}", clientId, currency, currencyId, BigDecimal.ZERO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateBalance(String clientId, String invoiceCode, String invoiceType) {
|
||||
|
@ -106,6 +131,13 @@ public class BalanceServiceImpl extends ServiceImpl<BalanceMapper, Balance> impl
|
|||
public void deleteBatchBalance(List<String> operationIds, String operationType) {
|
||||
balanceMapper.deleteBatchBalance(operationIds, operationType);
|
||||
}
|
||||
@Override
|
||||
public void deleteBalanceByClientId(String clientId) {
|
||||
QueryWrapper<Balance> query = new QueryWrapper<>();
|
||||
query.eq("client_id", clientId);
|
||||
this.remove(query);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void editBalance(String operationId, String operationType, String clientId, BigDecimal amount, String currencyId) throws Exception {
|
||||
|
|
|
@ -619,4 +619,10 @@ public class PlatformOrderServiceImpl extends ServiceImpl<PlatformOrderMapper, P
|
|||
public List<String> fetchPlatformOrderIdsByShopifyNote(String shopifyNote) {
|
||||
return platformOrderMap.fetchPlatformOrderIdsByShopifyNote(shopifyNote);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer countOrdersByShop(String shopId) {
|
||||
return platformOrderMap.countOrdersByShop(shopId);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -155,6 +155,22 @@ public class ClientPage {
|
|||
@Excel(name = "账户余额", width = 15)
|
||||
@ApiModelProperty(value = "账户余额")
|
||||
private java.math.BigDecimal balance;
|
||||
/**
|
||||
* 是否初始化余额
|
||||
*/
|
||||
@Excel(name = "是否使用余额", width = 15, dicCode = "yn")
|
||||
@Dict(dicCode = "yn")
|
||||
@ApiModelProperty("是否使用余额")
|
||||
private java.lang.String useBalance;
|
||||
|
||||
/**
|
||||
* 客户类型
|
||||
*/
|
||||
@Excel(name = "客户类型", width = 15, dictTable = "client_category", dicText = "name", dicCode = "id")
|
||||
@Dict(dictTable = "client_category", dicText = "name", dicCode = "id")
|
||||
@ApiModelProperty(value = "客户类型")
|
||||
private java.lang.String clientCategoryId;
|
||||
|
||||
/**
|
||||
* IOSS号码
|
||||
*/
|
||||
|
|
|
@ -26,6 +26,10 @@ import org.jeecg.common.system.query.QueryGenerator;
|
|||
import org.jeecg.common.system.util.JwtUtil;
|
||||
import org.jeecg.common.system.vo.LoginUser;
|
||||
import org.jeecg.common.util.*;
|
||||
import org.jeecg.modules.business.entity.Client;
|
||||
import org.jeecg.modules.business.entity.UserClient;
|
||||
import org.jeecg.modules.business.service.IBalanceService;
|
||||
import org.jeecg.modules.business.service.IUserClientService;
|
||||
import org.jeecg.modules.system.entity.*;
|
||||
import org.jeecg.modules.system.model.DepartIdModel;
|
||||
import org.jeecg.modules.system.model.SysUserSysDepartModel;
|
||||
|
@ -101,6 +105,11 @@ public class SysUserController {
|
|||
@Autowired
|
||||
private ISysUserTenantService userTenantService;
|
||||
|
||||
@Autowired
|
||||
private IUserClientService userClientService;
|
||||
@Autowired
|
||||
private IBalanceService balanceService;
|
||||
|
||||
/**
|
||||
* 获取租户下用户数据(支持租户隔离)
|
||||
* @param user
|
||||
|
@ -148,12 +157,12 @@ public class SysUserController {
|
|||
|
||||
@RequiresPermissions("system:user:add")
|
||||
@RequestMapping(value = "/add", method = RequestMethod.POST)
|
||||
public Result<SysUser> add(@RequestBody JSONObject jsonObject) {
|
||||
public Result<SysUser> add(@RequestBody JSONObject payload) {
|
||||
Result<SysUser> result = new Result<SysUser>();
|
||||
String selectedRoles = jsonObject.getString("selectedroles");
|
||||
String selectedDeparts = jsonObject.getString("selecteddeparts");
|
||||
String selectedRoles = payload.getString("selectedroles");
|
||||
String selectedDeparts = payload.getString("selecteddeparts");
|
||||
try {
|
||||
SysUser user = JSON.parseObject(jsonObject.toJSONString(), SysUser.class);
|
||||
SysUser user = JSON.parseObject(payload.toJSONString(), SysUser.class);
|
||||
user.setCreateTime(new Date());//设置创建时间
|
||||
String salt = oConvertUtils.randomGen(8);
|
||||
user.setSalt(salt);
|
||||
|
@ -165,8 +174,20 @@ public class SysUserController {
|
|||
user.setOrgCode(null);
|
||||
// 保存用户走一个service 保证事务
|
||||
//获取租户ids
|
||||
String relTenantIds = jsonObject.getString("relTenantIds");
|
||||
String relTenantIds = payload.getString("relTenantIds");
|
||||
sysUserService.saveUser(user, selectedRoles, selectedDeparts, relTenantIds);
|
||||
// 判断是否为 WIA客户(通过部门名称)
|
||||
String departId = selectedDeparts.split(",")[0]; // 取第一个部门ID
|
||||
SysDepart depart = sysDepartService.getDepartById(departId);
|
||||
|
||||
if (depart != null && "WIA客户".equals(depart.getDepartName())) {
|
||||
String clientId = payload.getString("client");
|
||||
log.info("Add user client association for user ID: {}", user.getId());
|
||||
UserClient userClient = new UserClient();
|
||||
userClient.setUser_id(user.getId());
|
||||
userClient.setClient_id(clientId);
|
||||
userClientService.save(userClient);
|
||||
}
|
||||
baseCommonService.addLog("添加用户,username: " +user.getUsername() ,CommonConstant.LOG_TYPE_2, 2);
|
||||
result.success("添加成功!");
|
||||
} catch (Exception e) {
|
||||
|
@ -374,6 +395,25 @@ public class SysUserController {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据部门ID获取部门详情
|
||||
* @param departId 部门ID
|
||||
* @return SysDepart
|
||||
*/
|
||||
@RequestMapping(value = "/getDepartById", method = RequestMethod.GET)
|
||||
public Result<SysDepart> getDepartById(@RequestParam(name = "departId", required = true) String departId) {
|
||||
try {
|
||||
SysDepart depart = sysDepartService.getDepartById(departId);
|
||||
if (depart == null) {
|
||||
return Result.error("未找到对应部门信息");
|
||||
}
|
||||
return Result.ok(depart);
|
||||
} catch (Exception e) {
|
||||
log.error("查询部门出错:", e);
|
||||
return Result.error("查询失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成在添加用户情况下没有主键的问题,返回给前端,根据该id绑定部门数据
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue