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 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 getSelectList() { QueryWrapper queryWrapper = new QueryWrapper<>(); List list = baseMapper.selectList(queryWrapper); return list; } @Override public List getDeviceSelectList(Long ledgerId) { List list = deviceMapper.getDeviceSelectListByLedgerId(ledgerId); return list; } @Override public R saveDeep(DeviceLeasingLedger deviceLeasingLedger) { //明细设备数量与合同中签订数据比对 QueryWrapper contractQueryWrapper = new QueryWrapper<>(); contractQueryWrapper.eq("number",deviceLeasingLedger.getContractCode()); Contract contract = contractMapper.selectOne(contractQueryWrapper); if (contract == null) { return R.failed("合同不存在"); } //查询合同明细中设备情况 QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("contract_id",contract.getId()); List 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() queryWrapperLedgerItem = new QueryWrapper<>(); queryWrapperLedgerItem.eq("contract_id",contract.getId()); queryWrapperLedgerItem.eq("device_id",contractItem.getDeviceId()); List noteItemList = itemMapper.selectList(queryWrapperLedgerItem); if (noteItemList != null&¬eItemList.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); } inventory.setLedgerItemId(item.getId()); //库存调整 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 queryWrapperLedgerItem = new QueryWrapper<>(); queryWrapperLedgerItem.eq("contract_id",contract.getId()); queryWrapperLedgerItem.eq("device_id",contractItem.getDeviceId()); List 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 getLedgerDeviceList(Long deviceId) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("device_id",deviceId); queryWrapper.eq("inventory_status",1); List list = inventoryMapper.selectList(queryWrapper); return list; } @Override public DeviceLeasingLedger getByIdDeep(Long id) { DeviceLeasingLedger deviceLeasingLedger = baseMapper.selectById(id); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("ledger_id",id); List itemList = itemMapper.selectList(queryWrapper); deviceLeasingLedger.setLedgerItemList(itemList); return deviceLeasingLedger; } }