后台下单接口
This commit is contained in:
parent
d175b69f93
commit
9c5dda67aa
|
|
@ -52,6 +52,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.math.RoundingMode;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
@ -140,13 +141,20 @@ public class OrderController extends BaseController {
|
||||||
*/
|
*/
|
||||||
@PostMapping("/sys/order")
|
@PostMapping("/sys/order")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public AjaxResult sysOrder(@RequestBody SysOrderAssignRequest request) {
|
public AjaxResult sysOrder(@RequestBody SysOrderAssignRequest request) {
|
||||||
String goodsBrand = request.getGoodsBrand();
|
String goodsBrand = request.getGoodsBrand();
|
||||||
Long deptId = getSysUser().getDeptId();
|
Long deptId = getSysUser().getDeptId();
|
||||||
|
List<OrderTemplateGoods> goodsList = request.getGoods();
|
||||||
ArrayList<Goods> goodList = new ArrayList<>();
|
// 订单总价 不能小于0
|
||||||
|
BigDecimal orderPrice = BigDecimal.valueOf(request.getPrice()).max(BigDecimal.ZERO);
|
||||||
|
// 商品单价
|
||||||
|
BigDecimal goodsPrice = orderPrice.divide(BigDecimal.valueOf(goodsList.size()), RoundingMode.HALF_UP);
|
||||||
|
// 累计商品价格 用来处理商品单价除不尽的情况
|
||||||
|
BigDecimal goodsPriceAccumulative = BigDecimal.ZERO;
|
||||||
|
int goodsNum = 0;
|
||||||
// 新增商品 -- 商品不关联师傅,状态为不展示在商城页面
|
// 新增商品 -- 商品不关联师傅,状态为不展示在商城页面
|
||||||
for (OrderTemplateGoods good : request.getGoods()) {
|
for (OrderTemplateGoods good : goodsList) {
|
||||||
List<Long> dgcIds = good.getDeptGoodsCategoryIds();
|
List<Long> dgcIds = good.getDeptGoodsCategoryIds();
|
||||||
List<String> dgcNames = good.getDeptGoodsCategoryNames();
|
List<String> dgcNames = good.getDeptGoodsCategoryNames();
|
||||||
// 商品的4级类要完整
|
// 商品的4级类要完整
|
||||||
|
|
@ -159,38 +167,106 @@ public class OrderController extends BaseController {
|
||||||
goods.setDeptGoodsCategoryId(dgcIds.get(2));
|
goods.setDeptGoodsCategoryId(dgcIds.get(2));
|
||||||
goods.setDeptGoodsCategoryName(dgcNames.get(2));
|
goods.setDeptGoodsCategoryName(dgcNames.get(2));
|
||||||
goods.setStatus(1);
|
goods.setStatus(1);
|
||||||
goodList.add(goods);
|
|
||||||
|
|
||||||
GoodsStandard standard = new GoodsStandard();
|
GoodsStandard standard = new GoodsStandard();
|
||||||
standard.setGoodsStandardName(good.getGoodsName());
|
standard.setGoodsStandardName(good.getGoodsName());
|
||||||
standard.setDeptGoodsCategoryId(dgcIds.get(3));
|
standard.setDeptGoodsCategoryId(dgcIds.get(3));
|
||||||
standard.setGoodsCategoryName(dgcNames.get(3));
|
standard.setGoodsCategoryName(dgcNames.get(3));
|
||||||
|
if ((++goodsNum) == (goodsList.size() - 1)) {
|
||||||
|
standard.setGoodsPrice(orderPrice.subtract(goodsPriceAccumulative));
|
||||||
|
} else {
|
||||||
|
standard.setGoodsPrice(goodsPrice);
|
||||||
|
goodsPriceAccumulative = goodsPriceAccumulative.add(goodsPrice);
|
||||||
|
}
|
||||||
|
|
||||||
goodsService.insertGoods(goods);
|
goodsService.insertGoods(goods);
|
||||||
}
|
standard.setGoodsId(goods.getGoodsId());
|
||||||
|
goodsStandardService.save(Collections.singletonList(standard));
|
||||||
List<AppGoodsRequest> appGoodsList = goodList.stream().map(x -> {
|
|
||||||
AppGoodsRequest agr = new AppGoodsRequest();
|
|
||||||
|
|
||||||
return agr;
|
|
||||||
}).collect(Collectors.toList());
|
|
||||||
BigDecimal totalPay = goodsService.calculate(appGoodsList);
|
|
||||||
try {
|
|
||||||
|
|
||||||
// 选择消费者(消费者类型为渠道商、不需要) --
|
|
||||||
|
|
||||||
// 生成消费者下单地址
|
|
||||||
|
|
||||||
// 生成主单
|
// 生成主单
|
||||||
|
OrderMaster orderMaster = new OrderMaster();
|
||||||
|
orderMaster.setDeptId(deptId);
|
||||||
|
orderMaster.setCode(orderMasterService.createOrderCode());
|
||||||
|
orderMaster.setOrderType(0);
|
||||||
|
orderMaster.setOrderStatus(OrderStatus.RECEIVE.code());
|
||||||
|
orderMaster.setAddressId(request.getAddressId());
|
||||||
|
orderMaster.setPayStatus(PayStatus.WAIT_PAY.getCode());
|
||||||
|
orderMaster.setCreateTime(new Date());
|
||||||
|
orderMaster.setWorkerId(goods.getWorkerId());
|
||||||
|
orderMaster.setGoodsId(goods.getGoodsId());
|
||||||
|
orderMasterService.insertOrderMaster(orderMaster);
|
||||||
|
Assert.notNull(orderMaster.getId(), "OrderMaster.id is null!");
|
||||||
|
|
||||||
// 生成财务单
|
// 生成财务主单
|
||||||
|
FinancialMaster financialMaster = new FinancialMaster(financialMasterService.createCode(), deptId,
|
||||||
|
orderMaster.getId(), orderMaster.getCode(), standard.getGoodsPrice(), BigDecimal.ZERO, standard.getGoodsPrice());
|
||||||
|
financialMaster.setServerMoney(financialMaster.getTotalMoney());
|
||||||
|
financialMasterService.insertFinancialMaster(financialMaster);
|
||||||
|
Assert.notNull(financialMaster.getId(), "FinancialMaster.id is null!");
|
||||||
|
|
||||||
// 生成财务细单 -- 平台扣点、提成
|
// 生成提成类子财务单
|
||||||
return AjaxResult.success("下单成功");
|
createDeptFinancialDetail(standard, deptId, financialMaster);
|
||||||
} catch (Exception e) {
|
|
||||||
|
// 生成商品订单
|
||||||
|
OrderGoods orderGoods = new OrderGoods(orderMaster.getId(), standard.getGoodsStandardId(),
|
||||||
|
standard.getGoodsStandardName(), good.getNumber(), 0);
|
||||||
|
orderGoodsService.insertOrderGoods(orderGoods);
|
||||||
|
|
||||||
return AjaxResult.error("系统异常");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 选择消费者(消费者类型为渠道商、不需要) --
|
||||||
|
|
||||||
|
// 生成消费者下单地址
|
||||||
|
|
||||||
|
// 生成主单
|
||||||
|
|
||||||
|
// 生成财务单
|
||||||
|
|
||||||
|
// 生成财务细单 -- 平台扣点、提成
|
||||||
|
return AjaxResult.success("下单成功");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 平台下单时生成子财务单的方法
|
||||||
|
* 只有平台扣点和截流扣点
|
||||||
|
*
|
||||||
|
* @param goodsStandard 商品
|
||||||
|
* @param deptId 企业ID
|
||||||
|
* @param financialMaster 主财务单
|
||||||
|
*/
|
||||||
|
private void createDeptFinancialDetail(GoodsStandard goodsStandard, Long deptId, FinancialMaster financialMaster) {
|
||||||
|
// 平台
|
||||||
|
BigDecimal deptMoney = BigDecimal.ZERO;
|
||||||
|
// 截流扣点
|
||||||
|
BigDecimal specialMoney = BigDecimal.ZERO;
|
||||||
|
// 服务金额
|
||||||
|
BigDecimal serverMoney = financialMaster.getServerMoney();
|
||||||
|
// 扣点设置类目
|
||||||
|
DeptGoodsCategory deptGoodsCategory = deptGoodsCategoryService.get(goodsStandard.getDeptGoodsCategoryId());
|
||||||
|
// 平台扣点 = 平台金额 + 商品单价 * 扣点比例
|
||||||
|
deptMoney = deptMoney.add(deptGoodsCategory.getDeptMoney())
|
||||||
|
.add(goodsStandard.getGoodsPrice().multiply(BigDecimal.valueOf(Double.parseDouble(deptGoodsCategory.getDeptRate()))));
|
||||||
|
// 截流扣点 = 截流金额 + 商品单价 * 扣点比例
|
||||||
|
specialMoney = specialMoney.add(deptGoodsCategory.getRetainMoney())
|
||||||
|
.add(goodsStandard.getGoodsPrice().multiply(BigDecimal.valueOf(Double.parseDouble(deptGoodsCategory.getRetainRate()))));
|
||||||
|
// 平台扣点记录
|
||||||
|
FinancialDetail deptDetail = new FinancialDetail(deptId, financialDetailService.createCode(),
|
||||||
|
financialMaster.getId(), financialMaster.getCode(), deptMoney, FinancialDetailType.PLATFORM_FEE.getCode(), null);
|
||||||
|
financialDetailService.insertFinancialDetail(deptDetail);
|
||||||
|
// 剩余服务金额
|
||||||
|
serverMoney = serverMoney.subtract(deptMoney);
|
||||||
|
|
||||||
|
// 截流扣点记录
|
||||||
|
FinancialDetail retainDetail = new FinancialDetail(deptId, financialDetailService.createCode(),
|
||||||
|
financialMaster.getId(), financialMaster.getCode(), specialMoney, FinancialDetailType.PLATFORM_FEE.getCode(), null);
|
||||||
|
financialDetailService.insertFinancialDetail(retainDetail);
|
||||||
|
// 剩余服务金额
|
||||||
|
serverMoney = serverMoney.subtract(specialMoney);
|
||||||
|
|
||||||
|
FinancialMaster fmUpdate = new FinancialMaster();
|
||||||
|
fmUpdate.setId(financialMaster.getId());
|
||||||
|
fmUpdate.setServerMoney(serverMoney);
|
||||||
|
financialMasterService.updateFinancialMaster(financialMaster);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -22,5 +22,5 @@ public class OrderTemplateGoods {
|
||||||
// 四层商品类型
|
// 四层商品类型
|
||||||
private List<String> deptGoodsCategoryNames;
|
private List<String> deptGoodsCategoryNames;
|
||||||
// 商品数量
|
// 商品数量
|
||||||
private Long number;
|
private Integer number;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,8 @@ public class SysOrderAssignRequest {
|
||||||
|
|
||||||
// 商品品牌
|
// 商品品牌
|
||||||
private String goodsBrand;
|
private String goodsBrand;
|
||||||
|
// 商品规格
|
||||||
|
private String goodsSpecification;
|
||||||
// 视频
|
// 视频
|
||||||
private String videoUrl;
|
private String videoUrl;
|
||||||
// 图片
|
// 图片
|
||||||
|
|
@ -21,5 +22,8 @@ public class SysOrderAssignRequest {
|
||||||
|
|
||||||
// 商品相关信息
|
// 商品相关信息
|
||||||
private Long goodsDeptCategoryId;
|
private Long goodsDeptCategoryId;
|
||||||
|
// 服务地址ID
|
||||||
|
private Long addressId;
|
||||||
|
// 发布价格(限制整数)
|
||||||
|
private Integer price;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue