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.*;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialUseRecordDO;
import com.zt.plat.module.qms.resource.material.service.MaterialUseRecordDetailService;
import com.zt.plat.module.qms.resource.material.service.MaterialUseRecordService;
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.beans.factory.annotation.Autowired;
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-use-record")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class MaterialUseRecordController implements BusinessControllerMarker {


    @Resource
    private MaterialUseRecordService materialUseRecordService;
    @Autowired
    private MaterialUseRecordDetailService materialUseRecordDetailService;

    @PostMapping("/add")
    @Operation(summary = "新增使用记录")
//    @PreAuthorize("@ss.hasPermission('qms:material-use-record:create')")
    public CommonResult<MaterialUseRecordRespVO> createMaterialUseRecord(@Valid @RequestBody MaterialUseRecordSaveReqVO createReqVO) {
        return success(materialUseRecordService.createMaterialUseRecord(createReqVO));
    }

    @PostMapping("/hzrd-make")
    @Operation(summary = "配置危化品")
//    @PreAuthorize("@ss.hasPermission('qms:material-use-record:create')")
    public CommonResult<MaterialInfomationRespVO> makeHazardousMaterial(@Valid @RequestBody MaterialHazardousMakeSaveReqVO makeReqVO) {
        return success(materialUseRecordDetailService.makeHazardousMaterial(makeReqVO));
    }

//    @PutMapping("/update")
    @Operation(summary = "更新使用记录")
    @PreAuthorize("@ss.hasPermission('qms:material-use-record:update')")
    public CommonResult<Boolean> updateMaterialUseRecord(@Valid @RequestBody MaterialUseRecordSaveReqVO updateReqVO) {
        materialUseRecordService.updateMaterialUseRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除使用记录")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('qms:material-use-record:delete')")
    public CommonResult<Boolean> deleteMaterialUseRecord(@RequestParam("id") Long id) {
        materialUseRecordService.deleteMaterialUseRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除使用记录")
                @PreAuthorize("@ss.hasPermission('qms:material-use-record:delete')")
    public CommonResult<Boolean> deleteMaterialUseRecordList(@RequestBody BatchDeleteReqVO req) {
        materialUseRecordService.deleteMaterialUseRecordListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得使用记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('qms:material-use-record:query')")
    public CommonResult<MaterialUseRecordRespVO> getMaterialUseRecord(@RequestParam("id") Long id) {
        MaterialUseRecordDO materialUseRecord = materialUseRecordService.getMaterialUseRecord(id);
        return success(BeanUtils.toBean(materialUseRecord, MaterialUseRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得使用记录分页")
//    @PreAuthorize("@ss.hasPermission('qms:material-use-record:query')")
    public CommonResult<PageResult<MaterialUseRecordRespVO>> getMaterialUseRecordPage(@Valid MaterialUseRecordPageReqVO pageReqVO) {
        PageResult<MaterialUseRecordRespVO> pageResult = materialUseRecordService.getMaterialUseRecordPageWithInf(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出使用记录 Excel")
    @PreAuthorize("@ss.hasPermission('qms:material-use-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialUseRecordExcel(@Valid MaterialUseRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialUseRecordDO> list = materialUseRecordService.getMaterialUseRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "使用记录.xls", "数据", MaterialUseRecordRespVO.class,
                        BeanUtils.toBean(list, MaterialUseRecordRespVO.class));
    }


    @PutMapping("/review")
    @Operation(summary = "监督确认")
    public CommonResult<Boolean> superviseReviews(@RequestBody List<Long> ids) {
        return success(materialUseRecordService.superviseReviews(ids));
    }

}