package com.qmth.sop.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.sop.business.bean.dto.DingStatisticByCustomDto;
import com.qmth.sop.business.bean.dto.SopPeriodDto;
import com.qmth.sop.business.entity.TBCrmDetail;
import com.qmth.sop.business.entity.TBDingStatistic;
import com.qmth.sop.business.entity.TBDingStatisticByCustom;
import com.qmth.sop.business.mapper.TBDingStatisticByCustomMapper;
import com.qmth.sop.business.service.SysCustomService;
import com.qmth.sop.business.service.TBCrmDetailService;
import com.qmth.sop.business.service.TBDingStatisticByCustomService;
import com.qmth.sop.business.service.TBDingStatisticService;
import com.qmth.sop.common.contant.SystemConstant;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 工时统计-按客户统计 服务实现类
 * </p>
 *
 * @author CaoZixuan
 * @since 2024-09-09
 */
@Service
public class TBDingStatisticByCustomServiceImpl extends ServiceImpl<TBDingStatisticByCustomMapper, TBDingStatisticByCustom> implements TBDingStatisticByCustomService {

    @Resource
    TBCrmDetailService tbCrmDetailService;

    @Resource
    TBDingStatisticService tbDingStatisticService;

    @Resource
    SysCustomService sysCustomService;

    @Transactional
    @Override
    public void buildStatisticByCustom(List<TBDingStatistic> datasource) {
        List<TBDingStatisticByCustom> result = new ArrayList<>();
        datasource = datasource.stream().filter(e -> SystemConstant.strNotNull(e.getSopNo())).collect(Collectors.toList());
        List<Long> serviceIdList = datasource.stream().map(TBDingStatistic::getServiceId).distinct().collect(Collectors.toList());
        for (Long serviceId : serviceIdList) {
            List<TBDingStatistic> serviceStatisticList = datasource.stream().filter(e -> serviceId.equals(e.getServiceId())).collect(Collectors.toList());
            List<Long> customIdList = serviceStatisticList.stream().map(TBDingStatistic::getCustomId).distinct().collect(Collectors.toList());

            for (Long customId : customIdList) {
                List<TBCrmDetail> tbCrmDetailList = tbCrmDetailService.findByServiceIdAndCustomId(serviceId, customId);
                int scanPeriod = 0;
                int markPaperPeriod = 0;
                for (TBCrmDetail tbCrmDetail : tbCrmDetailList) {
                    SopPeriodDto sopPeriodDto = tbDingStatisticService.findSopPeriod(tbCrmDetail);
                    scanPeriod = scanPeriod + sopPeriodDto.getScanPeriod();
                    markPaperPeriod = markPaperPeriod + sopPeriodDto.getMarkPaperPeriod();
                }

                List<TBDingStatistic> tbDingStatisticList = serviceStatisticList.stream().filter(e -> customId.equals(e.getCustomId())).collect(Collectors.toList());
                String serviceName = "";
                String customName = sysCustomService.getById(customId).getName();
                int actualDays = 0;
                int weekdays = 0;
                int weekends = 0;
                int legalHolidays = 0;
                BigDecimal workHours = new BigDecimal(0);
                int violationDays = 0;

                for (TBDingStatistic tbDingStatistic : tbDingStatisticList) {
                    serviceName = tbDingStatistic.getServiceName();
                    actualDays = actualDays + tbDingStatistic.getActualDays();
                    weekdays = weekdays + tbDingStatistic.getWeekdays();
                    weekends = weekends + tbDingStatistic.getWeekends();
                    legalHolidays = legalHolidays + tbDingStatistic.getLegalHolidays();
                    workHours = workHours.add(tbDingStatistic.getWorkHours());
                    violationDays = violationDays + tbDingStatistic.getViolationDays();
                }
                TBDingStatisticByCustom tbDingStatisticByCustom = new TBDingStatisticByCustom();
                tbDingStatisticByCustom.setServiceId(serviceId);
                tbDingStatisticByCustom.setServiceName(serviceName);
                tbDingStatisticByCustom.setCustomId(customId);
                tbDingStatisticByCustom.setCustomName(customName);
                tbDingStatisticByCustom.setActualDays(actualDays);
                tbDingStatisticByCustom.setWeekdays(weekdays);
                tbDingStatisticByCustom.setWeekends(weekends);
                tbDingStatisticByCustom.setLegalHolidays(legalHolidays);
                tbDingStatisticByCustom.setWorkHours(workHours);
                tbDingStatisticByCustom.setViolationDays(violationDays);
                tbDingStatisticByCustom.setScanPeriod(scanPeriod);
                tbDingStatisticByCustom.setMarkPaperPeriod(markPaperPeriod);
                result.add(tbDingStatisticByCustom);
            }
        }
        this.baseMapper.truncateTable();
        this.saveBatch(result);
    }

    @Override
    public IPage<DingStatisticByCustomDto> findStatisticByCustomPage(Long serviceId, Integer pageNumber,
            Integer pageSize) {
        return this.baseMapper.findStatisticByCustomPage(new Page<>(pageNumber, pageSize), serviceId);
    }

    @Override
    public List<DingStatisticByCustomDto> findStatisticByCustomList(Long serviceId) {
        return this.baseMapper.findStatisticByCustomList(serviceId);
    }
}
