package com.cweb.controller;

import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.security.*;
import com.hai.common.utils.IDGenerator;
import com.hai.common.utils.RedisUtil;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.HighUser;
import com.hai.model.HighUserModel;
import com.hai.model.ResponseData;
import com.hai.service.HighUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Objects;

@Controller
@RequestMapping(value = "/user")
@Api(value = "用户共用接口")
public class HighUserCommonController {

    private static final Logger log = LoggerFactory.getLogger(WechatController.class);

    @Resource
    private HighUserService highUserService;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private UserCenter userCenter;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "登录接口【用户不存在会自动注册】")
    public ResponseData login(@RequestParam(value = "phone", required = true) String phone,
                              @RequestParam(value = "code", required = true) String code,
                              HttpServletRequest request, HttpServletResponse response) {
        try {

            // 获取手机号验证码
            String phoneSmsCode = (String) redisUtil.get(phone);
            // 校验验证码
            if (StringUtils.isBlank(phoneSmsCode) || !Objects.equals(phoneSmsCode,code)) {
                log.error("HighUserController --> bindUserPhone() error!", "验证码错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "验证码错误");
            }
            redisUtil.del(phone);

            HighUser user = highUserService.findByPhone(phone);
            if(user == null) {
                user = new HighUser();
                user.setName("用户" + IDGenerator.nextId(5));
                user.setPhone(phone);
                user.setGold(0);
                user.setRegTime(new Date());
                user.setStatus(1);
                highUserService.insertUser(user);
            }

            HighUserModel highUserModel = new HighUserModel();
            HighUser detailData = highUserService.getDetailDataByUser(user.getId());
            detailData.setPassword(null);
            highUserModel.setHighUser(detailData);
            SessionObject so = new SessionObject(StringUtils.isNotBlank(user.getUnionId())?user.getUnionId():detailData.getId().toString(), 1 , highUserModel);
            userCenter.save(request, response, so);
            return ResponseMsgUtil.success(so);
        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }
}
