package com.kcim.service.impl;

import com.kcim.common.exception.CostException;
import com.kcim.common.util.BeanUtil;
import com.kcim.common.util.UserContext;
import com.kcim.dao.model.CostReport;
import com.kcim.dao.model.CostReportIndex;
import com.kcim.dao.repository.CostReportIndexRepository;
import com.kcim.dao.repository.CostReportRepository;
import com.kcim.service.CenterService;
import com.kcim.service.CostReportService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service("costReportService")
@Slf4j
@AllArgsConstructor
public class CostReportServiceImpl implements CostReportService {

    CostReportRepository repository;
    CenterService centerService;

    CostReportIndexRepository costReportIndexRepository;

    /**
     * 获取报表列表
     * @param reportName 报表名称
     * @param reportType 报表类型
     * @return
     */
    @Override
    public Object getList(String reportName, String reportType) {
        List<CostReport> list = repository.getList(reportName, reportType);
        if(CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        //查询检索条件
        List<Long> collect = list.stream().map(CostReport::getCode).collect(Collectors.toList());
        List<CostReportIndex> byReportCode = costReportIndexRepository.getByReportCode(collect);
        if(!CollectionUtils.isEmpty(byReportCode)){
            Map<Long, List<CostReportIndex>> collect1 = byReportCode.stream().collect(Collectors.groupingBy(CostReportIndex::getReportCode));
            for (CostReport costReport : list) {
                List<CostReportIndex> costReportIndices = collect1.get(costReport.getCode());
                if(!CollectionUtils.isEmpty(costReportIndices)){
                    costReport.setReportIndex(costReportIndices);
                }
            }
        }


//        DictDataVo dict = centerService.getDict(Constant.REPORT_TYPE);
//        List<DictDataVo> dataVoList = dict.getDataVoList();
//        Map<String,String> map = dataVoList.stream().collect(Collectors.toMap(DictDataVo::getCode, DictDataVo::getName, (a, b) -> b));
//        list.forEach(costReport -> costReport.setTypeName(map.get(costReport.getType())));
        return list;
    }

    /**
     * 新增报表
     * @param report 报表入参
     */
    @Override
    public void addReport(CostReport report) {
        report.setHospId(UserContext.getHospId());
        report.setCreateUser(String.valueOf(UserContext.getCurrentUser().getId()));
        report.setCreateTime(new Date());
        report.setCode(repository.getCode());

        repository.save(report);
        List<CostReportIndex> reportIndex = report.getReportIndex();
        if(!CollectionUtils.isEmpty(reportIndex)){
            reportIndex.forEach(f->f.setReportCode(report.getCode())
                    .setHospId(UserContext.getHospId())
                    .setCreateUser(String.valueOf(UserContext.getCurrentUser().getId()))
                    .setCreateTime(new Date()));
            costReportIndexRepository.saveBatch(reportIndex);
        }






    }

    /**
     * 编辑报表
     * @param report 报表入参
     */
    @Override
    public void editReport(CostReport report) {
        Integer id = report.getId();
        if(Objects.isNull(id)){
            log.error("编辑报表时，主键id必传");
            throw new CostException("编辑报表时，主键id必传");
        }
        CostReport costReport = repository.getById(id);
        Long code = costReport.getCode();
        BeanUtil.convertObj(report, costReport);
        costReport.setUpdateUser(String.valueOf(UserContext.getCurrentUser().getId()));
        costReport.setUpdateTime(new Date());
        repository.updateById(costReport);
        List<CostReportIndex> reportIndex = report.getReportIndex();
        if(!CollectionUtils.isEmpty(reportIndex)){
            costReportIndexRepository.removeByReportCode(code);

            reportIndex.forEach(f->f.setReportCode(code)
                    .setHospId(UserContext.getHospId())
                    .setCreateUser(String.valueOf(UserContext.getCurrentUser().getId()))
                    .setCreateTime(new Date()));
            costReportIndexRepository.saveBatch(reportIndex);
        }else {
            //走一下删除逻辑有可能是全部去除
            costReportIndexRepository.removeByReportCode(code);

        }


    }

    /**
     * 删除报表
     * @param id 报表主键
     */
    @Override
    public void deleteReport(Integer id) {
        //删除对照相关数据
        CostReport costReport = repository.getById(id);
        repository.deleteReport(id);
        costReportIndexRepository.removeByReportCode(costReport.getCode());

    }
}