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.PackCatalog;
import cn.gov.customs.wxjy.base.pojo.PackCatalogQuery;
import cn.gov.customs.wxjy.base.service.IPackCatalogService;
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/PackCatalog")
@RequiredArgsConstructor
public class PackCatalogController extends BaseController {

    private final IPackCatalogService packCatalogService;

    /**
     * 查询包装种类目录列表
     */
    @PostMapping("/get-list")
    public Result<PageInfo<PackCatalog>> list(@RequestBody PackCatalogQuery query) {
        PageHelper.startPage(query.getPageIndex(), query.getPageSize());
        PageInfo<PackCatalog> list = packCatalogService.selectPageList(query);
        return Result.success(list);
    }

    /**
     * 获取包装种类目录详细信息
     */
    @GetMapping(value = "/get-detail")
    public Result<PackCatalog> getInfo(@RequestParam String ID) {
        return Result.success(packCatalogService.selectByID(ID));
    }

    /**
     * 新增包装种类目录
     */
    @PostMapping("/insert-packCatalog")
    public Result<Integer> add(@RequestBody PackCatalog packCatalog) {
        return Result.success(packCatalogService.insert(packCatalog));
    }

    /**
     * 修改包装种类目录
     */
    @PostMapping("/update-packCatalog")
    public Result<Integer> edit(@RequestBody PackCatalog packCatalog) {
        return Result.success(packCatalogService.update(packCatalog));
    }

    /**
     * 删除包装种类目录
     */
    @PostMapping("/delete-packCatalog")
    public Result<Integer> remove(@RequestBody String[] ids) {
        return Result.success(packCatalogService.deleteByIDs(ids));
    }

    /**
    * 导入 包装种类目录
    * @param file
    * @param updateSupport
    * @return
    * @throws Exception
    */
    @PostMapping("/import-packCatalog")
    public Result<String> importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        CacpTransUser user = UserContextHolder.currentUser();
        ExcelUtil<PackCatalog> util = new ExcelUtil<PackCatalog>(PackCatalog.class);
        List<PackCatalog> packCatalogList = util.importExcel(file.getInputStream());
        String message = packCatalogService.importPackCatalog(packCatalogList, updateSupport, user);
        return Result.success(message);
    }

    /**
     * 导出包装种类目录列表
     */
    @PostMapping("/get-packCatalog-export")
    public void export(HttpServletResponse response, @RequestBody PackCatalogQuery query) {
        List<PackCatalog> list = packCatalogService.selectList(query);
        ExcelUtil<PackCatalog> util = new ExcelUtil<PackCatalog>(PackCatalog. class);
        util.exportExcel(response, list, "包装种类目录数据");
    }

    /**
    * 获取导入模板
    * @param response
    */
    @PostMapping("/template-packCatalog")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<PackCatalog> util = new ExcelUtil<PackCatalog>(PackCatalog.class);
        util.importTemplateExcel(response, "包装种类目录");
    }
}
