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


import com.zt.plat.module.base.controller.admin.templtp.vo.*;
import com.zt.plat.module.base.dal.dataobject.tmpltp.TmplTpDO;
import com.zt.plat.module.base.service.tmpltp.TmplTpService;
import jakarta.validation.constraints.NotEmpty;
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.exception.util.ServiceExceptionUtil.exception;
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 static com.zt.plat.module.tmpltp.enums.ErrorCodeConstants.PARAMS_IS_NULL_OR_ERR;


@Tag(name = "管理后台 - 模板分类")
@RestController
@RequestMapping("/base/tmpl-tp")
@Validated
@FileUploadController(source = "base.tmpltp")
public class TmplTpController extends AbstractFileUploadController implements BusinessControllerMarker{

    @Resource
    private TmplTpService tmplTpService;

    @PostMapping("/create")
    @Operation(summary = "创建模板分类")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-tp:create')")
    public CommonResult<TmplTpRespVO> createTmplTp(@Valid @RequestBody TmplTpSaveReqVO createReqVO) {
        return success(tmplTpService.createTmplTp(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模板分类")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-tp:update')")
    public CommonResult<Boolean> updateTmplTp(@Valid @RequestBody TmplTpSaveReqVO updateReqVO) {
        tmplTpService.updateTmplTp(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模板分类")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('base:tmpl-tp:delete')")
    public CommonResult<Boolean> deleteTmplTp(@RequestBody BatchDeleteReqVO req) {
        tmplTpService.deleteTmplTp(req.getIds());
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除模板分类")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-tp:delete')")
    public CommonResult<Boolean> deleteTmplTpList(@RequestBody BatchDeleteReqVO req) {
        tmplTpService.deleteTmplTpListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模板分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:tmpl-tp:query')")
    public CommonResult<TmplTpRespVO> getTmplTp(@Valid @NotEmpty(message = "编号不能为空") @RequestParam("id") String id) {
        TmplTpDO tmplTp = tmplTpService.getTmplTp(Long.valueOf(id));
        return success(BeanUtils.toBean(tmplTp, TmplTpRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模板分类分页")
    @PreAuthorize("@ss.hasPermission('base:tmpl-tp:query')")
    public CommonResult<PageResult<TmplTpRespVO>> getTmplTpPage(@Valid TmplTpPageReqVO pageReqVO) {
        PageResult<TmplTpDO> pageResult = tmplTpService.getTmplTpPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TmplTpRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模板分类 Excel")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-tp:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTmplTpExcel(@Valid TmplTpPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TmplTpDO> list = tmplTpService.getTmplTpPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模板分类.xls", "数据", TmplTpRespVO.class,
                        BeanUtils.toBean(list, TmplTpRespVO.class));
    }
    //字段和条款回显
    @GetMapping("/field-and-clause")
    @Operation(summary = "获得字段和条款",description = "字段和条款回显,传入模版分类的id")
    @PreAuthorize("@ss.hasPermission('base:tmpl-tp:query')")
    public CommonResult<Map<String, Object>> getFieldAndClause(@Valid @NotEmpty(message = "编号不能为空")@RequestParam("id") String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("field", tmplTpService.getField(Long.valueOf(id)));
        map.put("clause", tmplTpService.getClause(Long.valueOf(id)));
        return success(map);
    }
    //获取分类树
    @GetMapping("/tree")
    @Operation(summary = "获得分类树--上级")
    @PreAuthorize("@ss.hasPermission('base:tmpl-tp:query')")
    public CommonResult<List<TmplTpTreeVO>> getTree(@RequestParam(value = "num",required = false) String num,@RequestParam(value = "name",required = false) String name) {
        List<TmplTpTreeVO> tree = tmplTpService.buildTree(num,name);
        return success(tree);
    }

    //更新类型状态
    @PutMapping("/updateStatus")
    @Operation(summary = "更新模板类型状态")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-tp:update')")
    public CommonResult<Boolean> updateStatus(@RequestBody Map<String,Object> params) {
        if (params == null){
            throw exception(PARAMS_IS_NULL_OR_ERR);
        }
        if (!params.containsKey("id") || !params.containsKey("sts")){
            throw exception(PARAMS_IS_NULL_OR_ERR);
        }
        tmplTpService.updateStatus(Long.valueOf(params.get("id").toString()), params.get("sts").toString());
        return success(true);
    }

    //表单预览
//    @GetMapping("/preview")
//    @Operation(summary = "表单预览",description = "传入模版分类的id")
//    @PreAuthorize("@ss.hasPermission('base:tmpl-tp:query')")
//    public CommonResult< List<TmplFldRespVO>> preview(@Valid @RequestParam("id") Long id) {
//        List<TmplFldRespVO> field = tmplTpService.getField(id);
//        return success(field);
//    }

    // 字段编辑新增或者删除
    @PostMapping("/field-edit")
    @Operation(summary = "字段编辑新增或者删除")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-tp:update')")
    public CommonResult<Boolean> fieldEdit(@Valid @RequestBody TmplTpEditOrDeleteReqVO reqVO) {
        tmplTpService.fieldEdit(reqVO);
        return success(true);
    }

    @PostMapping("/ltm-edit")
    @Operation(summary = "条款编辑新增或者删除")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-tp:update')")
    public CommonResult<Boolean> ltmEdit(@Valid @RequestBody TmplTpEditOrDeleteReqVO reqVO) {
        tmplTpService.ltmEdit(reqVO);
        return success(true);
    }
}
