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

import com.zt.plat.module.qms.business.config.controller.vo.MaterialAssayStandardDetailPageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.MaterialAssayStandardDetailRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.MaterialAssayStandardDetailSaveReqVO;
import com.zt.plat.module.qms.business.config.dal.dataobject.MaterialAssayStandardDO;
import com.zt.plat.module.qms.business.config.service.MaterialAssayStandardService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.apilog.core.annotation.ApiAccessLog;
import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.*;

import com.zt.plat.module.qms.business.config.dal.dataobject.MaterialAssayStandardDetailDO;
import com.zt.plat.module.qms.business.config.service.MaterialAssayStandardDetailService;

@Tag(name = "管理后台 - 物料检测标准检测项目")
@RestController
@RequestMapping("/qms/material-assay-standard-detail")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class MaterialAssayStandardDetailController implements BusinessControllerMarker {


    @Resource private MaterialAssayStandardDetailService materialAssayStandardDetailService;
    @Resource private MaterialAssayStandardService materialAssayStandardService;


    @GetMapping("/queryGroupByaAssayCategory")
    @Operation(summary = "按检测分类分组查询")
    @Parameter(name = "id", description = "按检测分类分组查询", required = true, example = "1024")
    public CommonResult<JSONArray> queryGroupByaAssayCategory(@RequestParam("baseSampleID") Long baseSampleID) {

        List<MaterialAssayStandardDO> standardList = materialAssayStandardService.queryByBaseSampleID(baseSampleID);
        List<Long> standardIds = standardList.stream().map(MaterialAssayStandardDO::getId).toList();
        if(standardIds.isEmpty())
            return success(new JSONArray());
        MaterialAssayStandardDetailPageReqVO params = new MaterialAssayStandardDetailPageReqVO();
        params.setStandardIdList(standardIds);
        List<MaterialAssayStandardDetailRespVO> list = materialAssayStandardDetailService.selectListWithExtend(params);
        JSONArray jsonArray = new JSONArray();
        List<String> assayCategoryList = new ArrayList<>();
        assayCategoryList = list.stream().map(MaterialAssayStandardDetailRespVO::getAssayCategory).distinct().toList();
        for (String assayCategory : assayCategoryList) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("assayCategory", assayCategory);
            jsonObject.put("assayCategoryName", list.stream().filter(item ->
                    item.getAssayCategory().equals(assayCategory)).map(MaterialAssayStandardDetailRespVO::getAssayCategoryName).findFirst().get());
            jsonObject.put("list", list.stream().filter(item -> item.getAssayCategory().equals(assayCategory)).toList());
            jsonArray.add(jsonObject);
        }
        return success(jsonArray);
    }

    @GetMapping("/getByStandardId")
    @Operation(summary = "获得物料检测标准检测项目分页")
    //@PreAuthorize("@ss.hasPermission('qms:material-assay-standard-detail:query')")
    public CommonResult<List<MaterialAssayStandardDetailRespVO>> getByStandardId(@Valid MaterialAssayStandardDetailPageReqVO pageReqVO) {
        List<MaterialAssayStandardDetailRespVO> list = materialAssayStandardDetailService.getByStandardId(pageReqVO.getMaterialAssayStandardId());
        return success(list);
    }

    @PostMapping("/create")
    @Operation(summary = "创建物料检测标准检测项目")
    //@PreAuthorize("@ss.hasPermission('qms:material-assay-standard-detail:create')")
    public CommonResult<MaterialAssayStandardDetailRespVO> createMaterialAssayStandardDetail(@Valid @RequestBody MaterialAssayStandardDetailSaveReqVO createReqVO) {
        return success(materialAssayStandardDetailService.createMaterialAssayStandardDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料检测标准检测项目")
    //@PreAuthorize("@ss.hasPermission('qms:material-assay-standard-detail:update')")
    public CommonResult<Boolean> updateMaterialAssayStandardDetail(@Valid @RequestBody MaterialAssayStandardDetailSaveReqVO updateReqVO) {
        materialAssayStandardDetailService.updateMaterialAssayStandardDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料检测标准检测项目")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('qms:material-assay-standard-detail:delete')")
    public CommonResult<Boolean> deleteMaterialAssayStandardDetail(@RequestParam("id") Long id) {
        materialAssayStandardDetailService.deleteMaterialAssayStandardDetail(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除物料检测标准检测项目")
                //@PreAuthorize("@ss.hasPermission('qms:material-assay-standard-detail:delete')")
    public CommonResult<Boolean> deleteMaterialAssayStandardDetailList(@RequestBody BatchDeleteReqVO req) {
        materialAssayStandardDetailService.deleteMaterialAssayStandardDetailListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料检测标准检测项目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:material-assay-standard-detail:query')")
    public CommonResult<MaterialAssayStandardDetailRespVO> getMaterialAssayStandardDetail(@RequestParam("id") Long id) {
        MaterialAssayStandardDetailDO materialAssayStandardDetail = materialAssayStandardDetailService.getMaterialAssayStandardDetail(id);
        return success(BeanUtils.toBean(materialAssayStandardDetail, MaterialAssayStandardDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料检测标准检测项目分页")
    //@PreAuthorize("@ss.hasPermission('qms:material-assay-standard-detail:query')")
    public CommonResult<PageResult<MaterialAssayStandardDetailRespVO>> getMaterialAssayStandardDetailPage(@Valid MaterialAssayStandardDetailPageReqVO pageReqVO) {
        PageResult<MaterialAssayStandardDetailDO> pageResult = materialAssayStandardDetailService.getMaterialAssayStandardDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialAssayStandardDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料检测标准检测项目 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:material-assay-standard-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialAssayStandardDetailExcel(@Valid MaterialAssayStandardDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialAssayStandardDetailDO> list = materialAssayStandardDetailService.getMaterialAssayStandardDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料检测标准检测项目.xls", "数据", MaterialAssayStandardDetailRespVO.class,
                        BeanUtils.toBean(list, MaterialAssayStandardDetailRespVO.class));
    }

}