package com.happy.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.happy.Model.Admin;
import com.happy.Model.AdminManager;
import com.happy.Model.weixin.CodeEntity;
import com.happy.common.util.WechatUtil;
import com.happy.dao.AppDao;
import com.happy.service.AppService;
import org.springframework.stereotype.Service;

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

@Service("AppService")
public class AppImplService implements AppService {

    @Resource
    public AppDao appDao;

    public CodeEntity getCodeInfo(String code){
        CodeEntity entity = new CodeEntity();
        JSONObject wx = WechatUtil.getOpenId(code);
        // json数据转换成字符串
        assert wx != null;
        String openid = wx.get("openid").toString();
        String sessionkey = wx.get("session_key").toString();
        // 当主体账户绑定小程序后就可以获取到，未绑定无法获取
        String unionId="";
        if(wx.get("unionid")!=null){
            unionId=wx.get("unionid").toString();
        }
        entity.setOpenid(openid);
        entity.setSessionkey(sessionkey);
        entity.setUnionid(unionId);
        // 是否需要获取手机号，true需要
        entity.setFlagPhone(true);
        // 返回参数
        return entity;
    }

    // 登录
    public Admin login(String admin_name, String password){
        return appDao.login(admin_name, password);
    }

    public Admin queryByOpenid(String openid){
        return appDao.queryByOpenid(openid);
    }

    public Admin queryByNameAndOpenid(String admin_name,String openid){
        return appDao.queryByNameAndOpenid(admin_name, openid);
    }

    public int updateOpenid(String openid,String admin_name){
        return appDao.updateOpenid(openid, admin_name);
    }

    public int updateOpenidNull(String openid){
        return appDao.updateOpenidNull(openid);
    }

    /**********************
     * ********商户绑定**********
     * ********************/
    public AdminManager login_ma(String admin_name, String password){
        return appDao.login_ma(admin_name, password);
    }

    public AdminManager queryMaByOpenid(String openid){
        return appDao.queryMaByOpenid(openid);
    }

    public AdminManager queryMaByNameAndOpenid(String admin_name,String openid){
        return appDao.queryMaByNameAndOpenid(admin_name, openid);
    }

    public int updateMaOpenid(String openid,String admin_name){
        return appDao.updateMaOpenid(openid, admin_name);
    }

    public int updateMaOpenidNull(String openid){
        return appDao.updateMaOpenidNull(openid);
    }

}
