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

import com.zt.plat.module.base.controller.admin.plandate.vo.PlanDatePageReqVO;
import com.zt.plat.module.base.controller.admin.plandate.vo.PlanDateRespVO;
import com.zt.plat.module.base.controller.admin.plandate.vo.PlanDateSaveReqVO;
import com.zt.plat.module.base.dal.dataobject.plandate.PlanDateDO;
import com.zt.plat.module.base.service.plandate.PlanDateService;
import com.zt.plat.module.plandate.PlanApi;
import com.zt.plat.module.plandate.dto.ErpMonthProductivePlanCheckSaveReqDTO;
import com.zt.plat.module.plandate.dto.PlanDateSaveReqDTO;
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.constraints.*;
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.*;


@Tag(name = "管理后台 - 计划数据")
@RestController
@RequestMapping("/base/plan-date")
@Validated
public class PlanDateController implements BusinessControllerMarker {


    @Resource
    private PlanDateService planDateService;
    @Resource
    private PlanApi planApi;

    @PostMapping("/create")
    @Operation(summary = "创建计划数据")
    @PreAuthorize("@ss.hasPermission('base:plan-date:create')")
    public CommonResult<PlanDateRespVO> createPlanDate(@Valid @RequestBody PlanDateSaveReqVO createReqVO) {
        return success(planDateService.createPlanDateWithChildren(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新计划数据")
    @PreAuthorize("@ss.hasPermission('base:plan-date:update')")
    public CommonResult<Boolean> updatePlanDate(@Valid @RequestBody PlanDateSaveReqVO updateReqVO) {
        planDateService.updatePlanDate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除计划数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:plan-date:delete')")
    public CommonResult<Boolean> deletePlanDate(@RequestParam("id") Long id) {
        planDateService.deletePlanDate(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除计划数据")
    @PreAuthorize("@ss.hasPermission('base:plan-date:delete')")
    public CommonResult<Boolean> deletePlanDateList(@RequestBody BatchDeleteReqVO req) {
        planDateService.deletePlanDateListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得计划数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:plan-date:query')")
    public CommonResult<PlanDateRespVO> getPlanDate(@RequestParam("id") Long id) {
        PlanDateDO planDate = planDateService.getPlanDate(id);
        return success(BeanUtils.toBean(planDate, PlanDateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得计划数据分页")
    @PreAuthorize("@ss.hasPermission('base:plan-date:query')")
    public CommonResult<PageResult<PlanDateRespVO>> getPlanDatePage(@Valid PlanDatePageReqVO pageReqVO) {
        PageResult<PlanDateDO> pageResult = planDateService.getPlanDatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PlanDateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出计划数据 Excel")
    @PreAuthorize("@ss.hasPermission('base:plan-date:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPlanDateExcel(@Valid PlanDatePageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PlanDateDO> list = planDateService.getPlanDatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "计划数据.xls", "数据", PlanDateRespVO.class,
                BeanUtils.toBean(list, PlanDateRespVO.class));
    }

    @GetMapping("/tree/list-all")
    @Operation(summary = "查询所有计划数据（树形结构）")
    @PreAuthorize("@ss.hasPermission('base:plan-date:query')")
    public CommonResult<List<PlanDateRespVO>> listAllPlanDateTree(@Valid PlanDatePageReqVO pageReqVO) {
        return success(planDateService.listPlanDateTree(pageReqVO));
    }
    @PostMapping("/planApi")
    public  CommonResult<?>  planApi(@RequestBody ErpMonthProductivePlanCheckSaveReqDTO reqDTO) {
        return success(planApi.monthProductivePlanCheckCreate(reqDTO));
    }
}