package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.fire.domain.BaseCompanyPerson;
import com.usky.fire.domain.ReportConfig;
import com.usky.fire.domain.ReportResult;
import com.usky.fire.mapper.ReportResultMapper;
import com.usky.fire.service.ReportConfigService;
import com.usky.fire.service.ReportResultService;
import com.usky.common.mybatis.core.AbstractCrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 报告结果表 服务实现类
 * </p>
 *
 * @author han
 * @since 2023-06-28
 */
@Service
public class ReportResultServiceImpl extends AbstractCrudService<ReportResultMapper, ReportResult> implements ReportResultService {
    @Autowired
    private ReportConfigService reportConfigService;

    @Override
    public CommonPage<ReportResult> reportResultList(Integer companyId, String sourceType, Integer pageNum,
                                                     Integer pageSize) {
        IPage<ReportResult> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ReportResult> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ReportResult::getObjectId, companyId)
                    .orderByDesc(ReportResult::getId);
        page = this.page(page, queryWrapper);
        return ToCommonPage(page);
    }
    @Override
    public CommonPage<Object> newReportResultList(String reportObject, Integer pageNum, Integer pageSize) {
        List<Object> list = new ArrayList<>();
        IPage<ReportResult> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ReportResult> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StringUtils.isNotBlank(reportObject),ReportResult::getReportObject, reportObject)
                .orderByDesc(ReportResult::getId);
        page = this.page(page, queryWrapper);
        LambdaQueryWrapper<ReportConfig> queryWrapper1 = Wrappers.lambdaQuery();
        List<ReportConfig> list1 = reportConfigService.list(queryWrapper1);
        if (page.getTotal() > 0) {
            for (int i = 0; i < page.getRecords().size(); i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", page.getRecords().get(i).getId());
                map.put("reportObject", page.getRecords().get(i).getReportObject());
                map.put("overallScore", page.getRecords().get(i).getOverallScore());
                map.put("createDate", page.getRecords().get(i).getCreateDate());
                for (int j = 0; j < list1.size(); j++){
                    if (page.getRecords().get(i).getConfigId().equals(list1.get(j).getId())){
                        map.put("reportName", list1.get(j).getReportName());
                    }
                }
                list.add(map);
            }
        }
        return new CommonPage<>(list, page.getTotal(), pageSize, pageNum);
    }
}
