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.DingStatisticByCrmDto;
import com.qmth.sop.business.bean.dto.SopPeriodDto;
import com.qmth.sop.business.entity.TBCrm;
import com.qmth.sop.business.entity.TBCrmDetail;
import com.qmth.sop.business.entity.TBDingStatistic;
import com.qmth.sop.business.entity.TBDingStatisticByCrm;
import com.qmth.sop.business.mapper.TBDingStatisticByCrmMapper;
import com.qmth.sop.business.service.TBCrmDetailService;
import com.qmth.sop.business.service.TBCrmService;
import com.qmth.sop.business.service.TBDingStatisticByCrmService;
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 TBDingStatisticByCrmServiceImpl extends ServiceImpl<TBDingStatisticByCrmMapper, TBDingStatisticByCrm> implements TBDingStatisticByCrmService {

    @Resource
    TBCrmService tbCrmService;

    @Resource
    TBCrmDetailService tbCrmDetailService;

    @Resource
    TBDingStatisticService tbDingStatisticService;

    @Transactional
    @Override
    public void buildStatisticByCrm(List<TBDingStatistic> datasource) {
        List<TBDingStatisticByCrm> result = new ArrayList<>();

        datasource = datasource.stream()
                .filter(e -> SystemConstant.strNotNull(e.getSopNo()) && SystemConstant.strNotNull(e.getCrmNo()))
                .collect(Collectors.toList());
        List<String> crmNoList = datasource.stream().map(TBDingStatistic::getCrmNo).distinct().collect(Collectors.toList());

        for (String crmNo : crmNoList) {
            TBCrm tbCrm = tbCrmService.findByCrmNo(crmNo);
            List<TBCrmDetail> tbCrmDetailList = tbCrmDetailService.list(
                    new QueryWrapper<TBCrmDetail>().lambda().eq(TBCrmDetail::getCrmNo, crmNo));
            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 = datasource.stream().filter(e -> crmNo.equals(e.getCrmNo())).collect(Collectors.toList());
            Long serviceId = null;
            String serviceName = "";
            int actualDays = 0;
            int weekdays = 0;
            int weekends = 0;
            int legalHolidays = 0;
            BigDecimal workHours = new BigDecimal(0);
            int violationDays = 0;

            for (TBDingStatistic tbDingStatistic : tbDingStatisticList) {
                serviceId = tbDingStatistic.getServiceId();
                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();
            }
            TBDingStatisticByCrm tbDingStatisticByCrm = new TBDingStatisticByCrm();
            tbDingStatisticByCrm.setServiceId(serviceId);
            tbDingStatisticByCrm.setServiceName(serviceName);
            tbDingStatisticByCrm.setCrmNo(crmNo);
            tbDingStatisticByCrm.setCrmName(tbCrm.getName());
            tbDingStatisticByCrm.setActualDays(actualDays);
            tbDingStatisticByCrm.setWeekdays(weekdays);
            tbDingStatisticByCrm.setWeekends(weekends);
            tbDingStatisticByCrm.setLegalHolidays(legalHolidays);
            tbDingStatisticByCrm.setWorkHours(workHours);
            tbDingStatisticByCrm.setViolationDays(violationDays);
            tbDingStatisticByCrm.setScanPeriod(scanPeriod);
            tbDingStatisticByCrm.setMarkPaperPeriod(markPaperPeriod);
            result.add(tbDingStatisticByCrm);
        }
        this.baseMapper.truncateTable();
        this.saveBatch(result);
    }

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

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