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.ChemicalsCatalog;
import cn.gov.customs.wxjy.base.pojo.ChemicalsCatalogQuery;
import cn.gov.customs.wxjy.base.service.IChemicalsCatalogService;
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/ChemicalsCatalog")
@RequiredArgsConstructor
public class ChemicalsCatalogController extends BaseController {

    private final IChemicalsCatalogService chemicalsCatalogService;

    /**
     * 查询基数数据-危化品目录列表
     */
    @PostMapping("/get-list")
    public Result<PageInfo<ChemicalsCatalog>> list(@RequestBody ChemicalsCatalogQuery query) {
        PageHelper.startPage(query.getPageIndex(), query.getPageSize());
        PageInfo<ChemicalsCatalog> list = chemicalsCatalogService.selectPageList(query);
        return Result.success(list);
    }

    /**
     * 获取基数数据-危化品目录详细信息
     */
    @GetMapping(value = "/get-detail")
    public Result<ChemicalsCatalog> getInfo(@RequestParam String ID) {
        return Result.success(chemicalsCatalogService.selectByID(ID));
    }

    /**
     * 新增基数数据-危化品目录
     */
    @PostMapping("/insert-chemicalsCatalog")
    public Result<Integer> add(@RequestBody ChemicalsCatalog chemicalsCatalog) {
        return Result.success(chemicalsCatalogService.insert(chemicalsCatalog));
    }

    /**
     * 修改基数数据-危化品目录
     */
    @PostMapping("/update-chemicalsCatalog")
    public Result<Integer> edit(@RequestBody ChemicalsCatalog chemicalsCatalog) {
        return Result.success(chemicalsCatalogService.update(chemicalsCatalog));
    }

    /**
     * 删除基数数据-危化品目录
     */
    @PostMapping("/delete-chemicalsCatalog")
    public Result<Integer> remove(@RequestBody String[] ids) {
        return Result.success(chemicalsCatalogService.deleteByIDs(ids));
    }

    /**
    * 导入 基数数据-危化品目录
    * @param file
    * @param updateSupport
    * @return
    * @throws Exception
    */
    @PostMapping("/import-chemicalsCatalog")
    public Result<String> importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        CacpTransUser user = UserContextHolder.currentUser();
        ExcelUtil<ChemicalsCatalog> util = new ExcelUtil<ChemicalsCatalog>(ChemicalsCatalog.class);
        List<ChemicalsCatalog> chemicalsCatalogList = util.importExcel(file.getInputStream());
        String message = chemicalsCatalogService.importChemicalsCatalog(chemicalsCatalogList, updateSupport, user);
        return Result.success(message);
    }

    /**
     * 导出基数数据-危化品目录列表
     */
    @PostMapping("/get-chemicalsCatalog-export")
    public void export(HttpServletResponse response, @RequestBody ChemicalsCatalogQuery query) {
        List<ChemicalsCatalog> list = chemicalsCatalogService.selectList(query);
        ExcelUtil<ChemicalsCatalog> util = new ExcelUtil<ChemicalsCatalog>(ChemicalsCatalog. class);
        util.exportExcel(response, list, "基数数据-危化品目录数据");
    }

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