package cn.pluss.platform.activityConsumReturn.impl;

import cn.pluss.platform.activityConsumReturn.ActivityConsumReturnService;
import cn.pluss.platform.entity.ActivityConsumReturn;
import cn.pluss.platform.entity.MerchantMenberComsume;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.ActivityConsumReturnMapper;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("activityConsumReturnService")
public class ActivityConsumReturnServiceImpl extends ServiceImpl<ActivityConsumReturnMapper, ActivityConsumReturn> implements ActivityConsumReturnService {

    @Override
    public ActivityConsumReturn queryActivityConsumReturn(ActivityConsumReturn activityConsumReturn) {
        return baseMapper.queryActivityConsumReturn(activityConsumReturn);
    }

    @Override
    public List<ActivityConsumReturn> queryActivityConsumReturnList(ActivityConsumReturn activityConsumReturn) {
        return baseMapper.queryActivityConsumReturnList(activityConsumReturn);
    }

    @Override
    public void saveActivityConsumReturn(ActivityConsumReturn activityConsumReturn) {
        activityConsumReturn.setStoreName(activityConsumReturn.getMerchantName());
        baseMapper.saveActivityConsumReturn(activityConsumReturn);
    }

    @Override
    public void updateActivityConsumReturn(ActivityConsumReturn activityConsumReturn) {
        baseMapper.updateActivityConsumReturn(activityConsumReturn);
    }

    @Override
    public void deleteActivityConsumReturn(ActivityConsumReturn activityConsumReturn) {
        baseMapper.deleteActivityConsumReturn(activityConsumReturn);
    }

    @Override
    public List<ActivityConsumReturn> queryActivityConsumReturnPage(Map map) {
        return baseMapper.queryActivityConsumReturnPage(map);
    }

    @Override
    public List<ActivityConsumReturn> queryActivityNoConsumReturnPage(Map map) {
        return baseMapper.queryActivityNoConsumReturnPage(map);
    }

    @Override
    public Integer queryActivityNoConsumReturnPageCount(Map map) {
        return baseMapper.queryActivityNoConsumReturnPageCount(map);
    }

    @Override
    public Integer queryActivityConsumReturnPageCount(Map map) {
        return baseMapper.queryActivityConsumReturnPageCount(map);
    }

    @Override
    public void saveActivityConsumReturnBatch(List<ActivityConsumReturn> activityConsumReturnList) {
        baseMapper.saveActivityConsumReturnBatch(activityConsumReturnList);
    }

    /**
     * 获取充值金额对应的活动返现金额
     *
     * @param merchantCode
     * @param storeId
     * @param consumeAmt
     * @return
     */
    @Override
    public MerchantMenberComsume getActivityReturnParams(String merchantCode, String storeId, Double consumeAmt) {
        /*
         * 查询所有消费返活动，选择其中最大的反额为消费返的金额
         */
        ActivityConsumReturn activityConsumReturn = new ActivityConsumReturn();
        activityConsumReturn.setMerchantCode(merchantCode);
        activityConsumReturn.setStoreId(storeId);
        List<ActivityConsumReturn> queryActivityConsumReturnList = baseMapper.queryActivityConsumReturnList(activityConsumReturn);
        Double consumeFee = StringUtil.bigDecimal(consumeAmt);// 消费金额
        Double returnMoney = 0d;
        MerchantMenberComsume merchantMenberComsume = new MerchantMenberComsume();
        merchantMenberComsume.setReturnMoney(returnMoney);
        merchantMenberComsume.setGiveMoney(0d);
        if (queryActivityConsumReturnList != null && queryActivityConsumReturnList.size() > 0) {
            queryActivityConsumReturnList = comsumeMoneyByDesc(queryActivityConsumReturnList);
            for (ActivityConsumReturn a : queryActivityConsumReturnList) {
                if (1 == a.getTimeType()) {
                    if (a.getStartDt() == null || a.getEndDt() == null) {
                        continue;
                    }
                    boolean bol = isBoolean(a.getStartDt(), a.getEndDt());
                    if (!bol) {
                        // 删除过期的消费返活动
                        baseMapper.deleteActivityConsumReturn(a);
                        continue;
                    }
                }
                if (null != a.getComsumeMoney()) {
                    //分等级返现
                    if (a.getType() == 0) {
                        if (consumeFee >= StringUtil.bigDecimal(a.getComsumeMoney())) {
                            merchantMenberComsume.setGiveMoney(StringUtil.bigDecimal(a.getReturnMoney()));// 返现金额
                            merchantMenberComsume.setReturnMoney(a.getReturnMoney());
                            break;
                        }
                    } else if (a.getType() == 1) {
                        if (consumeFee >= StringUtil.bigDecimal(a.getComsumeMoney())) {
                            Double d = a.getReturnMoney();
                            Double countD = consumeFee / a.getComsumeMoney();
                            Integer count = countD.intValue();
                            Double returnAmt = d * count;
                            merchantMenberComsume.setGiveMoney(StringUtil.bigDecimal(returnAmt));// 返现金额
                            returnMoney = StringUtil.bigDecimal(returnAmt);
                            merchantMenberComsume.setReturnMoney(returnMoney);
                            break;
                        }
                    }
                }
                if (a.getType() == 2) {
                    Double percentRate = a.getPercent() / 100;
                    Double d = consumeFee * percentRate;
                    Double returnAmt = StringUtil.bigDecimal(d);
                    merchantMenberComsume.setGiveMoney(returnAmt);
                    merchantMenberComsume.setReturnMoney(returnAmt);
                    break;
                }
            }
        }
        return merchantMenberComsume;
    }

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

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

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


    public List<ActivityConsumReturn> comsumeMoneyByDesc(List<ActivityConsumReturn> list) {
        Collections.sort(list, (o1, o2) -> {
            int desc = (int) (Optional.ofNullable(o2.getComsumeMoney()).orElse(0d) - Optional.ofNullable(o1.getComsumeMoney()).orElse(0d));
            return desc;
        });
        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;
    }
}

