package com.jwipc.finance.controller.rest;

import com.jwipc.finance.dto.CreatePaaDetailDto;
import com.jwipc.finance.dto.InvoicedApplyPaDTO;
import com.jwipc.finance.logic.PaymentAdvanceApplyDetailLogic;
import com.jwipc.finance.logic.PaymentAdvanceCodDetailLogic;
import com.jwipc.finance.model.query.PaymentApplyItemQuery;
import com.jwipc.page.PageView;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

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

/**
 * <p>
 *
 * </p>
 *
 * @author wt
 * @version 1.0
 * @since 2024/3/8
 */
@RestController
@RequestMapping(MODULE_ROOT + "/paymentAdvance/apply/detail/rest")
@RequiredArgsConstructor
public class PaymentAdvanceApplyDetailRestController {

    private final PaymentAdvanceApplyDetailLogic paymentAdvanceApplyDetailLogic;
    private final PaymentAdvanceCodDetailLogic paymentAdvanceCodDetailLogic;

    /**
     * 根据预付款申请单ID获取明细信息
     *
     * @param applyId      预付款申请单ID
     * @param dataShowType 数据展示类型 1:编辑 2:查看
     * @return 结果
     */
    @RequestMapping("/find/paa/detail")
    public ResponseEntity findPaaDetailByPaaId(Long applyId, Integer dataShowType, Integer optType) {
        PageView pageView = paymentAdvanceApplyDetailLogic.findPaaDetailByPaaId(applyId, dataShowType,optType);
        return ResponseEntity.ok(pageView);
    }

    /**
     * 根据id获取该单据下的订单付款记录信息
     * @param applyId 单据id
     * @param type advance_payment 预付款 cash_on_delivery 货到付款
     * @return
     */
    @PostMapping("/find/payment/detail")
    public ResponseEntity<?> findPaymentDetail(@RequestParam(value = "applyId",required = false) Long applyId,
                                            @RequestParam(value = "type",required = false)String type,
                                            @RequestParam(value = "poCodeList[]",required = false) List<String> poCodeListFromParams){
        return ResponseEntity.ok(paymentAdvanceApplyDetailLogic.findPaymentDetail(applyId, type,poCodeListFromParams));

    }

    /**
     * 根据采购订单号查询该订单的详细的付款明细
     * @param poCode 采购订单号
     * @return
     */
    @PostMapping("find/payment/detailBypoCode")
    public ResponseEntity findDetailByPoCode(@RequestParam("poCode")String poCode){
        return ResponseEntity.ok(paymentAdvanceApplyDetailLogic.findDetailByPoCode(poCode));
    }

    /**
     * 根据采购订单号获取预付款的采购订单数据
     *
     * @param query 查询条件
     * @return 结果
     */
    @PostMapping("/find/po/data/by/po/code")
    public ResponseEntity findAdvanceApplyDataByPoCode(PaymentApplyItemQuery query) {
        List<CreatePaaDetailDto> result = paymentAdvanceApplyDetailLogic.findAdvanceApplyDataByPoCode(query);
        return ResponseEntity.ok(result);
    }

    /**
     * 开票申请获取预付款信息
     *
     * @param query 查询条件
     * @return 结果
     */
    @PostMapping("/provide/create/advance/apply")
    public ResponseEntity provideCreateAdvanceApply(PaymentApplyItemQuery query) {
        List<InvoicedApplyPaDTO> result = paymentAdvanceApplyDetailLogic.provideCreateAdvanceApply(query);
        return ResponseEntity.ok(result);
    }
}
