/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.lightinit.hsdatagateway.modules.gateway.service;

import java.util.List;

import com.lightinit.hsdatagateway.common.persistence.Page;
import com.lightinit.hsdatagateway.common.service.CrudService;
import com.lightinit.hsdatagateway.modules.gateway.entity.Charge;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lightinit.hsdatagateway.common.persistence.Page;
import com.lightinit.hsdatagateway.common.service.CrudService;
import com.lightinit.hsdatagateway.modules.gateway.entity.Charge;
import com.lightinit.hsdatagateway.modules.gateway.dao.ChargeDao;

/**
 * 计费Service
 * @Author: yangrui
 * @version 2019-03-05
 */
@Service
@Transactional(readOnly = true)
public class ChargeService extends CrudService<ChargeDao, Charge> {

	public Charge get(Long id) {
		return super.get(id);
	}
	
	public List<Charge> findList(Charge charge) {
		return super.findList(charge);
	}
	
	public Page<Charge> findPage(Page<Charge> page, Charge charge) {
		page.setOrderBy("created_at desc");
		return super.findPage(page, charge);
	}
	
	@Transactional(readOnly = false)
	public void save(Charge charge) {
		super.save(charge);
	}
	
	@Transactional(readOnly = false)
	public void delete(Charge charge) {
		super.delete(charge);
	}

    public Charge getByAppAndRouteAndUserId(String appId, String routeId, String userId) {
		Charge charge = new Charge();
		charge.setAppId(appId);
		charge.setRouteId(routeId);
		charge.setUserId(userId);
		return dao.getByAppAndRouteAndUserId(charge);
    }

    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public boolean updateByRouteIdAndRestValue(String routeId, Long restValue){
		Charge charge = new Charge();
		charge.setRouteId(routeId);
		charge.setRestValue(restValue);
		return dao.updateByRouteIdAndRestValue(charge) > 0;
	}

	@Transactional(readOnly = false, rollbackFor = Exception.class)
    public boolean updateByRouteId(String routeId){
		return dao.updateByRouteId(routeId) > 0;
	}

	/**
	 * api调用查询
	 * @param charge
	 * @return
	 */
	public List<Charge> findListByApi(Charge charge) {
		return dao.findListByApi(charge);
	}

	/**
	 * 根据用户、路由、应用Id修改
	 * @param charge
	 * @return
	 */
	public int updateByApi(Charge charge) {
		return dao.updateByApi(charge);
	}
}