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

import java.util.Date;
import java.util.List;
import java.util.Map;

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.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.weixin.sales.domain.LibStaffing;
import com.ssj.dao.weixin.sales.dao.LibStaffingDao;
import com.ssj.dao.weixin.sales.dao.LibStaffingQueryDao;
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.service.weixin.sales.service.LibStaffingService;


/** 
 * @author  ZhangCaibao 
 * @version 2017年4月24日 上午11:12:20 
 * 类说明 
 */
@Service(version="1.0")
@Transactional
public class LibStaffingServiceImpl extends BaseServiceImpl<LibStaffing, String> implements LibStaffingService{
    @Autowired
	private LibStaffingDao  libStaffingDao;
    @Autowired
    private LibStaffingQueryDao  libStaffingQueryDao;
	@Override
	public PagingAndSortingRepository<LibStaffing, String> getDao() {
		return libStaffingDao;
	}
	@Override
	public Page<LibStaffing> findByPage(Map<String, Object> params, Pageable sp) {
		   Map<String, SearchFilter> filters = SearchFilter.parse(params);
					Specification<LibStaffing> specification = DynamicSpecifications.bySearchFilter(filters.values(), LibStaffing.class);    
		 return  libStaffingDao.findAll(specification, sp);
	}
	@Override
	public Page<Map<String, Object>> getUserList(
			Map<String, Object> searchParams, Pageable pageable) {
		return libStaffingQueryDao.getUserList(searchParams, pageable);
	}
	@Override
	public Page<Map<String, Object>> getUserListZB(
			Map<String, Object> searchParams, Pageable pageable) {
		return libStaffingQueryDao.getUserListZB(searchParams, pageable);
	}
	@Override
	public void libStaffingSave(String orgCode,String libId,String[] ids,String type){
		if(ids.length>0){
			for (int i = 0; i < ids.length; i++) {
				LibStaffing tb=libStaffingDao.getLibStaffingByLibIdAndUserIdAndType(libId,ids[i],type);
				if(tb ==null){
			    	tb = new  LibStaffing();
			    	tb.setLibId(libId);
			    	tb.setType(type);
			    	tb.setUserId(ids[i]);
			    	tb.setCreateTime(new Date());
			    	tb.setOrgCode(orgCode);
			    }
				tb.setStat(1);
				this.save(tb);
			}
		}
		
	}
	@Override
	public String getLibJoinId(String orgCode) {
		return libStaffingDao.getLibJoinId(orgCode);
	}
	@Override
	public List<LibStaffing> findByLibId(String libId) {
		return libStaffingDao.findByLibId(libId);
	}
	@Override
	public Integer findByLibIdAndUserIdAndTypeCount(String libId,String userId,String type) {
		return libStaffingDao.getLibStaffingCount(libId, userId,type);
	}
	@Override
	public LibStaffing findByLibIdAndUserId(String libId,String userId) {
		return libStaffingDao.findByLibIdAndUserId(libId, userId);
	}
	@Override
	public void libStaffingZBSave(String orgCode, String libId, String[] ids,
			String type) {
		if(ids.length>0){
			for (int i = 0; i < ids.length; i++) {
				LibStaffing libzb=this.getById(ids[i]);
				LibStaffing tb=libStaffingDao.getLibStaffingByLibIdAndUserIdAndType(libId, libzb.getUserId(),type);
			    if(tb ==null){
			    	tb = new  LibStaffing();
			    	tb.setLibId(libId);
			    	tb.setType(type);
			    	tb.setUserId(libzb.getUserId());
			    	tb.setCreateTime(new Date());
			    	tb.setOrgCode(orgCode);
			    }
			    tb.setSex(libzb.getSex());
			    tb.setName(libzb.getName());
			    tb.setTelephone(libzb.getTelephone());
				tb.setStat(1);
				this.save(tb);
			}
		}
		
	}
	@Override
	public List<String> findByCDAndOpenId(String libId, String type) {
		return libStaffingDao.findByCDAndOpenId(libId, type);
	}
	@Override
	public Integer fingByCDcount(String libId) {
		return libStaffingDao.fingByCDcount(libId);
	}
	@Override
	public Page<Map<String, Object>> getUserListSR(
			Map<String, Object> searchParams, Pageable pageable) {
		return libStaffingQueryDao.getUserListSR(searchParams, pageable);
	}
	@Override
	public void updateStaffingById(String id, Integer stat) {
		libStaffingDao.updateStaffingById(id, stat);
	}
	@Override
	public LibStaffing findByUserIdAndType(String userId, String type) {
		
		return libStaffingDao.findByUserIdAndType(userId,type);
	}
	@Override
	public void updateStatByUserId(String userId, Integer stat) {
		libStaffingDao.updateStatByUserId(userId, stat);
	}
	@Override
	public List<LibStaffing> findByUserId(String userId) {
		return libStaffingDao.findByUserId(userId);
	}
	@Override
	public List<String> findByCDSDAndOpenId(String libId, String type,
			String types) {
		return libStaffingDao.findByCDSDAndOpenId(libId, type, types);
	}
	@Override
	public LibStaffing findByUserIdAndLib(String userid, String libId) {
		
		return libStaffingDao.findByUserIdAndLib(userid,libId);
	}
	@Override
	public Map<String, Object> findUserCost(String vipId,String libId) {
		
		return libStaffingQueryDao.findUserCost(vipId, libId);
	}
	@Override
	public LibStaffing findOnebyLibIdAndType(String libId, String type) {
		
		return libStaffingDao.findOnebyLibIdAndType(libId,type);
	}

	@Override
	public List<Map<String, Object>> findLibMasterByLibIdAndType(String libId, String type) {
		return libStaffingQueryDao.findLibMasterByLibIdAndType(libId, type);
	}

	@Override
	public Page<Map<String, Object>> findBylist(Map<String, Object> params, Pageable initPage) {
		return libStaffingQueryDao.findBylist(params,initPage);
	}
}
