package com.ssj.service.sys.fx.service.impl;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;

import com.ssj.bean.sys.fx.domain.MerchantSign;
import com.ssj.bean.sys.fx.domain.MerchantSignOrder;
import com.ssj.dao.sys.fx.dao.FxQueryDao;
import com.ssj.dao.sys.fx.dao.MerchantSignDao;
import com.ssj.framework.basic.utils.DateHelper;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.sys.fx.service.MerchantMealService;
import com.ssj.service.sys.fx.service.MerchantSignOrderService;
import com.ssj.service.sys.fx.service.MerchantSignService;



@Service
public class MerchantSignServiceImpl extends BaseServiceImpl<MerchantSign, String> implements MerchantSignService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private MerchantSignDao dao;
	
    @Autowired
    private FxQueryDao queryDao;
    
	@Autowired
	private MerchantSignOrderService merchantSignOrderService;
	
	@Autowired
	private MerchantMealService merchantMealService;
	
    @Override
    public PagingAndSortingRepository<MerchantSign, String> getDao() {
        return dao;
    }

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

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

	
	@Override
	public int getMerchantSignByCurLibService(String merchantId) {
		int libService=0;
		List<MerchantSign> merchantSigns=dao.getMerchantSignByCur(merchantId);
		if(merchantSigns!=null) {
			for (MerchantSign merchantSign : merchantSigns) {
				libService=libService+merchantSign.getLibService();
			}
		}
		return libService;
	}


	@Override
	public void saveMerchantSign(String merchantId, Integer type, Integer num, Integer libService, String signTime,Double unitPrice) {
		List<MerchantSign> list =new ArrayList<MerchantSign>();
		MerchantSignOrder signOrder=new MerchantSignOrder();
		if(type==1) {
			signOrder.setLibService(libService*num);
			signOrder.setMerchantId(merchantId);
			signOrder.setNum(num);
			signOrder.setSignTime(DateHelper.parseDate(signTime,"yyyy-MM-dd"));
			signOrder.setType(type);
			signOrder.setUnitPrice(unitPrice);
			signOrder.setCreateTime(new Date());
			merchantSignOrderService.save(signOrder);
			
			Date sTime=signOrder.getSignTime();
			for (int i = 1; i <=num; i++) {
				MerchantSign merchantSign=new MerchantSign();
				merchantSign.setCreateTime(new Date());
				merchantSign.setLibService(libService);
				merchantSign.setMerchantId(merchantId);
				merchantSign.setSignTime(sTime);
				merchantSign.setEndSignTime(DateHelper.getMonthDate(sTime,1));
				merchantSign.setStatus(1);
				merchantSign.setType(type);
				merchantSign.setOrderId(signOrder.getId());
				list.add(merchantSign);
				
				sTime=merchantSign.getEndSignTime();
			}
		}else {
			signOrder.setLibService(libService*num);
			signOrder.setMerchantId(merchantId);
			signOrder.setNum(num);
			signOrder.setSignTime(DateHelper.parseDate(signTime,"yyyy-MM-dd"));
			signOrder.setType(type);
			signOrder.setUnitPrice(unitPrice);
			signOrder.setCreateTime(new Date());
			merchantSignOrderService.save(signOrder);
			
			for (int i = 1; i <=num; i++) {
				MerchantSign merchantSign=new MerchantSign();
				merchantSign.setCreateTime(new Date());
				merchantSign.setLibService(libService);
				merchantSign.setMerchantId(merchantId);
				merchantSign.setSignTime(signOrder.getSignTime());
				merchantSign.setEndSignTime(DateHelper.getMonthDate(merchantSign.getSignTime(),1));
				merchantSign.setStatus(1);
				merchantSign.setType(type);
				merchantSign.setOrderId(signOrder.getId());
				list.add(merchantSign);
			}
		}
		this.save(list);
		merchantMealService.updateMerchantByMeal(merchantId);
	}




}


