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

import com.ssj.bean.weixin.user.domain.User;
import com.ssj.dao.weixin.adminsale.dao.AdminSaleQueryDao;
import com.ssj.framework.core.util.SystemResourceLocator;
import com.ssj.framework.weixin.util.BarcodeFactory;
import com.ssj.service.weixin.adminsale.service.AdminSaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class AdminSaleServiceImpl  implements AdminSaleService {

    @Autowired
    private AdminSaleQueryDao  adminSaleQueryDao;
	
	@Override
	public Page<Map<String, Object>> findVipBySaleAndLibIdPage(
			Map<String, Object> searchParams, Pageable pageable) {
		return adminSaleQueryDao.findVipBySaleAndLibIdPage(searchParams, pageable);
	}

	@Override
	public List<Map<String, Object>> findLibOrderDetailsByVipId(String vipId) {
		return adminSaleQueryDao.findLibOrderDetailsByVipId(vipId);
	}
	
	@Override
	public BigDecimal findSaleCommission(Integer num,String priceSpecies, String priceType,Integer type) {
		BigDecimal commission=new BigDecimal("0");
		if("S001".equals(priceSpecies) && "C003".equals(priceType) && type==1){ //2.月卡新签应付金额=销售数量*20
			commission=new BigDecimal("20");
		}else if("S001".equals(priceSpecies) && "C003".equals(priceType) && type==2){//3.月卡续费应付金额=销售数量*10
			commission=new BigDecimal("10");
		}else if("S001".equals(priceSpecies) && "C001".equals(priceType) && type==1){//4.年卡新签应付金额=销售数量*100
			commission=new BigDecimal("100");
		}else if("S001".equals(priceSpecies) && "C001".equals(priceType) && type==2){//5.年卡续费应付金额=销售数量*50
			commission=new BigDecimal("50");
		}else if("S003".equals(priceSpecies) && "C004".equals(priceType) && type==1){//6.VIP月卡新签应付金额=销售数量*80
			commission=new BigDecimal("80");
		}else if("S003".equals(priceSpecies) && "C004".equals(priceType) && type==2){//7.VIP月卡续费应付金额=销售数量*40
			commission=new BigDecimal("40");
		}else if("S003".equals(priceSpecies) && "C006".equals(priceType) && type==1){//8.VIP年卡新签应付金额=销售数量*600
			commission=new BigDecimal("600");
		}else if("S003".equals(priceSpecies) && "C006".equals(priceType) && type==2){//9.VIP年卡续费应付金额=销售数量*300
			commission=new BigDecimal("300");
		}else if("S001".equals(priceSpecies) && "C007".equals(priceType)){//周末
			commission=new BigDecimal("20");
		}else if("S001".equals(priceSpecies) && "C008".equals(priceType)){//寒假
			commission=new BigDecimal("100");
		}else if("S004".equals(priceSpecies) && "C009".equals(priceType)){//自然拼读应付金额=总部销售人员售卡数量*48元
			commission=new BigDecimal("48");
		}else if("S004".equals(priceSpecies) && "C010".equals(priceType)){//2.拼音识字班应付金额=销售数量*90
			commission=new BigDecimal("90");
		}else if("S004".equals(priceSpecies) && "C011".equals(priceType)){//3.数学思维班应付金额=销售数量*90
			commission=new BigDecimal("90");
		}else if("S004".equals(priceSpecies) && "C012".equals(priceType)){//4.全天班应付金额=销售数量*180元
			commission=new BigDecimal("180");
		}else {
			
		}
		return new BigDecimal(num).multiply(commission);
	}

	@Override
	public String saleQrcode(User user) {
		String fPath = "/signcode/";
		String file_path = (String) SystemResourceLocator.getValue("file_save_path");
		String sys_url = (String) SystemResourceLocator.getValue("sys_url");
		try {
			File  f = new File(file_path+fPath);
			if (!f.exists()) {
				f.mkdirs();
			}
			fPath = fPath + user.getId()+ ".png";
			f = new File(file_path+fPath);
			if (!f.exists()) {
				String url =sys_url+"/weixin/adminsale/subexperience_"+user.getId()+".html";
				BarcodeFactory.encode(url,file_path+"/images/favicon.png",file_path+fPath,226, 226, true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fPath;
	}
	
	
	@Override
	public List<Map<String, Object>> findBirthdayBySale(String saleUserId) {
		return adminSaleQueryDao.findBirthdayBySale(saleUserId);
	}

	@Override
	public List<Map<String, Object>> findServiceEndBySale(String saleUserId) {
		return adminSaleQueryDao.findServiceEndBySale(saleUserId);
	}

	@Override
	public List<Map<String, Object>> findLibOrderDetailsServiceBySale(
			String saleUserId) {
		return adminSaleQueryDao.findLibOrderDetailsServiceBySale(saleUserId);
	}

	@Override
	public List<Map<String, Object>> findSilentUserBySale(String saleUserId) {
		return adminSaleQueryDao.findSilentUserBySale(saleUserId);
	}

	@Override
	public List<Map<String, Object>> findMonthDetailBySale(String saleUserId) {
		return adminSaleQueryDao.findMonthDetailBySale(saleUserId);
	}

	@Override
	public List<Map<String, Object>> findCountOrderBySale(String saleUserId) {
		return adminSaleQueryDao.findCountOrderBySale(saleUserId);
	}

	@Override
	public Page<Map<String, Object>> findOrderRankByLibId(
			Map<String, Object> searchParams, Pageable pageable) {
		return adminSaleQueryDao.findOrderRankByLibId(searchParams,pageable);
	}

	@Override
	public List<Map<String, Object>> findExperienceBySale(String saleUserId) {
		return adminSaleQueryDao.findExperienceBySale(saleUserId);
	}

	@Override
	public List<Map<String, Object>> findServiceByVipId(String vipId) {
		return adminSaleQueryDao.findServiceByVipId(vipId);
	}

	@Override
	public List<Map<String, Object>> findSaleByUserId(String saleUserId) {
		return adminSaleQueryDao.findSaleByUserId(saleUserId);
	}

	@Override
	public Map<String, Object> findCountOrderByLib(String libId) {
		
		return adminSaleQueryDao.findCountOrderByLib(libId);
	}

	@Override
	public List<Map<String, Object>> findCdLibList(String userId,String searchContent) {
		
		return adminSaleQueryDao.findCdLibList(userId,searchContent);
	}

	@Override
	public Map<String, Object> findCdComeIn(String userId) {
		
		return adminSaleQueryDao.findCdComeIn(userId);
	}

	@Override
	public Map<String, Object> findOrderById(String detailId) {
		
		return adminSaleQueryDao.findOrderById(detailId);
	}

	


}
