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

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.module.bpm.api.oa.BpmOAEvectionApi;
import cn.iocoder.yudao.module.bpm.api.oa.BpmOAGoOutApi;
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.util.List;
import java.util.Map;

@Component
@Slf4j
@EnableScheduling
public class FieldworkJob {

    @Resource
    private AdminUserMapper adminUserMapper;

    @Resource
    private BpmOAGoOutApi goOutApi;

    @Resource
    private BpmOAEvectionApi evectionApi;

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

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

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

            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));
            }

            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));
            }
        }catch (Exception ex) {
            log.info(ex.toString());
        }

        // 修改出差人员的外勤打卡权限
        updateEvection();

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

    /**
     * 修改出差人员的外勤打卡权限
     */
    private void updateEvection() {

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

            // 获取用户编号
            Map<String, List<Long>> list = evectionApi.getEvectionUserList().getCheckedData();

            if (CollectionUtil.isNotEmpty(list.get("off"))) {

                // 将昨日出差的用户，关闭外勤打卡权限
                adminUserMapper.update(new AdminUserDO().setFieldworkFlag(0).setFieldworkType(0),
                        new LambdaQueryWrapper<AdminUserDO>()
                                .in(AdminUserDO::getId, list.get("off"))
                                .eq(AdminUserDO::getFieldworkType, 2));
            }

            if (CollectionUtil.isNotEmpty(list.get("on"))) {

                // 讲需要当天出差的用户，设置外勤打卡权限
                adminUserMapper.update(new AdminUserDO().setFieldworkFlag(1).setFieldworkType(2),
                        new LambdaQueryWrapper<AdminUserDO>()
                                .in(AdminUserDO::getId, list.get("on"))
                                .ne(AdminUserDO::getFieldworkType, 1));
            }
        }catch (Exception ex) {
            log.info(ex.toString());
        }
    }
}