package cn.wisenergy.service.impl;

import cn.wisenergy.common.utils.DateUtil;
import cn.wisenergy.common.utils.exception.BASE_RESP_CODE_ENUM;
import cn.wisenergy.common.utils.exception.BaseCustomException;
import cn.wisenergy.mapper.WorkTimeOrderMapper;
import cn.wisenergy.mapper.WorkUserDeptMapper;
import cn.wisenergy.model.app.WorkUser;
import cn.wisenergy.model.dto.DayWorkTimeAndType;
import cn.wisenergy.model.dto.GetMonthlyCollectParam;
import cn.wisenergy.model.dto.StatisticsTableDto;
import cn.wisenergy.model.enums.ManagerEnum;
import cn.wisenergy.service.StatisticsService;
import cn.wisenergy.service.WorkUserService;
import com.alibaba.excel.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: nh
 * @create: 2021-01-19 17:19
 **/

@Service
@Slf4j
public class StatisticsServiceImpl implements StatisticsService {
    @Autowired
    WorkUserService workUserService;

    @Autowired
    WorkTimeOrderMapper workOrderMapper;

    @Autowired
    WorkUserDeptMapper workUserDeptMapper;

    @Override
    public PageInfo getMonthlyCollect(GetMonthlyCollectParam param) {
        log.info("StatisticsServiceImpl[]getMonthlyCollect[]input.param" + param);
        if (param == null) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        //管理员用户
        WorkUser user = workUserService.getUserById(param.getUserId());
        if (user.getLevel() != ManagerEnum.IS_SYSTEM_MANAGER.getCode()) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.THE_USER_NOT_MANAGER_PLASE_MANAGER_LOGIN);
        }
        //默认时间为当月
        Integer year = param.getYear();
        Integer month = param.getMonth();
        if (StringUtils.isEmpty(param.getYear()) || StringUtils.isEmpty(param.getMonth())) {
            Date now = new Date();
            year = DateUtil.getYear(now);
            month = DateUtil.getMonth(now)+1;
        }
        //默认部门主键为1
        Integer deptId = param.getDeptId();
        if (param.getDeptId() == null) {
            deptId = 1;
        }
        //默认当前页码为1，每页数量为10
        if (param.getCurrentPage() == null) {
            param.setCurrentPage(1);
        }
        if (param.getSize() == null) {
            param.setSize(10);
        }

        //分页获取部门下所有员工主键
        PageHelper.startPage(param.getCurrentPage(), param.getSize());
        List<Integer> userIdList = workUserDeptMapper.listByDeptId(deptId);
        PageInfo pageInfo = new PageInfo(userIdList);
        //获取员工一个月中已审核、自动审核的工单单日汇总汇总
        ArrayList<StatisticsTableDto> statisticsTableDtos = new ArrayList<>();
        for (Integer id : userIdList) {
            WorkUser workUser = workUserService.getUserById(id);
            //获取当月每天工时信息
            List<DayWorkTimeAndType> dayWorkTimeAndTypes = workOrderMapper.listByDateAndUserId(year, month, id);
            //将一天中类型不为请假和调休的工时累加

            //封装成对象
            StatisticsTableDto statisticsTableDto = new StatisticsTableDto();
            statisticsTableDto.setUserId(id);
            statisticsTableDto.setUserName(workUser.getName());
            statisticsTableDto.setDayWorkTimeAndTypeList(dayWorkTimeAndTypes);
            statisticsTableDtos.add(statisticsTableDto);
        }
        pageInfo.setList(statisticsTableDtos);

        return pageInfo;
    }
}
