package com.ssj.service.weixin.sales.service.impl;

import com.ssj.bean.weixin.sales.domain.LibBorrowedService;
import com.ssj.dao.weixin.sales.dao.LibBorrowedServiceDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.persistence.DynamicSpecifications;
import com.ssj.framework.core.persistence.SearchFilter;
import com.ssj.framework.idworker.IdWorker;
import com.ssj.service.weixin.sales.service.LibBorrowedServiceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;


/** 
 * @author  ZhangCaibao 
 * @version 2017年6月22日 下午2:55:40 
 * 类说明 
 */
@Service
@Transactional
public class LibBorrowedServiceServiceImpl extends BaseServiceImpl<LibBorrowedService, String> implements  LibBorrowedServiceService{
	 @Autowired
	private LibBorrowedServiceDao  libBorrowedServiceDao;
	@Override
	public PagingAndSortingRepository<LibBorrowedService, String> getDao() {
		return libBorrowedServiceDao;
	}
	
	@Override
	public Page<LibBorrowedService> findByPage(Map<String, Object> params,
			Pageable sp) {
		  Map<String, SearchFilter> filters = SearchFilter.parse(params);
				Specification<LibBorrowedService> specification = DynamicSpecifications.bySearchFilter(filters.values(), LibBorrowedService.class);    
	       return  libBorrowedServiceDao.findAll(specification, sp);
	}

	@Override
	public void insertBorrowedService(String libId,Integer libType) {
		Integer count=libBorrowedServiceDao.getLibCount(libId);
		if(count==0){
			String addType = libType==1?"SSJ":"SSJGx";
			List<LibBorrowedService> list = libBorrowedServiceDao.findListByAddType(addType);
			if(Objects.nonNull(list) && list.size()>0){
				for (LibBorrowedService libBorrowedService:list){
					LibBorrowedService vo = new LibBorrowedService();
					BeanUtils.copyProperties(libBorrowedService,vo);
					vo.setId(IdWorker.generateId());
					vo.setLibId(libId);
					this.save(vo);
				}
			}
		}
	}

	@Override
	public LibBorrowedService findByLibIdAndSpeciesAndType(String libId,
			String species, String type) {
		return libBorrowedServiceDao.findByLibIdAndSpeciesAndType(libId, species, type);
	}

	

}
