package jnpf.attendance.controller;

import io.swagger.v3.oas.annotations.Operation;
import jnpf.attendance.service.AttendanceGroupService;
import jnpf.attendance.service.AttendanceLineSchedulingConfigService;
import jnpf.base.ActionResult;
import jnpf.entity.AttendanceGroup;
import jnpf.entity.attendance.FtbAttendanceLineSchedulingConfig;
import jnpf.permission.V2PositionApi;
import jnpf.permission.dto.v2.position.QueryBasePositionBatchDTO;
import jnpf.permission.vo.v2.position.PositionBaseInfoVO;
import jnpf.util.StringUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 划线排班配置控制器
 *
 * @author ahua
 * @version 2.1
 * @copyright 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * @date 2025-12-30
 */
@RestController
@RequestMapping(value = "/arranging/line/scheduling/config")
public class AttendanceLineSchedulingConfigController {

    @Resource
    private AttendanceLineSchedulingConfigService attendanceLineSchedulingConfigService;

    @Resource
    private V2PositionApi v2PositionApi;

    @Resource
    private AttendanceGroupService attendanceGroupService;

    /**
     * 根据考勤组ID获取划线排班配置详情
     *
     * @param groupId 考勤组ID
     * @return 划线排班配置详情
     */
    @GetMapping("detail")
    public ActionResult<FtbAttendanceLineSchedulingConfig> detail(@RequestParam(value = "groupId") String groupId) {
        FtbAttendanceLineSchedulingConfig config = attendanceLineSchedulingConfigService.getByGroupId(groupId);
        return ActionResult.success(config);
    }
    /**
     * 划线排班未排班通知
     *
     * @param tenantId 租户id
     * @return 划线排班配置详情
     */
    @GetMapping("noticeLineScheduling")
    public ActionResult noticeLineScheduling(@RequestParam(value = "tenantId") String tenantId) {
        attendanceLineSchedulingConfigService.noticeLineScheduling(tenantId);
        return ActionResult.success();
    }
    /**
     * 保存或更新划线排班配置
     *
     * @param config 划线排班配置
     * @return 保存结果
     */
    @PostMapping("saveOrUpdate")
    public ActionResult saveOrUpdate(@RequestBody FtbAttendanceLineSchedulingConfig config) {
        boolean result = attendanceLineSchedulingConfigService.saveOrUpdateLineSchedulingConfig(config);
        return ActionResult.success(result);
    }

    /**
     * 根据考勤组ID获取岗位基础信息列表
     *
     * @param groupId 考勤组ID
     * @return 岗位基础信息列表
     */
    @Operation(summary = "[列表]根据考勤组ID获取岗位基础信息列表")
    @GetMapping("/position/base/list")
    public ActionResult<List<PositionBaseInfoVO>> listPositionBaseInfo(@RequestParam(value = "groupId") String groupId) {
        // 验证groupId
        if (StringUtil.isBlank(groupId)) {
            return ActionResult.fail("考勤组ID不能为空");
        }

        // 查询AttendanceGroup
        AttendanceGroup attendanceGroup = attendanceGroupService.queryByGroupId(groupId);
        if (attendanceGroup == null) {
            return ActionResult.fail("未找到指定的考勤组");
        }

        // 获取orgId
        String orgId = attendanceGroup.getOrgId();
        if (StringUtil.isBlank(orgId)) {
            return ActionResult.fail("考勤组未绑定组织");
        }

        // 创建DTO并设置参数
        QueryBasePositionBatchDTO dto = new QueryBasePositionBatchDTO();
        dto.setOrganizeIds(Collections.singletonList(orgId));

        // 调用Feign客户端获取岗位基础信息
        return v2PositionApi.listPositionBaseInfo(dto);
    }
}