package com.dayou.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.dayou.common.BaseEntity;
import com.dayou.entity.FinanceClaim;
import com.dayou.entity.OrderFund;
import com.dayou.entity.ProductionFund;
import com.dayou.enums.MainBusinessEnum;
import com.dayou.mapper.OrderFundMapper;
import com.dayou.mapper.ProductionFundMapper;
import com.dayou.service.IFinanceClaimService;
import com.dayou.service.IOrderFundService;
import com.dayou.service.IProductionFundService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.vo.ProductionFundVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dayou.utils.ExcelUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;
import com.dayou.enums.BatchTaskTypeEnum;

/**
 * <p>
 * 业务产品收款表 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2023-12-08
 */
@DS("mbs")
@Service
public class ProductionFundServiceImpl extends ServiceImpl<ProductionFundMapper, ProductionFund> implements IProductionFundService {

    private static final BigDecimal E100 = new BigDecimal("1000000");
    private static final BigDecimal E1000 = new BigDecimal("10000000");
    private static final BigDecimal E5000 = new BigDecimal("50000000");
    private static final BigDecimal E10000 = new BigDecimal("100000000");
    private static final BigDecimal E100000 = new BigDecimal("1000000000");
    private static final BigDecimal R11 = new BigDecimal("0.011");
    private static final BigDecimal R4P5 = new BigDecimal("0.0045");
    private static final BigDecimal R1P2 = new BigDecimal("0.0012");
    private static final BigDecimal R0P75 = new BigDecimal("0.00075");
    private static final BigDecimal R0P15 = new BigDecimal("0.00015");
    private static final BigDecimal R0P1 = new BigDecimal("0.0001");

    @Autowired
    private IFinanceClaimService financeClaimService;

    @Autowired
    private IOrderFundService orderFundService;
    @Autowired
    private ProductionFundMapper productionFundMapper;
    @Autowired
    private OrderFundMapper orderFundMapper;

    @Override
    @SuppressWarnings("unchecked")
    public Page<ProductionFund> selectPage(Page page,ProductionFund productionFund){
        return this.page(page, new QueryWrapper<ProductionFund>(productionFund));
    }


    @Override
    public ProductionFund detail(Long id){
        return this.getById(id);
    }

    @Transactional
    @Override
    public Boolean add(ProductionFund productionFund){
        BigDecimal evaluateAmount = productionFund.getEvaluateAmount();
        BigDecimal standardAmount = calculateStandardAmount(evaluateAmount);
        productionFund.setStandardAmount(standardAmount);
        if (productionFund.getOrderFundId()==null){
            OrderFund orderFund = new OrderFund();
            orderFund.setOrderName(productionFund.getOrderName());
            orderFund.setOrderId(productionFund.getOrderId());
            orderFund.setBusinessId(productionFund.getBusinessId());
            orderFund.setBusinessType(productionFund.getBusinessType());
            orderFundService.save(orderFund);
            productionFund.setOrderFundId(orderFund.getId());
        }
        return  this.save(productionFund);
    }

    @Override
    public Boolean update(ProductionFund productionFund){
        return  this.updateById(productionFund);
    }

    @Override
    public Boolean delete(Long id){
        //逻辑删除
        return this.removeById(id);
    }

    @Override
    public Boolean updateRealFundForProduction(Long id) {
        BigDecimal totalClaimAmount =  financeClaimService.getTotalClaimAmountByProductionFundId(id);
        return this.update(new LambdaUpdateWrapper<ProductionFund>().set(ProductionFund::getRealAmount,totalClaimAmount).eq(BaseEntity::getId,id));
    }

    @Override
    public ProductionFundVO checkFund(ProductionFundVO productionFundVO) {
        //认领在产品上
        ProductionFund productionFund = this.getOne(new LambdaQueryWrapper<ProductionFund>().eq(ProductionFund::getBusinessType, productionFundVO.getBusinessType().name())
                .eq(ProductionFund::getBusinessId, productionFundVO.getBusinessId()).eq(ProductionFund::getProductionNo, productionFundVO.getProductionNo())
                .eq(ProductionFund::getProductionType, productionFundVO.getProductionType().name()).eq(BaseEntity::getDeleted, Boolean.FALSE));
        //认领在订单上
        if (productionFund==null || productionFund.getRealAmount()==null || productionFund.getRealAmount().compareTo(BigDecimal.ZERO)==0){
            //产品收款无记录，查询订单是否有收款
//            OrderFund orderFund = orderFundService.getOne(new LambdaQueryWrapper<OrderFund>().eq(OrderFund::getBusinessType, productionFundVO.getBusinessType().name())
//                    .eq(OrderFund::getBusinessId, productionFundVO.getBusinessId()).eq(BaseEntity::getDeleted, Boolean.FALSE));
//            if (orderFund == null || orderFund.getRealAmount()==null || orderFund.getRealAmount().compareTo(BigDecimal.ZERO)==0){
//                productionFundVO.setRealAmount(BigDecimal.ZERO);
//            }else {
//                productionFundVO.setRealAmount(orderFund.getRealAmount());
//            }
            // 以产品收款为准，不查询订单收款
            productionFundVO.setRealAmount(BigDecimal.ZERO);
        }else {
            productionFundVO.setRealAmount(productionFund.getRealAmount());
        }
        return productionFundVO;
    }


    @Override
    public BigDecimal calculateStandardAmount(BigDecimal evaluateAmount){
        BigDecimal standardAmount = BigDecimal.ZERO;
        if (evaluateAmount !=null){
            if (evaluateAmount.compareTo(E100)<=0 && evaluateAmount.compareTo(BigDecimal.ZERO)>0){
                standardAmount = evaluateAmount.multiply(R11);
            }else if (evaluateAmount.compareTo(E1000)<=0 && evaluateAmount.compareTo(E100)>0){
                standardAmount = evaluateAmount.multiply(R4P5);
            }
            else if (evaluateAmount.compareTo(E5000)<=0 && evaluateAmount.compareTo(E1000)>0){
                standardAmount = evaluateAmount.multiply(R1P2);
            }
            else if (evaluateAmount.compareTo(E10000)<=0 && evaluateAmount.compareTo(E5000)>0){
                standardAmount = evaluateAmount.multiply(R0P75);
            }
            else if (evaluateAmount.compareTo(E100000)<=0 && evaluateAmount.compareTo(E10000)>0){
                standardAmount = evaluateAmount.multiply(R0P15);
            }
            else if (evaluateAmount.compareTo(E100000)>0){
                standardAmount = evaluateAmount.multiply(R0P1);
            }
        }
        return standardAmount;
    }

    @Override
    public BigDecimal getProductionRealAmountByProductionNo(String productionNo) {
        try {
            if (StrUtil.isNotEmpty(productionNo)){
                ProductionFund one = this.getOne(new LambdaQueryWrapper<ProductionFund>().eq(ProductionFund::getProductionNo, productionNo)
                        .eq(BaseEntity::getDeleted, Boolean.FALSE));
                if (one==null){
                    return BigDecimal.ZERO;
                }
                if (one.getRealAmount()==null){
                    return BigDecimal.ZERO;
                }
                return one.getRealAmount();
            }
            return BigDecimal.ZERO;
        }catch (Exception e){
            return BigDecimal.ZERO;
        }
    }

    @Override
    public void updateProductionRealAmount(Long productionFundId , BigDecimal rollbackAmount) {
        productionFundMapper.updateProductionRealAmount(productionFundId,rollbackAmount);
    }

    @Override
    public List<ProductionFund> selectProductionFundIdByDyoaReportNo() {
        return productionFundMapper.selectProductionFundIdByDyoaReportNo();
    }

    @Override
    public List<ProductionFund> getProductionFundIdByOrderFundId() {
        return productionFundMapper.getProductionFundIdByOrderFundId();
    }


}
