package com.zt.plat.module.qms.resource.material.controller.admin;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;
import com.zt.plat.framework.excel.core.util.ExcelUtils;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialLifecycleDetailPageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialLifecycleDetailRespVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialLifecycleDetailSaveReqVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialLifecycleDetailDO;
import com.zt.plat.module.qms.resource.material.service.MaterialLifecycleDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 物料通用流程明细")
@RestController
@RequestMapping("/qms/resource/material-lifecycle-detail")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class MaterialLifecycleDetailController implements BusinessControllerMarker {


    @Resource
    private MaterialLifecycleDetailService materialLifecycleDetailService;

//    @PostMapping("/create")
    @Operation(summary = "创建物料通用流程明细")
    @PreAuthorize("@ss.hasPermission('t:material-lifecycle-detail:create')")
    public CommonResult<MaterialLifecycleDetailRespVO> createMaterialLifecycleDetail(@Valid @RequestBody MaterialLifecycleDetailSaveReqVO createReqVO) {
        return success(materialLifecycleDetailService.createMaterialLifecycleDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料通用流程明细")
//    @PreAuthorize("@ss.hasPermission('t:material-lifecycle-detail:update')")
    public CommonResult<Boolean> updateMaterialLifecycleDetail(@Valid @RequestBody MaterialLifecycleDetailSaveReqVO updateReqVO) {
        materialLifecycleDetailService.updateMaterialLifecycleDetail(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-batch")
    @Operation(summary = "批量更新物料通用流程明细")
//    @PreAuthorize("@ss.hasPermission('t:material-lifecycle-detail:update')")
    public CommonResult<Boolean> updateBatchMaterialLifecycleDetail(@Valid @RequestBody List<MaterialLifecycleDetailSaveReqVO> updateReqVOS) {
        materialLifecycleDetailService.updateBatchMaterialLifecycleDetail(updateReqVOS);
        return success(true);
    }

//    @DeleteMapping("/delete")
    @Operation(summary = "删除物料通用流程明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('t:material-lifecycle-detail:delete')")
    public CommonResult<Boolean> deleteMaterialLifecycleDetail(@RequestParam("id") Long id) {
        materialLifecycleDetailService.deleteMaterialLifecycleDetail(id);
        return success(true);
    }

//    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除物料通用流程明细")
                @PreAuthorize("@ss.hasPermission('t:material-lifecycle-detail:delete')")
    public CommonResult<Boolean> deleteMaterialLifecycleDetailList(@RequestBody BatchDeleteReqVO req) {
        materialLifecycleDetailService.deleteMaterialLifecycleDetailListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料通用流程明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('t:material-lifecycle-detail:query')")
    public CommonResult<MaterialLifecycleDetailRespVO> getMaterialLifecycleDetail(@RequestParam("id") Long id) {
        MaterialLifecycleDetailDO materialLifecycleDetail = materialLifecycleDetailService.getMaterialLifecycleDetail(id);
        return success(BeanUtils.toBean(materialLifecycleDetail, MaterialLifecycleDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料通用流程明细分页")
//    @PreAuthorize("@ss.hasPermission('t:material-lifecycle-detail:query')")
    public CommonResult<PageResult<MaterialLifecycleDetailRespVO>> getMaterialLifecycleDetailPage(@Valid MaterialLifecycleDetailPageReqVO pageReqVO) {
        PageResult<MaterialLifecycleDetailDO> pageResult = materialLifecycleDetailService.getMaterialLifecycleDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialLifecycleDetailRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获取未配置的溶液配置申请")
//    @PreAuthorize("@ss.hasPermission('t:material-lifecycle-detail:query')")
    public CommonResult<List<MaterialLifecycleDetailRespVO>> getNotMakeLifecycleDetailList(@Valid MaterialLifecycleDetailPageReqVO reqVO) {
        List<MaterialLifecycleDetailRespVO> detailList = materialLifecycleDetailService.getNotMakeLifecycleDetailList(reqVO);
        return success(detailList);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料通用流程明细 Excel")
    @PreAuthorize("@ss.hasPermission('t:material-lifecycle-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialLifecycleDetailExcel(@Valid MaterialLifecycleDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialLifecycleDetailDO> list = materialLifecycleDetailService.getMaterialLifecycleDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料通用流程明细.xls", "数据", MaterialLifecycleDetailRespVO.class,
                        BeanUtils.toBean(list, MaterialLifecycleDetailRespVO.class));
    }

}