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

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

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.TmpItmPageReqVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.TmplItmRespVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.TmplItmSaveReqVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.UpdateStsVO;
import com.zt.plat.module.base.dal.dataobject.tmpltp.TmplItmDO;
import com.zt.plat.module.base.service.tmpltp.TmplItmService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
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;

@Tag(name = "管理后台 - 模板条款")
@RestController
@RequestMapping("/base/tmpl-ltm")
@Validated
@FileUploadController(source = "bse.tmplltm")
@RequiredArgsConstructor
public class TmplItmController {
    private final TmplItmService tmplItmService;

    @PostMapping("/create")
    @Operation(summary = "创建模板条款")
    // @PreAuthorize("@ss.hasPermission('base:tmpl-ltm:create')")
    public CommonResult<TmplItmRespVO> createTmplItm(@Valid @RequestBody TmplItmSaveReqVO createReqVO) {
        TmplItmRespVO tmplItm = tmplItmService.createTmplItm(createReqVO);
        return success(tmplItm);
    }

    @PutMapping("/update")
    @Operation(summary = "更新模板条款")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-ltm:update')")
    public CommonResult<Boolean> updateTmplItm(@Valid @RequestBody TmplItmSaveReqVO updateReqVO) {
        tmplItmService.updateTmplItm(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模板条款", description = "")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-ltm:delete')")
    public CommonResult<Boolean> deleteTmplItm(@RequestBody BatchDeleteReqVO req) {
        return success(tmplItmService.deleteTmplItm(req.getIds()));
    }

    @GetMapping("/id")
    @Operation(summary = "根据id获得模板条款")
    @PreAuthorize("@ss.hasPermission('base:tmpl-ltm:query')")
    public CommonResult<TmplItmRespVO> getTmplItm(@RequestBody String id) {
        return success(BeanUtils.toBean(tmplItmService.getById(id), TmplItmRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得模板条款")
    @PreAuthorize("@ss.hasPermission('base:tmpl-ltm:query')")
    public CommonResult<List<TmplItmRespVO>> listTmplItm(Map<String, Object> params) {
        // 创建查询条件构造器
        QueryWrapper<TmplItmDO> queryWrapper = new QueryWrapper<>();

        // 遍历Map参数，动态添加查询条件
        if (params != null && !params.isEmpty()) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();

                // 过滤掉空值和空字符串
                if (value != null &&
                        !(value instanceof String && StringUtils.isBlank((String) value))) {
                    queryWrapper.eq(key, value);
                }
            }
        }
        // 执行带条件的查询
        List<TmplItmDO> list = tmplItmService.list(queryWrapper);
        return success(BeanUtils.toBean(list, TmplItmRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "分页获得模板条款")
    @PreAuthorize("@ss.hasPermission('base:tmpl-ltm:query')")
    public CommonResult<PageResult<TmplItmRespVO>> pageTmplItm(@Validated TmpItmPageReqVO pageReqVO) {
        PageResult<TmplItmDO> pageResult = tmplItmService.pageTmplItm(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TmplItmRespVO.class));
    }


    @PutMapping("/update-sts")
    @Operation(summary = "更新模板条款状态")
    //@PreAuthorize("@ss.hasPermission('bse:tmpl-tp-fld:update')")
    public CommonResult<Boolean> updateTmplTpSts(@RequestBody UpdateStsVO updateStsVO) {
        tmplItmService.updateTmplTpSts(updateStsVO);
        return success(true);
    }
}
