package com.sundot.airport.web.controller.attendance;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sundot.airport.common.core.domain.DataPermissionResult;
import com.sundot.airport.web.core.utils.DataPermissionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sundot.airport.common.annotation.Log;
import com.sundot.airport.common.core.controller.BaseController;
import com.sundot.airport.common.core.domain.AjaxResult;
import com.sundot.airport.common.enums.BusinessType;
import com.sundot.airport.attendance.domain.AttendanceTeamUserRecord;
import com.sundot.airport.attendance.service.IAttendanceTeamUserRecordService;
import com.sundot.airport.common.utils.poi.ExcelUtil;
import com.sundot.airport.common.core.page.TableDataInfo;

/**
 * 考勤班组成员Controller
 *
 * @author ruoyi
 * @date 2025-09-06
 */
@RestController
@RequestMapping("/attendance/record")
public class AttendanceTeamUserRecordController extends BaseController {
    @Autowired
    private IAttendanceTeamUserRecordService attendanceTeamUserRecordService;

    /**
     * 查询考勤班组成员列表
     *
     * @param attendanceTeamUserRecord
     * @return
     */
    @PreAuthorize("@ss.hasPermi('attendance:record:list')")
    @GetMapping("/pageList")
    public TableDataInfo pageList(AttendanceTeamUserRecord attendanceTeamUserRecord) {
        startPage();
        List<AttendanceTeamUserRecord> list = attendanceTeamUserRecordService.selectAttendanceTeamUserRecordPageList(attendanceTeamUserRecord);
        return getDataTable(list);
    }

    /**
     * 上岗记录中查询考勤班组成员列表
     */
    @PreAuthorize("@ss.hasPermi('attendance:record:list')")
    @GetMapping("/list")
    public AjaxResult list(AttendanceTeamUserRecord attendanceTeamUserRecord) {
        DataPermissionResult dataPermission = DataPermissionUtils.getDataPermission(getUserId(), getDeptId(), getLoginUser());
        switch (dataPermission.getPermissionType()) {
            case TEAM:
                attendanceTeamUserRecord.setCreateBy(getUsername());
                break;
            default:
                break;
        }
        attendanceTeamUserRecord.setCheckInType("1");
        List<AttendanceTeamUserRecord> list = attendanceTeamUserRecordService.selectAttendanceTeamUserRecordList(attendanceTeamUserRecord, true);
        return success(list);
    }

    /**
     * 导出考勤班组成员列表
     */
    @PreAuthorize("@ss.hasPermi('attendance:record:export')")
    @Log(title = "考勤班组成员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AttendanceTeamUserRecord attendanceTeamUserRecord) {
        if (attendanceTeamUserRecord == null) {
            attendanceTeamUserRecord = new AttendanceTeamUserRecord();
        }
        DataPermissionResult dataPermission = DataPermissionUtils.getDataPermission(getUserId(), getDeptId(), getLoginUser());
        switch (dataPermission.getPermissionType()) {
            case TEAM:
                attendanceTeamUserRecord.setCreateBy(getUsername());
                break;
            default:
                break;
        }
        attendanceTeamUserRecord.setCheckInType("1");
        List<AttendanceTeamUserRecord> list = attendanceTeamUserRecordService.selectAttendanceTeamUserRecordList(attendanceTeamUserRecord, true);
        ExcelUtil<AttendanceTeamUserRecord> util = new ExcelUtil<AttendanceTeamUserRecord>(AttendanceTeamUserRecord.class);
        util.exportExcel(response, list, "考勤班组成员数据");
    }

    /**
     * 获取考勤班组成员详细信息
     */
    @PreAuthorize("@ss.hasPermi('attendance:record:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId, @PathVariable("attendanceDate") Date attendanceDate) {
        return success(attendanceTeamUserRecordService.selectAttendanceTeamUserRecordByUserId(userId, attendanceDate, null));
    }

    /**
     * 新增考勤班组成员
     */
    @PreAuthorize("@ss.hasPermi('attendance:record:add')")
    @Log(title = "考勤班组成员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AttendanceTeamUserRecord attendanceTeamUserRecord) {
        return toAjax(attendanceTeamUserRecordService.insertAttendanceTeamUserRecord(attendanceTeamUserRecord));
    }

    /**
     * 修改考勤班组成员
     */
    @PreAuthorize("@ss.hasPermi('attendance:record:edit')")
    @Log(title = "考勤班组成员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AttendanceTeamUserRecord attendanceTeamUserRecord) {
        return toAjax(attendanceTeamUserRecordService.updateAttendanceTeamUserRecord(attendanceTeamUserRecord));
    }

    /**
     * 删除考勤班组成员
     */
    @PreAuthorize("@ss.hasPermi('attendance:record:remove')")
    @Log(title = "考勤班组成员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds) {
        return toAjax(attendanceTeamUserRecordService.deleteAttendanceTeamUserRecordByUserIds(userIds));
    }

    /**
     * 批量新增考勤班组成员
     *
     * @param attendanceTeamUserRecord
     * @return
     */
    @PreAuthorize("@ss.hasPermi('attendance:record:add:list')")
    @Log(title = "考勤班组成员", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add/list")
    public AjaxResult addList(@RequestBody List<AttendanceTeamUserRecord> attendanceTeamUserRecord) {
        return toAjax(attendanceTeamUserRecordService.insertAttendanceTeamUserRecordList(attendanceTeamUserRecord));
    }

    /**
     * 获取考勤班组成员列表
     */
    @GetMapping("/list/teamLeader")
    public AjaxResult listTeamLeader() {
        return success(attendanceTeamUserRecordService.selectAttendanceTeamLeaderId());
    }

    /**
     * 根据主键删除考勤班组成员
     */
    @PreAuthorize("@ss.hasPermi('attendance:record:remove')")
    @Log(title = "考勤班组成员", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeBy/{ids}")
    public AjaxResult removeBy(@PathVariable Long[] ids) {
        return toAjax(attendanceTeamUserRecordService.deleteAttendanceTeamUserRecordByIds(ids));
    }
}
