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.DingStatisticBySopDto;
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.TBDingStatisticBySop;
import com.qmth.sop.business.mapper.TBDingStatisticBySopMapper;
import com.qmth.sop.business.service.TBCrmDetailService;
import com.qmth.sop.business.service.TBDingStatisticBySopService;
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>
 * 工时统计-按sop统计 服务实现类
 * </p>
 *
 * @author CaoZixuan
 * @since 2024-09-09
 */
@Service
public class TBDingStatisticBySopServiceImpl extends ServiceImpl<TBDingStatisticBySopMapper, TBDingStatisticBySop> implements TBDingStatisticBySopService {

    @Resource
    TBCrmDetailService tbCrmDetailService;

    @Resource
    TBDingStatisticService tbDingStatisticService;

    @Transactional
    @Override
    public void buildStatisticBySop(List<TBDingStatistic> datasource) {
        List<TBDingStatisticBySop> result = new ArrayList<>();
        datasource = datasource.stream().filter(e -> SystemConstant.strNotNull(e.getSopNo())).collect(Collectors.toList());
        List<String> sopNoList = datasource.stream().map(TBDingStatistic::getSopNo).distinct().collect(Collectors.toList());

        for (String sopNo : sopNoList) {
            TBCrmDetail tbCrmDetail = tbCrmDetailService.findBySopNo(sopNo);
            SopPeriodDto sopPeriodDto = tbDingStatisticService.findSopPeriod(tbCrmDetail);

            List<TBDingStatistic> tbDingStatisticList = datasource.stream().filter(e -> sopNo.equals(e.getSopNo())).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();
            }
            TBDingStatisticBySop tbDingStatisticBySop = new TBDingStatisticBySop();
            tbDingStatisticBySop.setServiceId(serviceId);
            tbDingStatisticBySop.setServiceName(serviceName);
            tbDingStatisticBySop.setSopNo(sopNo);
            tbDingStatisticBySop.setCourseName(tbCrmDetail.getCourseName());
            tbDingStatisticBySop.setActualDays(actualDays);
            tbDingStatisticBySop.setWeekdays(weekdays);
            tbDingStatisticBySop.setWeekends(weekends);
            tbDingStatisticBySop.setLegalHolidays(legalHolidays);
            tbDingStatisticBySop.setWorkHours(workHours);
            tbDingStatisticBySop.setViolationDays(violationDays);
            tbDingStatisticBySop.setScanPeriod(sopPeriodDto.getScanPeriod());
            tbDingStatisticBySop.setMarkPaperPeriod(sopPeriodDto.getMarkPaperPeriod());
            result.add(tbDingStatisticBySop);
        }
        this.baseMapper.truncateTable();
        this.saveBatch(result);
    }

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

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