package com.njzscloud.common.gen;

import com.njzscloud.common.core.http.constant.Mime;
import com.njzscloud.common.core.utils.R;
import com.njzscloud.common.gen.support.Generator;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

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

    private final SysTplService sysTplService;

    /**
     * 新增
     *
     * @param sysTplEntity 数据
     */
    @PostMapping("/add")
    public R<?> add(@RequestBody SysTplEntity sysTplEntity) {
        sysTplService.add(sysTplEntity);
        return R.success();
    }

    /**
     * 修改
     *
     * @param sysTplEntity 数据
     */
    @PostMapping("/modify")
    public R<?> modify(@RequestBody SysTplEntity sysTplEntity) {
        sysTplService.modify(sysTplEntity);
        return R.success();
    }

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

    @PostMapping("/generate")
    public void generate(@RequestParam String tplName, @RequestBody(required = false) Map<String, Object> data, HttpServletResponse response) {
        try {
            response.setContentType(Mime.u8Val(Mime.TXT));
            Generator.generate(tplName, data, response.getOutputStream());
        } catch (IOException e) {
            log.error(e.getMessage());
        }
    }

    /**
     * 详情
     *
     * @param id Id
     * @return SysTplEntity 结果
     */
    @GetMapping("/detail")
    public R<SysTplEntity> detail(@RequestParam Long id) {
        return R.success(sysTplService.detail(id));
    }

    /**
     * 分页查询
     *
     * @param sysTplEntity 筛选条件
     * @param pageParam    分页参数
     * @return PageResult&lt;SysTplEntity&gt; 分页结果
     */
    @GetMapping("/paging")
    public R<PageResult<SysTplEntity>> paging(PageParam pageParam, SysTplEntity sysTplEntity) {
        return R.success(sysTplService.paging(pageParam, sysTplEntity));
    }

}
