package com.bjtdba.userCenter.service.impl;

import com.bjtdba.entity.userCenter.User;
import com.bjtdba.entity.userCenter.UserSignScore;
import com.bjtdba.userCenter.dao.SystemConfigValueDao;
import com.bjtdba.userCenter.dao.UserDao;
import com.bjtdba.userCenter.dao.UserSignScoreDao;
import com.bjtdba.userCenter.service.UserSignScoreService;
import com.bjtdba.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 新版签到积分表(RrxUserSignScore)表服务实现类
 *
 * @author makejava
 * @since 2022-08-16 10:49:31
 */
@Service("userSignScoreService")
public class UserSignScoreServiceImpl implements UserSignScoreService {
    @Autowired
    private UserSignScoreDao rrxUserSignScoreDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private  UserSignScoreService rrxUserSignScoreService;

    @Autowired
    private SystemConfigValueDao systemConfigValueDao;

    @Autowired
    private UserSignScoreDao userSignScoreDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public UserSignScore queryById(Integer id) {
        return this.rrxUserSignScoreDao.queryById(id);
    }

//    /**
//     * 分页查询
//     *
//     * @param rrxUserSignScore 筛选条件
//     * @param pageRequest      分页对象
//     * @return 查询结果
//     */
//    @Override
//    public Page<RrxUserSignScore> queryByPage(RrxUserSignScore rrxUserSignScore, PageRequest pageRequest) {
//        long total = this.rrxUserSignScoreDao.count(rrxUserSignScore);
//        return new PageImpl<>(this.rrxUserSignScoreDao.queryAllByLimit(rrxUserSignScore, pageRequest), pageRequest, total);
//    }

    /**
     * 新增数据
     *
     * @param rrxUserSignScore 实例对象
     * @return 实例对象
     */
    @Override
    public UserSignScore insert(UserSignScore rrxUserSignScore) {
        //更新用户积分
        User user=new User();
        user=userDao.selectById(rrxUserSignScore.getUid());
        if(user==null){
            return  null;
        }
        int i=userDao.updateUserscore(rrxUserSignScore.getUid(),rrxUserSignScore.getRewardSocre().add(user.getScore()));
        System.out.println("更新用户积分==="+i);
        if(i>0) {
            //找到最近想积分记录
           UserSignScore objuss=rrxUserSignScoreDao.queryByUid(rrxUserSignScore.getUid());
           //如果有记录进行累加
           if(objuss!=null) {
               System.out.println("更新用户积分===objuss!=null");
               rrxUserSignScore.setSurplus(rrxUserSignScore.getRewardSocre().add(objuss.getSurplus()));
           }else{
               //如果没有记录，不进行累加
               System.out.println("更新用户积分===objuss==null");
               rrxUserSignScore.setSurplus(rrxUserSignScore.getRewardSocre());
           }
            this.rrxUserSignScoreDao.insert(rrxUserSignScore);
        }

        return rrxUserSignScore;
    }

    /**
     * 修改数据
     *
     * @param rrxUserSignScore 实例对象
     * @return 实例对象
     */
    @Override
    public UserSignScore update(UserSignScore rrxUserSignScore) {
        this.rrxUserSignScoreDao.update(rrxUserSignScore);
        return this.queryById(rrxUserSignScore.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.rrxUserSignScoreDao.deleteById(id) > 0;
    }

    /**
     * 通过邀请码绑定用户关系
     * @param uid
     * @param number
     * @return
     */
    @Override
    public Object spreadNumber(int uid,String number){

        User user=userDao.selectById(uid);
        User queryUserObj=new User();

        queryUserObj.setUser_number(number);
        int i=0;
        if(user.getSpreadUid()>0){
            return ResponseUtil.fail("用户已被邀请");
        }
        if(user!=null&&user.getSpreadUid()==0){
          User userSelect= userDao.queryUser(queryUserObj);//通过userNumber查询当前用
            if(userSelect!=null&&userSelect.getUid()!=0) {
               i= userDao.updateUserspreadUid(user.getUid(), userSelect.getUid(),new Date());//进行绑定

                Date date=new Date();
                UserSignScore uss=new UserSignScore();
                uss.setUid(userSelect.getUid());
                uss.setContinuityDay(1);
                uss.setMark("首次邀请好友奖励积分");
                uss.setAddtime(Integer.parseInt(date.getTime()/1000+""));
                uss.setRewardSocre(new BigDecimal(99));
                uss.setPm(1);
                uss.setSigninTime(0);
                uss.setSourceUid(0);
                uss.setContinuityDay(1);
                uss.setOrderId(0);
                uss.setStatus(1);
                rrxUserSignScoreService.insert(uss);
                return ResponseUtil.ok();
            }else{
                return ResponseUtil.fail("邀请码不存在");
            }

        }else{
            return ResponseUtil.fail("用户不存在");
        }
    }
    /**
     * hyh
     * 登录的时候判断邀请人
     */

    public void bindSpread(int spread_uid,int pm,int status){
        //邀请人id 为条件
        User userSpread = userDao.selectById(spread_uid);
        //判断邀请人是否第一次邀请
        String value = "";
        String mark = "";
        if(userSpread.getSpread_count()==0){
            //第一次邀请新用户
            //查询配置表   得到第一次邀请新用户给多少积分
            value = systemConfigValueDao.selectValueByKey("first_yq_score");
            mark = "首次邀请好友奖励积分";
            sign_add(spread_uid,value,mark);
        }
        value = systemConfigValueDao.selectValueByKey("invitation");
        mark = "邀请好友注册奖励积分";
        sign_add(spread_uid,value,mark);
        User user = new User();
        user.setUid(spread_uid);
        user.setSpread_count(userSpread.getSpread_count()+1);
        userDao.updateSpreadCount(spread_uid,userSpread.getSpread_count()+1);
        if(pm==1&&status==1){//pm 1 收入
            //修改rrx_suer用户表中的
            userDao.updateUserscore(userSpread.getUid(),new BigDecimal(value));
        }
    }

    /**
     * 添加用户绑定关系
     */
    private void sign_add(int spread_uid,String value,String mark){
        //将积分添加进去  sign_add  user_sign_score表
        UserSignScore userSignScore = new UserSignScore();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = df.format(new Date());
        int times = (int) ((Timestamp.valueOf(time).getTime())/1000);
        userSignScore.setUid(spread_uid);
        userSignScore.setContinuityDay(0);
        userSignScore.setStatus(1);
        userSignScore.setMark(mark);
        userSignScore.setRewardSocre(new BigDecimal(value));
        userSignScore.setSigninTime(0);
        userSignScore.setAddtime(times);
        userSignScore.setOrderId(0);
        userSignScore.setPm(1);
        int insert = userSignScoreDao.insert(userSignScore);
    }
}
