package com.jpsoft.shinestar.modules.base.service.impl;

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

import com.jpsoft.shinestar.modules.base.dao.CanteenOutsourcerBalanceDAO;
import com.jpsoft.shinestar.modules.base.dao.CanteenOutsourcerRechargeRecordDAO;
import com.jpsoft.shinestar.modules.base.entity.CanteenOutsourcerBalance;
import com.jpsoft.shinestar.modules.base.entity.CanteenOutsourcerRechargeRecord;
import com.jpsoft.shinestar.modules.base.service.CanteenOutsourcerRechargeRecordService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.github.pagehelper.PageHelper;

@Transactional
@Component(value="canteenOutsourcerRechargeRecordService")
public class CanteenOutsourcerRechargeRecordServiceImpl implements CanteenOutsourcerRechargeRecordService {
	@Resource(name="canteenOutsourcerRechargeRecordDAO")
	private CanteenOutsourcerRechargeRecordDAO canteenOutsourcerRechargeRecordDAO;
	@Resource(name="canteenOutsourcerBalanceDAO")
	private CanteenOutsourcerBalanceDAO canteenOutsourcerBalanceDAO;

	@Override
	public CanteenOutsourcerRechargeRecord get(String id) {
		// TODO Auto-generated method stub
		return canteenOutsourcerRechargeRecordDAO.get(id);
	}

	@Override
	public int insert(CanteenOutsourcerRechargeRecord model) {
		// TODO Auto-generated method stub
		//model.setId(UUID.randomUUID().toString());
		
		return canteenOutsourcerRechargeRecordDAO.insert(model);
	}

	@Override
	public int insertAndBalance(CanteenOutsourcerRechargeRecord model) {
		int affectCount = 0;

		int count = canteenOutsourcerRechargeRecordDAO.insert(model);

		if(count>0){
			CanteenOutsourcerBalance canteenOutsourcerBalance = canteenOutsourcerBalanceDAO.findByPersonId(model.getPersonId());
			if(canteenOutsourcerBalance == null){
				canteenOutsourcerBalance = new CanteenOutsourcerBalance();
				canteenOutsourcerBalance.setId(UUID.randomUUID().toString());
				canteenOutsourcerBalance.setPersonId(model.getPersonId());
				canteenOutsourcerBalance.setDelFlag(false);
				canteenOutsourcerBalance.setCreateBy(model.getCreateBy());
				canteenOutsourcerBalance.setCreateTime(model.getCreateTime());

				canteenOutsourcerBalance.setAmount(model.getRechargeAmount());
				canteenOutsourcerBalance.setLastRechargeTime(model.getCreateTime());

				affectCount = canteenOutsourcerBalanceDAO.insert(canteenOutsourcerBalance);
			}
			else{
				BigDecimal curAmount = canteenOutsourcerBalance.getAmount();

				BigDecimal newAmount = curAmount.add(model.getRechargeAmount());

				canteenOutsourcerBalance.setAmount(newAmount);
				canteenOutsourcerBalance.setLastRechargeTime(model.getCreateTime());
				canteenOutsourcerBalance.setUpdateBy(model.getCreateBy());
				canteenOutsourcerBalance.setUpdateTime(model.getCreateTime());
				affectCount = canteenOutsourcerBalanceDAO.update(canteenOutsourcerBalance);
			}
		}

		return affectCount;
	}

	@Override
	public int update(CanteenOutsourcerRechargeRecord model) {
		// TODO Auto-generated method stub
		return canteenOutsourcerRechargeRecordDAO.update(model);		
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return canteenOutsourcerRechargeRecordDAO.delete(id);
	}

	@Override
	public boolean exist(String id) {
		// TODO Auto-generated method stub
		int count = canteenOutsourcerRechargeRecordDAO.exist(id);
		
		return count > 0 ? true : false;
	}
	
	@Override
	public List<CanteenOutsourcerRechargeRecord> list() {
		// TODO Auto-generated method stub
		return canteenOutsourcerRechargeRecordDAO.list();
	}

	@Override
	public int findCountByPersonId(Long personId){
		return canteenOutsourcerRechargeRecordDAO.findCountByPersonId(personId);
	}
		
	@Override
	public Page<CanteenOutsourcerRechargeRecord> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize,boolean count,List<Sort> sortList) {
        Page<CanteenOutsourcerRechargeRecord> page = PageHelper.startPage(pageNumber,pageSize,count).doSelectPage(()->{
            canteenOutsourcerRechargeRecordDAO.search(searchParams,sortList);
        });
        
        return page;
	}
}