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

import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleHandoverPageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleHandoverRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleHandoverSaveReqVO;
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.BusinessSampleHandoverDO;
import com.zt.plat.module.qms.business.bus.service.BusinessSampleHandoverService;
import com.alibaba.fastjson2.JSONObject;
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-sample-handover")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class BusinessSampleHandoverController {


    @Resource
    private BusinessSampleHandoverService businessSampleHandoverService;

    @PostMapping("/create")
    @Operation(summary = "创建样品交接单业务")
    //@PreAuthorize("@ss.hasPermission('qms:business-sample-handover:create')")
    public CommonResult<BusinessSampleHandoverRespVO> createBusinessSampleHandover(@Valid @RequestBody BusinessSampleHandoverSaveReqVO createReqVO) {
        return success(businessSampleHandoverService.createBusinessSampleHandover(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新样品交接单业务")
    //@PreAuthorize("@ss.hasPermission('qms:business-sample-handover:update')")
    public CommonResult<Boolean> updateBusinessSampleHandover(@Valid @RequestBody BusinessSampleHandoverSaveReqVO updateReqVO) {
        businessSampleHandoverService.updateBusinessSampleHandover(updateReqVO);
        return success(true);
    }

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

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除样品交接单业务")
                //@PreAuthorize("@ss.hasPermission('qms:business-sample-handover:delete')")
    public CommonResult<Boolean> deleteBusinessSampleHandoverList(@RequestBody BatchDeleteReqVO req) {
        businessSampleHandoverService.deleteBusinessSampleHandoverListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得样品交接单业务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:business-sample-handover:query')")
    public CommonResult<BusinessSampleHandoverRespVO> getBusinessSampleHandover(@RequestParam("id") Long id) {
        BusinessSampleHandoverDO businessSampleHandover = businessSampleHandoverService.getBusinessSampleHandover(id);
        return success(BeanUtils.toBean(businessSampleHandover, BusinessSampleHandoverRespVO.class));
    }

    @GetMapping("/reportDetail")
    @Operation(summary = "除样品交接详情数据")
    public CommonResult<?> reportDetail(@RequestParam("id") Long id) {
    	JSONObject jsonObject = businessSampleHandoverService.reportDetail(id);
    	return success(jsonObject);
    	
    }

    @GetMapping("/page")
    @Operation(summary = "获得样品交接单业务分页")
    //@PreAuthorize("@ss.hasPermission('qms:business-sample-handover:query')")
    public CommonResult<PageResult<BusinessSampleHandoverRespVO>> getBusinessSampleHandoverPage(@Valid BusinessSampleHandoverPageReqVO pageReqVO) {
    	if (pageReqVO.getDeptId() == null) {
    		//当前登录用户
    		LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
    		pageReqVO.setDeptId(loginUser.getVisitDeptId());
		}
        PageResult<BusinessSampleHandoverDO> pageResult = businessSampleHandoverService.getBusinessSampleHandoverPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BusinessSampleHandoverRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出样品交接单业务 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:business-sample-handover:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBusinessSampleHandoverExcel(@Valid BusinessSampleHandoverPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessSampleHandoverDO> list = businessSampleHandoverService.getBusinessSampleHandoverPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "样品交接单业务.xls", "数据", BusinessSampleHandoverRespVO.class,
                        BeanUtils.toBean(list, BusinessSampleHandoverRespVO.class));
    }

}