package com.keao.edu.user.service.impl;


import com.keao.edu.common.dal.BaseDAO;
import com.keao.edu.common.exception.BizException;
import com.keao.edu.common.page.PageInfo;
import com.keao.edu.common.service.impl.BaseServiceImpl;
import com.keao.edu.user.dao.ExamManualLedgerDao;
import com.keao.edu.user.dao.ExamOrganizationRelationDao;
import com.keao.edu.user.dto.ExamManualLedgerStatisticsDto;
import com.keao.edu.user.entity.ExamManualLedger;
import com.keao.edu.user.entity.ExamOrganizationRelation;
import com.keao.edu.user.page.ExamManualLedgerQueryInfo;
import com.keao.edu.user.service.ExamManualLedgerService;
import com.keao.edu.util.collection.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@Service
public class ExamManualLedgerServiceImpl extends BaseServiceImpl<Long, ExamManualLedger> implements ExamManualLedgerService {
	
	@Autowired
	private ExamManualLedgerDao examManualLedgerDao;
	@Autowired
	private ExamOrganizationRelationDao examOrganizationRelationDao;

	@Override
	public BaseDAO<Long, ExamManualLedger> getDAO() {
		return examManualLedgerDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
	public void addExamManualLedger(ExamManualLedger examManualLedger) {
		if(Objects.isNull(examManualLedger.getExaminationBasicId())){
			throw new BizException("请指定考级项目");
		}
		if(Objects.isNull(examManualLedger.getOrganId())){
			throw new BizException("暂无权限");
		}
		if(Objects.isNull(examManualLedger.getAmount())){
			throw new BizException("请指定交易金额");
		}
		if(Objects.nonNull(examManualLedger.getTargetOrganId())&&examManualLedger.getTargetOrganId()<=0){
			examManualLedger.setTargetOrganId(null);
		}
		examManualLedgerDao.insert(examManualLedger);

		ExamOrganizationRelation examOrganizationRelation = examOrganizationRelationDao.getExamOrganizationRelation(examManualLedger.getExaminationBasicId(), examManualLedger.getOrganId());
		if(Objects.isNull(examOrganizationRelation)){
			throw new BizException("合作单位信息异常");
		}
		examOrganizationRelation.setTotalTransAmount(examManualLedgerDao.countOrganLedgerWithExam(examManualLedger.getExaminationBasicId(), examManualLedger.getOrganId()));
		examOrganizationRelationDao.update(examOrganizationRelation);
	}

	@Override
	public PageInfo<ExamManualLedger> queryExamManualLedgers(ExamManualLedgerQueryInfo queryInfo) {
		PageInfo<ExamManualLedger> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<String, Object>();
		MapUtil.populateMap(params, queryInfo);

		List<ExamManualLedger> dataList = new ArrayList<>();
		int count = examManualLedgerDao.countExamManualLedgers(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = examManualLedgerDao.queryExamManualLedgers(params);
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	@Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
	public void deleteExamManualLedger(Integer operatorId,Long id) {
		if(Objects.isNull(id)){
			throw new BizException("请指定需要删除的支出记录");
		}
		ExamManualLedger examManualLedger = examManualLedgerDao.get(id);
		examManualLedgerDao.delete(id);

		ExamOrganizationRelation examOrganizationRelation = examOrganizationRelationDao.getExamOrganizationRelation(examManualLedger.getExaminationBasicId(), examManualLedger.getOrganId());
		if(Objects.isNull(examOrganizationRelation)){
			throw new BizException("合作单位信息异常");
		}
		BigDecimal amount = examManualLedgerDao.countOrganLedgerWithExam(examManualLedger.getExaminationBasicId(), examManualLedger.getOrganId());
		if(Objects.isNull(amount)){
			amount=BigDecimal.ZERO;
		}
		examOrganizationRelation.setTotalTransAmount(amount);
		examOrganizationRelationDao.update(examOrganizationRelation);
	}

	@Override
	public ExamManualLedgerStatisticsDto getExamManualLedgerStatistics(Integer organId, Long examId, Integer targetOrganId) {
		if(Objects.isNull(examId)){
			throw new BizException("请指定考级项目");
		}
		return examManualLedgerDao.getExamManualLedgerStatistics(organId, examId, targetOrganId);
	}
}
