package com.zt.plat.module.qms.business.bus.controller.admin;

import com.zt.plat.module.qms.business.bus.controller.vo.BusinessHandoverRecordSubExtendRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessHandoverRecordSubPageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessHandoverRecordSubRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessHandoverRecordSubSaveReqVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;

import static com.zt.plat.framework.common.pojo.CommonResult.success;

import com.zt.plat.framework.excel.core.util.ExcelUtils;
import com.zt.plat.framework.security.core.LoginUser;
import com.zt.plat.framework.security.core.util.SecurityFrameworkUtils;
import com.zt.plat.module.qms.business.bus.controller.vo.*;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessHandoverRecordSubDO;
import com.zt.plat.module.qms.business.bus.service.BusinessHandoverRecordSubService;
import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 子样交接记录业务")
@RestController
@RequestMapping("/qms/business-handover-record-sub")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class BusinessHandoverRecordSubController implements BusinessControllerMarker {


    @Resource
    private BusinessHandoverRecordSubService businessHandoverRecordSubService;

    @PostMapping("/create")
    @Operation(summary = "创建子样交接记录业务")
    //@PreAuthorize("@ss.hasPermission('qms:business-handover-record-sub:create')")
    public CommonResult<BusinessHandoverRecordSubRespVO> createBusinessHandoverRecordSub(@Valid @RequestBody BusinessHandoverRecordSubSaveReqVO createReqVO) {
        return success(businessHandoverRecordSubService.createBusinessHandoverRecordSub(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新子样交接记录业务")
    //@PreAuthorize("@ss.hasPermission('qms:business-handover-record-sub:update')")
    public CommonResult<Boolean> updateBusinessHandoverRecordSub(@Valid @RequestBody BusinessHandoverRecordSubSaveReqVO updateReqVO) {
        businessHandoverRecordSubService.updateBusinessHandoverRecordSub(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除子样交接记录业务")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('qms:business-handover-record-sub:delete')")
    public CommonResult<Boolean> deleteBusinessHandoverRecordSub(@RequestParam("id") Long id) {
        businessHandoverRecordSubService.deleteBusinessHandoverRecordSub(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除子样交接记录业务")
                //@PreAuthorize("@ss.hasPermission('qms:business-handover-record-sub:delete')")
    public CommonResult<Boolean> deleteBusinessHandoverRecordSubList(@RequestBody BatchDeleteReqVO req) {
        businessHandoverRecordSubService.deleteBusinessHandoverRecordSubListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得子样交接记录业务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:business-handover-record-sub:query')")
    public CommonResult<BusinessHandoverRecordSubRespVO> getBusinessHandoverRecordSub(@RequestParam("id") Long id) {
        BusinessHandoverRecordSubDO businessHandoverRecordSub = businessHandoverRecordSubService.getBusinessHandoverRecordSub(id);
        return success(BeanUtils.toBean(businessHandoverRecordSub, BusinessHandoverRecordSubRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得子样交接记录业务分页")
    //@PreAuthorize("@ss.hasPermission('qms:business-handover-record-sub:query')")
    public CommonResult<PageResult<BusinessHandoverRecordSubExtendRespVO>> getBusinessHandoverRecordSubPage(@Valid BusinessHandoverRecordSubPageReqVO pageReqVO) {
    	if (pageReqVO.getDeptId() == null) {
    		//当前登录用户
    		LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
    		pageReqVO.setDeptId(loginUser.getVisitDeptId());
		}
        PageResult<BusinessHandoverRecordSubExtendRespVO> pageResult = businessHandoverRecordSubService.getBusinessHandoverRecordSubPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出子样交接记录业务 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:business-handover-record-sub:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBusinessHandoverRecordSubExcel(@Valid BusinessHandoverRecordSubPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessHandoverRecordSubExtendRespVO> list = businessHandoverRecordSubService.getBusinessHandoverRecordSubPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "子样交接记录业务.xls", "数据", BusinessHandoverRecordSubRespVO.class,
                        BeanUtils.toBean(list, BusinessHandoverRecordSubRespVO.class));
    }

}