package com.sinata.modular.mall.controller;
|
|
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONUtil;
|
import com.baomidou.mybatisplus.enums.SqlLike;
|
import com.baomidou.mybatisplus.mapper.Condition;
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
import com.baomidou.mybatisplus.mapper.Wrapper;
|
import com.baomidou.mybatisplus.plugins.Page;
|
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
|
import com.google.common.collect.Lists;
|
import com.sinata.common.enums.EnumCityRole;
|
import com.sinata.common.enums.EnumMemberGrade;
|
import com.sinata.common.enums.mall.EnumMallGoodsGroupType;
|
import com.sinata.common.enums.mall.EnumMallOrderState;
|
import com.sinata.core.base.controller.BaseController;
|
import com.sinata.core.common.annotion.BussinessLog;
|
import com.sinata.core.common.annotion.Permission;
|
import com.sinata.core.common.constant.factory.PageFactory;
|
import com.sinata.core.shiro.ShiroKit;
|
import com.sinata.core.shiro.ShiroUser;
|
import com.sinata.core.util.DateUtils2;
|
import com.sinata.core.util.ExcelExportUtil;
|
import com.sinata.core.util.ExpressApi;
|
import com.sinata.core.util.PayUtil;
|
import com.sinata.modular.mall.dto.OrderSearchDto;
|
import com.sinata.modular.mall.model.*;
|
import com.sinata.modular.mall.service.*;
|
import com.sinata.modular.member.model.MemUser;
|
import com.sinata.modular.member.model.MemUserRelation;
|
import com.sinata.modular.member.service.IMemUserRelationService;
|
import com.sinata.modular.member.service.IMemUserService;
|
import com.sinata.modular.system.model.Role;
|
import com.sinata.modular.system.service.IMyCouponService;
|
import com.sinata.modular.system.service.IRoleService;
|
import java.util.Map;
|
import java.util.Set;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.ui.Model;
|
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.IOException;
|
import java.math.BigDecimal;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Objects;
|
import java.util.stream.Collectors;
|
|
/**
|
* 商品订单控制器
|
*
|
* @author goku
|
*/
|
@Controller
|
@RequestMapping("/mallOrder")
|
public class MallOrderController extends BaseController {
|
|
private String PREFIX = "/mall/mallOrder/";
|
|
@Resource
|
private IMemUserService memUserService;
|
|
@Resource
|
private IMemUserRelationService userRelationService;
|
|
@Resource
|
private IMyCouponService myCouponService;
|
|
@Autowired
|
private IMallOrderService mallOrderService;
|
|
@Resource
|
private IMallGoodsSetService mallGoodsSetService;
|
|
@Resource
|
private IMallGoodsService mallGoodsService;
|
|
@Resource
|
private IMallOrderDetailService mallOrderDetailService;
|
|
@Resource
|
private IMallExpressCompanyService mallExpressCompanyService;
|
|
@Resource
|
private IMallOrderDetailGroupSpecService mallOrderDetailGroupSpecService;
|
@Resource
|
private IRoleService roleService;
|
@Resource
|
private IMallGoodsSkuService mallGoodsSkuService;
|
|
private MallOrder selectById(String orderNo) {
|
return this.mallOrderService.selectOne(new EntityWrapper<MallOrder>()
|
.eq("order_no", orderNo));
|
}
|
|
/**
|
* 跳转到商品订单首页
|
*/
|
@RequestMapping("")
|
public String index() {
|
return PREFIX + "mallOrder.html";
|
}
|
|
/**
|
* 跳转到添加商品订单
|
*/
|
@RequestMapping("/mallOrder_add")
|
public String mallOrderAdd() {
|
return PREFIX + "mallOrder_add.html";
|
}
|
|
/**
|
* 跳转到设置运费
|
*/
|
@RequestMapping("/setFreightAuth/{orderNo}")
|
public String mallOrderUpdate(@PathVariable String orderNo, Model model) {
|
model.addAttribute("item", selectById(orderNo));
|
return PREFIX + "setFreightAuth.html";
|
}
|
|
/**
|
* 退款页面
|
*/
|
@RequestMapping(value = "/deliverAuth/{orderNo}")
|
public String deliverAuth(@PathVariable String orderNo, Model model) {
|
model.addAttribute("orderNo", orderNo);
|
model.addAttribute("expressCompanyList", this.mallExpressCompanyService.selectList(Condition.empty()));
|
return PREFIX + "deliverAuth.html";
|
}
|
|
/**
|
* 获取商品订单列表
|
*/
|
@ResponseBody
|
@RequestMapping(value = "/list")
|
public Object list(OrderSearchDto dto) {
|
Page<MallOrder> page = new PageFactory().defaultPage();
|
|
Wrapper<?> wrapper = wrapperList(dto);
|
|
page = mallOrderService.findPage(page, wrapper);
|
return super.packForBT(page);
|
}
|
|
/**
|
* 新增商品订单
|
*/
|
@Permission
|
@ResponseBody
|
@BussinessLog(value = "新增商品订单")
|
@RequestMapping(value = "/add")
|
public Object add(MallOrder mallOrder) {
|
mallOrderService.insert(mallOrder);
|
return SUCCESS_TIP;
|
}
|
|
/**
|
* 删除/批量删除
|
*/
|
@Permission
|
@ResponseBody
|
@BussinessLog(value = "删除商品订单")
|
@RequestMapping(value = "/delete")
|
public Object delete(@RequestParam String orderNo) {
|
// 逻辑删除
|
mallOrderService.updateForSet("cms_delete = 1", new EntityWrapper<MallOrder>().in("order_no", orderNo));
|
return SUCCESS_TIP;
|
}
|
|
/**
|
* 修改商品订单
|
*/
|
@Permission
|
@ResponseBody
|
@BussinessLog(value = "修改商品订单")
|
@RequestMapping(value = "/update")
|
public Object update(MallOrder mallOrder) {
|
mallOrderService.updateById(mallOrder);
|
return SUCCESS_TIP;
|
}
|
|
/**
|
* 修改商品订单状态
|
*/
|
@ResponseBody
|
@BussinessLog(value = "修改商品订单状态")
|
@RequestMapping(value = "/updateState")
|
public Object updateState(Integer mallOrderId, Integer state) {
|
mallOrderService.updateForSet("state = " + state, new EntityWrapper<MallOrder>().eq("id", mallOrderId));
|
return SUCCESS_TIP;
|
}
|
|
/**
|
* 跳转物流详情
|
*/
|
@RequestMapping(value = "/logisticsAuth/{orderNo}")
|
public Object logisticsAuth(@PathVariable("orderNo") String orderNo, Model model) {
|
model.addAttribute("item", this.mallOrderService.selectById(orderNo));
|
return PREFIX + "mallOrder_express.html";
|
}
|
|
/**
|
* 物流信息
|
*/
|
@ResponseBody
|
@RequestMapping(value = "/express")
|
public Object express(String expressNo) {
|
return ExpressApi.query(expressNo, null);
|
}
|
|
|
/**
|
* 跳转商品订单详情
|
*/
|
@RequestMapping(value = "/detail")
|
public Object detail(String orderNo, Integer orderType, Model model) {
|
Page<MallOrder> page = new PageFactory().defaultPage(1, 0);
|
|
Wrapper<?> wrapper = new EntityWrapper<MallOrder>()
|
.eq("o.order_no", orderNo);
|
MallOrder mallOrder = mallOrderService.findPage(page, wrapper).getRecords().get(0);
|
|
// 订单详情
|
List<MallOrderDetail> orderDetailList = this.mallOrderDetailService.selectList(new EntityWrapper<MallOrderDetail>().eq("order_no", orderNo));
|
|
// 订单详情-商品信息
|
List<Integer> goodsIdList = orderDetailList.stream().map(MallOrderDetail::getGoodsId).collect(Collectors.toList());
|
|
// 订单详情套餐规格组商品
|
List<MallOrderDetailGroupSpec> mallOrderDetailGroupSpecList = mallOrderDetailGroupSpecService.selectList(new EntityWrapper<MallOrderDetailGroupSpec>().eq("order_no", mallOrder.getOrderNo()));
|
if (CollUtil.isNotEmpty(mallOrderDetailGroupSpecList)) {
|
List<Integer> mallOrderDetailGroupSpecGroupSpecGoodsIds = mallOrderDetailGroupSpecList.stream().map(MallOrderDetailGroupSpec::getGroupSpecGoodsId).collect(Collectors.toList());
|
goodsIdList.addAll(mallOrderDetailGroupSpecGroupSpecGoodsIds);
|
}
|
|
List<MallGoods> goodsList = mallGoodsService.selectList(new EntityWrapper<MallGoods>().in("id", goodsIdList));
|
|
// 封装商品信息
|
mallOrder.setOrderDetails(
|
orderDetailList.stream().map(o -> {
|
String goodsNo = "";
|
for (MallGoods g : goodsList) {
|
if (g.getId().equals(o.getGoodsId())) {
|
goodsNo = g.getGoodsNo();
|
o.setGoodsName(g.getGoodsName());
|
}
|
}
|
o.setGoodsNo(goodsNo);
|
return o;
|
}).collect(Collectors.toList())
|
);
|
MemUser memUser = this.memUserService.selectById(mallOrder.getUserId());
|
if (memUser != null) {
|
mallOrder.setUserShowId(memUser.getShowId());
|
mallOrder.setUserPhone(memUser.getPhone());
|
mallOrder.setUserNickname(memUser.getNickName());
|
}
|
//mallOrder.setUseList(useService.getUseList(mallOrder.getOrderNo()));
|
mallOrder.setUseOrderDetailGroupSpecList(mallOrderDetailGroupSpecService.getUseOrderDetailGroupSpec(mallOrder.getOrderNo()));
|
model.addAttribute("item", mallOrder);
|
|
String v4ShowId = "", v4Team = "", v5ShowId = "", v5Team = "";
|
|
// 计算订单业绩
|
BigDecimal saleGoodsMoney = BigDecimal.ZERO;
|
try {
|
if (mallOrder.getSaleUserId() != null && mallOrder.getSaleUserId() != 0 && mallOrder.getGoodsId() != null) {
|
MemUser saleUser = memUserService.selectById(mallOrder.getSaleUserId());
|
MallGoodsSet goodsSet = mallGoodsSetService.selectById(mallOrder.getGoodsId());
|
if (saleUser.getMemberGradeId() == EnumMemberGrade.G_3.index) {
|
saleGoodsMoney = mallOrder.getPayMoney().multiply(goodsSet.getV3BuyCoef());
|
}
|
if (saleUser.getMemberGradeId() == EnumMemberGrade.G_4.index) {
|
saleGoodsMoney = mallOrder.getPayMoney().multiply(goodsSet.getV4BuyCoef());
|
}
|
if (saleUser.getMemberGradeId() == EnumMemberGrade.G_5.index) {
|
saleGoodsMoney = mallOrder.getPayMoney().multiply(goodsSet.getV5BuyCoef());
|
}
|
|
// 获取合伙人、市场总监
|
MemUserRelation saleUserRelation = userRelationService.selectById(mallOrder.getSaleUserId());
|
Integer saleUserMemberGradeId = saleUser.getMemberGradeId();
|
MemUser[] parentV4V5User = userRelationService.getParentV4V5ByRelationPath(null, null, saleUserRelation.getRelationPath(), saleUserMemberGradeId);
|
if (parentV4V5User[0] != null) {
|
v4ShowId = parentV4V5User[0].getShowId();
|
v4Team = parentV4V5User[0].getRealName();
|
}
|
if (parentV4V5User[1] != null) {
|
v5ShowId = parentV4V5User[1].getShowId();
|
v5Team = parentV4V5User[1].getRealName();
|
}
|
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
model.addAttribute("v4ShowId", v4ShowId);
|
model.addAttribute("v4Team", v4Team);
|
model.addAttribute("v5ShowId", v5ShowId);
|
model.addAttribute("v5Team", v5Team);
|
|
model.addAttribute("saleGoodsMoney", saleGoodsMoney.setScale(2, BigDecimal.ROUND_HALF_UP));
|
|
model.addAttribute("orderType", orderType);
|
if (orderType == null || orderType == EnumMallGoodsGroupType.GOODS.index) {
|
// 普通商品
|
return PREFIX + "mallOrder_detail.html";
|
} else {
|
model.addAttribute("mallOrderDetailGroupSpecList", mallOrderDetailGroupSpecList.stream()
|
.map(o -> {
|
JSONObject json = JSONUtil.parseObj(o);
|
|
for (MallGoods g : goodsList) {
|
if (g.getId().equals(o.getGroupSpecGoodsId())) {
|
json.set("groupSpecGoodsName", g.getGoodsName());
|
json.set("groupSpecGoodsNo", g.getGoodsNo());
|
json.set("groupSpecGoodsImage", g.getGoodsImage());
|
}
|
}
|
|
return json;
|
})
|
.collect(Collectors.toList()));
|
|
// 套餐商品
|
return PREFIX + "mallOrder_detail_group.html";
|
}
|
}
|
|
/**
|
* 跳转商品订单详情
|
*/
|
@RequestMapping(value = "/goodsDetails/{orderNo}")
|
public Object goodsDetails(@PathVariable("orderNo") String orderNo, Model model) {
|
MallOrder mallOrder = mallOrderService.selectById(orderNo);
|
|
mallOrder.setOrderDetails(this.mallOrderDetailService.selectList(new EntityWrapper<MallOrderDetail>()
|
.eq("order_no", orderNo)));
|
MemUser memUser = this.memUserService.selectById(mallOrder.getUserId());
|
if (memUser != null) {
|
mallOrder.setUserShowId(memUser.getShowId());
|
mallOrder.setUserPhone(memUser.getPhone());
|
mallOrder.setUserNickname(memUser.getNickName());
|
}
|
|
model.addAttribute("item", mallOrder);
|
return PREFIX + "mallOrder_goods.html";
|
}
|
|
/**
|
* 获取订单商品
|
*
|
* @param orderNo
|
* @return
|
*/
|
@ResponseBody
|
@RequestMapping(value = "/getOrderGoods")
|
public Object updateState(Integer orderNo) {
|
return this.mallOrderDetailService.selectList(new EntityWrapper<MallOrderDetail>()
|
.eq("order_no", orderNo));
|
}
|
|
public Wrapper wrapperList(OrderSearchDto dto) {
|
Wrapper<?> wrapper = new EntityWrapper<MallOrder>()
|
.orderBy("o.create_time", false)
|
.ge(StrUtil.isNotBlank(dto.getBeginTime()), "o.create_time", dto.getBeginTime())
|
.le(StrUtil.isNotBlank(dto.getEndTime()), "o.create_time", dto.getEndTime() + " 23:59:59")
|
.eq(StrUtil.isNotBlank(dto.getOrderNo()), "o.order_no", dto.getOrderNo())
|
.eq(StrUtil.isNotBlank(dto.getUserShowId()), "u.show_id", dto.getUserShowId())
|
.eq(StrUtil.isNotBlank(dto.getUserPhone()), "u.phone", dto.getUserPhone())
|
//.like(StrUtil.isNotBlank(dto.getUserNickname()), "user.nick_name", dto.getUserNickname());
|
.like(StrUtil.isNotBlank(dto.getUserNickname()), "o.take_name", dto.getUserNickname())
|
.like(StrUtil.isNotBlank(dto.getGoodsName()), "order_goods.goods_name", dto.getGoodsName());
|
|
try {
|
// 【城市管理员】数据查询
|
ShiroUser shiroUser = ShiroKit.getUser();
|
List<Integer> roleList = shiroUser.getRoleList();
|
List<Integer> cityRoleList = roleService.selectList(
|
new EntityWrapper<Role>().in("id", roleList)).stream().map(Role::getCityRole)
|
.collect(
|
Collectors.toList());
|
if (cityRoleList.contains(EnumCityRole.CITY_ROLE.index)) {
|
// 市级城市管理员
|
wrapper.like("o.city_code", shiroUser.getCityCode().substring(0, 4), SqlLike.RIGHT);
|
}
|
else if (cityRoleList.contains(EnumCityRole.PROVINCE_ROLE.index)) {
|
// 省级城市管理员
|
wrapper.like("o.city_code", shiroUser.getCityCode().substring(0, 2), SqlLike.RIGHT);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
if (null != dto.getState()) {
|
if (dto.getState().equals(1)) {
|
wrapper.in("o.state", Lists.newArrayList(1, 2));
|
} else {
|
wrapper.eq("o.state", dto.getState());
|
}
|
}
|
return wrapper;
|
}
|
|
|
@ResponseBody
|
@RequestMapping(value = "/export")
|
public void exportOrder(OrderSearchDto dto, HttpServletResponse response) throws IOException {
|
Wrapper wrapper = wrapperList(dto);
|
List<MallOrder> orders = this.mallOrderService.findList(wrapper);
|
|
List<List<Object>> dataList = new ArrayList<>();
|
List<Object> titles = CollUtil.newArrayList(
|
"营销员工号",
|
"营销员姓名",
|
"营销业绩",
|
"下单时间",
|
"订单编号",
|
"订单来源",
|
"订单类型",
|
"商品名称",
|
"规格组",
|
"购买数量",
|
"客户姓名",
|
"下单用户手机",
|
"预约门店",
|
"核销次数",
|
"订单金额",
|
"优惠金额",
|
"实付款",
|
"订单状态",
|
"退款时间"
|
);
|
dataList.add(titles);
|
|
for (MallOrder order : orders) {
|
dataList.add(
|
CollUtil.newArrayList(
|
order.getSaleShowId(),
|
order.getSaleUserName(),
|
order.getGoodsMoney(),
|
DateUtils2.format(order.getCreateTime(), "YYYY-MM-dd HH:mm:ss"),
|
order.getOrderNo(),
|
Objects.nonNull(order.getSaleShowId()) ? "推广链接" : "自然",
|
getOrderType(order.getOrderType()),
|
order.getGoodsName(),
|
order.getGrepName(),
|
order.getGoodsNum(),
|
order.getTakeName(),
|
order.getUserPhone(),
|
order.getMerchantName(),
|
order.getUseNum(),
|
order.getGoodsMoney(),
|
order.getGoodsMoney().subtract(order.getPayMoney()),
|
order.getPayMoney(),
|
order.getStateName(),
|
Objects.nonNull(order.getRefundTime()) ? DateUtils2.format(order.getRefundTime(), "YYYY-MM-dd HH:mm:ss") : ""
|
));
|
}
|
ExcelExportUtil.easySheet("导出数据" + DateUtils2.formatDate(new Date(), "YYYYMMddHHmmSS"), "导出数据", dataList, response);
|
}
|
|
private String getOrderType(int key) {
|
String value = "普通商品";
|
if (key == 1) {
|
value = "黄金套餐";
|
} else if (key == 2) {
|
value = "钻石套餐";
|
}
|
return value;
|
}
|
|
/**
|
* 取消订单
|
*
|
* @param orderNo
|
*/
|
@ResponseBody
|
@RequestMapping(value = "/cancelOrder")
|
public Object cancelOrder(String orderNo) {
|
this.mallOrderService.cancelOrder(orderNo);
|
return SUCCESS_TIP;
|
}
|
|
|
/**
|
* 退款操作
|
*/
|
@ResponseBody
|
@RequestMapping(value = "/orderShip")
|
@Transactional(rollbackFor = Exception.class)
|
public Object orderShip(String orderNo, Integer expressCompany) {
|
MallOrder order = this.mallOrderService.selectOne(
|
new EntityWrapper<MallOrder>()
|
.eq("order_no", orderNo)
|
.last("for update")
|
);
|
|
if (expressCompany.equals(0)) {
|
order.setState(EnumMallOrderState.WAIT_CHECK.index);
|
} else {
|
// 取消订单扣积分
|
memUserService.subIntegralCancelOrder(orderNo);
|
|
if (order.getCouponId() != null && order.getCouponId() != 0) {
|
// 退还优惠券
|
myCouponService.updateUseCoupon(order.getUserId(), order.getCouponId(), 0);
|
}
|
|
order.setState(EnumMallOrderState.REFUNDED.index);
|
BigDecimal refundPrice = order.getPayMoney();
|
PayUtil.wechatRefund("T".concat(order.getOrderNo()), order.getTransactionNo(), order.getOutTradeNo(),
|
order.getPayTotalFee(),
|
refundPrice.multiply(Convert.toBigDecimal(100)),
|
"用户申请退款", "rest/mall/pay/notify/wx"
|
);
|
order.setRefundPrice(refundPrice);
|
order.setRefundTime(new Date());
|
}
|
|
this.mallOrderService.updateById(order);
|
//退回库存
|
List<MallOrderDetail> orderDetailList = this.mallOrderDetailService.selectList(new EntityWrapper<MallOrderDetail>().eq("order_no", orderNo));
|
if (CollectionUtils.isNotEmpty(orderDetailList)) {
|
Map<Integer, MallOrderDetail> orderDetailMap = orderDetailList.stream()
|
.collect(Collectors.toMap(MallOrderDetail::getSkuId, e -> e));
|
Set<Integer> skuIdList = orderDetailList.stream().map(MallOrderDetail::getSkuId)
|
.collect(Collectors.toSet());
|
List<MallGoodsSku> skuList = mallGoodsSkuService.selectList(
|
new EntityWrapper<MallGoodsSku>().in("id", skuIdList));
|
for (MallGoodsSku mallGoodsSku : skuList) {
|
MallOrderDetail mallOrderDetail = orderDetailMap.get(mallGoodsSku.getId());
|
mallGoodsSku.setStock(mallGoodsSku.getStock() + mallOrderDetail.getGoodsNum());
|
mallGoodsSkuService.updateById(mallGoodsSku);
|
}
|
}
|
return SUCCESS_TIP;
|
}
|
}
|