package com.iamberry.wechat.handles.wx;

import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.WechatUtils;
import com.iamberry.wechat.core.entity.apparatus.Apparatus;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.face.apparatus.ApparatusService;
import com.iamberry.wechat.face.member.MemberService;
import com.iamberry.wechat.tools.ResultInfo;
import com.iamberry.wechat.tools.payUtil.StringUtil;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/29.
 */
@Controller("scannQr")
@RequestMapping("/wechat")
public class QrCodeHandler {

    private Logger logger = LoggerFactory.getLogger(QrCodeHandler.class);

    @Autowired
    ApparatusService apparatusService;
    @Autowired
    MemberService memberService;

    @RequestMapping("/qr/{qrCode}")
    public void getQrCode(@PathVariable("qrCode") String qrCode, HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException {
        /*res.sendRedirect("http://w.iamberry.com/tooth/view/default.html?qrCode="+qrCode);*/
        res.sendRedirect("https://s.iamberry.com/soodo/wechat/register-vip.html?qrCode="+qrCode);
    }
    @ResponseBody
    @RequestMapping("/getActivationAndMember")
    public ResultMsg getActivationAndMember(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException {
        ResultMsg msg = new ResultMsg();
        String qrCode = req.getParameter("qrCode");
        if (!StringUtil.isNotEmpty(qrCode)) {
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            msg.setMessage("未扫描到二维码，请重新扫描!");
            return msg;
        }
        boolean isActivation = false;//是否激活
        Member memberInfo = WechatUtils.getUserBySession(req);

        logger.info("扫描二维码："+qrCode+"===微信openid为："+memberInfo.getUserOpenid());
        System.out.println("扫描二维码："+qrCode+"===微信openid为："+memberInfo.getUserOpenid());

        Apparatus apparatus = new Apparatus();
        apparatus.setApparatusBarcode(qrCode);
        /*apparatus.setUserOpenid(memberInfo.getUserOpenid());*/
        apparatus =  apparatusService.getApparatus(apparatus);
        if(apparatus != null){//不为空表示已激活
            isActivation = true;
        }
        //查询用户信息
        memberInfo = memberService.getMemberByUserOpenId(memberInfo.getUserOpenid());

        Apparatus appar = new Apparatus();
        appar.setUserOpenid(memberInfo.getUserOpenid());
        List<Apparatus> apparatusList = apparatusService.listApparatusPage(appar);

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("memberInfo",memberInfo);
        map.put("isActivation",isActivation);
        map.put("memberToothNumber",apparatusList.size());
        msg.setData(map);
        msg.setResultCode(ResultInfo.SUCCESSCODE);
        msg.setStatus(true);
        return msg;
    }
}
