shiyunteng
5 天以前 1eb6f4824c270de1a3d0bf766d44f1abb2c7f55d
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
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.constant.CommonStatusContant;
import com.by4cloud.platformx.device.entity.*;
import com.by4cloud.platformx.device.mapper.*;
import com.by4cloud.platformx.device.service.ContractService;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;
 
import java.io.Serializable;
import java.util.List;
import java.util.Objects;
/**
 * 合同表
 *
 * @author pig
 * @date 2025-03-13 10:20:35
 */
@Service
@RequiredArgsConstructor
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements ContractService {
  private final ContractItemMapper contractItemMapper;
  private final DeviceLeasingLedgerMapper leasingLedgerMapper;
  private final DeviceLeasingLedgerItemMapper ledgerItemMapper;
  private final DeviceInventoryMapper inventoryMapper;
  private final InventoryFlowWaterMapper flowWaterMapper;
  private final DeviceMapper deviceMapper;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveDeep(Contract contract) {
        contract.setStatus(CommonStatusContant.CONTRACT_STATUS_PENDING_APPROVAL);
        baseMapper.insert(contract);
        for (ContractItem contractItem : contract.getContractItemList()) {
            contractItem.setContractId(contract.getId());
            contractItemMapper.insert( contractItem);
        }
 
        return Boolean.TRUE;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateDeep(Contract contract) {
        baseMapper.updateById(contract);
        if (contract.getContractItemList() != null&&contract.getContractItemList().size()>0) {
            QueryWrapper<ContractItem> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("contract_id",contract.getId());
            contractItemMapper.delete(queryWrapper);
            for (ContractItem  contractItem : contract.getContractItemList()) {
                contractItem.setId(null);
                contractItem.setContractId(contract.getId());
                contractItemMapper.insert( contractItem);
            }
        }
 
        return Boolean.TRUE;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeDeep(Long[] ids) {
        baseMapper.deleteBatchIds(CollUtil.toList(ids));
        contractItemMapper.delete(Wrappers.<ContractItem>lambdaQuery().in(ContractItem::getContractId, ids));
        return Boolean.TRUE;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeChild(Long[] ids) {
        contractItemMapper.deleteBatchIds(CollUtil.toList(ids));
        return Boolean.TRUE;
    }
 
    @Override
    public Contract getDetailById(Long id) {
        Contract contract = baseMapper.selectById(id);
        QueryWrapper<ContractItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("contract_id",id);
        List<ContractItem> itemList = contractItemMapper.selectList(queryWrapper);
        contract.setContractItemList(itemList);
        return contract;
    }
 
    @Override
    public Boolean approved(Long id) {
        Contract contract = baseMapper.selectById(id);
        contract.setStatus(CommonStatusContant.CONTRACT_STATUS_APPROVED);
        baseMapper.updateById(contract);
        return Boolean.TRUE;
    }
 
    @Override
    public List<Contract> getApprovedContractList() {
        QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",CommonStatusContant.CONTRACT_STATUS_APPROVED);
        List<Contract> list = baseMapper.selectList(queryWrapper);
        return list;
    }
 
    @Override
    public R approvelInvalid(Long id) {
        Contract contract = baseMapper.selectById(id);
        contract.setStatus(CommonStatusContant.CONTRACT_STATUS_INVALID_PENDING_APPROVAL);
        baseMapper.updateById(contract);
        return R.ok();
    }
 
    @Override
    public R invalidById(Long id) {
        Contract contract = baseMapper.selectById(id);
        contract.setStatus(CommonStatusContant.CONTRACT_STATUS_INVALID);
        baseMapper.updateById(contract);
        //根据合同查找相关租赁台账
        QueryWrapper<DeviceLeasingLedger> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("contract_id",id);
        DeviceLeasingLedger ledger = leasingLedgerMapper.selectOne(queryWrapper);
        if (ledger!=null){
            //查找相关台账明细
            QueryWrapper<DeviceLeasingLedgerItem> ledgerItemQueryWrapper = new QueryWrapper<>();
            ledgerItemQueryWrapper.eq("ledger_id",ledger.getId());
            List<DeviceLeasingLedgerItem> ledgerItemList = ledgerItemMapper.selectList(ledgerItemQueryWrapper);
            if(ledgerItemList.size()>0){
                //根据台账明细查询设备库存
                ledgerItemList.stream().forEach(deviceLeasingLedgerItem -> {
                    QueryWrapper<DeviceInventory> inventoryQueryWrapper = new QueryWrapper<>();
                    inventoryQueryWrapper.eq("ledger_item_id",deviceLeasingLedgerItem.getId());
                    List<DeviceInventory> inventoryList = inventoryMapper.selectList(inventoryQueryWrapper);
                    //将设备库存状态更为可用
                    if (inventoryList.size()>0){
                        inventoryList.stream().forEach(deviceInventory -> {
                            deviceInventory.setInventoryStatus(CommonStatusContant.DEVICE_INVENTORY_USABLE);
                            inventoryMapper.updateById(deviceInventory);
                            //库存流水
                            InventoryFlowWater flowWater = new InventoryFlowWater();
                            flowWater.setDeviceId(deviceInventory.getDeviceId());
                            Device device = deviceMapper.selectById(deviceInventory.getDeviceId());
                            if (device!=null){
                                flowWater.setClassId(device.getClassId());
                            }
                            flowWater.setInventoryId(deviceInventory.getId());
                            flowWater.setSerialNo(deviceInventory.getSerialNo());
                            flowWater.setOperateType(CommonStatusContant.DEVICE_INVENTORY_FLOW_WATER_IN);
                            flowWaterMapper.insert(flowWater);
                        });
                    }
                });
            }
        }
        return R.ok();
    }
}