package com.zt.plat.module.base.controller.admin.businessindicator;

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 com.zt.plat.framework.business.annotation.FileUploadController;
import com.zt.plat.framework.business.controller.AbstractFileUploadController;

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 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.base.controller.admin.businessindicator.vo.*;
import com.zt.plat.module.base.dal.dataobject.businessindicator.BusinessIndicatorDO;
import com.zt.plat.module.base.service.businessindicator.BusinessIndicatorService;

@Tag(name = "管理后台 - 经营指标")
@RestController
@RequestMapping("/base/business-indicator")
@Validated
@FileUploadController(source = "base.businessindicator")
public class BusinessIndicatorController extends AbstractFileUploadController implements BusinessControllerMarker{

    static {
        FileUploadController annotation = BusinessIndicatorController.class.getAnnotation(FileUploadController.class);
        if (annotation != null) {
        setFileUploadInfo(annotation);
        }
    }

    @Resource
    private BusinessIndicatorService businessIndicatorService;

    @PostMapping("/create")
    @Operation(summary = "创建经营指标")
    @PreAuthorize("@ss.hasPermission('base:business-indicator:create')")
    public CommonResult<BusinessIndicatorRespVO> createBusinessIndicator(@Valid @RequestBody BusinessIndicatorSaveReqVO createReqVO) {
        return success(businessIndicatorService.createBusinessIndicator(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新经营指标")
    @PreAuthorize("@ss.hasPermission('base:business-indicator:update')")
    public CommonResult<Boolean> updateBusinessIndicator(@Valid @RequestBody BusinessIndicatorSaveReqVO updateReqVO) {
        businessIndicatorService.updateBusinessIndicator(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除经营指标")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:business-indicator:delete')")
    public CommonResult<Boolean> deleteBusinessIndicator(@RequestParam("id") Long id) {
        businessIndicatorService.deleteBusinessIndicator(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除经营指标")
                @PreAuthorize("@ss.hasPermission('base:business-indicator:delete')")
    public CommonResult<Boolean> deleteBusinessIndicatorList(@RequestBody BatchDeleteReqVO req) {
        businessIndicatorService.deleteBusinessIndicatorListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得经营指标")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:business-indicator:query')")
    public CommonResult<BusinessIndicatorRespVO> getBusinessIndicator(@RequestParam("id") Long id) {
//        BusinessIndicatorDO businessIndicator = businessIndicatorService.getBusinessIndicator(id);
//        使用getBusinessIndicatorPageWithRelations方法
        BusinessIndicatorPageReqVO pageReqVO =  new BusinessIndicatorPageReqVO();
        pageReqVO.setId(id);
        BusinessIndicatorRespVO businessIndicator = businessIndicatorService.getBusinessIndicatorPageWithRelations(pageReqVO).getList().get(0);
        return success(BeanUtils.toBean(businessIndicator, BusinessIndicatorRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得经营指标分页")
    @PreAuthorize("@ss.hasPermission('base:business-indicator:query')")
    public CommonResult<PageResult<BusinessIndicatorRespVO>> getBusinessIndicatorPage(@Valid BusinessIndicatorPageReqVO pageReqVO) {
        PageResult<BusinessIndicatorRespVO> pageResult = businessIndicatorService.getBusinessIndicatorPageWithRelations(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出经营指标 Excel")
    @PreAuthorize("@ss.hasPermission('base:business-indicator:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBusinessIndicatorExcel(@Valid BusinessIndicatorPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessIndicatorDO> list = businessIndicatorService.getBusinessIndicatorPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "经营指标.xls", "数据", BusinessIndicatorRespVO.class,
                        BeanUtils.toBean(list, BusinessIndicatorRespVO.class));
    }
    
    @GetMapping("/list-by-parent-id")
    @Operation(summary = "获得经营指标列表通过父级ID")
    @Parameter(name = "parentId", description = "父级ID", example = "0")
    @PreAuthorize("@ss.hasPermission('base:business-indicator:query')")
    public CommonResult<List<BusinessIndicatorRespVO>> getBusinessIndicatorListByParentId(@RequestParam(value = "parentId", required = false) Long parentId) {
        List<BusinessIndicatorDO> list = businessIndicatorService.getBusinessIndicatorListByParentId(parentId);
        return success(BeanUtils.toBean(list, BusinessIndicatorRespVO.class));
    }

}