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
133
134
135
136
137
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.by4cloud.platformx.device.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.by4cloud.platformx.common.core.util.R;
import com.by4cloud.platformx.device.entity.*;
import com.by4cloud.platformx.device.mapper.*;
import com.by4cloud.platformx.device.service.DeviceLeasingLedgerService;
import lombok.AllArgsConstructor;
import net.sf.jsqlparser.expression.operators.arithmetic.Concat;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
/**
 * 设备租赁台账
 *
 * @author syt
 * @date 2025-03-27 09:30:29
 */
@Service
@AllArgsConstructor
public class DeviceLeasingLedgerServiceImpl extends ServiceImpl<DeviceLeasingLedgerMapper, DeviceLeasingLedger> implements DeviceLeasingLedgerService {
 
    private final DeviceMapper deviceMapper;
    private final DeviceLeasingLedgerItemMapper itemMapper;
    private final ContractItemMapper contractItemMapper;
    private final ContractMapper contractMapper;
    private final DeviceInventoryMapper inventoryMapper;
    private final InventoryFlowWaterMapper inventoryFlowWaterMapper;
 
    @Override
    public List<DeviceLeasingLedger> getSelectList() {
        QueryWrapper<DeviceLeasingLedger> queryWrapper = new QueryWrapper<>();
        List<DeviceLeasingLedger> list = baseMapper.selectList(queryWrapper);
        return list;
    }
 
    @Override
    public List<Device> getDeviceSelectList(Long ledgerId) {
        List<Device> list = deviceMapper.getDeviceSelectListByLedgerId(ledgerId);
        return list;
    }
 
    @Override
    public R saveDeep(DeviceLeasingLedger deviceLeasingLedger) {
        //明细设备数量与合同中签订数据比对
        QueryWrapper<Contract> contractQueryWrapper = new QueryWrapper<>();
        contractQueryWrapper.eq("number",deviceLeasingLedger.getContractCode());
        Contract contract = contractMapper.selectOne(contractQueryWrapper);
        if (contract == null) {
            return R.failed("合同不存在");
        }
 
        //查询合同明细中设备情况
        QueryWrapper<ContractItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("contract_id",contract.getId());
        List<ContractItem> list = contractItemMapper.selectList(queryWrapper);
 
        //比较设备种类是否一致
        if (list.size()!=deviceLeasingLedger.getLedgerItemList().size()){
            return R.failed("合同中设备种类与台账明细中不一致");
        }
 
        //比较单个设备台账数量是否与合同中一致
        for (ContractItem contractItem:list
             ) {
            for (DeviceLeasingLedgerItem item:deviceLeasingLedger.getLedgerItemList()
            ) {
                if (contractItem.getDeviceId().equals(item.getDeviceId())){
                    if (contractItem.getNum()<item.getNum()) {
                        return R.failed("合同中"+item.getDeviceName()+"设备数量与台账明细中不一致,无法建立台账");
                    }
                    //查询是否有历史出账
                    QueryWrapper<DeviceLeasingLedgerItem> queryWrapperLedgerItem = new QueryWrapper<>();
                    queryWrapperLedgerItem.eq("contract_id",contract.getId());
                    queryWrapperLedgerItem.eq("device_id",contractItem.getDeviceId());
                    List<DeviceLeasingLedgerItem> noteItemList = itemMapper.selectList(queryWrapperLedgerItem);
                    if (noteItemList != null&&noteItemList.size()>0) {
                        Integer deviceNum = noteItemList.stream().mapToInt(DeviceLeasingLedgerItem::getNum).sum();
                        if (contractItem.getNum()<(item.getNum()+deviceNum)) {
                            return R.failed("合同中"+item.getDeviceName()+"设备数量出租台账已超出设备库存,无法建立台账");
                        }
                    }
                }
            }
        }
 
        //入库
        baseMapper.insert(deviceLeasingLedger);
        for (DeviceLeasingLedgerItem item:deviceLeasingLedger.getLedgerItemList()
             ) {
            item.setLedgerId(deviceLeasingLedger.getId());
            itemMapper.insert(item);
            for (String serialNo:item.getSerialNos()
            ) {
                Device device = deviceMapper.selectById(item.getDeviceId());
                if (device!=null) {
                    DeviceInventory inventory = new DeviceInventory();
                    inventory.setDeviceId(item.getDeviceId());
                    inventory.setDeviceNumber(item.getDeviceCode());
                    inventory.setName(device.getName());
                    inventory.setSerialNo(serialNo);
                    //根据合同类型 购买 承租 合同类型为入库操作
                    if (contract!=null&&(contract.getType()==1||contract.getType()==2)){
                        inventory.setSource(1);
                        //入库操作 库存状态为 可用
                        inventory.setInventoryStatus(1);
                    }
                    //根据合同类型 租赁 合同类型为出库操作
                    if (contract!=null&&contract.getType()==0){
                        inventory.setSource(0);
                        //出库操作 库存状态为  租赁中
                        inventory.setInventoryStatus(2);
                    }
                    //库存调整
                    inventoryMapper.insert(inventory);
 
                    //设备库存流水同步新增
                    InventoryFlowWater flowWater = new InventoryFlowWater();
                    flowWater.setDeviceId(item.getDeviceId());
                    flowWater.setSerialNo(serialNo);
                    flowWater.setInventoryId(inventory.getId());
                    flowWater.setClassId(device.getClassId());
                    //根据合同类型 购买 承租 流水操作类型为入库操作
                    if (contract!=null&&(contract.getType()==1||contract.getType()==2)){
                        flowWater.setOperateType(2);
                    }
                    //根据合同类型 租赁 流水操作类型为出库操作
                    if (contract!=null&&contract.getType()==0){
                        flowWater.setOperateType(1);
                    }
                    //记录库存流水
                    inventoryFlowWaterMapper.insert(flowWater);
                }
            }
        }
        //合同明细中与台账对比
        for (ContractItem contractItem:list
        ) {
            //查询实际台账情况
            QueryWrapper<DeviceLeasingLedgerItem> queryWrapperLedgerItem = new QueryWrapper<>();
            queryWrapperLedgerItem.eq("contract_id",contract.getId());
            queryWrapperLedgerItem.eq("device_id",contractItem.getDeviceId());
            List<DeviceLeasingLedgerItem> ledgerItemList = itemMapper.selectList(queryWrapperLedgerItem);
            if (ledgerItemList != null&&ledgerItemList.size()>0) {
                Integer deviceNum = ledgerItemList.stream().mapToInt(DeviceLeasingLedgerItem::getNum).sum();
                if (contractItem.getNum()!=deviceNum) {
                    return R.ok();
                }
            }else {
                return R.ok();
            }
        }
        //更新合同状态为完成
        contract.setStatus(2);
        contractMapper.updateById(contract);
 
        return R.ok();
    }
 
    @Override
    public List<DeviceInventory> getLedgerDeviceList(Long deviceId) {
        QueryWrapper<DeviceInventory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_id",deviceId);
        queryWrapper.eq("inventory_status",1);
        List<DeviceInventory> list = inventoryMapper.selectList(queryWrapper);
        return list;
    }
 
    @Override
    public DeviceLeasingLedger getByIdDeep(Long id) {
        DeviceLeasingLedger deviceLeasingLedger = baseMapper.selectById(id);
        QueryWrapper<DeviceLeasingLedgerItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ledger_id",id);
        List<DeviceLeasingLedgerItem> itemList = itemMapper.selectList(queryWrapper);
        deviceLeasingLedger.setLedgerItemList(itemList);
        return deviceLeasingLedger;
    }
}