package com.liquidnet.service.controller;

import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.commons.lang.util.CurrentUtil;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.goblin.constant.SmileRedisConst;
import com.liquidnet.service.goblin.dto.vo.SmileSchoolVo;
import com.liquidnet.service.goblin.dto.vo.SmileUserVO;
import com.liquidnet.service.service.impl.SmileFrontServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhangfuxin
 * @Description:用户接口
 * @date 2021/12/27 下午6:25
 */
@Api(tags = "前端接口管理(需要登陆状态)")
@RestController
@RequestMapping("/front")
public class SmileUserController {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SmileFrontServiceImpl smileFrontService;

    @GetMapping("getUser")
    @ApiOperation("获取用户状态，如果没查到，则返回null")
    public ResponseDto<SmileUserVO> getUser() {
        String userId = CurrentUtil.getCurrentUid();
        SmileUserVO smileUserVO= (SmileUserVO) redisUtil.get(SmileRedisConst.SMILE_USER.concat(userId));
        return ResponseDto.success(smileUserVO);
    }
    @GetMapping("getSchool")
    @ApiOperation("获得学校")
    public ResponseDto<List<SmileSchoolVo>> getSchool() {
        String userId = CurrentUtil.getCurrentUid();
        List<SmileSchoolVo> list= (List<SmileSchoolVo>) redisUtil.get(SmileRedisConst.SMILE_USER.concat(userId));
        return ResponseDto.success(list);
    }

    //
    @PostMapping("saveOrUpdateUserOneStep")
    @ApiOperation("增加或者删除第一步")
    public ResponseDto saveOrUpdateUserOneStep(SmileUserVO smileUserVO) {
        String userId = CurrentUtil.getCurrentUid();
        SmileUserVO smileUserVORedis= (SmileUserVO) redisUtil.get(SmileRedisConst.SMILE_USER.concat(userId));
        smileUserVO.setUid(userId);
        if(null==smileUserVORedis){
            smileUserVO.setTag("1");
        }else{
            smileUserVO.setTag(smileUserVORedis.getTag().concat(",1"));
        }
        smileFrontService.saveOrUpdateUser(smileUserVO,smileUserVORedis,"1");
        return  ResponseDto.success();
    }

    @PostMapping("saveOrUpdateUserTwoStep")
    @ApiOperation("增加或者删除第二步")
    public ResponseDto saveOrUpdateUserTwoStep(SmileUserVO smileUserVO) {
        String userId = CurrentUtil.getCurrentUid();
        smileUserVO.setUid(userId);
        SmileUserVO smileUserVORedis= (SmileUserVO) redisUtil.get(SmileRedisConst.SMILE_USER.concat(userId));
        if(null==smileUserVORedis){
            smileUserVORedis.setTag("2");
        }else{
            if(smileUserVORedis.getTag().contains("2")){
                ResponseDto.success(null);
            }else{
                smileUserVO.setTag(smileUserVORedis.getTag().concat(",2"));
            }
        }
        smileFrontService.saveOrUpdateUser(smileUserVO,smileUserVORedis,"2");
        return  ResponseDto.success();
    }
    @PostMapping("saveOrUpdateUserThreeStep")
    @ApiOperation("增加或者删除第三步")
    public ResponseDto saveOrUpdateUserThreeStep(SmileUserVO smileUserVO) {
        String userId = CurrentUtil.getCurrentUid();
        smileUserVO.setUid(userId);
        SmileUserVO smileUserVORedis= (SmileUserVO) redisUtil.get(SmileRedisConst.SMILE_USER.concat(userId));
        if(null==smileUserVORedis){
            smileUserVORedis.setTag("3");
        }else{
            if(smileUserVORedis.getTag().contains("2")){
            }else{
                smileUserVO.setTag(smileUserVORedis.getTag().concat(",3"));
            }
        }
        smileFrontService.saveOrUpdateUser(smileUserVO,smileUserVORedis,"3");
        return  ResponseDto.success();
    }



}
