Blame view

src/main/java/com/huaheng/pc/shipment/shipmentHeader/service/ShipmentHeaderServiceImpl.java 9.58 KB
tangying authored
1
2
package com.huaheng.pc.shipment.shipmentHeader.service;
3
4
5
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.common.exception.service.ServiceException;
pengcheng authored
6
import com.huaheng.common.support.Convert;
pengcheng authored
7
8
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
pengcheng authored
9
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
10
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
pengcheng authored
11
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
12
13
14
15
import com.huaheng.pc.shipment.shipmentDetailHistory.domain.ShipmentDetailHistory;
import com.huaheng.pc.shipment.shipmentDetailHistory.service.ShipmentDetailHistoryService;
import com.huaheng.pc.shipment.shipmentHeaderHistory.domain.ShipmentHeaderHistory;
import com.huaheng.pc.shipment.shipmentHeaderHistory.service.ShipmentHeaderHistoryService;
pengcheng authored
16
import com.huaheng.pc.system.dict.service.IDictDataService;
17
import org.apache.commons.beanutils.BeanUtils;
pengcheng authored
18
import org.springframework.beans.factory.annotation.Autowired;
tangying authored
19
20
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
21
import java.lang.reflect.InvocationTargetException;
pengcheng authored
22
import java.text.SimpleDateFormat;
23
import java.util.ArrayList;
pengcheng authored
24
import java.util.Date;
mahuandong authored
25
import java.util.List;
pengcheng authored
26
27
import java.util.Map;
mahuandong authored
28
29
30
31
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.mapper.ShipmentHeaderMapper;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
32
33
import org.springframework.transaction.annotation.Transactional;
tangying authored
34
@Service
mahuandong authored
35
public class ShipmentHeaderServiceImpl extends ServiceImpl<ShipmentHeaderMapper, ShipmentHeader> implements ShipmentHeaderService{
tangying authored
36
pengcheng authored
37
38
39
40
    @Autowired
    private IDictDataService dictDataService;
    @Resource
    private ShipmentHeaderMapper shipmentHeaderMapper;
pengcheng authored
41
42
43
44
    @Autowired
    private ShipmentDetailService shipmentDetailService;
    @Autowired
    private ShipmentContainerHeaderService shipmentContainerHeaderService;
45
46
47
48
49
    @Autowired
    private ShipmentHeaderHistoryService shipmentHeaderHistoryService;
    @Autowired
    private ShipmentDetailHistoryService shipmentDetailHistoryService;
pengcheng authored
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


    //新增出库主单
    @Override
    public AjaxResult<Boolean> saveHeader(ShipmentHeader shipmentHeader) {
        if(dictDataService.checkConfig("shipmentType", shipmentHeader.getShipmentType()) == false)
        {
            return  AjaxResult.error("没有对应的出库单类型");
        }
        String code = createCode(shipmentHeader.getShipmentType());
        shipmentHeader.setId(null);
        shipmentHeader.setFirstStatus(0);
        shipmentHeader.setLastStatus(0);
        shipmentHeader.setLastUpdated(null);
        shipmentHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
        shipmentHeader.setCreated(null);
        shipmentHeader.setCreatedBy(ShiroUtils.getLoginName());
        shipmentHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
        shipmentHeader.setCode(code);
        boolean result = this.save(shipmentHeader);
        return  AjaxResult.toAjax(result);
    }


    //根据单据类型建单据号
    public String createCode(String shipmentType)
    {
        String code = null;
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        String maxCode = shipmentHeaderMapper.createCode(shipmentType);
        if (maxCode != null && maxCode.length() > 13 && maxCode.substring(maxCode.length() - 13, maxCode.length() - 5).equals(df.format(now)))
        {
            Integer Count = Integer.valueOf(maxCode.substring(maxCode.length() - 5, maxCode.length()));
            code = shipmentType + df.format(now) + String.format("%05d", Count + 1);
        }
        else
        {
            code = shipmentType + df.format(now) + "00001";
        }
        return code;
    }
pengcheng authored
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

    /**
     * 根据Id更新这个单据的首尾状态
     * @param shipmentId
     * @return
     */
    @Override
    public AjaxResult updateShipmentStatus(int shipmentId)  {
        //获取这个单
        ShipmentHeader shipmentHeader = this.getById(shipmentId);
        if(shipmentHeader==null){
            return AjaxResult.error("单据未找到,Id:"+shipmentId);
        }
        //查询是否有生成出库货箱,如果有,则返回出库货箱的最高与最低状态
        Map<String,Integer> map = shipmentContainerHeaderService.getShipmentContainerMaxAndMinStatusByShipmentID(shipmentId);
        if(map==null){
            //说明没有货箱,则直接首位均为新建
pengcheng authored
109
110
            shipmentHeader.setFirstStatus(100);
            shipmentHeader.setLastStatus(100);
pengcheng authored
111
112
113
114
            this.saveOrUpdate(shipmentHeader);
        }else {
            int firstStatus = map.get("maxStatus");
            int lastStatus = map.get("minStatus");
pengcheng authored
115
            if(firstStatus<=20){
pengcheng authored
116
117
118
119
120
                shipmentHeader.setFirstStatus(300);
            }
            if(firstStatus==30){
                shipmentHeader.setFirstStatus(500);
            }
pengcheng authored
121
            if(lastStatus <=20){
pengcheng authored
122
123
124
125
126
127
128
129
                shipmentHeader.setLastStatus(300);
            }
            if(lastStatus==30){
                shipmentHeader.setLastStatus(500);
            }
            //是否存在未配盘的数量,如果是,则尾状态为新建
            Integer UnCompleted = shipmentDetailService.countUnCompleted(shipmentId);
            if(UnCompleted != null && UnCompleted.intValue() > 0){
pengcheng authored
130
                shipmentHeader.setLastStatus(100);
pengcheng authored
131
132
133
134
135
136
137
            }
            this.saveOrUpdate(shipmentHeader);
        }

        return AjaxResult.success("");

    }
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

    //出库单字段变为历史出库单
    @Override
    @Transactional
    public AjaxResult addHistory(ShipmentHeader shipmentHeader) throws InvocationTargetException, IllegalAccessException {
        //历史出库主单
        ShipmentHeaderHistory shipmentHeaderHistory=new ShipmentHeaderHistory();
        BeanUtils.copyProperties(shipmentHeaderHistory,shipmentHeader);
        if(shipmentHeaderHistoryService.save(shipmentHeaderHistory)==false){
            throw new ServiceException("存入历史出库主单失败");
        }
        LambdaQueryWrapper<ShipmentDetail> lambdaQueryWrapper=Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ShipmentDetail::getWarehouseCode,shipmentHeader.getWarehouseCode())
                .eq(ShipmentDetail::getShipmentCode,shipmentHeader.getCode());
        List<ShipmentDetail> shipmentDetails=shipmentDetailService.list(lambdaQueryWrapper);

        //历史出库子单
        List<ShipmentDetailHistory> shipmentDetailHistories=new ArrayList<>();
        for(ShipmentDetail item:shipmentDetails){
            ShipmentDetailHistory shipmentDetailHistory=new ShipmentDetailHistory();
            BeanUtils.copyProperties(shipmentDetailHistory,item);
            shipmentDetailHistories.add(shipmentDetailHistory);
        }
        Boolean flag=shipmentDetailHistoryService.saveBatch(shipmentDetailHistories);
        if(flag==false){
            throw new ServiceException("存入历史出库子单失败");
        }
        return null;
    }
pengcheng authored
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

    /**审核出库单,加入订单池
     * 1、找到主单,确定主单的状态是否小于100
     * 2.小于100时,把主单和相应的子单加入到list中,大于等于100时,直接跳过
     * 3.将修改后的主单列表和子单列表进行修改,加入订单池
     *
     * @param ids
     * @return
     */

    @Override
    public AjaxResult review(String ids) {

        List<ShipmentHeader> shipmentHeaders = new ArrayList<>();
        List<ShipmentDetail> shipmentDetails = new ArrayList<>();

        //1、找到主单,确定主单的状态是否小于100
        for (Integer id : Convert.toIntArray(ids)){
            ShipmentHeader shipmentHeader = this.getById(id);
            if(shipmentHeader == null){
                return AjaxResult.error("id为"+id+"的主单在系统不存在");
            }

            //2.小于100时,把主单和相应的子单加入到list中,大于等于100时,直接跳过
            if(shipmentHeader.getFirstStatus() < 100 && shipmentHeader.getLastStatus() <100){
                shipmentHeader.setFirstStatus(100);
                shipmentHeader.setLastStatus(100);
                shipmentHeaders.add(shipmentHeader);

                LambdaQueryWrapper<ShipmentDetail> lam = Wrappers.lambdaQuery();
                lam.eq(ShipmentDetail::getShipmentId,id)
                        .eq(ShipmentDetail::getWarehouseCode,ShiroUtils.getWarehouseCode());
                List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(lam);
                if(shipmentDetailList != null){
                    for(ShipmentDetail shipmentDetail : shipmentDetailList){
                        shipmentDetail.setStatus(100);
                    }
                    shipmentDetails.addAll(shipmentDetailList);
                }else {
                    return AjaxResult.error("id为"+id+"的出库明细在系统不存在");
                }
            }
        }

        // 3.将修改后的主单列表和子单列表进行修改,加入订单池
        Boolean flag = this.updateBatchById(shipmentHeaders);
        if (flag == false){
            throw new ServiceException("修改主单状态失败");
        }
        flag = shipmentDetailService.updateBatchById(shipmentDetails);
        if (flag == false){
            throw new ServiceException("修改明细状态失败");
        }
        return AjaxResult.success("订单审核成功,成功加入订单池");
    }
tangying authored
222
}