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.CollectionUtils;
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.BaseCompany;
import com.usky.fire.domain.BaseCompanyAttach1;
import com.usky.fire.domain.DemReportInfo;
import com.usky.fire.mapper.DemReportInfoMapper;
import com.usky.fire.service.DemReportInfoService;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.fire.service.vo.CompanyDataVo;
import com.usky.fire.service.vo.DemReportInfoIdVo;
import com.usky.fire.service.vo.DemReportInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 报告信息表 服务实现类
 * </p>
 *
 * @author han
 * @since 2023-03-02
 */
@Service
public class DemReportInfoServiceImpl extends AbstractCrudService<DemReportInfoMapper, DemReportInfo> implements DemReportInfoService {
    @Override
    public CommonPage<DemReportInfo> reportInfoList(String companyId,String sourceType,Integer pageNum, Integer pageSize) {
        IPage<DemReportInfo> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DemReportInfo> queryWrapper = Wrappers.lambdaQuery();
        if ("2".equals(sourceType)){
            queryWrapper.eq(StringUtils.isNotBlank(companyId), DemReportInfo::getCompanyId, companyId)
                    .eq(DemReportInfo::getReportStatus, 1)
                    .orderByDesc(DemReportInfo::getId);
        }else {
            queryWrapper.eq(StringUtils.isNotBlank(companyId), DemReportInfo::getCompanyId, companyId)
                    .orderByDesc(DemReportInfo::getId);
        }
        page = this.page(page, queryWrapper);
        return ToCommonPage(page);
    }

    @Override
    public List<DemReportInfoVo> reportInfoAdd(DemReportInfoVo demReportInfoVo) {
        List<DemReportInfoVo> list = new ArrayList<>();
        List<CompanyDataVo> companyList = demReportInfoVo.getCompanyList();
        Calendar now = Calendar.getInstance();
        if (CollectionUtils.isNotEmpty(companyList)) {
            List<String> companyIdList = new ArrayList<>();
            for (int i = 0; i < companyList.size(); i++) {
                companyIdList.add(companyList.get(i).getCompanyId());
            }
            List<DemReportInfo> list2 = new ArrayList<>();
            LambdaQueryWrapper<DemReportInfo> queryWrapper1 = Wrappers.lambdaQuery();
            queryWrapper1.select(DemReportInfo::getId, DemReportInfo::getCompanyId)
                    .in(DemReportInfo::getCompanyId, companyIdList)
                    .eq(DemReportInfo::getStatisticsTime, now.get(Calendar.YEAR)+"-"+now.get(Calendar.MONTH)).groupBy(DemReportInfo::getCompanyId);
            list2 = this.list(queryWrapper1);
            for (int i = 0; i < companyList.size(); i++) {
                for (int j = 0; j < list2.size(); j++) {
                    if (companyList.get(i).getCompanyId().equals(list2.get(j).getCompanyId())) {
                        DemReportInfoVo demReportInfoVo1 = new DemReportInfoVo();
                        demReportInfoVo1.setId(list2.get(j).getId());
                        demReportInfoVo1.setCompanyName(companyList.get(i).getCompanyName());
                        demReportInfoVo1.setCompanyId(companyList.get(i).getCompanyId());
                        list.add(demReportInfoVo1);
                    }
                }
            }
        }
        return list;
    }

    @Transactional
    @Override
    public void propelsReport(DemReportInfoIdVo demReportInfoIdVo) {
        List<Integer> reportInfoIdList = demReportInfoIdVo.getReportIdList();
        if (CollectionUtils.isNotEmpty(reportInfoIdList)) {
            for (int i = 0; i < reportInfoIdList.size(); i++) {
                DemReportInfo demReportInfo = new DemReportInfo();
                demReportInfo.setId(reportInfoIdList.get(i));
                demReportInfo.setReportStatus(1);
                demReportInfo.setSendTime(LocalDateTime.now());
                this.updateById(demReportInfo);
            }
        }
    }

    @Override
    public List<DemReportInfo> reportById(Integer id) {
        LambdaQueryWrapper<DemReportInfo> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.select(DemReportInfo::getId, DemReportInfo::getReportPath)
                .eq(DemReportInfo::getId, id);
        List<DemReportInfo> list2 = this.list(queryWrapper1);
        return list2;
    }
}
