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.DingStatisticByPersonDto;
import com.qmth.sop.business.entity.TBDingStatistic;
import com.qmth.sop.business.entity.TBDingStatisticByPerson;
import com.qmth.sop.business.mapper.TBDingStatisticByPersonMapper;
import com.qmth.sop.business.service.TBDingStatisticByPersonService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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 TBDingStatisticByPersonServiceImpl extends ServiceImpl<TBDingStatisticByPersonMapper, TBDingStatisticByPerson> implements TBDingStatisticByPersonService {

    @Transactional
    @Override
    public void buildStatisticByPerson(List<TBDingStatistic> datasource) {
        List<TBDingStatisticByPerson> 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> userIdList = serviceStatisticList.stream().map(TBDingStatistic::getUserId).distinct().collect(Collectors.toList());

            for (Long userId : userIdList) {
                List<TBDingStatistic> tbDingStatisticList = serviceStatisticList.stream().filter(e -> userId.equals(e.getUserId())).collect(Collectors.toList());
                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) {
                    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();
                }
                TBDingStatisticByPerson tbDingStatisticByPerson = new TBDingStatisticByPerson();
                tbDingStatisticByPerson.setServiceId(serviceId);
                tbDingStatisticByPerson.setServiceName(serviceName);
                tbDingStatisticByPerson.setSupplierName(tbDingStatisticList.get(0).getSupplierName());
                tbDingStatisticByPerson.setUserInfo(
                        tbDingStatisticList.get(0).getUserArchivesName() + "(" + tbDingStatisticList.get(0).getUserArchivesCode() + ")");
                tbDingStatisticByPerson.setActualDays(actualDays);
                tbDingStatisticByPerson.setWeekdays(weekdays);
                tbDingStatisticByPerson.setWeekends(weekends);
                tbDingStatisticByPerson.setLegalHolidays(legalHolidays);
                tbDingStatisticByPerson.setWorkHours(workHours);
                tbDingStatisticByPerson.setViolationDays(violationDays);
                result.add(tbDingStatisticByPerson);
            }
        }
        this.baseMapper.truncateTable();
        this.saveBatch(result);
    }

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

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