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

import com.ssj.bean.sys.fx.domain.DeviceBalance;
import com.ssj.dao.sys.fx.dao.DeviceBalanceDao;
import com.ssj.framework.basic.utils.StringUtils;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.sys.finance.service.IFinanceService;
import com.ssj.service.sys.fx.service.DeviceBalanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

	
@Service
public class DeviceBalanceServiceImpl extends BaseServiceImpl<DeviceBalance, String> implements DeviceBalanceService {

	@Autowired
	private DeviceBalanceDao dao;
	
	@Autowired
	private IFinanceService financeService;
	
	@Override
    public PagingAndSortingRepository<DeviceBalance, String> getDao() {
        return dao;
    }

	@Override
	public DeviceBalance getDeviceBalanceByLibId(String libId, String balanceMonth) {
		return dao.getDeviceBalanceByLibId(libId, balanceMonth);
	}

	@Override
	public void runCountDeviceBalance(String balanceMonth) {
		Map<String, Object> params = new HashMap<String, Object>();
		
		if(StringUtils.isEmpty(balanceMonth)) {
			Calendar calendar = Calendar.getInstance();
		    calendar.setTime(new Date());
		    calendar.add(Calendar.DATE, -1);
		    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			balanceMonth=sdf.format(calendar.getTime());
		}
		
		params.put("startMonth", balanceMonth);
        params.put("unitName", "");
        
        Pageable initPage = new PageRequest(0, 10000);
        Page<Map<String,Object>> page = financeService.findDeviceBalanceAndLossList(params, initPage);
        List<Map<String,Object>> list=page.getContent();
        List<DeviceBalance> deviceBalances=new ArrayList<DeviceBalance>();
        for (Map<String, Object> map : list) {
        	String libId=map.get("lib_id").toString();
        	
        	DeviceBalance deviceBalance=this.getDeviceBalanceByLibId(libId, balanceMonth);
        	if(deviceBalance==null) {
	        	deviceBalance=new DeviceBalance();
	        	deviceBalance.setBalanceMonth(balanceMonth);
	        	deviceBalance.setCreateTime(new Date());
        	}
        	deviceBalance.setUpdateTime(new Date());
        	deviceBalance.setLibId(libId);
        	deviceBalance.setLastHaveBalance(Integer.parseInt(map.get("success").toString()));
        	deviceBalance.setLastNotBalance(Integer.parseInt(map.get("success2").toString()));
        	deviceBalance.setCurHaveLoss(Integer.parseInt(map.get("success3").toString()));
        	deviceBalance.setCurNotLoss(Integer.parseInt(map.get("success4").toString()));
        	deviceBalance.setCurHaveAdd(Integer.parseInt(map.get("success5").toString()));
        	deviceBalance.setCurNotAdd(Integer.parseInt(map.get("success6").toString()));
        	deviceBalance.setCurHaveCount(Integer.parseInt(map.get("success7").toString()));
        	deviceBalance.setCurNotCount(Integer.parseInt(map.get("success8").toString()));
        	deviceBalances.add(deviceBalance);
		}
        this.save(deviceBalances);
	}
	
	
	

}
