shiyunteng
6 天以前 fc8c86e7a365d5c6bdc37c2b05b9f83115ac2bc3
platformx-device-biz/src/main/java/com/by4cloud/platformx/device/service/impl/DeviceLeasingLedgerServiceImpl.java
@@ -2,12 +2,13 @@
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.by4cloud.platformx.device.entity.Device;
import com.by4cloud.platformx.device.entity.DeviceLeasingLedger;
import com.by4cloud.platformx.device.mapper.DeviceLeasingLedgerMapper;
import com.by4cloud.platformx.device.mapper.DeviceMapper;
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.DeviceLeasingLedgerService;
import lombok.AllArgsConstructor;
import net.sf.jsqlparser.expression.operators.arithmetic.Concat;
import org.springframework.stereotype.Service;
import java.util.List;
@@ -23,6 +24,11 @@
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() {
@@ -36,4 +42,115 @@
      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()+"设备数量与台账明细中不一致,无法建立台账");
               }
               Integer deviceNum = itemMapper.selectDeviceNumByContractId(contract.getId(),contractItem.getDeviceId());
               //查询是否有历史出账
               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());
            QueryWrapper<DeviceInventory> inventoryQueryWrapper = new QueryWrapper<>();
            inventoryQueryWrapper.eq("device_id",item.getDeviceId());
            inventoryQueryWrapper.eq("serial_no",serialNo);
            DeviceInventory inventory = inventoryMapper.selectOne(inventoryQueryWrapper);
            if (device!=null&&inventory!=null) {
               inventory.setInventoryStatus(CommonStatusContant.DEVICE_INVENTORY_RENTING);
               inventory.setLedgerItemId(item.getId());
               //库存调整
               inventoryMapper.updateById(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(CommonStatusContant.DEVICE_INVENTORY_FLOW_WATER_IN);
               }
               //根据合同类型 租赁 流水操作类型为出库操作
               if (contract!=null&&contract.getType()==0){
                  flowWater.setOperateType(CommonStatusContant.DEVICE_INVENTORY_FLOW_WATER_OUT);
               }
               //记录库存流水
               inventoryFlowWaterMapper.insert(flowWater);
            }
         }
      }
      //合同明细中与台账对比
      for (ContractItem contractItem:list
      ) {
         Integer deviceNum = itemMapper.selectDeviceNumByContractId(contract.getId(),contractItem.getDeviceId());
         //查询是否有历史出账
         if (contractItem.getNum()!=deviceNum) {
            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;
   }
}