1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.ruoyi.sales.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.sales.mapper.ShipmentApprovalMapper;
import com.ruoyi.sales.mapper.ShippingInfoMapper;
import com.ruoyi.sales.pojo.SalesLedger;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import com.ruoyi.sales.pojo.ShipmentApproval;
import com.ruoyi.sales.pojo.ShippingInfo;
import com.ruoyi.sales.service.ISalesLedgerProductService;
import com.ruoyi.sales.service.ISalesLedgerService;
import com.ruoyi.sales.service.ShippingInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import java.util.List;
 
/**
 * @author :yys
 * @date : 2025/10/22 9:34
 */
@RestController
@RequestMapping("/shippingInfo")
@Api(tags = "发货信息管理")
public class ShippingInfoController extends BaseController {
 
    @Autowired
    private ShippingInfoService shippingInfoService;
    @Autowired
    private ShipmentApprovalMapper shipmentApprovalMapper;
    @Autowired
    private ISalesLedgerProductService salesLedgerProductService;
 
 
    @GetMapping("/listPage")
    @ApiOperation("发货信息列表")
    public AjaxResult listPage(Page page, ShippingInfo req) {
        IPage<ShippingInfo> listPage = shippingInfoService.listPage(page,req);
        return AjaxResult.success(listPage);
    }
 
    @PostMapping("/add")
    @ApiOperation("添加发货信息")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(@RequestBody ShippingInfo req) {
        LambdaQueryWrapper<ShippingInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShippingInfo::getSalesLedgerId, req.getSalesLedgerId());
        wrapper.eq(ShippingInfo::getSalesLedgerProductId, req.getSalesLedgerProductId());
        List<ShippingInfo> list = shippingInfoService.list(wrapper);
        if(!CollectionUtils.isEmpty(list)){
            return AjaxResult.error("发货信息已存在");
        }
        boolean save = shippingInfoService.save(req);
        if(save){
            ShippingInfo shippingInfo = shippingInfoService.getOne(wrapper);
            ShipmentApproval shipmentApproval = new ShipmentApproval();
            shipmentApproval.setSalesLedgerId(req.getSalesLedgerId());
            shipmentApproval.setSalesLedgerProductId(req.getSalesLedgerProductId());
            shipmentApproval.setApproveUserId(req.getApproverId());
            shipmentApproval.setApproveStatus(2);
            shipmentApproval.setShippingInfoId(shippingInfo.getId());
            shipmentApprovalMapper.insert(shipmentApproval);
 
            SalesLedgerProduct salesLedgerProduct = salesLedgerProductService.getById(req.getSalesLedgerProductId());
            if(salesLedgerProduct != null){
                salesLedgerProduct.setApproveStatus(2);
                salesLedgerProductService.updateById(salesLedgerProduct);
            }
 
        }
        return save ? AjaxResult.success() : AjaxResult.error();
    }
 
    @PostMapping("/update")
    @ApiOperation("修改发货信息")
    public AjaxResult update(@RequestBody ShippingInfo req) {
        ShippingInfo byId = shippingInfoService.getById(req.getId());
        if (byId == null) {
            return AjaxResult.error("发货信息不存在");
        }
        Long userId = getLoginUser().getUserId();
        if(!userId.equals(Long.parseLong(byId.getCreateUser().toString()))){
            return AjaxResult.error("您没有权限修改此发货信息");
        }
        boolean update = shippingInfoService.updateById(req);
        return update ? AjaxResult.success() : AjaxResult.error();
    }
 
    @DeleteMapping("/delete")
    @ApiOperation("删除发货信息")
    public AjaxResult delete(@RequestBody List<Long> ids) {
        Long userId = getLoginUser().getUserId();
        ids.forEach(id -> {
            ShippingInfo byId = shippingInfoService.getById(id);
            if (byId == null) {
                throw new RuntimeException("发货信息不存在");
            }
            if(!userId.equals(Long.parseLong(byId.getCreateUser().toString()))){
                throw new RuntimeException("您没有权限删除此发货信息");
            }
        });
        boolean delete = shippingInfoService.removeBatchByIds(ids);
        return delete ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }
 
    @Autowired
    private ShippingInfoMapper shippingInfoMapper;
 
    /**
     * 导出发货信息管理
     */
    @PostMapping("/export")
    @ApiOperation("导出发货信息")
    public void export(HttpServletResponse response) {
        List<ShippingInfo> list = shippingInfoMapper.listAll();
        ExcelUtil<ShippingInfo> util = new ExcelUtil<ShippingInfo>(ShippingInfo.class);
        util.exportExcel(response, list, "发货信息");
    }
 
}