package com.jwipc.finance.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jwipc.finance.dto.CustomsDeclarationDTO;
import com.jwipc.finance.dto.PaymentMatchingDTO;
import com.jwipc.finance.entity.CustomsDeclaration;
import com.jwipc.finance.entity.CustomsDeclarationDetail;
import com.jwipc.finance.logic.CustomsDeclarationLogic;
import com.jwipc.finance.logic.PaymentMatchingLogic;
import com.jwipc.finance.service.ICustomsDeclarationDetailService;
import com.jwipc.finance.service.ICustomsDeclarationService;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.client.UserPurchaseGroupClient;
import com.jwipc.sys.dto.AttachmentTypeConfigDTO;
import com.jwipc.sys.logic.AttachmentTypeConfigLogic;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

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

/**
 * <p>
 * 对账模块-报关单主表 前端控制器
 * </p>
 *
 * @author shenyang.ou
 * @since 2025-10-22
 */
@Controller
@RequestMapping(MODULE_ROOT + "/payment/matching")
@RequiredArgsConstructor
public class PaymentMatchingPageController {
    private final PaymentMatchingLogic paymentMatchingLogic;
    private final ICustomsDeclarationService customsDeclarationService;
    private final ICustomsDeclarationDetailService customsDeclarationDetailService;
    private final UserPurchaseGroupClient userPurchaseGroupClient;
    private final AttachmentTypeConfigLogic attachmentTypeConfigLogic;

    @GetMapping("/index")
    public String index(Model model){
        model.addAttribute("menuType", 0);
        return MODULE_ROOT + "/paymentMatching/list";
    }

    @GetMapping("/purchase/index")
    public String purchaseIndex(Model model){
        model.addAttribute("menuType", 1);
        return MODULE_ROOT + "/paymentMatching/list";
    }

    @GetMapping("/finance/index")
    public String financeIndex(Model model){
        model.addAttribute("menuType", 2);
        return MODULE_ROOT + "/paymentMatching/finance_list";
    }

    @GetMapping("/detail")
    public String detail(Model model, Long id,Integer optType) {
        PaymentMatchingDTO paymentMatchingDTO = new PaymentMatchingDTO();
        if (ObjectUtil.isNotNull(id)){
            paymentMatchingDTO = paymentMatchingLogic.getById(id);
            if (optType==3){
                paymentMatchingDTO.setCode(null);
            }
            CustomsDeclarationDetail customsDeclarationDetail = customsDeclarationDetailService.getOne(new LambdaQueryWrapper<>(CustomsDeclarationDetail.class)
                    .eq(CustomsDeclarationDetail::getCode, paymentMatchingDTO.getCustomsDeclarationDetailCode()).last(" limit 1"));
            if (ObjectUtil.isNotNull(customsDeclarationDetail)){
                model.addAttribute("mainId", customsDeclarationDetail.getMainId());
                CustomsDeclaration customsDeclaration = customsDeclarationService.getById(customsDeclarationDetail.getMainId());
                if (ObjectUtil.isNotNull(customsDeclaration)){
                    model.addAttribute("imgCode", customsDeclaration.getImgCode());
                }
            }
        }else {
            //默认采购组
            paymentMatchingDTO.setPurchaseGroupCode(userPurchaseGroupClient.getMarkPurchaseGroupCodeByUserId(LoginUtil.getUserId()));
            if (LoginUtil.isSupplier()){
                paymentMatchingDTO.setPurchaseGroupCode("501");
            }
        }
        model.addAttribute("data", paymentMatchingDTO);
        List<AttachmentTypeConfigDTO> attachmentTypeCreateConfigs = attachmentTypeConfigLogic.listByBusinessTypeCode("payment_matching_create");
        List<AttachmentTypeConfigDTO> attachmentTypeCheckConfigs = attachmentTypeConfigLogic.listByBusinessTypeCode("payment_matching_check");
        model.addAttribute("attachmentTypeCreateConfigs", JSONUtil.toJsonStr(attachmentTypeCreateConfigs));
        model.addAttribute("attachmentTypeCheckConfigs", JSONUtil.toJsonStr(attachmentTypeCheckConfigs));
        model.addAttribute("tbAttachmentLookFlag", 0);
        model.addAttribute("optType", optType);
        model.addAttribute("lookFlag", optType==2);
        return MODULE_ROOT + "/paymentMatching/detail";
    }

    @GetMapping("/financePost")
    public String financePost(Long id, Model model){
        PaymentMatchingDTO paymentMatchingDTO = paymentMatchingLogic.getById(id);
        model.addAttribute("id",id);
        model.addAttribute("data", paymentMatchingDTO);
        return MODULE_ROOT + "/paymentMatching/finance_post";
    }
    @GetMapping("/updateFinancePost")
    public String updateFinancePost(Long id, Model model){
        PaymentMatchingDTO paymentMatchingDTO = paymentMatchingLogic.getById(id);
        if (StrUtil.isEmpty(paymentMatchingDTO.getVoucherYear())){
            paymentMatchingDTO.setVoucherYear(DateUtil.format(new java.util.Date(), "yyyy"));
        }
        model.addAttribute("id",id);
        model.addAttribute("data", paymentMatchingDTO);
        return MODULE_ROOT + "/paymentMatching/update_finance_post";
    }
}
