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

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.DingStatisticBySupplierDto;
import com.qmth.sop.business.entity.TBDingStatistic;
import com.qmth.sop.business.entity.TBDingStatisticBySupplier;
import com.qmth.sop.business.mapper.TBDingStatisticBySupplierMapper;
import com.qmth.sop.business.service.SysSupplierService;
import com.qmth.sop.business.service.TBDingStatisticBySupplierService;
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 TBDingStatisticBySupplierServiceImpl extends ServiceImpl<TBDingStatisticBySupplierMapper, TBDingStatisticBySupplier> implements TBDingStatisticBySupplierService {

    @Resource
    SysSupplierService sysSupplierService;

    @Transactional
    @Override
    public void buildStatisticBySupplier(List<TBDingStatistic> datasource) {
        List<TBDingStatisticBySupplier> result = new ArrayList<>();
        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> supplierIdList = serviceStatisticList.stream().map(TBDingStatistic::getSupplierId).distinct().collect(Collectors.toList());

            for (Long supplierId : supplierIdList) {
                List<TBDingStatistic> tbDingStatisticList = serviceStatisticList.stream().filter(e -> supplierId.equals(e.getSupplierId())).collect(Collectors.toList());
                String serviceName = "";
                String supplierName = sysSupplierService.getById(supplierId).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();
                }
                TBDingStatisticBySupplier tbDingStatisticBySupplier = new TBDingStatisticBySupplier();
                tbDingStatisticBySupplier.setServiceId(serviceId);
                tbDingStatisticBySupplier.setServiceName(serviceName);
                tbDingStatisticBySupplier.setSupplierId(supplierId);
                tbDingStatisticBySupplier.setSupplierName(supplierName);
                tbDingStatisticBySupplier.setActualDays(actualDays);
                tbDingStatisticBySupplier.setWeekdays(weekdays);
                tbDingStatisticBySupplier.setWeekends(weekends);
                tbDingStatisticBySupplier.setLegalHolidays(legalHolidays);
                tbDingStatisticBySupplier.setWorkHours(workHours);
                tbDingStatisticBySupplier.setViolationDays(violationDays);
                result.add(tbDingStatisticBySupplier);
            }
        }
        this.baseMapper.truncateTable();
        this.saveBatch(result);
    }

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

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