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

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

import cn.hutool.core.collection.CollUtil;
import com.sundot.airport.common.core.domain.ApprovalWorkflowBatchDto;
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.check.domain.CheckCorrection;
import com.sundot.airport.check.service.ICheckCorrectionService;
import com.sundot.airport.common.utils.poi.ExcelUtil;
import com.sundot.airport.common.core.page.TableDataInfo;

/**
 * 问题整改Controller
 *
 * @author ruoyi
 * @date 2025-09-08
 */
@RestController
@RequestMapping("/check/checkCorrection")
public class CheckCorrectionController extends BaseController {
    @Autowired
    private ICheckCorrectionService checkCorrectionService;

    /**
     * 查询问题整改列表
     */
    @PreAuthorize("@ss.hasPermi('check:checkCorrection:list')")
    @GetMapping("/list")
    public TableDataInfo list(CheckCorrection checkCorrection) {
        DataPermissionResult dataPermission = DataPermissionUtils.getDataPermission(getUserId(), getDeptId(), getLoginUser());
        switch (dataPermission.getPermissionType()) {
            case BRIGADE:
                checkCorrection.setCheckedBrigadeId(dataPermission.getValue());
                break;
            case DEPARTMENT:
                checkCorrection.setCheckedDepartmentId(dataPermission.getValue());
                break;
            case TEAM:
            case SELF:
                return getDataTable(Collections.emptyList());
            default:
                break;
        }
        startPage();
        List<CheckCorrection> list = checkCorrectionService.selectCheckCorrectionList(checkCorrection);
        return getDataTable(list);
    }

    /**
     * 导出问题整改列表
     */
    @PreAuthorize("@ss.hasPermi('check:checkCorrection:export')")
    @Log(title = "问题整改", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CheckCorrection checkCorrection) {
        List<CheckCorrection> list = Collections.emptyList();
        DataPermissionResult dataPermission = DataPermissionUtils.getDataPermission(getUserId(), getDeptId(), getLoginUser());
        switch (dataPermission.getPermissionType()) {
            case ALL:
            case STATION:
                list = checkCorrectionService.selectCheckCorrectionList(checkCorrection);
                break;
            case BRIGADE:
                checkCorrection.setCheckedBrigadeId(dataPermission.getValue());
                list = checkCorrectionService.selectCheckCorrectionList(checkCorrection);
                break;
            case DEPARTMENT:
                checkCorrection.setCheckedDepartmentId(dataPermission.getValue());
                list = checkCorrectionService.selectCheckCorrectionList(checkCorrection);
                break;
            default:
                break;
        }
        ExcelUtil<CheckCorrection> util = new ExcelUtil<CheckCorrection>(CheckCorrection.class);
        util.exportExcel(response, list, "问题整改数据");
    }

    /**
     * 获取问题整改详细信息
     */
    @PreAuthorize("@ss.hasPermi('check:checkCorrection:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(checkCorrectionService.selectCheckCorrectionById(id));
    }

    /**
     * 新增问题整改
     */
    @PreAuthorize("@ss.hasPermi('check:checkCorrection:add')")
    @Log(title = "问题整改", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CheckCorrection checkCorrection) {
        checkCorrection.setCreateBy(getUsername());
        return toAjax(checkCorrectionService.insertCheckCorrection(checkCorrection));
    }

    /**
     * 修改问题整改
     */
    @PreAuthorize("@ss.hasPermi('check:checkCorrection:edit')")
    @Log(title = "问题整改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CheckCorrection checkCorrection) {
        checkCorrection.setUpdateBy(getUsername());
        if (CollUtil.isNotEmpty(checkCorrection.getCheckProjectItemList())) {
            checkCorrection.getCheckProjectItemList().forEach(checkProjectItem -> {
                checkProjectItem.setUpdateBy(getUsername());
                if (CollUtil.isNotEmpty(checkProjectItem.getCheckUserList())) {
                    checkProjectItem.getCheckUserList().forEach(checkUser -> {
                        checkUser.setUpdateBy(getUsername());
                    });
                }
            });
        }
        if (CollUtil.isNotEmpty(checkCorrection.getBaseAttachmentList())) {
            checkCorrection.getBaseAttachmentList().forEach(baseAttachment -> {
                baseAttachment.setUpdateBy(getUsername());
            });
        }
        if (CollUtil.isNotEmpty(checkCorrection.getCheckRecordBaseAttachmentList())) {
            checkCorrection.getCheckRecordBaseAttachmentList().forEach(baseAttachment -> {
                baseAttachment.setUpdateBy(getUsername());
            });
        }
        return toAjax(checkCorrectionService.updateCheckCorrection(checkCorrection));
    }

    /**
     * 删除问题整改
     */
    @PreAuthorize("@ss.hasPermi('check:checkCorrection:remove')")
    @Log(title = "问题整改", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(checkCorrectionService.deleteCheckCorrectionByIds(ids));
    }

    /**
     * 审批任务（同意）
     */
    @PreAuthorize("@ss.hasPermi('check:checkCorrection:edit')")
    @Log(title = "问题整改", businessType = BusinessType.UPDATE)
    @PostMapping("/approveTask")
    public AjaxResult approveTask(@RequestBody CheckCorrection checkCorrection) {
        return success(checkCorrectionService.approveTask(checkCorrection));
    }

    /**
     * 审批任务（驳回）
     */
    @PreAuthorize("@ss.hasPermi('check:checkCorrection:edit')")
    @Log(title = "问题整改", businessType = BusinessType.UPDATE)
    @PostMapping("/rejectTask")
    public AjaxResult rejectTask(@RequestBody CheckCorrection checkCorrection) {
        return success(checkCorrectionService.rejectTask(checkCorrection));
    }

    /**
     * 批量审批任务（同意）
     */
    @PreAuthorize("@ss.hasPermi('check:checkCorrection:edit')")
    @Log(title = "批量审批任务（同意）", businessType = BusinessType.UPDATE)
    @PostMapping("/approveTaskBatch")
    public AjaxResult approveTaskBatch(@RequestBody ApprovalWorkflowBatchDto approvalWorkflowBatchDto) {
        return success(checkCorrectionService.approveTaskBatch(approvalWorkflowBatchDto));
    }

    /**
     * 批量审批任务（驳回）
     */
    @PreAuthorize("@ss.hasPermi('check:checkCorrection:edit')")
    @Log(title = "批量审批任务（驳回）", businessType = BusinessType.UPDATE)
    @PostMapping("/rejectTaskBatch")
    public AjaxResult rejectTaskBatch(@RequestBody ApprovalWorkflowBatchDto approvalWorkflowBatchDto) {
        return success(checkCorrectionService.rejectTaskBatch(approvalWorkflowBatchDto));
    }
}
