package cn.iocoder.yudao.module.bpm.api.oa;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAGoOutDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAGoOutService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

/**
 * Flowable 流程实例 Api 实现类
 */
@RestController
@Validated
public class BpmOAGoOutApiImpl implements BpmOAGoOutApi{

    @Resource
    private BpmOAGoOutService goOutService;

    @Override
    @PermitAll
    public CommonResult<Map<String, List<Long>>> getLeaveListByTime() {

        // 定义格式化模式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        List<BpmOAGoOutDO> goOutNow = goOutService.getGoOutByStartTime(LocalDate.now().format(formatter));
        List<BpmOAGoOutDO> goOutYesterday = goOutService.getGoOutByStartTime(LocalDate.now().minusDays(1).format(formatter));

        Map<String, List<Long>> result = new HashMap<>();
        result.put("now", convertList(goOutNow, BpmOAGoOutDO::getUserId));
        result.put("yesterday", convertList(goOutYesterday, BpmOAGoOutDO::getUserId));
        return success(result);
    }
}
