/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.guosen.bussinessPurchaseRecord.service.impl;

import org.springblade.modules.guosen.bussiness.entity.Bussiness;
import org.springblade.modules.guosen.bussinessGoods.dto.BussinessGoodsDTO;
import org.springblade.modules.guosen.bussinessGoods.entity.BussinessGoods;
import org.springblade.modules.guosen.bussinessPurchaseRecord.entity.BussinessPurchaseRecord;
import org.springblade.modules.guosen.bussinessPurchaseRecord.vo.BussinessPurchaseRecordVO;
import org.springblade.modules.guosen.bussinessPurchaseRecord.mapper.BussinessPurchaseRecordMapper;
import org.springblade.modules.guosen.bussinessPurchaseRecord.service.IBussinessPurchaseRecordService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.ArrayList;
import java.util.List;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2021-09-24
 */
@Service
public class BussinessPurchaseRecordServiceImpl extends BaseServiceImpl<BussinessPurchaseRecordMapper, BussinessPurchaseRecord> implements IBussinessPurchaseRecordService {

	@Override
	public IPage<BussinessPurchaseRecordVO> selectBussinessPurchaseRecordPage(IPage<BussinessPurchaseRecordVO> page, BussinessPurchaseRecordVO bussinessPurchaseRecord) {
		return page.setRecords(baseMapper.selectBussinessPurchaseRecordPage(page, bussinessPurchaseRecord));
	}

	@Override
	public boolean purchase(List<BussinessGoodsDTO> goodsList, Bussiness bussiness, String orderNo, Long billRecordId) {
		if(goodsList == null && goodsList.size() <= 0){
			return false;
		}
		List<BussinessPurchaseRecord> list = new ArrayList<>();
		for(BussinessGoodsDTO goods : goodsList){
			BussinessPurchaseRecord record = new BussinessPurchaseRecord();
			record.setBussinessId(bussiness.getId());
			record.setBussinessName(bussiness.getName());
			record.setGoodsId(goods.getId());
			record.setGoodsNo(goods.getGoodsNo());
			record.setGoodsName(goods.getGoodsName());
			record.setGoodsPrice(goods.getGoodsPrice());
			record.setPurchaseAmount(goods.getAmount());
			record.setPurchaseNum(goods.getPurchaseNum());
			record.setOrderNo(orderNo);
			record.setBillRecordId(billRecordId);
			list.add(record);
		}
		if(list.size() > 0){
			boolean saveBatch = this.saveBatch(list);
			return saveBatch;
		}else{
			return false;
		}
	}

}
