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

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

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.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import cn.gov.customs.wxjy.common.core.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.gov.customs.wxjy.base.pojo.GoodsCatalog;
import cn.gov.customs.wxjy.base.pojo.GoodsCatalogQuery;
import cn.gov.customs.wxjy.base.service.IGoodsCatalogService;
import cn.gov.customs.wxjy.common.utils.poi.ExcelUtil;
import cn.gov.customs.wxjy.common.core.page.TableDataInfo;

/**
 * 基础数据-危险货物目录Controller
 *
 * @author xiong
 * @date 2025-12-16
 */
@RestController
@RequestMapping("/base/GoodsCatalog")
@RequiredArgsConstructor
public class GoodsCatalogController extends BaseController {

    private final IGoodsCatalogService goodsCatalogService;

    /**
     * 查询基础数据-危险货物目录列表
     */
    @PostMapping("/get-list")
    public Result<PageInfo<GoodsCatalog>> list(@RequestBody GoodsCatalogQuery query) {
        PageHelper.startPage(query.getPageIndex(), query.getPageSize());
        PageInfo<GoodsCatalog> list = goodsCatalogService.selectPageList(query);
        return Result.success(list);
    }

    /**
     * 获取基础数据-危险货物目录详细信息
     */
    @GetMapping(value = "/get-detail")
    public Result<GoodsCatalog> getInfo(@RequestParam String ID) {
        return Result.success(goodsCatalogService.selectByID(ID));
    }

    /**
     * 新增基础数据-危险货物目录
     */
    @PostMapping("/insert-goodsCatalog")
    public Result<Integer> add(@RequestBody GoodsCatalog goodsCatalog) {
        return Result.success(goodsCatalogService.insert(goodsCatalog));
    }

    /**
     * 修改基础数据-危险货物目录
     */
    @PostMapping("/update-goodsCatalog")
    public Result<Integer> edit(@RequestBody GoodsCatalog goodsCatalog) {
        return Result.success(goodsCatalogService.update(goodsCatalog));
    }

    /**
     * 删除基础数据-危险货物目录
     */
    @PostMapping("/delete-goodsCatalog")
    public Result<Integer> remove(@RequestBody String[] ids) {
        return Result.success(goodsCatalogService.deleteByIDs(ids));
    }

    /**
    * 导入 基础数据-危险货物目录
    * @param file
    * @param updateSupport
    * @return
    * @throws Exception
    */
    @PostMapping("/import-goodsCatalog")
    public Result<String> importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        CacpTransUser user = UserContextHolder.currentUser();
        ExcelUtil<GoodsCatalog> util = new ExcelUtil<GoodsCatalog>(GoodsCatalog.class);
        List<GoodsCatalog> goodsCatalogList = util.importExcel(file.getInputStream());
        String message = goodsCatalogService.importGoodsCatalog(goodsCatalogList, updateSupport, user);
        return Result.success(message);
    }

    /**
     * 导出基础数据-危险货物目录列表
     */
    @PostMapping("/get-goodsCatalog-export")
    public void export(HttpServletResponse response, @RequestBody GoodsCatalogQuery query) {
        List<GoodsCatalog> list = goodsCatalogService.selectList(query);
        ExcelUtil<GoodsCatalog> util = new ExcelUtil<GoodsCatalog>(GoodsCatalog. class);
        util.exportExcel(response, list, "基础数据-危险货物目录数据");
    }

    /**
    * 获取导入模板
    * @param response
    */
    @PostMapping("/template-goodsCatalog")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<GoodsCatalog> util = new ExcelUtil<GoodsCatalog>(GoodsCatalog.class);
        util.importTemplateExcel(response, "基础数据-危险货物目录");
    }
}
