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.annotation.LogonUser;
import cn.gov.customs.cacp.sdks.core.user.pojo.CacpLogonUser;
import cn.gov.customs.wxjy.analyze.pojo.EntryInfo;
import cn.gov.customs.wxjy.analyze.pojo.EntryList;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import cn.gov.customs.wxjy.common.core.controller.BaseController;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.gov.customs.wxjy.analyze.pojo.Entry;
import cn.gov.customs.wxjy.analyze.pojo.EntryHead;
import cn.gov.customs.wxjy.analyze.pojo.EntryQuery;
import cn.gov.customs.wxjy.analyze.service.IGoodsEntryService;
import cn.gov.customs.wxjy.common.utils.poi.ExcelUtil;
import cn.gov.customs.wxjy.common.utils.StringUtils;

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

    private final IGoodsEntryService service;

    @GetMapping("/get-entry")
    public Result<EntryInfo> getEntryHead(@RequestParam String entryId) {
        EntryHead head = this.service.selectByEntryId(entryId);
        EntryInfo info = new EntryInfo();
        info.setInfo(head);
        List<EntryList> list = this.service.getEntryList(entryId);
        info.setList(list);
        return Result.success(info);
    }

    /**
     * 查询危险品报关单头列表
     */
    @PostMapping("/get-list")
    public Result<PageInfo<Entry>> list(@LogonUser CacpLogonUser user, @RequestBody EntryQuery query) {
        PageHelper.startPage(query.getPageIndex(), query.getPageSize());
        String customsCode = user.getCustomsCode();
        //总关查所有，判断是否为总关用户
        if(StringUtils.isEmpty(query.getCustomsCode())){
            if(!"4700".equals(customsCode)){
                query.setCustomsCode(customsCode);
            }
        }
        PageInfo<Entry> list = this.service.selectPageList(query);
        return Result.success(list);
    }

    /**
     * 导出危险品报关单头列表
     */
    @PostMapping("/goods-export")
    public void export(@LogonUser CacpLogonUser user, HttpServletResponse response, @RequestBody EntryQuery query) {
        String customsCode = user.getCustomsCode();
        //总关查所有，判断是否为总关用户
        if(StringUtils.isEmpty(query.getCustomsCode())){
            if(!"4700".equals(customsCode)){
                query.setCustomsCode(customsCode);
            }
        }
        List<Entry> list = this.service.exportList(query);
        ExcelUtil<Entry> util = new ExcelUtil<Entry>(Entry.class);
        util.exportExcel(response, list, "危险品报关单数据");
    }
}
