package com.ssm.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.SsmStaffAttendanceRecordDao;
import com.ssm.manage.entity.StaffAttendanceRecord;
import com.ssm.manage.query.StaffAttendanceRecordRequest;
import com.ssm.manage.response.StaffAttendanceRecordResponse;
import com.ssm.manage.service.SsmStaffAttendanceRecordService;
import com.ssm.manage.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SsmStaffAttendanceRecordServiceImpl extends BaseServiceImpl<StaffAttendanceRecord> implements SsmStaffAttendanceRecordService {
    @Autowired
    private SsmStaffAttendanceRecordDao dao;

    @Override
    public SsmStaffAttendanceRecordDao getDao() {
        return dao;
    }

    @Override
    public List<StaffAttendanceRecord> selectPage(StaffAttendanceRecordRequest staffAttendanceRecordRequest) {
        PageInfo<StaffAttendanceRecord> pageInfo = dao.selectPage(staffAttendanceRecordRequest);
        List<StaffAttendanceRecord> list = pageInfo.getList();
        Map<Long, List<StaffAttendanceRecord>> map = new HashMap<>();
        List<StaffAttendanceRecord> recordList = new ArrayList<>();
        if (!list.isEmpty()){
            map = list.stream().collect(Collectors.groupingBy(StaffAttendanceRecord::getStaffId));
            for (Map.Entry<Long, List<StaffAttendanceRecord>> longListEntry : map.entrySet()) {
                List<StaffAttendanceRecord> sortList = longListEntry.getValue().stream().
                        sorted(Comparator.comparing(StaffAttendanceRecord::getDate).reversed()).collect(Collectors.toList());
                //计算工时
                BigDecimal workTime = sortList.stream().filter(staffAttendanceRecord -> null != staffAttendanceRecord.getWorkTime()).map(StaffAttendanceRecord::getWorkTime).reduce(BigDecimal.ZERO, BigDecimal::add);
                StaffAttendanceRecord staffAttendanceRecord = sortList.get(0);
                staffAttendanceRecord.setWorkTime(workTime);
                recordList.add(staffAttendanceRecord);
            }
        }
        return recordList;
    }

    @Override
    public int addRecord(StaffAttendanceRecordRequest staffAttendanceRecordRequest) {
        StaffAttendanceRecord staffAttendanceRecord = new StaffAttendanceRecord();
        BeanUtils.copyProperties(staffAttendanceRecordRequest, staffAttendanceRecord);
        staffAttendanceRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        staffAttendanceRecord.setDate(new Date());
        //休息状态下计算此间工时
        if (staffAttendanceRecord.getState() == 2){
            //查询上次工作记录时间
            Example example = new Example(StaffAttendanceRecord.class);
            example.setOrderByClause("date desc");
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("storeId", staffAttendanceRecordRequest.getStoreId());
            criteria.andEqualTo("staffId", staffAttendanceRecordRequest.getStaffId());
            List<StaffAttendanceRecord> list = dao.selectByExample(example);
            Long workTime = DateUtils.getHourDiff(list.get(0).getDate(), staffAttendanceRecord.getDate());
            staffAttendanceRecord.setWorkTime(BigDecimal.valueOf(workTime));
        }
        int i = dao.saveNotNull(staffAttendanceRecord);
        return i;
    }

//    @Override
//    public StaffAttendanceRecordResponse staffWorkTime(StaffAttendanceRecordRequest staffAttendanceRecordRequest) {
//        Example example = new Example(StaffAttendanceRecord.class);
//        example.setOrderByClause("date desc");
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("staffId", staffAttendanceRecordRequest.getStaffId());
//        criteria.andEqualTo("storeId", staffAttendanceRecordRequest.getStoreId());
//        criteria.andEqualTo("state", 2);
//        if (null != staffAttendanceRecordRequest.getStartTime() && null != staffAttendanceRecordRequest.getEndTime()){
//            criteria.andBetween("date", staffAttendanceRecordRequest.getStartTime(), staffAttendanceRecordRequest.getEndTime());
//        }
//        List<StaffAttendanceRecord> resultList = dao.selectByExample(example);
//        BigDecimal workTime = resultList.stream().map(staffAttendanceRecord -> staffAttendanceRecord.getWorkTime()).reduce(BigDecimal.ZERO, BigDecimal::add);
//
//        return workTime;
//    }
}
