package com.sv.service.api;

import com.github.pagehelper.PageHelper;
import com.sv.entity.VenueCard;
import com.sv.mapper.VenueCardMapper;
import com.ydd.framework.core.common.Pagination;
import com.ydd.framework.core.entity.enums.StatusEnum;
import com.ydd.framework.core.service.impl.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Service - 场馆会员卡
 *
 * @author lihong
 * @since 2018-08-02
 */
@Service("VenueCardServiceApi")
@Transactional(readOnly = true)
public class VenueCardService extends BaseServiceImpl {

	private final Logger logger = LoggerFactory.getLogger(VenueCardService.class);

	@Resource
	private VenueCardMapper venueCardMapper;

	/**
	 * 创建场馆会员卡
	 *
	 * @param venueCard 场馆会员卡
	 */
	@Transactional
	public void save(VenueCard venueCard) {
		if (venueCard.getId() != null && venueCard.getId() > 0) {
			// 更新
			venueCardMapper.update(venueCard);
		} else {
			// 新建
			venueCardMapper.insert(venueCard);
		}
	}

	/**
	 * 更新场馆会员卡
	 *
	 * @param venueCard 场馆会员卡
	 */
	@Transactional
	public void update(VenueCard venueCard) {
		venueCardMapper.update(venueCard);
	}

	/**
	 * 删除场馆会员卡
	 *
	 * @param id 编号
	 * @return 删除数量
	 */
	@Transactional
	public Integer delete(Integer id) {
		if (id == null || id <= 0) {
			return 0;
		}
		return venueCardMapper.delete(id);
	}

	/**
	 * 删除场馆会员卡
	 *
	 * @param ids 编号数组
	 * @return 删除数量
	 */
	@Transactional
	public Integer deleteByIds(Integer[] ids) {
		if (ids == null || ids.length == 0) {
			return 0;
		}
		return venueCardMapper.deleteByIds(ids);
	}

	/**
	 * 查询场馆会员卡
	 *
	 * @param id 编号
	 * @return 场馆会员卡
	 */
	public VenueCard findById(Integer id) {
		return venueCardMapper.findById(id);
	}

	/**
	 * 查询场馆会员卡
	 *
	 * @param pagination 分页信息
	 * @return 分页结果
	 */
	public Pagination findPage(Pagination pagination) {
		PageHelper.startPage(pagination.getPage(), pagination.getPageSize());
		PageHelper.orderBy("id desc");
		pagination.setQueryResult(venueCardMapper.findAll());
		return pagination;
	}

	/**
	 * 更新状态
	 *
	 * @param ids 编号
	 * @param status 状态
	 * @return 更新数量
	 */
	@Transactional
	public Integer updateStatus(Integer[] ids, Byte status) {
		if (ids == null) {
			return 0;
		}
		if (status == null) {
			status = StatusEnum.DISABLE.value;
		}

		return venueCardMapper.updateStatus(ids, status);
	}

	/**
	 * 根据场馆ID，会员卡类型，场馆类型查找购买会员需要的金额
	 * @param cardType
	 * @param type
	 * @param venueId
	 * @return
	 */
	VenueCard findPrice(Integer cardType,Integer type,Integer venueId){
		return venueCardMapper.findPrice(cardType,type,venueId);
	}
}

