package cn.iocoder.yudao.module.system.job.fieldwork;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.module.bpm.api.oa.BpmOAEntryApi;
import cn.iocoder.yudao.module.bpm.api.oa.BpmOAGoOutApi;
import cn.iocoder.yudao.module.bpm.api.oa.vo.BpmOAEntryDTO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
@EnableScheduling
public class FieldworkJob {

    @Resource
    private AdminUserMapper adminUserMapper;

    @Resource
    private BpmOAGoOutApi goOutApi;

    @Resource
    private BpmOAEntryApi entryApi;

    /**
     * 外勤打卡权限调整， 针对外出申请流程
     */
    @XxlJob("fieldworkJob")
    @TenantJob // --- ⚠️ 这个注解 会将租户列表拉出来 完了后逐个租户执行 定时任务需要注意
    public ReturnT<String> execute() {

        // 外勤打卡权限变更
        try {

            // 获取用户编号
            Map<String, List<Long>> goOutList = goOutApi.getLeaveListByTime().getCheckedData();

            if (CollectionUtil.isNotEmpty(goOutList.get("now"))) {

                // 讲需要当天外出的用户，设置外勤打卡权限
                adminUserMapper.update(new AdminUserDO().setFieldworkFlag(1).setFieldworkType(2),
                        new LambdaQueryWrapper<AdminUserDO>()
                                .in(AdminUserDO::getId, goOutList.get("now"))
                                .ne(AdminUserDO::getFieldworkType, 1));
            }

            if (CollectionUtil.isNotEmpty(goOutList.get("yesterday"))) {

                // 将昨日外出的用户，关闭外勤打卡权限
                adminUserMapper.update(new AdminUserDO().setFieldworkFlag(0).setFieldworkType(0),
                        new LambdaQueryWrapper<AdminUserDO>()
                                .in(AdminUserDO::getId, goOutList.get("yesterday"))
                                .eq(AdminUserDO::getFieldworkType, 2));
            }
        }catch (Exception ex) {
            log.info(ex.toString());
        }

        // 修改的入职日期为当天的用户状态
        updEntryUser();

        // 返回执行成功
        return ReturnT.SUCCESS;
    }

    /**
     * 修改的入职日期为当天的用户状态
     */
    private void updEntryUser() {

        LocalDate now = LocalDate.now();
        // 获得入职信息
        List<BpmOAEntryDTO> entryDTOS = entryApi.getEntryListByTime(now).getCheckedData();

        if (CollectionUtil.isNotEmpty(entryDTOS)) {
            // 开启入职用户状态
            List<AdminUserDO> userDOS = entryDTOS.stream().map(entry -> new AdminUserDO()
                    .setId(entry.getEntryUserId())
                    .setStatus(CommonStatusEnum.ENABLE.getStatus())).collect(Collectors.toList());

            adminUserMapper.updateBatch(userDOS);
        }
    }
}