package com.mortals.xhx.module.staff.service.impl;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.module.staff.dao.StaffPerformSummaryDao;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.model.StaffPerformSummaryEntity;
import com.mortals.xhx.module.staff.service.StaffPerformSummaryService;
import com.mortals.xhx.module.staff.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

/**
* StaffPerformSummaryService
* 员工绩效统计 service实现
*
* @author zxfei
* @date 2023-07-05
*/
@Service("staffPerformSummaryService")
@Slf4j
public class StaffPerformSummaryServiceImpl extends AbstractCRUDServiceImpl<StaffPerformSummaryDao, StaffPerformSummaryEntity, Long> implements StaffPerformSummaryService {

    @Autowired
    private StaffService staffService;

    @Override
    protected StaffPerformSummaryEntity findBefore(StaffPerformSummaryEntity params, PageInfo pageInfo, Context context) throws AppException {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        if(params.getYear()==null){
            params.setYear(year);
        }
        if(params.getMonth()==null){
            params.setMonth(month);
        }
        return params;
    }

    @Override
    protected void findAfter(StaffPerformSummaryEntity params, PageInfo pageInfo, Context context, List<StaffPerformSummaryEntity> list) throws AppException {
        if(CollectionUtils.isNotEmpty(list)){
            for(StaffPerformSummaryEntity item:list) {
                StaffEntity staffEntity = staffService.get(item.getStaffId());
                if(staffEntity!=null){
                    item.setWorkNum(staffEntity.getWorkNum());
                    item.setPhoneNumber(staffEntity.getPhoneNumber());
                }
            }
        }
    }
}
