package com.youlai.boot.system.controller;

import com.alibaba.excel.EasyExcel;
import com.youlai.boot.system.model.dto.UserExportDTO;
import com.youlai.boot.system.service.MineDemandService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.MineDemandForm;
import com.youlai.boot.system.model.query.MineDemandQuery;
import com.youlai.boot.system.model.vo.MineDemandVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 煤矿需求管理前端控制层
 *
 * @author youlaitech
 * @since 2025-07-23 14:40
 */
@Tag(name = "煤矿需求管理接口")
@RestController
@RequestMapping("/api/v1/mineDemands")
@RequiredArgsConstructor
public class MineDemandController  {

    private final MineDemandService mineDemandService;

    @Operation(summary = "煤矿需求管理分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('system:mineDemand:query')")
    public PageResult<MineDemandVO> getMineDemandPage(MineDemandQuery queryParams) {

        IPage<MineDemandVO> result = mineDemandService.getMineDemandPage(queryParams);
        return PageResult.success(result);
    }


    @GetMapping("/export")
    public void exportMineDemand(MineDemandQuery queryParams, HttpServletResponse response) throws IOException {
        IPage<MineDemandVO> result = mineDemandService.getMineDemandPage(queryParams);

        for (MineDemandVO record : result.getRecords()) {

        }

        String fileName = "需求列表.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));

        EasyExcel.write(response.getOutputStream(), MineDemandVO.class).sheet("用户列表")
                .doWrite(result.getRecords());

    }

    @Operation(summary = "新增煤矿需求管理")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:mineDemand:add')")
    public Result<Void> saveMineDemand(@RequestBody @Valid MineDemandForm formData ) {
        boolean result = mineDemandService.saveMineDemand(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取煤矿需求管理表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:mineDemand:edit')")
    public Result<MineDemandForm> getMineDemandForm(
        @Parameter(description = "煤矿需求管理ID") @PathVariable Long id
    ) {
        MineDemandForm formData = mineDemandService.getMineDemandFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改煤矿需求管理")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('system:mineDemand:edit')")
    public Result<Void> updateMineDemand(
            @Parameter(description = "煤矿需求管理ID") @PathVariable Long id,
            @RequestBody @Validated MineDemandForm formData
    ) {
        boolean result = mineDemandService.updateMineDemand(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除煤矿需求管理")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:mineDemand:delete')")
    public Result<Void> deleteMineDemands(
        @Parameter(description = "煤矿需求管理ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = mineDemandService.deleteMineDemands(ids);
        return Result.judge(result);
    }
}
