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.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.NewDeclaredGoods;
import cn.gov.customs.wxjy.analyze.pojo.NewDeclaredGoodsQuery;
import cn.gov.customs.wxjy.analyze.service.INewDeclaredGoodsService;
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/NewDeclaredGoods")
@RequiredArgsConstructor
public class NewDeclaredGoodsController extends BaseController {

    private final INewDeclaredGoodsService newDeclaredGoodsService;

    /**
     * 查询新申报商品提示列表
     */
    @PostMapping("/get-list")
    public Result<PageInfo<NewDeclaredGoods>> list(@LogonUser CacpLogonUser user, @RequestBody NewDeclaredGoodsQuery query) {
        String customsCode = user.getCustomsCode();
        //总关查所有，判断是否为总关用户
        if(StringUtils.isEmpty(query.getCustomsCode())){
            if(!"4700".equals(customsCode)){
                query.setCustomsCode(customsCode);
            }
        }
        PageHelper.startPage(query.getPageIndex(), query.getPageSize());
        PageInfo<NewDeclaredGoods> list = newDeclaredGoodsService.selectPageList(query);
        return Result.success(list);
    }

    /**
     * 获取新申报商品提示详细信息
     */
    @GetMapping(value = "/get-detail")
    public Result<NewDeclaredGoods> getInfo(@RequestParam String ID) {
        return Result.success(newDeclaredGoodsService.selectByID(ID));
    }

    /**
     * 新增新申报商品提示
     */
    @PostMapping("/insert-newDeclaredGoods")
    public Result<Integer> add(@RequestBody NewDeclaredGoods newDeclaredGoods) {
        return Result.success(newDeclaredGoodsService.insert(newDeclaredGoods));
    }

    /**
     * 修改新申报商品提示
     */
    @PostMapping("/update-newDeclaredGoods")
    public Result<Integer> edit(@RequestBody NewDeclaredGoods newDeclaredGoods) {
        return Result.success(newDeclaredGoodsService.update(newDeclaredGoods));
    }

    /**
     * 删除新申报商品提示
     */
    @PostMapping("/delete-newDeclaredGoods")
    public Result<Integer> remove(@RequestBody String[] ids) {
        return Result.success(newDeclaredGoodsService.deleteByIDs(ids));
    }

    /**
    * 导入 新申报商品提示
    * @param file
    * @param updateSupport
    * @return
    * @throws Exception
    */
    @PostMapping("/import-newDeclaredGoods")
    public Result<String> importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        CacpTransUser user = UserContextHolder.currentUser();
        ExcelUtil<NewDeclaredGoods> util = new ExcelUtil<NewDeclaredGoods>(NewDeclaredGoods.class);
        List<NewDeclaredGoods> newDeclaredGoodsList = util.importExcel(file.getInputStream());
        String message = newDeclaredGoodsService.importNewDeclaredGoods(newDeclaredGoodsList, updateSupport, user);
        return Result.success(message);
    }

    /**
     * 导出新申报商品提示列表
     */
    @PostMapping("/get-newDeclaredGoods-export")
    public void export(HttpServletResponse response, @RequestBody NewDeclaredGoodsQuery query) {
        List<NewDeclaredGoods> list = newDeclaredGoodsService.selectList(query);
        ExcelUtil<NewDeclaredGoods> util = new ExcelUtil<NewDeclaredGoods>(NewDeclaredGoods. class);
        util.exportExcel(response, list, "新申报商品提示数据");
    }

    /**
    * 获取导入模板
    * @param response
    */
    @PostMapping("/template-newDeclaredGoods")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<NewDeclaredGoods> util = new ExcelUtil<NewDeclaredGoods>(NewDeclaredGoods.class);
        util.importTemplateExcel(response, "新申报商品提示");
    }
}
