package com.platform.yijia.controller;

import com.google.gson.Gson;
import com.platform.yijia.param.request.AppUserInfoRequest;
import com.platform.yijia.pojo.AppUserInfo;
import com.platform.yijia.service.AppUserInfoService;
import com.platform.yijia.utils.CodeMsg;
import com.platform.yijia.utils.ResultData;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/***
 * <Title> AppUserInfoController </Title>
 * <Description> 公众号小程序注册用户信息接口  </Description>
 * @date  2020年12月15日
 * @Author JK
 */
@Controller
@RequestMapping("/appUser")
public class AppUserInfoController {
    private static Logger logger =(Logger) LoggerFactory.getLogger(AppUserInfoController.class);
    @Autowired
    private AppUserInfoService appUserInfoService;

    /*
     * 添加用户信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/addAppUserInfo", method = RequestMethod.POST)
    @ResponseBody
    public String addAppUserInfo(@RequestBody AppUserInfoRequest request){
        Gson gson =new Gson();
        ResultData resultData=null;
        try{
            AppUserInfo appUserInfo =new AppUserInfo();
            //String openId = "";     //此参数用于查询该用户是否已注册过
            String token ="";       //用户token
            if(request.getUserType()!=null){
                //1-微信公众号
                if(request.getUserType().equals("1") &&StringUtils.isNotBlank(request.getBlogOpenid())&&StringUtils.isNotBlank(request.getBlogToken())){
                    appUserInfo.setUserType(request.getUserType());
                    token = request.getBlogToken();
                    appUserInfo.setBlogToken(token);
                    //openId = request.getBlogOpenid();
                    appUserInfo.setBlogOpenid(request.getBlogOpenid());
                 //2-微信小程序
                }else if(request.getUserType().equals("2") &&StringUtils.isNotBlank(request.getMinaOpenid())&&StringUtils.isNotBlank(request.getMinaToken())){
                    appUserInfo.setUserType(request.getUserType());
                    token = request.getMinaToken();
                    appUserInfo.setMinaToken(token);
                    //openId = request.getMinaOpenid();
                    appUserInfo.setMinaOpenid(request.getMinaOpenid());
                }
            }
            if(StringUtils.isNotBlank(request.getBlogNickName())){
                appUserInfo.setBlogNickName(request.getBlogNickName());
            }
            if(StringUtils.isNotBlank(request.getMobilePhone())){
                appUserInfo.setMobilePhone(request.getMobilePhone());
            }
            if(StringUtils.isNotBlank(request.getBlogProfilePhoto())){
                appUserInfo.setBlogProfilePhoto(request.getBlogProfilePhoto());
            }

            appUserInfo.setRegisteDate(new Date());
            appUserInfo.setUpdateDate(new Date());

            if(StringUtils.isNotBlank(request.getSexFlag())){
                appUserInfo.setSexFlag(request.getSexFlag());
            }

            Map<String, Object> params = new HashMap<>();
            //params.put("openId", openId);                   //注册用户openId
            params.put("token", token);                     //用户登录凭证
            params.put("userType", request.getUserType());  //用户类型
            //判断该用户是否已存在
            int count = appUserInfoService.isExistAppUser(params);
            if(count > 0){
                resultData = ResultData.error(CodeMsg.USER_EXSIST);
            }else {
                appUserInfoService.AddAppUserInfo(appUserInfo);
                resultData = ResultData.success(CodeMsg.SUCCESS);
            }
        }catch (Exception e){
            resultData = ResultData.error(CodeMsg.INSERT_FAIL);
            e.printStackTrace();
        }
        return gson.toJson(resultData);
    }

}
