package com.jwipc.finance.controller.rest;

import com.google.common.collect.Lists;
import com.jwipc.finance.dto.CustomsDeclarationDetailImportDTO;
import com.jwipc.finance.dto.CustomsDeclarationParamDTO;
import com.jwipc.finance.logic.CustomsDeclarationLogic;
import com.jwipc.finance.model.query.AccountStatementQuery;
import com.jwipc.finance.model.query.CustomsDeclarationQuery;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.jwipc.finance.constant.ModuleConst.MODULE_ROOT;

/**
 * <p>
 * 对账模块-报关单主表 前端控制器
 * </p>
 *
 * @author shenyang.ou
 * @since 2025-10-22
 */
@RestController
@RequestMapping(MODULE_ROOT + "/customs/declaration/rest")
@RequiredArgsConstructor
public class CustomsDeclarationRestController {
    private final CustomsDeclarationLogic accountStatementLogic;
    @PostMapping("/list")
    public PageView list(CustomsDeclarationQuery query){
        if (LoginUtil.isSupplier()){
            query.setSupplierCode(LoginUtil.getSupplierCode());
        }else {
            query.setUserId(LoginUtil.getUserId());
        }
        return accountStatementLogic.findByPage(query);
    }

    /**
     * 保存更新
     *
     * @param param 参数
     */
    @PostMapping("/saveOrUpdate")
    public ResponseEntity saveOrUpdate(@RequestBody CustomsDeclarationParamDTO param) {
        accountStatementLogic.saveOrUpdate(param);
        return ResponseEntity.ok(true);
    }

    /**
     * 删除
     *
     * @param id id
     */
    @PostMapping("/remove")
    public ResponseEntity remove(Long id) {
        accountStatementLogic.remove(id);
        return ResponseEntity.ok(true);
    }

    /**
     * 提交确认
     *
     * @param id id
     */
    @PostMapping("/pushConfirm")
    public ResponseEntity pushConfirm(Long id) {
        accountStatementLogic.pushConfirm(id);
        return ResponseEntity.ok(true);
    }

    /**
     * 导入物料信息
     * @param file 导入文件
     * @return
     */
    @PostMapping("importData")
    public ResponseEntity<List<CustomsDeclarationDetailImportDTO>> importData(@RequestParam(value = "file") MultipartFile file) {
        List<CustomsDeclarationDetailImportDTO> inquiryMaterialImportDTOS = accountStatementLogic.importData(file);
        return ResponseEntity.ok(inquiryMaterialImportDTOS);
    }

    /**
     * 财务确认
     *
     * @param param 参数
     */
    @PostMapping("/financeConfirm")
    public ResponseEntity financeConfirm(@RequestBody CustomsDeclarationParamDTO param) {
        accountStatementLogic.financeConfirm(param);
        return ResponseEntity.ok(true);
    }
    @PostMapping("/financeConfirmCheck")
    public ResponseEntity financeConfirmCheck(@RequestBody CustomsDeclarationParamDTO param) {
        return ResponseEntity.ok( accountStatementLogic.financeConfirmCheck(param));
    }

    /**
     * 财务撤销已确认状态的对账单
     *
     * @param id ID
     * @param revokeReason 撤销原因
     * @return 结果
     */
    @PostMapping("/revoke/fin/confirmed")
    public ResponseEntity revokeFinConfirmed(Long id, String revokeReason){
        accountStatementLogic.revokeFinConfirmed(id, revokeReason);
        return ResponseEntity.ok(true);
    }
}
