package com.happy.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.happy.Model.Admin;
import com.happy.Model.AdminManager;
import com.happy.Model.Hotel;
import com.happy.Model.app.Around_product;
import com.happy.Model.app.Arounds;
import com.happy.Model.app.News;
import com.happy.Model.weixin.CodeEntity;
import com.happy.Model.weixin.WechatResult;
import com.happy.common.http.Get_airticle;
import com.happy.common.model.airticle.Item_content;
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 JSONObject getGCodeInfo(String code){
        JSONObject jsonObject = new JSONObject();
        JSONObject wx = WechatUtil.getGOpenId(code);
        // json数据转换成字符串
        assert wx != null;
        WechatResult result = new Gson().fromJson(wx.toString(), WechatResult.class);
        String openid = result.getOpenid();
        String access_token = result.getAccess_token();
        jsonObject.put("openid",openid);
        jsonObject.put("token",access_token);
        return jsonObject;
    }

    // 登录
    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);
    }

    /** ================================资讯============================= **/
    public int insertNews(News news){
        return appDao.insertNews(news);
    }

    public int delNews(int id){
        return appDao.delNews(id);
    }

    public int updateNews(News news){
        return appDao.updateNews(news);
    }

    public int updateTop(News news){
        return appDao.updateTop(news);
    }

    public News queryByTit(String title){
        return appDao.queryByTit(title);
    }

    public News queryById(int id){
        return appDao.queryById(id);
    }

    public List<Item_content> getNews(int page, int rows) throws Exception {
        return Get_airticle.get(page,rows);
    }

    public List<News> queryNewPage(int page, int rows, String sqlx){
        return appDao.queryNewPage(page, rows, sqlx);
    }

    public int queryNewTotal(String sqlx){
        return appDao.queryNewTotal(sqlx);
    }

    /** ==============================周边=================================== **/
    public int insertRound(Arounds arounds){
        return appDao.insertRound(arounds);
    }

    public int updateRoundById(Arounds arounds){
        return appDao.updateRoundById(arounds);
    }

    public int updateRoundCountById(Arounds arounds){
        return appDao.updateRoundCountById(arounds);
    }

    public int delAround(int id){
        return appDao.delAround(id);
    }

    public Arounds queryAroundById(int id){
        return appDao.queryAroundById(id);
    }

    public Arounds queryAroundByName(String rname){
        return appDao.queryAroundByName(rname);
    }

    public List<Arounds> queryAroundPage(String sqlx, int page, int rows){
        return appDao.queryAroundPage(sqlx, page, rows);
    }

    public int queryAroundTotal(String sqlx){
        return appDao.queryAroundTotal(sqlx);
    }

    /** =========================产品============================== **/
    public int insertProduct(Around_product around_product){
        return appDao.insertProduct(around_product);
    }

    public int updateProductById(Around_product around_product){
        return appDao.updateProductById(around_product);
    }

    public int delAroundProduct(int id){
        return appDao.delAroundProduct(id);
    }

    public Around_product queryAPById(int id){
        return appDao.queryAPById(id);
    }

    public Around_product queryAPByName(int aid,String product_name){
        return appDao.queryAPByName(aid, product_name);
    }

    public List<Around_product> queryAPPage(int aid,String sqlx, int page, int rows){
        return appDao.queryAPPage(aid,sqlx, page, rows);
    }

    public int queryAPTotal(int aid,String sqlx){
        return appDao.queryAPTotal(aid,sqlx);
    }

    public List<Around_product> queryAP(int aid,String sqlx){
        return appDao.queryAP(aid, sqlx);
    }

    public List<Hotel> queryMs(String town){
        return appDao.queryMs(town);
    }

}
