package com.jwipc.finance.controller.rest;

import com.jwipc.exception.DaoException;
import com.jwipc.finance.dto.*;
import com.jwipc.finance.entity.InvoicedApply;
import com.jwipc.finance.logic.CustomsDeclarationLogic;
import com.jwipc.finance.logic.PaymentMatchingLogic;
import com.jwipc.finance.model.query.CustomsDeclarationQuery;
import com.jwipc.finance.model.query.PaymentMatchingQuery;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
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/matching/rest")
@RequiredArgsConstructor
public class PaymentMatchingRestController {
    private final PaymentMatchingLogic paymentMatchingLogic;
    @PostMapping("/list")
    public PageView list(PaymentMatchingQuery query){
        if (LoginUtil.isSupplier()){
            query.setSupplierCode(LoginUtil.getSupplierCode());
        }else {
//            query.setUserId(LoginUtil.getUserId());
        }
        return paymentMatchingLogic.findByPage(query);
    }

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

    @PostMapping("/toImg")
    public ResponseEntity toImg(Long iaId) {
        PaymentMatchingDTO invoicedApplyDTO = paymentMatchingLogic.getById(iaId);
        if (StringUtils.isNotEmpty(invoicedApplyDTO.getImgCode())&&invoicedApplyDTO.getImgTime()!=null){
            throw new DaoException("该单据已影像化，不可重复操作");
        }
        paymentMatchingLogic.toImg(iaId);
        return ResponseEntity.ok(true);
    }

    @PostMapping("/deleteImg")
    public ResponseEntity deleteImg(Long iaId) {
        PaymentMatchingDTO invoicedApplyDTO = paymentMatchingLogic.getById(iaId);
        if (invoicedApplyDTO.getImgTime()==null){
            throw new DaoException("该单据未影像化，不可删除");
        }
        paymentMatchingLogic.deleteImg(iaId);
        return ResponseEntity.ok(true);
    }

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

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

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

    /**
     * 发票过账
     * @param ids 单据ids
     * @param type 1:预制 2:删除
     * @return
     */
    @PostMapping("/create/prefabricate/invoice")
    public ResponseEntity createPrefabricateInvoice(String ids,Integer type,String invoicePostDate,String statementType){
        List<String> idList = Arrays.asList(StringUtils.split(ids, ","));
        for (String id : idList){
            paymentMatchingLogic.createPrefabricateInvoice(Long.valueOf(id),type,invoicePostDate,statementType);
        }
        return ResponseEntity.ok(true);
    }

    /**
     * 发票过账
     * @param ids 单据ids
     * @param type 1:预制 2:删除
     * @return
     */
    @PostMapping("/create/prefabricate/updateInvoice")
    public ResponseEntity updatePrefabricateInvoice(String ids,
                                                    String invoiceVoucher,
//                                                    String invoicePostDate,
                                                    String paymentExpireDate,
                                                    String voucherYear,
                                                    Integer postStatus){
        List<String> idList = Arrays.asList(StringUtils.split(ids, ","));
        for (String id : idList){
            paymentMatchingLogic.updatePrefabricateInvoice(Long.valueOf(id),invoiceVoucher,paymentExpireDate,voucherYear,postStatus);
        }
        return ResponseEntity.ok(true);
    }
}
