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

import com.ssj.bean.sys.fx.domain.Device;
import com.ssj.bean.sys.fx.domain.DeviceActive;
import com.ssj.bean.sys.fx.domain.Merchant;
import com.ssj.dao.sys.fx.dao.DeviceActiveDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.sys.fx.service.DeviceActiveService;
import com.ssj.service.sys.fx.service.DeviceService;
import com.ssj.service.sys.fx.service.MerchantService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;

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


@Service
public class DeviceActiveServiceImpl extends BaseServiceImpl<DeviceActive, String> implements  DeviceActiveService {

	private   Logger logger = LoggerFactory.getLogger(this.getClass());
	 
	@Autowired
	private DeviceActiveDao dao;
	
	@Autowired
	private DeviceService deviceService;
	
	@Autowired
	private MerchantService merchantService;
	
    @Override
    public PagingAndSortingRepository<DeviceActive, String> getDao() {
        return dao;
    }
    
    @Override
	public DeviceActive getDeviceActiveByPosCode(String posCode) {
		return dao.getDeviceActiveByPosCode(posCode);
	}

	@Override
	public List<DeviceActive> findDeviceActiveByBatchCode(String batchCode) {
		return dao.findDeviceActiveByBatchCode(batchCode);
	}


	@Override
	public void saveDeviceActiveByImei(Merchant merchant,String id) {
		if(merchant==null || merchant.getLevel()==null || merchant.getLevel()<=0){//查询是否是最上级了，不是的话，再保存DeviceActive数据
			return;
		}
		String batchCode =UUID.randomUUID().toString();
		List<DeviceActive> deviceActives=new ArrayList<DeviceActive>();
		Integer activeLevel=0;
		Device device=deviceService.getById(id);
		if(device!=null){
			//激活时的商户数据
			if(merchant!=null){
				activeLevel=merchant.getLevel();
				DeviceActive deviceActive=new DeviceActive();
				deviceActive.setImei(null);
				deviceActive.setActiveLevel(activeLevel);
				deviceActive.setIsActive(0);
				//deviceActive.setType(merchant.getType());
				deviceActive.setLevel(merchant.getLevel());
				deviceActive.setMerchantId(merchant.getId());
				deviceActive.setPosCode(merchant.getPosCode());
				deviceActive.setActiveTime(null);
				deviceActive.setCreateTime(new Date());
				deviceActive.setBatchCode(batchCode);
				deviceActives.add(deviceActive);
				
				while (true) {
					if(StringUtils.isNotEmpty(merchant.getParentPosCode())){
						merchant=merchantService.getMerchantByPosCode(merchant.getParentPosCode());
						if(merchant!=null && merchant.getLevel()!=null && merchant.getLevel()>=1){//查询是否是最上级了，不是的话，再保存DeviceActive数据
							deviceActive=new DeviceActive();
							deviceActive.setImei(null);
							deviceActive.setActiveLevel(activeLevel);
							deviceActive.setIsActive(0);
							//deviceActive.setType(merchant.getType());
							deviceActive.setLevel(merchant.getLevel());
							deviceActive.setMerchantId(merchant.getId());
							deviceActive.setPosCode(merchant.getPosCode());
							deviceActive.setActiveTime(null);
							deviceActive.setCreateTime(new Date());
							deviceActive.setBatchCode(batchCode);
							deviceActives.add(deviceActive);
						}else{
							break;
						}	
					}else{
						break;
					}
				}
				this.save(deviceActives);
			}
		}
	}

	@Override
	public void activeDeviceActiveByImei(Device device,Merchant merchant,String imei) {
		Date activeTime=new Date();
		DeviceActive deviceActive=this.getDeviceActiveByPosCode(device.getPosCode());
		if(deviceActive!=null){
			List<DeviceActive> deviceActives=this.findDeviceActiveByBatchCode(deviceActive.getBatchCode());
			for (DeviceActive da : deviceActives) {
				if(merchant.getId().equals(da.getMerchantId())){
					da.setIsActive(1);	
				}
				da.setImei(imei);
				da.setActiveTime(activeTime);
			}
			this.save(deviceActives);	
		}
		device.setActiveTime(activeTime);
		deviceService.save(device);
	}

}


