package com.friday.custody.service.basicService.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.friday.custody.dao.SubscribeFundInfoDao;
import com.friday.custody.entity.SubscribeFundInfoEntity;
import com.friday.custody.service.basicService.SubscribeFundInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;

import static com.friday.custody.common.CustodyContants.NOTICE_TYPE_CHANGE;
import static com.friday.custody.common.CustodyContants.NOTICE_TYPE_OVER;


@Service("subscribeFundInfoService")
public class SubscribeFundInfoServiceImpl extends ServiceImpl<SubscribeFundInfoDao, SubscribeFundInfoEntity> implements SubscribeFundInfoService {


    @Override
    public List<SubscribeFundInfoEntity> queryByFund(SubscribeFundInfoEntity entity) {
        LambdaQueryWrapper<SubscribeFundInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(entity.getAccountType())) {
            queryWrapper.eq(SubscribeFundInfoEntity::getAccountType, entity.getAccountType());
        }
        if (StringUtils.isNotBlank(entity.getSubscribeFundCode())) {
            queryWrapper.eq(SubscribeFundInfoEntity::getSubscribeFundCode, entity.getSubscribeFundCode());
        }
        if (StringUtils.isNotBlank(entity.getAdmin())) {
            queryWrapper.eq(SubscribeFundInfoEntity::getAdmin, entity.getAdmin());
        }
        if (StringUtils.isNotBlank(entity.getTrustNo())) {
            queryWrapper.eq(SubscribeFundInfoEntity::getTrustNo, entity.getTrustNo());
        }
        List<SubscribeFundInfoEntity> subscribeFundInfoEntities = baseMapper.selectList(queryWrapper);
        return subscribeFundInfoEntities;
    }

    @Override
    public Boolean isReminder(BigDecimal accountBalance, String admin, String trustNo, String accountType, String fundCode) {

        //查询出此产品的提醒类型 如果是超限提醒, 则判断是否超过上下限
        List<SubscribeFundInfoEntity> subscribeFundInfoEntities = queryByFund(SubscribeFundInfoEntity.builder()
                .subscribeFundCode(fundCode)
                .accountType(accountType)
                .admin(admin)
                .trustNo(trustNo)
                .build());
        if (CollectionUtils.isEmpty(subscribeFundInfoEntities)) {
            return false;
        }
        SubscribeFundInfoEntity subscribeFundInfoEntity = subscribeFundInfoEntities.get(0);
        BigDecimal bigDecimal = new BigDecimal(10000);
        BigDecimal highLimitValue = subscribeFundInfoEntity.getHighLimitValue();//上限值 (万元)
        BigDecimal lowLimitValue = subscribeFundInfoEntity.getLowLimitValue();//下限值 (万元)
        if (subscribeFundInfoEntity.getReminderType() == NOTICE_TYPE_CHANGE) {
            return true;
        }
        if (subscribeFundInfoEntity.getReminderType() == NOTICE_TYPE_OVER) {
            if (!ObjectUtils.isEmpty(highLimitValue)) {
                BigDecimal highLimit = highLimitValue.multiply(bigDecimal).setScale(2); //上限值 (元)
                if (accountBalance.compareTo(highLimit) > 0) {
                    return true;
                }
            }
            if (!ObjectUtils.isEmpty(lowLimitValue)) {
                BigDecimal lowLimit = lowLimitValue.multiply(bigDecimal).setScale(2); //上限值 (元)
                if (accountBalance.compareTo(lowLimit) < 0) {
                    return true;
                }
            }
        }
        return false;
    }
}