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

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 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.businessalgorithm.vo.*;
import com.zt.plat.module.base.dal.dataobject.businessalgorithm.BusinessAlgorithmDO;
import com.zt.plat.module.base.service.businessalgorithm.BusinessAlgorithmService;

@Tag(name = "管理后台 - 业务算法")
@RestController
@RequestMapping("/base/business-algorithm")
@Validated
public class BusinessAlgorithmController implements BusinessControllerMarker {


    @Resource
    private BusinessAlgorithmService businessAlgorithmService;

    @PostMapping("/create")
    @Operation(summary = "创建业务算法")
    @PreAuthorize("@ss.hasPermission('base:business-algorithm:create')")
    public CommonResult<BusinessAlgorithmRespVO> createBusinessAlgorithm(@Valid @RequestBody BusinessAlgorithmSaveReqVO createReqVO) {
        return success(businessAlgorithmService.createBusinessAlgorithm(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新业务算法")
    @PreAuthorize("@ss.hasPermission('base:business-algorithm:update')")
    public CommonResult<Boolean> updateBusinessAlgorithm(@Valid @RequestBody BusinessAlgorithmSaveReqVO updateReqVO) {
        businessAlgorithmService.updateBusinessAlgorithm(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除业务算法")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:business-algorithm:delete')")
    public CommonResult<Boolean> deleteBusinessAlgorithm(@RequestParam("id") Long id) {
        businessAlgorithmService.deleteBusinessAlgorithm(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除业务算法")
                @PreAuthorize("@ss.hasPermission('base:business-algorithm:delete')")
    public CommonResult<Boolean> deleteBusinessAlgorithmList(@RequestBody BatchDeleteReqVO req) {
        businessAlgorithmService.deleteBusinessAlgorithmListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得业务算法")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:business-algorithm:query')")
    public CommonResult<BusinessAlgorithmRespVO> getBusinessAlgorithm(@RequestParam("id") Long id) {
        BusinessAlgorithmDO businessAlgorithm = businessAlgorithmService.getBusinessAlgorithm(id);
        return success(BeanUtils.toBean(businessAlgorithm, BusinessAlgorithmRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得业务算法分页")
    @PreAuthorize("@ss.hasPermission('base:business-algorithm:query')")
    public CommonResult<PageResult<BusinessAlgorithmRespVO>> getBusinessAlgorithmPage(@Valid BusinessAlgorithmPageReqVO pageReqVO) {
        PageResult<BusinessAlgorithmDO> pageResult = businessAlgorithmService.getBusinessAlgorithmPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BusinessAlgorithmRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出业务算法 Excel")
    @PreAuthorize("@ss.hasPermission('base:business-algorithm:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBusinessAlgorithmExcel(@Valid BusinessAlgorithmPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessAlgorithmDO> list = businessAlgorithmService.getBusinessAlgorithmPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "业务算法.xls", "数据", BusinessAlgorithmRespVO.class,
                        BeanUtils.toBean(list, BusinessAlgorithmRespVO.class));
    }

}