package com.jwipc.finance.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jwipc.finance.dto.CustomsDeclarationDTO;
import com.jwipc.finance.entity.CustomsDeclarationApprove;
import com.jwipc.finance.logic.CustomsDeclarationLogic;
import com.jwipc.finance.service.ICustomsDeclarationApproveService;
import com.jwipc.security.LoginUtil;
import com.jwipc.supplier.clent.dto.SupplierPurchaseCategoryDTO;
import com.jwipc.supplier.client.SupplierClient;
import com.jwipc.sys.client.UserPurchaseGroupClient;
import com.jwipc.sys.dto.AttachmentTypeConfigDTO;
import com.jwipc.sys.logic.AttachmentTypeConfigLogic;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;

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
 */
@Controller
@RequestMapping(MODULE_ROOT + "/customs/declaration")
@RequiredArgsConstructor
public class CustomsDeclarationPageController {
    private final CustomsDeclarationLogic customsDeclarationLogic;
    private final UserPurchaseGroupClient userPurchaseGroupClient;
    private final SupplierClient supplierClient;
    private final AttachmentTypeConfigLogic attachmentTypeConfigLogic;
    private final ICustomsDeclarationApproveService customsDeclarationApproveService;

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

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

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

    @GetMapping("/dialog")
    public String dialog(Model model,Integer single){
        model.addAttribute("single", single);
        return MODULE_ROOT + "/customs/dialog";
    }

    @GetMapping("/detail")
    public String detail(Model model, Long id,Integer optType) {
        CustomsDeclarationDTO customsDeclarationDTO = new CustomsDeclarationDTO();
        if (ObjectUtil.isNotNull(id)){
            customsDeclarationDTO = customsDeclarationLogic.getById(id);
            if (optType==3){
                customsDeclarationDTO.setCode(null);
            }
        }else {
            //默认采购组
            customsDeclarationDTO.setPurchaseGroupCode(userPurchaseGroupClient.getMarkPurchaseGroupCodeByUserId(LoginUtil.getUserId()));
            if(LoginUtil.isSupplier()){
                customsDeclarationDTO.setSupplierCode(LoginUtil.getSupplierCode());
                customsDeclarationDTO.setSupplierName(LoginUtil.getName());
                List<SupplierPurchaseCategoryDTO> supplierPurchaseCategoryDTOS = supplierClient.getList(String.valueOf(LoginUtil.getSupplierId()));
                if (CollectionUtil.isNotEmpty(supplierPurchaseCategoryDTOS)){
                    //过滤优先标记
                    List<SupplierPurchaseCategoryDTO> filterSupplierPurchaseCategoryDTOS = supplierPurchaseCategoryDTOS.stream().filter(supplierPurchaseCategoryDTO -> supplierPurchaseCategoryDTO.getFirstFlag()==1).toList();
                    if (CollectionUtil.isEmpty(filterSupplierPurchaseCategoryDTOS)){
                        //过滤公司
                        List<SupplierPurchaseCategoryDTO> filterCompanyDTOS = supplierPurchaseCategoryDTOS.stream().filter(supplierPurchaseCategoryDTO -> "1000".equals(supplierPurchaseCategoryDTO.getCompanyCode())).toList();
                        if (CollectionUtil.isNotEmpty(filterCompanyDTOS)){
                            customsDeclarationDTO.setCompanyCode(filterCompanyDTOS.get(0).getCompanyCode());
                            customsDeclarationDTO.setCurrencyCode(filterCompanyDTOS.get(0).getCurrencyCode());
//                            customsDeclarationDTO.setTaxCode(filterCompanyDTOS.get(0).getTaxCode());
//                            customsDeclarationDTO.setPaymentTermCode(filterCompanyDTOS.get(0).getPaymentTerms());
//                            customsDeclarationDTO.setPaymentWay(filterCompanyDTOS.get(0).getPaymentMethod());
                            customsDeclarationDTO.setPurchaseGroupCode(filterCompanyDTOS.get(0).getPurchaseGroupCode());
                        }else {
                            customsDeclarationDTO.setCompanyCode(supplierPurchaseCategoryDTOS.get(0).getCompanyCode());
                            customsDeclarationDTO.setCurrencyCode(supplierPurchaseCategoryDTOS.get(0).getCurrencyCode());
//                            customsDeclarationDTO.setTaxCode(supplierPurchaseCategoryDTOS.get(0).getTaxCode());
//                            customsDeclarationDTO.setPaymentTermCode(supplierPurchaseCategoryDTOS.get(0).getPaymentTerms());
//                            customsDeclarationDTO.setPaymentWay(supplierPurchaseCategoryDTOS.get(0).getPaymentMethod());
                            customsDeclarationDTO.setPurchaseGroupCode(supplierPurchaseCategoryDTOS.get(0).getPurchaseGroupCode());
                        }
                    }else {
                        customsDeclarationDTO.setCompanyCode(filterSupplierPurchaseCategoryDTOS.get(0).getCompanyCode());
                        customsDeclarationDTO.setCurrencyCode(filterSupplierPurchaseCategoryDTOS.get(0).getCurrencyCode());
//                        customsDeclarationDTO.setTaxCode(filterSupplierPurchaseCategoryDTOS.get(0).getTaxCode());
//                        customsDeclarationDTO.setPaymentTermCode(filterSupplierPurchaseCategoryDTOS.get(0).getPaymentTerms());
//                        customsDeclarationDTO.setPaymentWay(filterSupplierPurchaseCategoryDTOS.get(0).getPaymentMethod());
                        customsDeclarationDTO.setPurchaseGroupCode(filterSupplierPurchaseCategoryDTOS.get(0).getPurchaseGroupCode());
                    }

                }

                customsDeclarationDTO.setPurchaseGroupCode("501");
            }
        }
        model.addAttribute("data", customsDeclarationDTO);
        model.addAttribute("optType", optType);
        model.addAttribute("lookFlag", optType==2);
        model.addAttribute("isSupplier", LoginUtil.isSupplier()?1:0);
        model.addAttribute("supplierId", LoginUtil.isSupplier() ? LoginUtil.getSupplierId() : 0);
        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);
        if (optType==2){
            CustomsDeclarationApprove customsDeclarationApprove = customsDeclarationApproveService.getOne(new LambdaQueryWrapper<>(CustomsDeclarationApprove.class)
                    .eq(CustomsDeclarationApprove::getMainId, customsDeclarationDTO.getId()).orderByDesc(CustomsDeclarationApprove::getApproveTime).last(" limit 1"));
            model.addAttribute("approveOpinion", ObjectUtil.isNull(customsDeclarationApprove)?"":customsDeclarationApprove.getApproveOpinion());
        }
        return MODULE_ROOT + "/customs/detail";
    }
}
