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

import java.io.IOException;
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 cn.gov.customs.wxjy.base.service.BaseCodeService;
import cn.gov.customs.wxjy.common.utils.DictUtils;
import cn.gov.customs.wxjy.common.utils.poi.DynamicExcelUtil;
import cn.gov.customs.wxjy.system.service.DictDataService;
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.StringUtils;

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

    private final IGoodsEntryService service;
    private final BaseCodeService baseCodeService;
    private final DictDataService dictDataService;

    @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) throws IOException {
        String customsCode = user.getCustomsCode();
        // 获取前端传递的动态列配置
        String exportHeadList = query.getExportHeadList();
        //总关查所有，判断是否为总关用户
        if(StringUtils.isEmpty(query.getCustomsCode())){
            if(!"4700".equals(customsCode)){
                query.setCustomsCode(customsCode);
            }
        }
        List<Entry> list = this.service.exportList(query);
        list.forEach(excel -> {
            excel.setTrafMode(baseCodeService.getTransByCode(excel.getTrafMode())==null?"":baseCodeService.getTransByCode(excel.getTrafMode()).getTrafSpec());
            excel.setTradeMode(baseCodeService.getTradeByCode(excel.getTradeMode())==null?"":baseCodeService.getTradeByCode(excel.getTradeMode()).getAbbrTrade());
            excel.setIePort(baseCodeService.getCustomsByCode(excel.getIePort())==null?"":baseCodeService.getCustomsByCode(excel.getIePort()).getCustomsName());
            excel.setTradeCountry(baseCodeService.getCountryByIso(excel.getTradeCountry())==null?"":baseCodeService.getCountryByIso(excel.getTradeCountry()).getCounCName());
            excel.setGoodsType(dictDataService.selectDictLabel("goods_type",excel.getGoodsType()));
        });
        // 使用动态Excel工具类
        DynamicExcelUtil<Entry> util = new DynamicExcelUtil<>(Entry.class);
        util.exportExcel(response, list, "危险货物组合查询数据","",exportHeadList);
    }
}
