/*
 *      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.business.service.impl;

import lombok.AllArgsConstructor;
import org.springblade.common.enums.StatusEnum;
import org.springblade.modules.business.entity.Facility;
import org.springblade.modules.business.entity.FacilityCargoWay;
import org.springblade.modules.business.mapper.FacilityCargoWayMapper;
import org.springblade.modules.business.service.IFacilityCargoWayService;
import org.springblade.modules.business.vo.FacilityVO;
import org.springblade.modules.business.mapper.FacilityMapper;
import org.springblade.modules.business.service.IFacilityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.mall.entity.GoodsInfo;
import org.springblade.modules.mall.vo.GoodsInfoVO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 售货设备表 服务实现类
 *
 * @author BladeX
 * @since 2024-07-16
 */
@Service
public class FacilityServiceImpl extends ServiceImpl<FacilityMapper, Facility> implements IFacilityService {

	@Resource
	private IFacilityCargoWayService iFacilityCargoWayService;

	@Override
	public IPage<FacilityVO> selectFacilityPage(IPage<FacilityVO> page, FacilityVO facility) {
		return page.setRecords(baseMapper.selectFacilityPage(page, facility));
	}

	@Override
	public List<GoodsInfoVO> getGoods(Long deviceId) {
		return baseMapper.selectGoods(deviceId, null);
	}

	@Transactional
	@Override
	public Boolean add(Facility facility) {
		this.save(facility);
		List<FacilityCargoWay> list = new ArrayList<>();

		for (int i = 1; i <= this.getCargoQuantity(); i++) {
			FacilityCargoWay facilityCargoWay = new FacilityCargoWay();
			facilityCargoWay.setFacilityId(facility.getId());
			facilityCargoWay.setCargoNo(i + "");
			facilityCargoWay.setVolume(this.getCargoCapacity());
			facilityCargoWay.setCargoStatus(StatusEnum.ENABLE.getValue());
			facilityCargoWay.setEnable(StatusEnum.ENABLE.getValue());
			facilityCargoWay.setDiscount(0.0);
			list.add(facilityCargoWay);
		}

		// 生成货道信息
		return iFacilityCargoWayService.saveBatch(list);
	}

	/**
	 * 获取货道数量
	 */
	private Integer getCargoQuantity() {
		return 8;
	}

	/**
	 * 获取货道容量
	 */
	private Integer getCargoCapacity() {
		return 8;
	}

}
