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

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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.sundot.airport.common.core.domain.DataPermissionResult;
import com.sundot.airport.common.core.domain.entity.SysUser;
import com.sundot.airport.common.enums.CheckRecordStatusEnum;
import com.sundot.airport.common.enums.SourceTypeEnum;
import com.sundot.airport.system.service.ISysUserService;
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.RequestHeader;
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.CheckRecord;
import com.sundot.airport.check.service.ICheckRecordService;
import com.sundot.airport.common.utils.poi.ExcelUtil;
import com.sundot.airport.common.core.page.TableDataInfo;

/**
 * 检查记录Controller
 *
 * @author ruoyi
 * @date 2025-07-11
 */
@RestController
@RequestMapping("/check/checkRecord")
public class CheckRecordController extends BaseController {
    @Autowired
    private ICheckRecordService checkRecordService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询检查记录列表
     */
    @PreAuthorize("@ss.hasPermi('check:checkRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(CheckRecord checkRecord, @RequestHeader(value = "X-Request-Source", defaultValue = "mobile") String source) {
        DataPermissionResult dataPermission = DataPermissionUtils.getDataPermission(getUserId(), getDeptId(), getLoginUser());
        List<CheckRecord> list;
        if (StrUtil.equals(SourceTypeEnum.mobile.getCode(), source)) {
            startPage();
            list = checkRecordService.selectCheckRecordList(checkRecord);
        } else if (StrUtil.equals(SourceTypeEnum.web.getCode(), source)) {
            switch (dataPermission.getPermissionType()) {
                case ALL:
                case STATION:
                    break;
                case BRIGADE:
                case DEPARTMENT:
                    List<SysUser> sysUserList = sysUserService.selectUserByDeptId(dataPermission.getValue());
                    if (CollUtil.isEmpty(sysUserList)) {
                        return getDataTable(Collections.emptyList());
                    }
                    List<Long> userIdList = sysUserList.stream().map(SysUser::getUserId).distinct().collect(Collectors.toList());
                    checkRecord.setQueryCheckerIdList(userIdList);
                    break;
                default:
                    return getDataTable(Collections.emptyList());
            }
            startPage();
            list = checkRecordService.selectCheckRecordList(checkRecord);
        } else {
            return getDataTable(Collections.emptyList());
        }
        return getDataTable(list);
    }

    /**
     * 导出检查记录列表
     */
    @PreAuthorize("@ss.hasPermi('check:checkRecord:export')")
    @Log(title = "检查记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CheckRecord checkRecord) {
        DataPermissionResult dataPermission = DataPermissionUtils.getDataPermission(getUserId(), getDeptId(), getLoginUser());
        List<CheckRecord> list = Collections.emptyList();
        switch (dataPermission.getPermissionType()) {
            case ALL:
            case STATION:
                list = checkRecordService.selectCheckRecordList(checkRecord);
                break;
            case BRIGADE:
            case DEPARTMENT:
                List<SysUser> sysUserList = sysUserService.selectUserByDeptId(dataPermission.getValue());
                if (CollUtil.isNotEmpty(sysUserList)) {
                    List<Long> userIdList = sysUserList.stream().map(SysUser::getUserId).distinct().collect(Collectors.toList());
                    checkRecord.setQueryCheckerIdList(userIdList);
                    list = checkRecordService.selectCheckRecordList(checkRecord);
                }
                break;
            default:
                break;
        }
        ExcelUtil<CheckRecord> util = new ExcelUtil<CheckRecord>(CheckRecord.class);
        util.exportExcel(response, list, "检查记录数据");
    }

    /**
     * 获取检查记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('check:checkRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(checkRecordService.selectCheckRecordById(id));
    }

    /**
     * 新增检查记录
     */
    @PreAuthorize("@ss.hasPermi('check:checkRecord:add')")
    @Log(title = "检查记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody CheckRecord checkRecord) {
        checkRecord.setCreateBy(getUsername());
        checkRecord.setCheckRecordStatus(CheckRecordStatusEnum.FORMAL.getCode());
        checkRecord.setCheckRecordStatusDesc(CheckRecordStatusEnum.FORMAL.getDesc());
        return toAjax(checkRecordService.insertOrUpdateCheckRecord(checkRecord, true));
    }

    /**
     * 新增草稿检查记录
     */
    @PreAuthorize("@ss.hasPermi('check:checkRecord:add')")
    @Log(title = "草稿检查记录", businessType = BusinessType.INSERT)
    @PostMapping("/draft")
    public AjaxResult draft(@RequestBody CheckRecord checkRecord) {
        checkRecord.setCreateBy(getUsername());
        checkRecord.setCheckRecordStatus(CheckRecordStatusEnum.DRAFT.getCode());
        checkRecord.setCheckRecordStatusDesc(CheckRecordStatusEnum.DRAFT.getDesc());
        return toAjax(checkRecordService.insertOrUpdateCheckRecord(checkRecord, false));
    }

    /**
     * 修改检查记录
     */
    @PreAuthorize("@ss.hasPermi('check:checkRecord:edit')")
    @Log(title = "检查记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CheckRecord checkRecord) {
        checkRecord.setUpdateBy(getUsername());
        return toAjax(checkRecordService.updateCheckRecord(checkRecord));
    }

    /**
     * 删除检查记录
     */
    @PreAuthorize("@ss.hasPermi('check:checkRecord:remove')")
    @Log(title = "检查记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(checkRecordService.deleteCheckRecordByIds(ids));
    }
}
