package com.liquidnet.service.platform.service.impl.goblin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liquidnet.commons.lang.util.IDGenerator;
import com.liquidnet.commons.lang.util.StringUtil;
import com.liquidnet.service.goblin.entity.GoblinNftExActivity;
import com.liquidnet.service.goblin.entity.GoblinNftExCode;
import com.liquidnet.service.goblin.mapper.GoblinNftExActivityMapper;
import com.liquidnet.service.goblin.mapper.GoblinNftExCodeMapper;
import com.liquidnet.service.goblin.param.GoblinNftExActivityParam;
import com.liquidnet.service.goblin.service.IGoblinNftExActivityService;
import com.liquidnet.service.platform.utils.GoblinRedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: wll
 * @Description: nft兑换活动服务实现
 * @Date:Create：in 2022/4/19 4:35 下午
 */


@Service
@Slf4j
public  class GoblinNftExActivityServiceImpl implements IGoblinNftExActivityService {

    @Autowired
    private GoblinNftExActivityMapper goblinNftExActivityMapper;
    @Autowired
    private GoblinRedisUtils goblinRedisUtils;
    @Autowired
    private GoblinNftExCodeMapper goblinNftExCodeMapper;

    @Override
    public String add(GoblinNftExActivityParam goblinNftExActivityParam) {


        // 获取活动列表
        List<GoblinNftExActivity> goblinNftExActivities =  goblinRedisUtils.getActivitySku();

        LocalDateTime now = LocalDateTime.now();

        GoblinNftExActivity goblinNftExActivity = GoblinNftExActivity.getNew();
        BeanUtils.copyProperties(goblinNftExActivityParam, goblinNftExActivity);
        goblinNftExActivity.setActivityId(IDGenerator.nextSnowId());
        goblinNftExActivity.setCreatedAt(now);

        // mysql数据
        goblinNftExActivityMapper.addGoblinNftExActivity(goblinNftExActivity);

        // 添加redis数据
        // goblinRedisUtils.incrActivitySku(goblinNftExActivities, goblinNftExActivity);

        return goblinNftExActivity.getActivityId();
    }

    @Override
    public Object pageList(GoblinNftExActivityParam goblinNftExActivityParam) {

        PageHelper.startPage(goblinNftExActivityParam.getPageNum(),20,true);

        /*
         * 获取分页nft兑换活动数据
         */
        List<GoblinNftExActivity> goblinNftExActivities = goblinNftExActivityMapper.selectPageList(goblinNftExActivityParam.getTitle(), goblinNftExActivityParam.getStartTime(),
                goblinNftExActivityParam.getEndTime());


        StringBuffer activityIds = new StringBuffer();
        for (GoblinNftExActivity GoblinNftExActivity : goblinNftExActivities) {
            activityIds.append(GoblinNftExActivity.getActivityId()).append(",");
        }

        if (StringUtil.isNotBlank(activityIds)){
            activityIds.deleteCharAt(activityIds.length()-1);
            List<GoblinNftExCode> goblinNftExCodes =  goblinNftExCodeMapper.selectByActivityIds(activityIds.toString());


            for (GoblinNftExActivity goblinNftExActivity :goblinNftExActivities) {
                Integer countNumber = 0;
                Integer useNumber = 0;
                Integer unUseNumber = 0;
                for (GoblinNftExCode goblinNftExCode : goblinNftExCodes) {
                    if (goblinNftExActivity.getActivityId().equals(goblinNftExCode.getActivityId())){
                        countNumber++;
                        switch (goblinNftExCode.getState()){
                            case 1:
                                useNumber++;
                                break;
                            default:
                                unUseNumber++;
                        }
                    }
                }

                goblinNftExActivity.setCountNumber(countNumber);
                goblinNftExActivity.setUseNumber(useNumber);
                goblinNftExActivity.setUnUseNumber(unUseNumber);
            }
        }

        PageInfo<GoblinNftExActivity> pageInfo = new PageInfo<GoblinNftExActivity>(goblinNftExActivities);
        return pageInfo;
    }
}
