mitao
2024-06-06 3d2b51ea4520533de5e78f88dddf5b5c7dce4247
meiya-admin/src/main/java/com/sinata/modular/mall/controller/MallOrderController.java
@@ -5,11 +5,13 @@
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
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;
@@ -32,9 +34,15 @@
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 lombok.extern.slf4j.Slf4j;
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;
@@ -56,6 +64,7 @@
 *
 * @author goku
 */
@Slf4j
@Controller
@RequestMapping("/mallOrder")
public class MallOrderController extends BaseController {
@@ -88,7 +97,10 @@
    @Resource
    private IMallOrderDetailGroupSpecService mallOrderDetailGroupSpecService;
    @Resource
    private IRoleService roleService;
    @Resource
    private IMallGoodsSkuService mallGoodsSkuService;
    private MallOrder selectById(String orderNo) {
        return this.mallOrderService.selectOne(new EntityWrapper<MallOrder>()
@@ -377,12 +389,18 @@
        try {
            // 【城市管理员】数据查询
            ShiroUser shiroUser = ShiroKit.getUser();
            if (shiroUser.getRoleList().contains(EnumCityRole.PROVINCE_ROLE.index)) {
                // 省级城市管理员
                wrapper.like("o.city_code", shiroUser.getCityCode().substring(0, 2), SqlLike.RIGHT);
            } else if (shiroUser.getRoleList().contains(EnumCityRole.CITY_ROLE.index)) {
            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);
                wrapper.like("user2.agent_county_code", shiroUser.getCityCode().substring(0, 4), SqlLike.RIGHT);
            }
            else if (cityRoleList.contains(EnumCityRole.PROVINCE_ROLE.index)) {
                // 省级城市管理员
                wrapper.like("user2.agent_county_code", shiroUser.getCityCode().substring(0, 2), SqlLike.RIGHT);
            }
        } catch (Exception e) {
            e.printStackTrace();
@@ -484,6 +502,7 @@
     */
    @ResponseBody
    @RequestMapping(value = "/orderShip")
    @Transactional(rollbackFor = Exception.class)
    public Object orderShip(String orderNo, Integer expressCompany) {
        MallOrder order = this.mallOrderService.selectOne(
                new EntityWrapper<MallOrder>()
@@ -511,6 +530,44 @@
            );
            order.setRefundPrice(refundPrice);
            order.setRefundTime(new Date());
            //退回库存
            List<MallOrderDetail> orderDetailList = this.mallOrderDetailService.selectList(new EntityWrapper<MallOrderDetail>().eq("order_no", orderNo));
            log.info("----------------------------处理退款回退库存--------------------------");
            log.info("orderDetailList:{}", JSON.toJSONString(orderDetailList));
            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);
                }
                //退销量
                Set<Integer> goodsIdList = orderDetailList.stream().map(MallOrderDetail::getGoodsId)
                        .collect(Collectors.toSet());
                Wrapper<MallGoods> wrapper = new EntityWrapper<MallGoods>().in("id", goodsIdList);
                List<MallGoods> mallGoods = mallGoodsService.selectList(wrapper);
                Map<Integer, MallOrderDetail> orderDetailMap2 = orderDetailList.stream()
                        .collect(Collectors.toMap(MallOrderDetail::getGoodsId, e -> e));
                if (CollectionUtils.isNotEmpty(mallGoods)) {
                    for (MallGoods mallGoods1 : mallGoods) {
                        MallOrderDetail mallOrderDetail1 = orderDetailMap2.get(mallGoods1.getId());
                        if (Objects.nonNull(mallOrderDetail1)) {
                            log.info("----------------------------处理退款回退销量--------------------------");
                            log.info("mallGoods1:{}", JSON.toJSONString(mallGoods1));
                            mallGoods1.setBuyCount(mallGoods1.getBuyCount() - mallOrderDetail1.getGoodsNum());
                            mallGoodsService.updateById(mallGoods1);
                        }
                    }
                }
            }
        }
        this.mallOrderService.updateById(order);