package cn.pluss.platform.activityRecharge.impl;

import cn.pluss.platform.activityRecharge.ActivityRechargeService;
import cn.pluss.platform.entity.ActivityRecharge;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.ActivityRechargeMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

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

@Service("activityRechargeService")
public class ActivityRechargeServiceImpl extends ServiceImpl<ActivityRechargeMapper, ActivityRecharge> implements ActivityRechargeService {

    @Override
    public ActivityRecharge queryActivityRecharge(ActivityRecharge activityRecharge) {
        return baseMapper.selectOne(new QueryWrapper<>(activityRecharge));
    }

    @Override
    public List<ActivityRecharge> queryActivityRechargeList(ActivityRecharge activityRecharge) {
        return baseMapper.queryActivityRechargeList(activityRecharge);
    }

    @Override
    public void saveActivityRecharge(ActivityRecharge activityRecharge) {
        baseMapper.insert(activityRecharge);
    }

    @Override
    public void updateActivityRecharge(ActivityRecharge activityRecharge) {
        baseMapper.updateById(activityRecharge);
    }

    @Override
    public void deleteActivityRecharge(ActivityRecharge activityRecharge) {
        baseMapper.deleteById(activityRecharge.getId());
    }

    @Override
    public void delByUserId(Integer userId) {
        if (userId == null) {
            throw new MsgException("缺少用户id");
        }

        ActivityRecharge entity = new ActivityRecharge();
        entity.setUserId(userId);

        baseMapper.delete(new QueryWrapper<>(entity));
    }

    @Override
    public List<ActivityRecharge> queryActivityRechargePage(Map<String, Object> map) {
        return baseMapper.queryActivityRechargePage(map);
    }

    @Override
    public List<ActivityRecharge> queryActivityNoRechargePage(Map<String, Object> map) {
        return baseMapper.queryActivityNoRechargePage(map);
    }

    @Override
    public Integer queryActivityRechargePageCount(Map<String, Object> map) {
        return baseMapper.queryActivityRechargePageCount(map);
    }

    @Override
    public Integer queryActivityNoRechargePageCount(Map<String, Object> map) {
        return baseMapper.queryActivityNoRechargePageCount(map);
    }

    @Override
    public double getRechargeReturnAmt(String merchantCode, String storeId, double rechargeMoney) {
        ActivityRecharge activityRecharge = new ActivityRecharge();
        activityRecharge.setMerchantCode(merchantCode);
        activityRecharge.setStoreId(storeId);
        double giveMoney = 0.0d;
        List<ActivityRecharge> queryActivityRechargeList = queryActivityRechargeList(activityRecharge);
        if (queryActivityRechargeList != null && queryActivityRechargeList.size() > 0) {
            queryActivityRechargeList = rechargeMoneyDesc(queryActivityRechargeList);
            for (ActivityRecharge a : queryActivityRechargeList) {
                if (1 == a.getTimeType()) {
                    if (a.getStartDt() == null || a.getEndDt() == null) {
                        continue;
                    }
                    boolean bol = isBoolean(a.getStartDt(), a.getEndDt());
                    if (!bol) {
                        deleteActivityRecharge(a);
                        continue;
                    }
                }
                if (null != a.getRechargeMoney()) {
                    if (BigDecimal.valueOf(rechargeMoney).compareTo(BigDecimal.valueOf(a.getRechargeMoney())) > -1) {
                        // 返现金额
                        giveMoney = a.getGiveMoney() == null ? 0d : a.getGiveMoney();
                        break;
                    }
                }
            }
        }
        return giveMoney;
    }

    /**
     * rechargeMoneyDesc:(充值金额从大到小). <br/>
     *
     * @param list
     * @return
     *
     * @author Administrator
     * @since JDK 1.8
     */
    public List<ActivityRecharge> rechargeMoneyDesc(List<ActivityRecharge> list) {
        list.sort((o1, o2) -> (int) (o2.getRechargeMoney() - o1.getRechargeMoney()));
        return list;

    }

    /**
     * isBoolean:(当前时间大于活动开始时间，并且当前时间小于活动结束时间). <br/>
     *
     * @return
     *
     * @author Administrator
     * @since JDK 1.8
     */
    public boolean isBoolean(Date startTime, Date endTime) {
        boolean flag = false;
        long current = System.currentTimeMillis();
        boolean start = current >= startTime.getTime();
        boolean end = current <= endTime.getTime();
        if (start && end) {
            flag = true;
        }
        return flag;
    }

    /**
     * 查询进行中的活动
     * @param activityRecharge
     * @return
     */
    @Override
    public List<ActivityRecharge> queryUsableActivityRechageList(ActivityRecharge activityRecharge) {
        return baseMapper.queryUsableActivityRechageList(activityRecharge);
    }
}

