package com.bjtdba.userCenter.controller;

import com.bjtdba.entity.userCenter.UserClockes;
import com.bjtdba.userCenter.service.UserClockesService;
import com.bjtdba.userCenter.vo.ClockesVo;
import com.bjtdba.utils.DateUtils;
import com.bjtdba.utils.ResponseUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 用户步数表(UserClockes)表控制层
 *
 * @author 何海强
 * @since 2022-11-11 10:08:55
 */
@RestController
@RequestMapping("userCenter_service/userClocke")
public class UserClockesController {
    /**
     * 服务对   象
     */
    @Resource
    private UserClockesService userClockesService;


    /**
     * 打卡签到
     */
    @PostMapping("/add_clocke")
    public Object addClocke(@RequestHeader Map<String, String> Hmap, @RequestBody Map<String, String> map) {
        if (!Hmap.containsKey("uid")) {
            return ResponseUtil.fail("uid参数错误");
        }

        if (!map.containsKey("num")) {
            return ResponseUtil.fail("参数错误");
        }
        Integer num = Integer.parseInt(map.get("num"));
        Integer uid = Integer.parseInt(Hmap.get("uid"));
        //判断当天是否签到过
        Integer dayNum = userClockesService.queryClockeUid(uid);
        if (dayNum != null && dayNum == 0) {
            //更新
            UserClockes clockesObj = userClockesService.queryByUid(uid);
            clockesObj.setNum(clockesObj.getNum() + num);
            userClockesService.update(clockesObj);
            return ResponseUtil.ok("更新成功", null);
        } else {
            //添加
            UserClockes userClockes = new UserClockes();
            userClockes.setUid(uid);
            userClockes.setAddtime(new Date());
            userClockes.setSigninTime(new Date());
            userClockes.setCycleNum(DateUtils.getWeekInt(DateUtils.dateToStr(new Date())));//获取当前是周几
            userClockes.setNum(Integer.parseInt(map.get("num")));
            //连续第几天打卡
            if (dayNum == null) {
                userClockes.setContinuityDay(1);
            } else if (dayNum.equals(1)) {
                UserClockes clockesObj = userClockesService.queryByUid(uid);
                userClockes.setContinuityDay(clockesObj.getContinuityDay() + 1);
            }

            userClockesService.insert(userClockes);
            return ResponseUtil.ok("添加成功", null);
        }

    }


    @PostMapping("/lst")
    public Object lst(@RequestHeader Map<String, String> Hmap) {
        if (!Hmap.containsKey("uid")) {
            return ResponseUtil.fail("uid参数错误");
        }
        Integer uid = Integer.parseInt(Hmap.get("uid"));
        UserClockes queryUserClockes = new UserClockes();
        queryUserClockes.setUid(uid);
        queryUserClockes.setCycle_numDesc("11");
        queryUserClockes.setBenzhou("11");

        Long sumNum = userClockesService.sumNum(uid);

        List<UserClockes> lst = userClockesService.queryList(queryUserClockes);
        Map<String, Object> data = new HashMap<String, Object>();
        int num = lst.get(lst.size() - 1).getContinuityDay();
        data.put("sumNum", sumNum);
        data.put("continuityDayNum", num);
        data.put("list", lst);

        return ResponseUtil.ok(data);
    }


    @GetMapping("/all_lst")
    public Object allLst(@RequestHeader Map<String, String> Hmap, @RequestParam Map<String, String> pmap) {
        if (!Hmap.containsKey("uid")) {
            return ResponseUtil.fail("uid参数错误");
        }

        if (!pmap.containsKey("page") || !pmap.containsKey("limit")) {
            return ResponseUtil.fail("参数错误");
        }
        int page = Integer.parseInt(pmap.get("page") + "");
        int limit = Integer.parseInt(pmap.get("limit"));


        Integer uid = Integer.parseInt(Hmap.get("uid"));
        UserClockes queryUserClockes = new UserClockes();
        queryUserClockes.setUid(uid);
        PageHelper.startPage(page, limit);
        List<UserClockes> lst = userClockesService.queryList(queryUserClockes);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("list", lst);
        return ResponseUtil.ok(data);
    }


    @GetMapping("/count_list")
    public Object countLst(@RequestHeader Map<String, String> Hmap, @RequestParam Map<String, String> pmap) {
        if (!Hmap.containsKey("uid")) {
            return ResponseUtil.fail("uid参数错误");
        }
        if (!pmap.containsKey("type")){
            return ResponseUtil.fail("参数错误");
        }

        List<ClockesVo> clockesVoList=userClockesService.countList(pmap.get("type"));
        Map<String,Object> data=new HashMap<String,Object>();

        Integer uid=Integer.parseInt(Hmap.get("uid"));
        List<ClockesVo> sortList=new ArrayList<ClockesVo>();
        //查看自己当前的排名
        int sort=0;
        ClockesVo uidck=new ClockesVo();
        for(int i=0;i<clockesVoList.size();i++){
            ClockesVo ckvo=new ClockesVo();
            ckvo=clockesVoList.get(i);
            if(ckvo.getUid().equals(uid)){
                sort=i+1;
                uidck=ckvo;
                uidck.setSort(sort);
            }
        }
        //排名小于前十名
        if(sort>10){
            for (int a=0;a<clockesVoList.size();a++){
                if (a<9){
                    ClockesVo ckvores=new ClockesVo();
                    ckvores=clockesVoList.get(a);
                    ckvores.setSort(a+1);
                    sortList.add(ckvores);
                }else{
                    break;
                }

            }
            sortList.add(uidck);
        }else{
            for (int b=0;b<clockesVoList.size();b++){
                if (b<=9){
                    ClockesVo ckvores=new ClockesVo();
                    ckvores=clockesVoList.get(b);
                    ckvores.setSort(b+1);
                    sortList.add(ckvores);
                }else{
                    break;
                }

            }
        }

        data.put("list",sortList);
        return  ResponseUtil.ok(data);
    }

}

