package com.bs.jxc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bs.common.core.domain.AjaxResult;
import com.bs.jxc.domain.*;
import com.bs.jxc.mapper.BmVesselCargoMapper;
import com.bs.jxc.service.*;
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 船舶载货清单Service业务层处理
 *
 * @author bs
 * @date 2023-07-10
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BmVesselCargoServiceImpl  extends MPJBaseServiceImpl<BmVesselCargoMapper, BmVesselCargo> implements IBmVesselCargoService {

    @Resource
    private ICkCargoStockService ckCargoStockService;
    @Resource
    private ICkStockChangeService ckStockChangeService;
    @Resource
    private ICmCargoService cmCargoService;
    @Resource
    private ICmStockService cmStockService;
    @Resource
    private ICkBillStockService ckBillStockService;
    @Resource
    private ICkBillCargoService ckBillCargoService;
    @Resource
    private ICkBillService ckBillService;

    /**
     * 保存单据，单据明细
     * @param bmVesselCargo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult outPutWarehouse(BmVesselCargo bmVesselCargo){
        //创建单据
        CkBill ckBill = new CkBill();
        ckBill.setBillType(bmVesselCargo.getOpType());
        ckBill.setBillDate(new Date());
        ckBill.setBillNumber(bmVesselCargo.getBillNumber());
        ckBill.setBillNo(bmVesselCargo.getBillNo());
        ckBill.setVesselId(bmVesselCargo.getVesselId());
        ckBill.setVesselName(bmVesselCargo.getVesselName());
        ckBill.setVoyageId(bmVesselCargo.getVoyageId());
        ckBill.setVoyageNo(bmVesselCargo.getVoyageNo());
        ckBill.setStatus("1");
        ckBillService.save(ckBill);
        CmCargo cmCargo = cmCargoService.getById(bmVesselCargo.getCargoId());
        List<CkBillStock> billStockList = bmVesselCargo.getBillStockList();
        billStockList.forEach(ele->{
            //创建单据明细
            CkBillCargo ckBillCargo = new CkBillCargo();
            ckBillCargo.setBillId(ckBill.getId());
            ckBillCargo.setBillDate(ckBill.getBillDate());
            ckBillCargo.setBillNumber(ckBill.getBillNumber());
            ckBillCargo.setBillNo(ckBill.getBillNo());
            ckBillCargo.setInoutType(ckBill.getBillType());
            ckBillCargo.setVoyageId(ckBill.getVoyageId());
            ckBillCargo.setVoyageNo(ckBill.getVoyageNo());
            ckBillCargo.setVesselId(ckBill.getVesselId());
            ckBillCargo.setVesselName(ckBill.getVesselName());
            ckBillCargo.setCargoId(bmVesselCargo.getCargoId());
            ckBillCargo.setHsCode(bmVesselCargo.getHsCode());
            ckBillCargo.setCargoName(cmCargo.getCargoName());
            ckBillCargo.setCargoSpec(cmCargo.getCargoSpec());
            ckBillCargo.setOriginCountry(cmCargo.getOriginCountry());
            ckBillCargo.setCustId(bmVesselCargo.getCustId());
            ckBillCargo.setCustName(bmVesselCargo.getCustName());
            ckBillCargo.setOpType(bmVesselCargo.getOpType());
            ckBillCargo.setTradType(bmVesselCargo.getTradType());
            ckBillCargo.setCargoNum(ele.getCargoNum());
            ckBillCargo.setCargoWt(ele.getCargoWt());
            ckBillCargo.setCargoVol(ele.getCargoVol());
            ckBillCargo.setCargoValue(ele.getCargoValue());
            ckBillCargo.setExpiryDate(bmVesselCargo.getExpiryDate());
            ckBillCargo.setCargoNumber(bmVesselCargo.getCargoNumber());
            ckBillCargo.setItemNumber(bmVesselCargo.getItemNumber());
            ckBillCargo.setBookNumber(bmVesselCargo.getBookNumber());
            ckBillCargo.setCustomsNo(bmVesselCargo.getCustomsNo());
            ckBillCargo.setListNumber(bmVesselCargo.getListNumber());
            ckBillCargoService.save(ckBillCargo);
            ele.setBillCargoId(ckBillCargo.getId());
            ele.setInoutType(ckBillCargo.getInoutType());
        });
        //明细单，出入库仓库明细
        ckBillStockService.saveBatch(billStockList);
        //根据选中的库存，判断是入库操作，还是出库操作，直接进行加减
        return ckBillService.warehouseWork(ckBill,false);
    }



}
