package com.tofly.feesapi.wxgl.wechatpay.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.StringUtil;
import com.tofly.feesapi.common.znj.ZnjCal;
import com.tofly.feesapi.sfgl.entity.YyQfmx;
import com.tofly.feesapi.sfgl.service.YyQfmxService;
import com.tofly.feesapi.sfgl.service.YySspjxxService;
import com.tofly.feesapi.wxgl.wechatpay.entity.PaySelect;
import com.tofly.feesapi.wxgl.wechatpay.entity.vo.*;
import com.tofly.feesapi.wxgl.wechatpay.mapper.PaySelectMapper;
import com.tofly.feesapi.wxgl.wechatpay.service.IPaySelectService;
import com.tofly.feesapi.yhgl.entity.YhKhjbxx;
import com.tofly.feesapi.yhgl.entity.YyZjzh;
import com.tofly.feesapi.yhgl.service.IYyZjzhService;
import com.tofly.feesapi.yhgl.service.YhKhjbxxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

/**
 * 支付查询接口
 */
@Service("PaySelectService")
public class PaySelectServiceImpl extends ServiceImpl<PaySelectMapper, PaySelect> implements IPaySelectService {

    @Autowired
    public YySspjxxService yySspjxxService;
    @Autowired
    private ZnjCal znjCal;
    @Autowired
    private YyQfmxService yyQfmxService;
    @Autowired
    private IYyZjzhService zjzhService;

    @Autowired
    private YhKhjbxxService yhKhjbxxService;

    /**
     * 获取用户欠费信息
     */
    public PaySelectQfVo getYhqfxx(PaySelect dto) {
        YhKhjbxx yhKhjbxx = yhKhjbxxService.getById(dto.getYhbh());
        PaySelectQfVo paySelectQfVo;
        if (StringUtil.isNotNull(yhKhjbxx) && yhKhjbxx.getDxh().equals(1)) {
            paySelectQfVo = baseMapper.getDhYhqfxx(dto, yhKhjbxx.getKhbm());
            paySelectQfVo.setDxh(1);
            paySelectQfVo.setSfbz("1");
            return paySelectQfVo;
        } else {
            List<YyQfmx> list = yyQfmxService.lambdaQuery()
                    .eq(YyQfmx::getSfbz, "0")
                    .eq(YyQfmx::getChbz, "0")
                    .eq(YyQfmx::getYhbh, dto.getYhbh())
                    .list();
            if (!list.isEmpty()) {
                //先进行违约金计算
                //znjCal.YyQfmxZnj(dto.getYhbh(), list);
                //用户资金账号
                YyZjzh zjzh = zjzhService.getById(dto.getYhbh());
                if (StringUtil.isNotNull(zjzh)) {
                    //进行自动扣减
                    YyQfmx yyQfmx = list.stream().min(Comparator.comparing(YyQfmx::getCbrq)).orElse(null);
                    if (zjzh.getSyje().compareTo(yyQfmx.getYsje().add(yyQfmx.getZnj())) >= 0) {
                        yySspjxxService.yffkj(dto.getYhbh(), zjzh.getSyje(), list);
                    }
                }
            }
            paySelectQfVo = baseMapper.getYhqfxx(dto);
        }
        return paySelectQfVo;
    }

    /**
     * 获取用户订单状态
     */
    public PaySelectOrderStatusVo getOrderStatus(PaySelect dto) {
        PaySelectOrderStatusVo orderStatusVo = baseMapper.getOrderStatus(dto);
        if (null == orderStatusVo) {
            orderStatusVo = new PaySelectOrderStatusVo();
            orderStatusVo.setOrderstatus("不存在");
        }
        return orderStatusVo;
    }

    /**
     * 获取用户订单状态
     */
    public List<PaySelectSfxmVo> getSfmxList(PaySelect dto) {
        return baseMapper.getSfmxList(dto);
    }

    @Override
    public List<PaySelectSfxmVo> getYhSfmxList(PaySelect dto) {
        return baseMapper.getYhSfmxList(dto);
    }

    @Override
    public List<QfSelectCbxmVo> getYhCbmxList(PaySelect dto) {
        return baseMapper.getYhCbmxList(dto);
    }

    @Override
    public List<YhSelectxxVo> getYhxxList(PaySelect dto) {
        return baseMapper.getYhxxList(dto);
    }
}
