package cn.gov.customs.wxjy.generator.controller;

import cn.gov.customs.cacp.sdks.core.result.Result;
import cn.gov.customs.cacp.sdks.core.user.trans.CacpTransUser;
import cn.gov.customs.cacp.sdks.core.user.trans.UserContextHolder;
import cn.gov.customs.wxjy.common.core.controller.BaseController;
import cn.gov.customs.wxjy.common.utils.poi.ExcelUtil;
import cn.gov.customs.wxjy.generator.pojo.GenTableColumn;
import cn.gov.customs.wxjy.generator.pojo.GenTableColumnQuery;
import cn.gov.customs.wxjy.generator.service.IGenTableColumnService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 代码生成业务字段Controller
 *
 * @author xiong
 * @date 2025-12-25
 */
@RestController
@RequestMapping("/system/GenTableColumn")
@RequiredArgsConstructor
public class GenTableColumnController extends BaseController {

    private final IGenTableColumnService genTableColumnService;

    /**
     * 查询代码生成业务字段列表
     */
    @PostMapping("/get-list")
    public Result<PageInfo<GenTableColumn>> list(@RequestBody GenTableColumnQuery query) {
        PageHelper.startPage(query.getPageIndex(), query.getPageSize());
        PageInfo<GenTableColumn> list = genTableColumnService.selectPageList(query);
        return Result.success(list);
    }

    /**
     * 获取代码生成业务字段详细信息
     */
    @GetMapping(value = "/get-detail")
    public Result<GenTableColumn> getInfo(@RequestParam String columnId) {
        return Result.success(genTableColumnService.selectByColumnId(columnId));
    }

    /**
     * 新增代码生成业务字段
     */
    @PostMapping("/insert-genTableColumn")
    public Result<Integer> add(@RequestBody GenTableColumn genTableColumn) {
        return Result.success(genTableColumnService.insert(genTableColumn));
    }

    /**
     * 修改代码生成业务字段
     */
    @PostMapping("/update-genTableColumn")
    public Result<Integer> edit(@RequestBody GenTableColumn genTableColumn) {
        return Result.success(genTableColumnService.update(genTableColumn));
    }

    /**
     * 删除代码生成业务字段
     */
    @PostMapping("/delete-genTableColumn")
    public Result<Integer> remove(@RequestBody String[] ids) {
        return Result.success(genTableColumnService.deleteByColumnIds(ids));
    }

    /**
    * 导入 代码生成业务字段
    * @param file
    * @param updateSupport
    * @return
    * @throws Exception
    */
    @PostMapping("/import-genTableColumn")
    public Result<String> importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        CacpTransUser user = UserContextHolder.currentUser();
        ExcelUtil<GenTableColumn> util = new ExcelUtil<GenTableColumn>(GenTableColumn.class);
        List<GenTableColumn> genTableColumnList = util.importExcel(file.getInputStream());
        String message = genTableColumnService.importGenTableColumn(genTableColumnList, updateSupport, user);
        return Result.success(message);
    }

    /**
     * 导出代码生成业务字段列表
     */
    @PostMapping("/get-genTableColumn-export")
    public void export(HttpServletResponse response, @RequestBody GenTableColumnQuery query) {
        List<GenTableColumn> list = genTableColumnService.selectList(query);
        ExcelUtil<GenTableColumn> util = new ExcelUtil<GenTableColumn>(GenTableColumn. class);
        util.exportExcel(response, list, "代码生成业务字段数据");
    }

    /**
    * 获取导入模板
    * @param response
    */
    @PostMapping("/template-genTableColumn")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<GenTableColumn> util = new ExcelUtil<GenTableColumn>(GenTableColumn.class);
        util.importTemplateExcel(response, "代码生成业务字段");
    }
}
