package com.zt.plat.module.qms.common.data.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.common.data.dal.dataobject.DataCollectionDO;
import com.zt.plat.module.qms.common.data.dal.dataobject.DataCollectionFieldDO;
import com.zt.plat.module.qms.common.data.controller.vo.DataCollectionFieldPageReqVO;
import com.zt.plat.module.qms.common.data.controller.vo.DataCollectionFieldRespVO;
import com.zt.plat.module.qms.common.data.controller.vo.DataCollectionFieldSaveReqVO;
import com.zt.plat.module.qms.common.data.service.DataCollectionFieldService;
import com.zt.plat.module.qms.common.data.service.DataCollectionService;
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.util.ObjectUtils;
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/common/data/data-collection-field")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class DataCollectionFieldController implements BusinessControllerMarker {


    @Resource private DataCollectionFieldService dataCollectionFieldService;
    @Resource private DataCollectionService dataCollectionService;


    @GetMapping("/queryEffectiveFields")
    @Operation(summary = "通过数据集Key查询字段列表")
    public CommonResult<List<DataCollectionFieldRespVO>> queryEffectiveFields(@Valid DataCollectionFieldPageReqVO pageReqVO) {
        String dataCollectionKey = pageReqVO.getDataCollectionKey();
        Long dataCollectionId = pageReqVO.getDataCollectionId();
        if(!ObjectUtils.isEmpty(dataCollectionKey)){
            DataCollectionDO dataCollectionDO = dataCollectionService.getLatestDataCollectionByKey(dataCollectionKey);
            if(dataCollectionDO == null)
                return CommonResult.error(0,"数据集字段获取失败，请联系管理员处理：" + dataCollectionKey);
            dataCollectionId = dataCollectionDO.getId();
        }
        if(dataCollectionId == null)
            return CommonResult.error(0,"数据集字段获取失败，缺少参数，请联系管理员处理");
        List<DataCollectionFieldDO> dataCollectionFieldDOList = dataCollectionFieldService.queryEffectiveListByDataCollectionId(dataCollectionId);
        return success(BeanUtils.toBean(dataCollectionFieldDOList, DataCollectionFieldRespVO.class));
    }

    @PostMapping("/create")
    @Operation(summary = "创建数据集字段")
    @PreAuthorize("@ss.hasPermission('qms:data-collection-field:create')")
    public CommonResult<DataCollectionFieldRespVO> createDataCollectionField(@Valid @RequestBody DataCollectionFieldSaveReqVO createReqVO) {
        return success(dataCollectionFieldService.createDataCollectionField(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据集字段")
    @PreAuthorize("@ss.hasPermission('qms:data-collection-field:update')")
    public CommonResult<Boolean> updateDataCollectionField(@Valid @RequestBody DataCollectionFieldSaveReqVO updateReqVO) {
        dataCollectionFieldService.updateDataCollectionField(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据集字段")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:data-collection-field:delete')")
    public CommonResult<Boolean> deleteDataCollectionField(@RequestParam("id") Long id) {
        dataCollectionFieldService.deleteDataCollectionField(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除数据集字段")
                @PreAuthorize("@ss.hasPermission('qms:data-collection-field:delete')")
    public CommonResult<Boolean> deleteDataCollectionFieldList(@RequestBody BatchDeleteReqVO req) {
        dataCollectionFieldService.deleteDataCollectionFieldListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据集字段")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:data-collection-field:query')")
    public CommonResult<DataCollectionFieldRespVO> getDataCollectionField(@RequestParam("id") Long id) {
        DataCollectionFieldDO dataCollectionField = dataCollectionFieldService.getDataCollectionField(id);
        return success(BeanUtils.toBean(dataCollectionField, DataCollectionFieldRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据集字段分页")
    public CommonResult<PageResult<DataCollectionFieldRespVO>> getDataCollectionFieldPage(@Valid DataCollectionFieldPageReqVO pageReqVO) {
        PageResult<DataCollectionFieldDO> pageResult = dataCollectionFieldService.getDataCollectionFieldPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DataCollectionFieldRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据集字段 Excel")
    @PreAuthorize("@ss.hasPermission('qms:data-collection-field:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDataCollectionFieldExcel(@Valid DataCollectionFieldPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DataCollectionFieldDO> list = dataCollectionFieldService.getDataCollectionFieldPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数据集字段.xls", "数据", DataCollectionFieldRespVO.class,
                        BeanUtils.toBean(list, DataCollectionFieldRespVO.class));
    }

}