package com.cf.imes.module.executor.controller.admin.veneer;

import com.alibaba.excel.EasyExcelFactory;
import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.pojo.PageParam;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.excel.core.util.ExcelUtils;
import com.cf.imes.framework.operatelog.core.annotations.OperateLog;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerClassificationNameRespVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerClassificationNameSaveReqVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerDefectSaveReqVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerImportExcelVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerPageReqVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerRespVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerSaveReqVO;
import com.cf.imes.module.executor.service.veneer.VeneerExcelListener;
import com.cf.imes.module.executor.service.veneer.VeneerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

import static com.cf.imes.framework.common.pojo.CommonResult.success;
import static com.cf.imes.module.executor.enums.ErrorCodeConstants.VENEER_PIC_EXCEED_SIZE;
import static com.cf.imes.framework.operatelog.core.enums.OperateTypeEnum.CREATE;
import static com.cf.imes.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static com.cf.imes.framework.operatelog.core.enums.OperateTypeEnum.IMPORT;

/**
 *
 *
 * @author Gqr
 * @since 2026/1/4 14:26
 */
@Tag(name = "生产管理 - 贴皮库管理")
@RestController
@RequestMapping("/executor/veneer")
@Validated
public class VeneerController {

    private static final long VENEER_PIC_MAX_SIZE = 5L * 1024 * 1024;

    @Resource
    private VeneerService veneerService;

    @GetMapping("/page")
    @Operation(summary = "分页查询")
    public CommonResult<PageResult<VeneerRespVO>> getVeneerPage(@Valid VeneerPageReqVO pageReqVO) {
        return success(veneerService.getPage(pageReqVO));
    }

    @GetMapping("/export")
    @Operation(summary = "导出贴皮明细")
    @OperateLog(type = EXPORT)
    public void exportVeneerPage(@Valid VeneerPageReqVO pageReqVO,
                                       HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VeneerRespVO> list = veneerService.getPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "贴皮明细.xls", "数据", VeneerRespVO.class, list);
    }

    @GetMapping("/import/template")
    @Operation(summary = "下载贴皮导入模版")
    @OperateLog(type = EXPORT)
    public void getVeneerImportTemplate(HttpServletResponse response) {
        veneerService.importTemplateDownload(response);
    }

    @PostMapping("/import")
    @Operation(summary = "导入贴皮")
    @OperateLog(type = IMPORT)
    public void importExcelVeneers(@RequestPart("file") MultipartFile file) throws IOException {
        VeneerExcelListener listener = new VeneerExcelListener(veneerService);
        EasyExcelFactory.read(file.getInputStream(), VeneerImportExcelVO.class, listener).headRowNumber(1).sheet().doRead();
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据id获取贴皮")
    public CommonResult<VeneerRespVO> getVeneer(@PathVariable Long id) {
        return success(veneerService.get(id));
    }

    @PostMapping("")
    @Operation(summary = "创建贴皮")
    public CommonResult<Long> createVeneer(@Valid @RequestBody VeneerSaveReqVO reqVO) {
        return success(veneerService.create(reqVO));
    }

    @PutMapping("")
    @Operation(summary = "编辑贴皮")
    public CommonResult<Boolean> updateVeneer(@Valid @RequestBody VeneerSaveReqVO reqVO) {
        veneerService.update(reqVO);
        return success(true);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除贴皮")
    public CommonResult<Boolean> deleteVeneer(@PathVariable Long id) {
        veneerService.delete(id);
        return success(true);
    }

    @PostMapping("/pic")
    @Operation(summary = "上传贴皮照片")
    @OperateLog(logArgs = false, type = CREATE)
    public CommonResult<Long> uploadVeneerPic(@RequestParam(value = "id", required = false) Long id,
                                              @RequestPart(value = "file") MultipartFile file) throws IOException {
        if (file.getSize() > VENEER_PIC_MAX_SIZE) {
            throw new ServiceException(VENEER_PIC_EXCEED_SIZE);
        }
        return success(veneerService.uploadPic(id, file));
    }

    @PostMapping("/defect")
    @Operation(summary = "保存瑕疵数据")
    public CommonResult<Boolean> saveDefect(@Valid @RequestBody VeneerDefectSaveReqVO reqVO) {
        veneerService.saveDefect(reqVO);
        return success(true);
    }

    @PostMapping("/classificationName")
    @Operation(summary = "创建分类名称")
    public CommonResult<Long> createClassificationName(@Valid @RequestBody VeneerClassificationNameSaveReqVO saveReqVO) {
        return success(veneerService.addClassificationName(saveReqVO));
    }

    @DeleteMapping("/classificationName/{id}")
    @Operation(summary = "删除分类名称")
    public CommonResult<Boolean> deleteClassificationName(@PathVariable Long id) {
        veneerService.deleteClassificationName(id);
        return success(true);
    }

    @GetMapping("/org/classificationName")
    @Operation(summary = "获取分类名称列表")
    public CommonResult<List<VeneerClassificationNameRespVO>> getOrgVeneerClassificationNameList() {
        return success(veneerService.getOrgVeneerClassificationNameList());
    }
}
