package com.jwipc.finance.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.base.Joiner;
import com.jwipc.finance.dto.CostMatchingDTO;
import com.jwipc.finance.dto.PaymentMatchingDTO;
import com.jwipc.finance.entity.CustomsDeclaration;
import com.jwipc.finance.entity.CustomsDeclarationDetail;
import com.jwipc.finance.entity.PaymentNotice;
import com.jwipc.finance.entity.PaymentNoticeDetail;
import com.jwipc.finance.logic.CostMatchingLogic;
import com.jwipc.finance.service.IPaymentNoticeDetailService;
import com.jwipc.finance.service.IPaymentNoticeService;
import com.jwipc.order.client.DeliverOrderClient;
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 + "/cost/matching")
@RequiredArgsConstructor
public class CostMatchingPageController {
    private final CostMatchingLogic costMatchingLogic;
    private final UserPurchaseGroupClient userPurchaseGroupClient;
    private final AttachmentTypeConfigLogic attachmentTypeConfigLogic;
    private final DeliverOrderClient deliverOrderClient;
    private final IPaymentNoticeService paymentNoticeService;
    private final IPaymentNoticeDetailService paymentNoticeDetailService;

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

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

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

    @GetMapping("/detail")
    public String detail(Model model, Long id,Integer optType) {
        CostMatchingDTO costMatchingDTO = new CostMatchingDTO();
        if (ObjectUtil.isNotNull(id)){
            costMatchingDTO = costMatchingLogic.getById(id);
            if (optType==3){
                costMatchingDTO.setCode(null);
            }
            PaymentNoticeDetail paymentNoticeDetail = paymentNoticeDetailService.getOne(new LambdaQueryWrapper<>(PaymentNoticeDetail.class)
                    .eq(PaymentNoticeDetail::getCode, costMatchingDTO.getPaymentNoticeDetailCode()).last(" limit 1"));
            if (ObjectUtil.isNotNull(paymentNoticeDetail)){
                model.addAttribute("mainId", paymentNoticeDetail.getMainId());
                PaymentNotice paymentNotice = paymentNoticeService.getById(paymentNoticeDetail.getMainId());
                if (ObjectUtil.isNotNull(paymentNotice)){
                    model.addAttribute("imgCode", paymentNotice.getImgCode());
                }
            }
        }else {
            //默认采购组
            costMatchingDTO.setPurchaseGroupCode(userPurchaseGroupClient.getMarkPurchaseGroupCodeByUserId(LoginUtil.getUserId()));
            if (LoginUtil.isSupplier()){
                costMatchingDTO.setPurchaseGroupCode("501");
            }
        }
        model.addAttribute("data", costMatchingDTO);
        model.addAttribute("optType", optType);
        model.addAttribute("tbAttachmentLookFlag", 0);
        List<AttachmentTypeConfigDTO> attachmentTypeCreateConfigs = attachmentTypeConfigLogic.listByBusinessTypeCode("cost_matching_create");
        List<AttachmentTypeConfigDTO> attachmentTypeCheckConfigs = attachmentTypeConfigLogic.listByBusinessTypeCode("cost_matching_check");
        model.addAttribute("attachmentTypeCreateConfigs", JSONUtil.toJsonStr(attachmentTypeCreateConfigs));
        model.addAttribute("attachmentTypeCheckConfigs", JSONUtil.toJsonStr(attachmentTypeCheckConfigs));
        model.addAttribute("lookFlag", optType==2);
        return MODULE_ROOT + "/costMatching/detail";
    }

    @GetMapping("/financePost")
    public String financePost(Long id, Model model){
        CostMatchingDTO costMatchingDTO = costMatchingLogic.getById(id);
        model.addAttribute("id",id);
        model.addAttribute("data", costMatchingDTO);
        return MODULE_ROOT + "/costMatching/finance_post";
    }
}
