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

import com.zt.plat.framework.common.util.object.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;

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 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.departmentmaterial.vo.*;
import com.zt.plat.module.base.dal.dataobject.departmentmaterial.DepartmentMaterialDO;
import com.zt.plat.module.base.service.departmentmaterial.DepartmentMaterialService;

@Tag(name = "管理后台 - 组织架构物料")
@RestController
@RequestMapping("/base/department-material")
@Validated
public class DepartmentMaterialController {


    @Resource
    private DepartmentMaterialService departmentMaterialService;

    @PostMapping("/create")
    @Operation(summary = "创建组织架构物料")
    @PreAuthorize("@ss.hasPermission('base:department-material:create')")
    public CommonResult<DepartmentMaterialRespVO> createDepartmentMaterial(@Valid @RequestBody DepartmentMaterialSaveReqVO createReqVO) {
        return success(departmentMaterialService.createDepartmentMaterial(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新组织架构物料")
    @PreAuthorize("@ss.hasPermission('base:department-material:update')")
    public CommonResult<Boolean> updateDepartmentMaterial(@Valid @RequestBody DepartmentMaterialSaveReqVO updateReqVO) {
        departmentMaterialService.updateDepartmentMaterial(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除组织架构物料")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:department-material:delete')")
    public CommonResult<Boolean> deleteDepartmentMaterial(@RequestParam("id") Long id) {
        departmentMaterialService.deleteDepartmentMaterial(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除组织架构物料")
                @PreAuthorize("@ss.hasPermission('base:department-material:delete')")
    public CommonResult<Boolean> deleteDepartmentMaterialList(@RequestBody BatchDeleteReqVO req) {
        departmentMaterialService.deleteDepartmentMaterialListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得组织架构物料")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:department-material:query')")
    public CommonResult<DepartmentMaterialRespVO> getDepartmentMaterial(@RequestParam("id") Long id) {
    DepartmentMaterialRespVO departmentMaterial = departmentMaterialService.getDepartmentMaterial(id);
    return success(departmentMaterial);
    }

    @GetMapping("/page")
    @Operation(summary = "获得组织架构物料分页")
    @PreAuthorize("@ss.hasPermission('base:department-material:query')")
    public CommonResult<PageResult<DepartmentMaterialRespVO>> getDepartmentMaterialPage(@Valid DepartmentMaterialPageReqVO pageReqVO) {
        PageResult<DepartmentMaterialRespVO> pageResult = departmentMaterialService.getDepartmentMaterialPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出组织架构物料 Excel")
    @PreAuthorize("@ss.hasPermission('base:department-material:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDepartmentMaterialExcel(@Valid DepartmentMaterialPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DepartmentMaterialRespVO> list = departmentMaterialService.getDepartmentMaterialPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "组织架构物料.xls", "数据", DepartmentMaterialRespVO.class,
                        list);
    }

    @PostMapping("/clone")
    @Operation(summary = "克隆组织架构物料")
    @Parameter(name = "ids", description = "要克隆的数据ID", required = true)
    @Parameter(name = "deptId", description = "目标组织架构ID", required = true)
    @PreAuthorize("@ss.hasPermission('base:department-material:clone')")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> getDepartmentMaterial(@RequestBody DepartmentMaterialCloneReqVO req) {
        List<DepartmentMaterialRespVO> list = departmentMaterialService.getDepartmentMaterialByIds(req.getIds());
        for (DepartmentMaterialRespVO departmentMaterial : list) {
        DepartmentMaterialSaveReqVO createReqVO = BeanUtils.toBean(departmentMaterial, DepartmentMaterialSaveReqVO.class);
        createReqVO.setId( null);
        createReqVO.setDeptId(req.getDeptId());
        departmentMaterialService.createDepartmentMaterial(createReqVO);
        }
        return success(true);
    }

}