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

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhgd.xmgl.modules.basicdata.entity.AttendanceRecord;
import com.zhgd.xmgl.modules.basicdata.mapper.AttendanceRecordMapper;
import com.zhgd.xmgl.modules.basicdata.service.IAttendanceRecordService;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 人员出勤统计记录
 * @author： pengj
 * @date：   2023-03-16
 * @version： V1.0
 */
@Service
public class AttendanceRecordServiceImpl extends ServiceImpl<AttendanceRecordMapper, AttendanceRecord> implements IAttendanceRecordService {

    @Override
    public List<Map<String, Object>> filterList(Map<String, Object> map) {
        List<Map<String, Object>> result = new ArrayList<>();
        Integer personType = MapUtils.getInteger(map, "personType");
        String startTime = MapUtils.getString(map, "startTime");
        String endTime = MapUtils.getString(map, "endTime");
        LambdaQueryWrapper<AttendanceRecord> wrapper = Wrappers.<AttendanceRecord>lambdaQuery();
        wrapper.ge(AttendanceRecord::getDayDate, startTime);
        wrapper.le(AttendanceRecord::getDayDate, endTime);
        List<AttendanceRecord> list = baseMapper.selectList(wrapper);
        for (AttendanceRecord record : list) {
            Map<String, Object> resultMap = new HashMap<>();
            if (personType!= null && personType == 1) {
                resultMap.put("personNum", record.getInServiceWorker());
                resultMap.put("attend", record.getAttendWorker());
                resultMap.put("ratio", record.getInServiceWorker() == 0 ? 0 : NumberUtil.round(record.getAttendWorker() * 100F / record.getInServiceWorker(), 2).floatValue());
            } else if (personType!= null && personType == 2) {
                resultMap.put("personNum", record.getInServiceManager());
                resultMap.put("attend", record.getAttendManager());
                resultMap.put("ratio", record.getInServiceManager() == 0 ? 0 : NumberUtil.round(record.getAttendManager() * 100F / record.getInServiceManager(), 2).floatValue());
            } else {
                int totalInService = record.getInServiceWorker() + record.getInServiceManager();
                int totalAttend = record.getAttendManager() + record.getAttendWorker();
                resultMap.put("personNum", totalInService);
                resultMap.put("attend", totalAttend);
                resultMap.put("ratio", totalInService == 0 ? 0 : NumberUtil.round(totalAttend * 100F / totalInService, 2).floatValue());
            }
            result.add(resultMap);
        }
        return result;
    }
}
