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

import com.zt.plat.framework.business.annotation.FileUploadController;
import com.zt.plat.framework.common.pojo.CommonResult;
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.module.base.controller.admin.templtp.vo.TmplFldPageReqVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.TmplFldRespVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.TmplTpFldSaveReqVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.UpdateStsVO;
import com.zt.plat.module.base.dal.dataobject.tmpltp.TmplTpFldDO;
import com.zt.plat.module.base.service.tmpltp.TmplTpFldService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.zt.plat.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 模板字段")
@RestController
@RequestMapping("/base/tmpl-fld")
@Validated
@FileUploadController(source = "base.tmplfld")
@RequiredArgsConstructor
public class TmplTpFldController {
    private final TmplTpFldService tmplTpFldService;
    @PostMapping("/create")
    @Operation(summary = "创建模板字段")
    // @PreAuthorize("@ss.hasPermission('base:tmpl-tp-fld:create')")
    public CommonResult<TmplFldRespVO> createTmplFld(@Valid @RequestBody TmplTpFldSaveReqVO tmplTpFldSaveReqVO) {
        return success(tmplTpFldService.createTmplFld(tmplTpFldSaveReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模板字段")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-tp-fld:update')")
    public CommonResult<Boolean> updateTmplTp(@Valid @RequestBody TmplTpFldSaveReqVO updateReqVO) {
        tmplTpFldService.updateTmplFld(updateReqVO);
        return success(true);
    }
    @DeleteMapping("/delete")
    @Operation(summary = "删除模板字段")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-tp-fld:delete')")
    public CommonResult<Boolean> deleteTmplTp(@RequestBody BatchDeleteReqVO req) {
        tmplTpFldService.deleteTmplTpByIds(req.getIds());
        return success(true);
    }
    @GetMapping("/page")
    @Operation(summary = "获得模板字段列表")
    @PreAuthorize("@ss.hasPermission('base:tmpl-tp-fld:query')")
    public CommonResult<PageResult<TmplFldRespVO>> getTmplTpList( @Valid TmplFldPageReqVO pageReqVO) {
        PageResult<TmplTpFldDO> pageResult = tmplTpFldService.tmplTpFldPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TmplFldRespVO.class));
    }
    @GetMapping("/class-fld")
    @Operation(summary = "获得类固定模板字段列表", description = "clazz为VO类类名")
    @PreAuthorize("@ss.hasPermission('base:tmpl-tp-fld:query')")
    public CommonResult<List<Map<String, Object>>> getTmplTpListByClass(String clazz) {
        return success(tmplTpFldService.getTmplTpListByClass(clazz));
    }
    @PutMapping("/update-sts")
    @Operation(summary = "更新模板字段状态")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-tp-fld:update')")
    public CommonResult<Boolean> updateTmplTpSts(@RequestBody UpdateStsVO updateStsVO) {
        tmplTpFldService.updateTmplTpSts(updateStsVO);
        return success(true);
    }
}
