package com.jwipc.finance.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jwipc.finance.dto.PaymentAdvanceApplyListDTO;
import com.jwipc.finance.dto.PaymentAdvanceApplySaveDto;
import com.jwipc.finance.dto.PaymentCodApplyDTO;
import com.jwipc.finance.entity.PaymentChange;
import com.jwipc.finance.logic.PaymentAdvanceApplyLogic;
import com.jwipc.finance.logic.PaymentCodApplyLogic;
import com.jwipc.finance.service.IPaymentChangeService;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.security.LoginUtil;
import com.jwipc.supplier.clent.dto.SupplierDTO;
import com.jwipc.supplier.client.SupplierClient;
import lombok.RequiredArgsConstructor;
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.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Objects;

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

/**
 * <p>
 * 对账付款申请主表 前端控制器
 * </p>
 *
 * @author WT
 * @since 2024-03-08
 */
@Controller
@RequestMapping(MODULE_ROOT + "/payment/cod/apply")
@RequiredArgsConstructor
public class PaymentCodApplyPageController {
    private final PaymentCodApplyLogic paymentCodApplyLogic;
    private final PaymentAdvanceApplyLogic paymentAdvanceApplyLogic;
    private final SupplierClient supplierClient;
    private final IPaymentChangeService paymentChangeService;
    /**
     * 对账付款申请首页
     *
     * @param model model
     */
    @GetMapping("/index")
    public String index(Model model,Integer type) {
        if(LoginUtil.isSupplier()){
            // 限制新增对账标识 0:不限制 1:限制
            model.addAttribute("restrictFlag", 0);
        }
        model.addAttribute("type",type);
        model.addAttribute("isSupplier",LoginUtil.isSupplier()?1:0);
        return MODULE_ROOT + "/payment/cod/index";
    }

    /**
     * 新增对账付款申请
     *
     * @param model model
     */
    @AuditLogInter(value = "数据详情【optType：0新增、1编辑】", module = "对账付款申请")
    @GetMapping("/detail")
    public String detail(Model model, Long id,Integer optType) {
        PaymentCodApplyDTO paymentCodApplyDTO = new PaymentCodApplyDTO();
        if (ObjectUtil.isNotNull(id)){
            paymentCodApplyDTO = paymentCodApplyLogic.getById(id);
            paymentCodApplyDTO.setPayPlanMoney(paymentCodApplyDTO.getPayPlanMoney() == null ? BigDecimal.ZERO: paymentCodApplyDTO.getPayPlanMoney());
            // 设置小数点后两位，使用RoundingMode.HALF_UP进行四舍五入
            paymentCodApplyDTO.setPayPlanMoney(paymentCodApplyDTO.getPayPlanMoney() == null? BigDecimal.ZERO: paymentCodApplyDTO.getPayPlanMoney());
            BigDecimal roundedNumber = paymentCodApplyDTO.getPayPlanMoney().setScale(2, RoundingMode.HALF_UP);
            paymentCodApplyDTO.setPayPlanMoney(roundedNumber);
            SupplierDTO supplierDTO = supplierClient.getSupplierByCode(paymentCodApplyDTO.getSupplierCode());
            model.addAttribute("supplierId", supplierDTO.getId());
        }else {
            paymentCodApplyDTO.setFeePayer("02");
            paymentCodApplyDTO.setUrgentFlag(0);
        }
        model.addAttribute("isSupplier", LoginUtil.isSupplier()?1:0);
        model.addAttribute("optType",optType);
        model.addAttribute("data", paymentCodApplyDTO);
        model.addAttribute("lookFlag", optType==2);
        model.addAttribute("userAccount", LoginUtil.getAccount());
        return MODULE_ROOT + "/payment/cod/detail";
    }

    @GetMapping("/change")
    public String change(Model model, Long id,Integer businessType) {
        if (businessType==0){
            PaymentCodApplyDTO paymentCodApplyDTO = new PaymentCodApplyDTO();
            if (ObjectUtil.isNotNull(id)){
                paymentCodApplyDTO = paymentCodApplyLogic.getById(id);
                paymentCodApplyDTO.setPayPlanMoney(paymentCodApplyDTO.getPayPlanMoney() == null ? BigDecimal.ZERO: paymentCodApplyDTO.getPayPlanMoney());
                // 设置小数点后两位，使用RoundingMode.HALF_UP进行四舍五入
                paymentCodApplyDTO.setPayPlanMoney(paymentCodApplyDTO.getPayPlanMoney() == null? BigDecimal.ZERO: paymentCodApplyDTO.getPayPlanMoney());
                BigDecimal roundedNumber = paymentCodApplyDTO.getPayPlanMoney().setScale(2, RoundingMode.HALF_UP);
                paymentCodApplyDTO.setPayPlanMoney(roundedNumber);
                SupplierDTO supplierDTO = supplierClient.getSupplierByCode(paymentCodApplyDTO.getSupplierCode());
                model.addAttribute("supplierId", supplierDTO.getId());
            }
            model.addAttribute("data", paymentCodApplyDTO);
            PaymentChange change = paymentChangeService.getOne(new LambdaQueryWrapper<PaymentChange>().eq(PaymentChange::getAppNo, paymentCodApplyDTO.getCpApplyNo())
                    .eq(PaymentChange::getBusinessType, businessType));
            model.addAttribute("change", change);
        }else {
            PaymentAdvanceApplyListDTO apply = new PaymentAdvanceApplyListDTO();
            if (ObjectUtil.isNotNull(id)){
                apply = paymentAdvanceApplyLogic.selectById(id);
                apply.setBudMoney(apply.getBudOutMoney().add(apply.getBudInnerMoney()));
                // 设置小数点后两位，使用RoundingMode.HALF_UP进行四舍五入
                apply.setPayPlanMoney(apply.getPayPlanMoney() == null? BigDecimal.ZERO: apply.getPayPlanMoney());
                BigDecimal roundedNumber = apply.getPayPlanMoney().setScale(2, RoundingMode.HALF_UP);
                apply.setPayPlanMoney(roundedNumber);

                SupplierDTO supplierDTO = supplierClient.getSupplierByCode(apply.getSupplierCode());
                if (ObjectUtil.isNotNull(supplierDTO)){
                    model.addAttribute("supplierId", supplierDTO.getId());
                }
            }
            model.addAttribute("data", apply);
            PaymentChange change = paymentChangeService.getOne(new LambdaQueryWrapper<PaymentChange>().eq(PaymentChange::getAppNo, apply.getApApplyNo())
                    .eq(PaymentChange::getBusinessType, businessType));
            model.addAttribute("change", change);
        }


        model.addAttribute("isSupplier", LoginUtil.isSupplier()?1:0);

        model.addAttribute("businessType", businessType);
        model.addAttribute("userAccount", LoginUtil.getAccount());
        return MODULE_ROOT + "/payment/cod/change";
    }


//    /**
//     * 编辑对账付款申请
//     *
//     * @param model model
//     * @param id 付款申请ID
//     * @return 结果
//     */
//    @GetMapping("/edit")
//    public String edit(Model model, Long id,String companyType){
//        model.addAttribute("isSupplier", LoginUtil.isSupplier()?1:0);
//        final PaymentCodApplyDTO apply = paymentCodApplyLogic.getById(id);
//        Long supplierId = paymentCodApplyService.findBySupplierCode(apply.getSupplierCode());
//        apply.setBudMoney(apply.getBudOutMoney().add(apply.getBudInnerMoney()));
//        model.addAttribute("data", apply);
//        model.addAttribute("supplierId", supplierId);
//        model.addAttribute("companyType",companyType);
//        model.addAttribute("pageType","edit");
//        return MODULE_ROOT + "/payment/cod/edit";
//    }

    /**
     * 订单选择器
     * @param companyCode 公司
     * @param supplierCode 供应商
     * @param currencyCode 币种
     * @return
     */
    @GetMapping("/choice/invoice")
    public String choiceOrder(Model model, String companyCode, String supplierCode, String currencyCode, String parentFrameName){
        model.addAttribute("parentFrameName", parentFrameName);
        model.addAttribute("companyCode",companyCode);
        model.addAttribute("supplierCode",supplierCode);
        model.addAttribute("currencyCode",currencyCode);
        return MODULE_ROOT + "/payment/cod/choiceInvoice";
    }
}
