package com.bonus.canteen.core.drp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.canteen.core.drp.constant.DrpOrderTypeEnum;
import com.bonus.canteen.core.drp.mapper.DrpDetailFlowMapper;
import com.bonus.canteen.core.drp.mapper.DrpInventoryMapper;
import com.bonus.canteen.core.drp.model.DrpDetailFlow;
import com.bonus.canteen.core.drp.model.DrpIntoDetail;
import com.bonus.canteen.core.drp.model.DrpIntoInventory;
import com.bonus.canteen.core.drp.model.DrpOutInventory;
import com.bonus.canteen.core.drp.po.DrpOutDetailModel;
import com.bonus.canteen.core.drp.service.DrpDetailFlowService;
import com.bonus.canteen.core.drp.model.*;
import com.bonus.common.houqin.utils.id.Id;
import com.github.pagehelper.page.PageMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.lang.invoke.SerializedLambda;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class DrpDetailFlowServiceImpl extends ServiceImpl<DrpDetailFlowMapper, DrpDetailFlow> implements DrpDetailFlowService {
   @Autowired
   @Lazy
   private DrpInventoryMapper drpInventoryMapper;
//   @Autowired
//   @Lazy
//   private DrpAuthorityApi drpAuthorityApi;

   public void saveDetailFlow(DrpIntoInventory drpIntoInventory, DrpIntoDetail drpIntoDetail, DrpOutInventory drpOutInventory,
                              DrpOutDetailModel drpOutDetail, DrpDetailFlow overdraftModel, Integer drpOrderType) {
      DrpDetailFlow drpDetailFlow;
      DrpInventory inventoryNumModel;
      if (ObjectUtil.equal(drpOrderType, DrpOrderTypeEnum.INTO.key())) {
         drpDetailFlow = new DrpDetailFlow();
         BeanUtil.copyProperties(drpIntoDetail, drpDetailFlow, new String[0]);
         drpDetailFlow.setDrpFlowId(Id.next());
         drpDetailFlow.setDrpDetailId(drpIntoDetail.getDetailId());
         drpDetailFlow.setDrpOrderId(drpIntoInventory.getIntoId());
         drpDetailFlow.setNum(drpIntoDetail.getPurNum());
         drpDetailFlow.setDrpOrderType(drpOrderType);
         drpDetailFlow.setWarehouseId(drpIntoInventory.getWarehouseId());
         drpDetailFlow.setDrpOrderTime(drpIntoInventory.getIntoDate());
         drpDetailFlow.setRecordTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS").format(LocalDateTime.now()));
         drpDetailFlow.setInventoryId(drpIntoDetail.getInventoryId());
         inventoryNumModel = (DrpInventory)this.drpInventoryMapper.selectOne(Wrappers.
                 lambdaQuery(DrpInventory.class).eq(DrpInventory::getInventoryId,
                         drpDetailFlow.getInventoryId()).select(DrpInventory::getMaterialNum));
         drpDetailFlow.setInventoryNum(inventoryNumModel.getMaterialNum());
         drpDetailFlow.setSource(1);
         ((DrpDetailFlowMapper)this.baseMapper).insert(drpDetailFlow);
      } else if (ObjectUtil.equal(drpOrderType, DrpOrderTypeEnum.OUT.key())) {
         drpDetailFlow = new DrpDetailFlow();
         BeanUtil.copyProperties(drpOutDetail, drpDetailFlow, new String[0]);
         drpDetailFlow.setDrpFlowId(Id.next());
         drpDetailFlow.setDrpDetailId(drpOutDetail.getDetailId());
         drpDetailFlow.setInventoryId(drpOutDetail.getInventoryId());
         drpDetailFlow.setDrpOrderId(drpOutInventory.getOutId());
         drpDetailFlow.setNum(drpOutDetail.getFetchNum());
         drpDetailFlow.setDrpOrderType(DrpOrderTypeEnum.OUT.key());
         drpDetailFlow.setWarehouseId(drpOutInventory.getWarehouseId());
         drpDetailFlow.setDrpOrderTime(drpOutInventory.getOutDate());
         drpDetailFlow.setRecordTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS").format(LocalDateTime.now()));
         inventoryNumModel = this.drpInventoryMapper.selectOne(Wrappers
                 .lambdaQuery(DrpInventory.class).eq(DrpInventory::getInventoryId, drpDetailFlow.getInventoryId())
                 .select(DrpInventory::getMaterialNum));
         drpDetailFlow.setInventoryNum(inventoryNumModel.getMaterialNum());
         drpDetailFlow.setSource(1);
         ((DrpDetailFlowMapper)this.baseMapper).insert(drpDetailFlow);
      } else {
         inventoryNumModel = this.drpInventoryMapper.selectOne(Wrappers
                 .lambdaQuery(DrpInventory.class).eq(DrpInventory::getInventoryId, overdraftModel.getInventoryId())
                 .select(DrpInventory::getMaterialNum));
         overdraftModel.setInventoryNum(inventoryNumModel.getMaterialNum());
         overdraftModel.setSource(1);
         ((DrpDetailFlowMapper)this.baseMapper).insert(overdraftModel);
      }

   }

//   public List<DrpOverdraftRecordsPageVO> pageOverdraftRecords(OverdraftRecordsPageDTO content) {
//      List<Long> warehouseAuthority = this.drpAuthorityApi.listWarehouseAuthority();
//      List<Long> areaAuth = this.drpAuthorityApi.listAreaAuth();
//      if (CharSequenceUtil.isNotEmpty(content.getMaterialName())) {
//         content.setMaterialName(LeBeanUtil.fieldLikeHandle(content.getMaterialName()));
//      }
//
//      PageMethod.startPage(content);
//      return ((DrpDetailFlowMapper)this.baseMapper).pageOverdraftRecords(content, warehouseAuthority, DrpOrderTypeEnum.OVERDRAFT.key(), areaAuth);
//   }

}
