package com.bjtdba.userCenter.controller;

import com.bjtdba.entity.userCenter.UserSignScore;
import com.bjtdba.userCenter.service.UserSignScoreService;
import com.bjtdba.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 20220712新版签到积分表(RrxUserSignScore)表控制层
 *
 * @author makejava
 * @since 2022-08-16 10:49:31
 */
@CrossOrigin
@RestController
@RequestMapping("userCenter_service/userShareProductService")
public class UserSignScoreController {
    /**
     * 服务对象
     */
    @Autowired
    private UserSignScoreService rrxUserSignScoreService;


//    /**
//     * 分页查询
//     *
//     * @param rrxUserSignScore 筛选条件
//     * @param pageRequest      分页对象
//     * @return 查询结果
//     */
//    @GetMapping
//    public ResponseEntity<Page<UserSignScore>> queryByPage(UserSignScore rrxUserSignScore, PageRequest pageRequest) {
//        return ResponseEntity.ok(this.rrxUserSignScoreService.queryByPage(rrxUserSignScore, pageRequest));
//    }

//    /**
//     * 通过主键查询单条数据
//     *
//     * @param id 主键
//     * @return 单条数据
//     */
//    @GetMapping("{id}")
//    public ResponseEntity<UserSignScore> queryById(@PathVariable("id") Integer id) {
//        return ResponseUtil.ok(this.rrxUserSignScoreService.queryById(id));
//    }

    /**
     * 看广告增加积分
     *
     * @param map 用户uid
     * @return 新增结果
     */
    @PostMapping("/add")
    public Object  add(@RequestBody Map<String,Object> map ) {
        int uid=Integer.parseInt(map.get("uid")+"");
        Map<String,Object> data=new HashMap<String,Object>();
        Random rand=new Random();
        int jifen =rand.nextInt(10);//随机生成积分
        if(jifen==0){
            jifen=1;
        }
        Date date=new Date();
        UserSignScore uss=new UserSignScore();
        uss.setUid(uid);
        uss.setContinuityDay(1);
        uss.setMark("观看广告积分");
        uss.setAddtime(Integer.parseInt(date.getTime()/1000+""));
        uss.setRewardSocre(new BigDecimal(jifen));
        uss.setPm(1);
        uss.setSigninTime(0);
        uss.setSourceUid(0);
        uss.setContinuityDay(1);
        uss.setOrderId(0);
        uss.setStatus(1);
        rrxUserSignScoreService.insert(uss);
        data.put("rewardsocr",jifen);
        return ResponseUtil.ok(data);
    }

//    /**
//     * 编辑数据
//     *
//     * @param rrxUserSignScore 实体
//     * @return 编辑结果
//     */
//    @PutMapping
//    public ResponseEntity<RrxUserSignScore> edit(RrxUserSignScore rrxUserSignScore) {
//        return ResponseEntity.ok(this.rrxUserSignScoreService.update(rrxUserSignScore));
//    }
//
//    /**
//     * 删除数据
//     *
//     * @param id 主键
//     * @return 删除是否成功
//     */
//    @DeleteMapping
//    public ResponseEntity<Boolean> deleteById(Integer id) {
//        return ResponseEntity.ok(this.rrxUserSignScoreService.deleteById(id));
//    }

    /**
     * 通过邀请码进行绑定用户
     * @param map
     * @return
     */
    @PostMapping("/spreadNumber")
    public Object spreadNumber(@RequestBody Map<String,Object>  map){
        int uid = Integer.parseInt(map.get("uid")+"");
        String number = map.get("number")+"";
        if(uid<=0){
            return  ResponseUtil.fail();
        }
        if("".trim().equals(number)){
            return  ResponseUtil.fail();
        }
            return  rrxUserSignScoreService.spreadNumber(uid,number);

    }

}

