package cn.gov.customs.wxjy.analyze.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.analyze.pojo.EntryHead;
import cn.gov.customs.wxjy.analyze.pojo.EntryHeadQuery;
import cn.gov.customs.wxjy.analyze.service.IEntryHeadService;
import cn.gov.customs.wxjy.common.utils.poi.ExcelUtil;
import cn.gov.customs.wxjy.common.core.page.TableDataInfo;

/**
 * 危险品报关单头Controller
 *
 * @author xiong
 * @date 2025-12-04
 */
@RestController
@RequestMapping("/analyze/EntryHead")
@RequiredArgsConstructor
public class EntryHeadController extends BaseController {

    private final IEntryHeadService entryHeadService;

    /**
     * 查询危险品报关单头列表
     */
    @PostMapping("/get-list")
    public Result<PageInfo<EntryHead>> list(@RequestBody EntryHeadQuery query) {
        PageHelper.startPage(query.getPageIndex(), query.getPageSize());
        PageInfo<EntryHead> list = entryHeadService.selectPageList(query);
        return Result.success(list);
    }

    /**
     * 获取危险品报关单头详细信息
     */
    @GetMapping(value = "/get-detail")
    public Result<EntryHead> getInfo(@RequestParam String entryId) {
        return Result.success(entryHeadService.selectByEntryId(entryId));
    }

    /**
     * 新增危险品报关单头
     */
    @PostMapping("/insert-entryHead")
    public Result<Integer> add(@RequestBody EntryHead entryHead) {
        return Result.success(entryHeadService.insert(entryHead));
    }

    /**
     * 修改危险品报关单头
     */
    @PostMapping("/update-entryHead")
    public Result<Integer> edit(@RequestBody EntryHead entryHead) {
        return Result.success(entryHeadService.update(entryHead));
    }

    /**
     * 删除危险品报关单头
     */
    @PostMapping("/delete-entryHead")
    public Result<Integer> remove(@PathVariable String[] ids) {
        return Result.success(entryHeadService.deleteByEntryIds(ids));
    }

    /**
    * 导入 危险品报关单头
    * @param file
    * @param updateSupport
    * @return
    * @throws Exception
    */
    @PostMapping("/import-entryHead")
    public Result<String> importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        CacpTransUser user = UserContextHolder.currentUser();
        ExcelUtil<EntryHead> util = new ExcelUtil<EntryHead>(EntryHead.class);
        List<EntryHead> entryHeadList = util.importExcel(file.getInputStream());
        String message = entryHeadService.importEntryHead(entryHeadList, updateSupport, user);
        return Result.success(message);
    }

    /**
     * 导出危险品报关单头列表
     */
    @PostMapping("/get-entryHead-export")
    public void export(HttpServletResponse response, @RequestBody EntryHeadQuery query) {
        List<EntryHead> list = entryHeadService.selectList(query);
        ExcelUtil<EntryHead> util = new ExcelUtil<EntryHead>(EntryHead. class);
        util.exportExcel(response, list, "危险品报关单头数据");
    }

    /**
    * 获取导入模板
    * @param response
    */
    @PostMapping("/template-entryHead")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<EntryHead> util = new ExcelUtil<EntryHead>(EntryHead.class);
        util.importTemplateExcel(response, "危险品报关单头");
    }
}
