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

import com.mortals.framework.service.impl.AbstractCRUDCacheServiceImpl;
import com.mortals.xhx.common.code.StaffSatusEnum;
import com.mortals.xhx.common.code.StaffTypeEnum;
import com.mortals.xhx.common.code.StatusEnum;
import com.mortals.xhx.module.staff.dao.StaffDao;
import com.mortals.xhx.module.staff.dao.ibatis.StaffDaoImpl;
import com.mortals.xhx.module.staff.dao.ibatis.StaffLeaveDaoImpl;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.model.StaffQuery;
import com.mortals.xhx.module.staff.model.vo.StaffInfoVo;
import com.mortals.xhx.module.staff.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * StaffService
 * 员工基本信息 service实现
 *
 * @author zxfei
 * @date 2023-04-07
 */
@Service("staffService")
public class StaffServiceImpl extends AbstractCRUDCacheServiceImpl<StaffDao, StaffEntity, Long> implements StaffService {

    @Override
    protected String getExtKey(StaffEntity data) {
        //工号作为redis 扩展key
        return data.getWorkNum();
    }

    @Override
    public StaffInfoVo queryAll() {
        StaffInfoVo staffInfoVo = new StaffInfoVo();
        //在职
        StaffQuery staffQuery = new StaffQuery();
        staffQuery.setStatusList(Arrays.asList(StaffSatusEnum.正式.getValue(), StaffSatusEnum.试用.getValue()));
        List<StaffEntity> staffEntities = this.find(staffQuery);
        staffInfoVo.setInWorkStaff(staffEntities.size());
        //全职
        Map<Integer, Long> collect = staffEntities.stream().collect(Collectors.groupingBy(x -> x.getStaffType(), Collectors.counting()));
        staffInfoVo.setFullStaff(collect.getOrDefault(StaffTypeEnum.全职.getValue(), 0L).intValue());
        //实习
        staffInfoVo.setPricateStaff(collect.getOrDefault(StaffTypeEnum.实习.getValue(), 0L).intValue());
        //兼职
        staffInfoVo.setConcurrentlyStaff(collect.getOrDefault(StaffTypeEnum.兼职.getValue(), 0L).intValue());
        //试用
        staffInfoVo.setOnTrialStaff(this.find(new StaffQuery().status(StaffSatusEnum.试用.getValue())).size());
        //正式
        staffInfoVo.setFormalStaff(this.find(new StaffQuery().status(StaffSatusEnum.正式.getValue())).size());
        //待离职
        staffInfoVo.setResignationStaff(this.find(new StaffQuery().status(StaffSatusEnum.离职.getValue())).size());
        return staffInfoVo;
    }


    @Override
    public int queryAllPerson() {
        StaffQuery staffQuery = new StaffQuery();
        staffQuery.setStatusList(Arrays.asList(StaffSatusEnum.正式.getValue(), StaffSatusEnum.试用.getValue()));
        List<StaffEntity> staffEntities = this.find(staffQuery);
        return staffEntities.size();
    }

}