package com.fuint.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuint.common.dto.PointDto;
import com.fuint.common.enums.StatusEnum;
import com.fuint.common.enums.WxMessageEnum;
import com.fuint.common.service.GrowService;
import com.fuint.common.service.MemberService;
import com.fuint.common.service.PointService;
import com.fuint.common.service.WeixinService;
import com.fuint.common.util.DateUtil;
import com.fuint.framework.annoation.OperationServiceLog;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.framework.pagination.PaginationRequest;
import com.fuint.framework.pagination.PaginationResponse;
import com.fuint.repository.mapper.MtGrowMapper;
import com.fuint.repository.mapper.MtPointMapper;
import com.fuint.repository.mapper.MtUserMapper;
import com.fuint.repository.model.MtGrow;
import com.fuint.repository.model.MtPoint;
import com.fuint.repository.model.MtRaffleItem;
import com.fuint.repository.model.MtUser;
import com.fuint.utils.StringUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 积分管理业务实现类
 *
 * Created by FSQ
 * CopyRight https://www.fuint.cn
 */
@Service
@AllArgsConstructor
public class GrowServiceImpl extends ServiceImpl<MtGrowMapper, MtGrow> implements GrowService {

    private MtGrowMapper mtGrowMapper;

    private PointService pointService;

    private CouponServiceImpl couponService;

    @Override
    public MtGrow querySignInfo(Integer userId) {
        LambdaQueryWrapper<MtGrow> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(MtGrow::getUserId, userId);
        List<MtGrow> signInfoList = mtGrowMapper.selectList(lambdaQueryWrapper);
        if (signInfoList != null && signInfoList.size() > 0) {
            return signInfoList.get(0);
        }
        MtGrow signInfo = new MtGrow();
        signInfo.setUserId(userId);
        signInfo.setAdvNum(0);
        signInfo.setAdvTotal(3);
        signInfo.setGrowTotal(0);
        signInfo.setCreateTime(new Date());
        signInfo.setUpdateTime(new Date());
        signInfo.setOperator("SYSTEM");
        mtGrowMapper.insertForId(signInfo);

        return signInfo;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean doSign(Integer id) throws BusinessCheckException {

        //
        MtGrow grow = mtGrowMapper.selectById(id);
        MtGrow newGrow = new MtGrow();
        newGrow.setId(grow.getId());
        newGrow.setSignTime(new Date());
        newGrow.setUpdateTime(new Date());
        mtGrowMapper.updateById(newGrow);

        MtPoint reqPointDto = new MtPoint();
        reqPointDto.setAmount(1);
        reqPointDto.setUserId(grow.getUserId());
        reqPointDto.setDescription("签到1积分");
        reqPointDto.setOperator("系统");
        pointService.addPoint(reqPointDto);

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean doAdv(Integer id) throws BusinessCheckException {
        //
        MtGrow grow = mtGrowMapper.selectById(id);
        MtGrow newGrow = new MtGrow();
        newGrow.setId(grow.getId());
        newGrow.setAdvNum(grow.getAdvNum()+1);
        newGrow.setGrowTotal(grow.getGrowTotal() + 1);
        newGrow.setUpdateTime(new Date());
        mtGrowMapper.updateById(newGrow);

        MtPoint reqPointDto = new MtPoint();
        reqPointDto.setAmount(1);
        reqPointDto.setUserId(grow.getUserId());
        reqPointDto.setDescription("视频广告1积分");
        reqPointDto.setOperator("系统");
        pointService.addPoint(reqPointDto);

        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        int nine = (newGrow.getGrowTotal())%9;
        if (nine == 0) {
            couponService.sendCoupon(13, grow.getUserId(),1,true,uuid,"SYSTEM");
        }

        int eleven = (newGrow.getGrowTotal())%11;
        if (eleven == 0) {
            couponService.sendCoupon(14, grow.getUserId(),1,true,uuid,"SYSTEM");
        }
        return true;
    }

    @Override
    public void clearAdvNum() {

        MtGrow et = new MtGrow();
        et.setAdvNum(0);
        Wrapper<MtGrow> ew = new LambdaQueryWrapper<>();
        mtGrowMapper.update(et,ew);

    }
}
