package com.jwipc.finance.controller.rest;

import com.jwipc.exception.DaoException;
import com.jwipc.finance.dto.CostMatchingDTO;
import com.jwipc.finance.dto.CostMatchingParamDTO;
import com.jwipc.finance.dto.InvoicedApplyDTO;
import com.jwipc.finance.logic.CostMatchingLogic;
import com.jwipc.finance.model.query.CostMatchingQuery;
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.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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 + "/cost/matching/rest")
@RequiredArgsConstructor
public class CostMatchingRestController {
    private final CostMatchingLogic costMatchingLogic;
    @PostMapping("/list")
    public PageView list(CostMatchingQuery query){
        if (LoginUtil.isSupplier()){
            query.setSupplierCode(LoginUtil.getSupplierCode());
        }else {
//            query.setUserId(LoginUtil.getUserId());
        }
        return costMatchingLogic.findByPage(query);
    }

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

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

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

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

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

    /**
     * 财务确认
     *
     * @param param 参数
     */
    @PostMapping("/financeConfirm")
    public ResponseEntity financeConfirm(@RequestBody CostMatchingParamDTO param) {
        costMatchingLogic.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){
            costMatchingLogic.createPrefabricateInvoice(Long.valueOf(id),type,invoicePostDate,statementType);
        }
        return ResponseEntity.ok(true);
    }
}
