package com.ag.account.service;

import com.ag.account.dao.AccountPointsCalcDao;
import com.ag.base.SqlUtil;
import com.ag.entity.account.vo.BizVO;
import com.ag.entity.fee.AccountPoints;
import com.ag.entity.fee.BaseChargingItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

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

/**
 * Created by Administrator on 2017/5/15 0015.
 */
@Service
public class AccountPointsCalcService {

    @Autowired
    private AccountPointsCalcDao apDao;

    /**
     * 通过计费点找费目编号
     * @param bizVO
     * @param accountPointId
     */
    void getFeeItems(BizVO bizVO, String accountPointId){
        AccountPoints point = new AccountPoints();
        point.setTenancyId(bizVO.getTenancyId());
        point.setPointCode(accountPointId);
        List<AccountPoints> pointLst = apDao.findByPointId(point);
        bizVO.setPointLst(pointLst);
        Assert.notEmpty(pointLst, "计费事件无费目！");
        String items = new SqlUtil<AccountPoints>().getAddInIds(pointLst, "itemId");
        bizVO.setItemIds(items);
    }

    void findBaseItemList(BizVO bizVO){
        List<BaseChargingItem> items = apDao.findInItemId(bizVO.getItemIds(), bizVO.getTenancyId());
        Map<String, BaseChargingItem> map = new HashMap<>();
        for (BaseChargingItem item : items){
            map.put(item.getId(), item);
        }
        bizVO.setItemMap(map);
    }

    /**
     * 获取所有计费点费目
     * @param bizVO
     */
    void putAllPonits(BizVO bizVO){
        AccountPoints point = new AccountPoints();
        point.setTenancyId(bizVO.getTenancyId());
        List<AccountPoints> ponits = apDao.findAll(point);
        Map<String, List<AccountPoints>> map = new HashMap<>();
        for (AccountPoints apt : ponits){
            if (map.containsKey(apt.getPointCode())){
                map.get(apt.getPointCode()).add(apt);
            }else {
                List<AccountPoints> apts = new ArrayList<>();
                apts.add(apt);
                map.put(apt.getPointCode(), apts);
            }
        }
        bizVO.setPointLst(ponits);
        bizVO.setAllPonitsMap(map);
        //bizVO.setItemList(apDao.findAll(bizVO.getTenancyId()));
    }

    /**
     * 费目编号为空，查询计费点
     * @param bizVO
     * @return
     */
    private List<AccountPoints> findFeeAccount(BizVO bizVO){
        //final String accountPointId = "BusinessFeeAccount";
        AccountPoints point = new AccountPoints();
        point.setTenancyId(bizVO.getTenancyId());
        point.setPointCode(bizVO.getAccountPointId());
        return apDao.findByPointId(point);
    }
}
