package com.zhgd.xmgl.modules.basicdata.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhgd.xmgl.modules.basicdata.entity.WorkerAttendance;
import com.zhgd.xmgl.modules.basicdata.entity.WorkerInfo;
import com.zhgd.xmgl.modules.basicdata.mapper.WorkerAttendanceMapper;
import com.zhgd.xmgl.modules.basicdata.mapper.WorkerInfoMapper;
import com.zhgd.xmgl.modules.basicdata.service.IWorkerAttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 人员考勤
 * @author： pengj
 * @date：   2023-03-14
 * @version： V1.0
 */
@Service
public class WorkerAttendanceServiceImpl extends ServiceImpl<WorkerAttendanceMapper, WorkerAttendance> implements IWorkerAttendanceService {

    @Autowired
    private WorkerInfoMapper workerInfoMapper;

    @Override
    public Map<String, Object> statistics() {
        Map<String, Object> resultMap = new HashMap<>();
        Integer inServiceWorker = workerInfoMapper.selectCount(Wrappers.<WorkerInfo>lambdaQuery().eq(WorkerInfo::getInServiceType, 1));
        Integer workerAttendanceNum = baseMapper.getWorkerAttendanceNum(DateUtil.beginOfDay(new Date()).toString());
        Integer workerAttendanceTotal = baseMapper.selectCount(Wrappers.<WorkerAttendance>lambdaQuery()
                .ge(WorkerAttendance::getCreateTime, DateUtil.beginOfDay(new Date()).toString()));
        resultMap.put("inServiceWorker", inServiceWorker);
        resultMap.put("workerAttendanceNum", workerAttendanceNum);
        resultMap.put("workerAttendanceTotal", workerAttendanceTotal);
        resultMap.put("ratio", inServiceWorker == 0 ? 0 : NumberUtil.round(workerAttendanceNum * 100F / inServiceWorker, 2).floatValue());
        return resultMap;
    }
}
