package com.jwipc.finance.controller.rest;

import com.jwipc.finance.dto.PaymentNoticeDetailImportDTO;
import com.jwipc.finance.dto.PaymentNoticeParamDTO;
import com.jwipc.finance.logic.PaymentNoticeLogic;
import com.jwipc.finance.model.query.PaymentNoticeQuery;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
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 + "/payment/notice/rest")
@RequiredArgsConstructor
public class PaymentNoticeRestController {
    private final PaymentNoticeLogic paymentNoticeLogic;
    @PostMapping("/list")
    public PageView list(PaymentNoticeQuery query){
        if (LoginUtil.isSupplier()){
            query.setSupplierCode(LoginUtil.getSupplierCode());
        }else {
            query.setUserId(LoginUtil.getUserId());
        }
        return paymentNoticeLogic.findByPage(query);
    }

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

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

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

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

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

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