package com.njzscloud.common.gen;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.njzscloud.common.core.utils.Mime;
import com.njzscloud.common.core.utils.R;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.common.mvc.util.ServletUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;

/**
 * 代码模板
 */
@Slf4j
@RestController
@RequestMapping("/tpl")
@RequiredArgsConstructor
public class TplController {

    private final TplService tplService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public R<?> add(@RequestBody TplEntity tplEntity) {
        tplService.add(tplEntity);
        return R.success();
    }

    /**
     * 修改
     */
    @PostMapping("/modify")
    public R<?> modify(@RequestBody TplEntity tplEntity) {
        tplService.modify(tplEntity);
        return R.success();
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    public R<?> del(@RequestBody List<Long> ids) {
        tplService.del(ids);
        return R.success();
    }

    @PostMapping("/download")
    public void download(@RequestParam("lang") String lang,
                         @RequestParam("tableName") String tableName,
                         @RequestBody(required = false) Map<String, Object> data,
                         HttpServletResponse response) {

        ByteArrayOutputStream out = tplService.download(lang, tableName, data);
        ServletUtil.download(response, out.toByteArray(), Mime.ZIP, DateTime.now() + ".zip");
    }

    @PostMapping("/preview")
    public R<?> preview(@RequestParam("lang") String lang,
                        @RequestParam("tableName") String tableName,
                        @RequestBody(required = false) Map<String, Object> data) {

        Map<String, Map<String, String>> map = tplService.preview(lang, tableName, data);

        return R.success(map);
    }


    /**
     * 获取表信息
     */
    @GetMapping("/table/paging")
    public R<PageResult<Map<String, Object>>> tablePaging(PageParam pageParam,
                                                          @RequestParam(value = "tableSchema", required = false) String tableSchema,
                                                          @RequestParam(value = "tableName", required = false) String tableName
    ) {
        return R.success(tplService.tablePaging(pageParam, tableSchema, tableName));
    }


    /**
     * 获取所有模板
     */
    @GetMapping("/list_all")
    public R<?> listAll(@RequestParam("lang") String lang) {
        return R.success(tplService
            .list(Wrappers.lambdaQuery(TplEntity.class)
                .eq(TplEntity::getLang, lang)
                .orderByAsc(TplEntity::getTplName)));
    }


    /**
     * 详情
     */
    @GetMapping("/detail")
    public R<TplEntity> detail(@RequestParam("id") Long id) {
        return R.success(tplService.detail(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/paging")
    public R<PageResult<TplEntity>> paging(PageParam pageParam, TplEntity tplEntity) {
        return R.success(tplService.paging(pageParam, tplEntity));
    }

}



