package com.yinet.xcqh.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yinet.common.core.text.Convert;
import com.yinet.common.utils.http.HttpUtils;
import com.yinet.wsyy.mapper.WsyyQuantumMapper;
import com.yinet.wsyy.mapper.WsyySettingMapper;
import com.yinet.wx.domain.WxCache;
import com.yinet.wx.service.WxService;
import com.yinet.xcqh.domain.TestMsg;
import com.yinet.xcqh.domain.Xcqh;
import com.yinet.xcqh.mapper.XcqhMapper;
import com.yinet.xcqh.service.XcqhService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

/**
 * 现场取号列表Service业务层处理
 *
 * @author yinet
 * @date 2019-09-02
 */
@Service
public class XcqhServiceImpl implements XcqhService {
    @Autowired
    private XcqhMapper xcqhMapper;
    @Autowired
    private WsyyQuantumMapper xcqhQuantumMapper;
    @Autowired
    private WsyySettingMapper xcqhSettingMapper;
    @Autowired
    private WxService wxService;

    /**
     * 查询现场取号列表列表
     *
     * @param xcqh 现场取号列表
     * @return 现场取号列表
     */
    @Override
    public List<Xcqh> selectXcqhList(Xcqh xcqh) {
        return xcqhMapper.selectXcqhList(xcqh);
    }

    /**
     * 新增现场取号列表
     *
     * @param xcqh 现场取号列表
     * @return 结果
     */
    @Override
    public int insertXcqh(Xcqh xcqh) throws IOException {
        xcqh.setXcqhId(UUID.randomUUID().toString().replaceAll("-", ""));
        //{"cmd":"AddClient","QueueId":"47","IsPrint":"false","Name":"测试"}
        String param = "data={\"cmd\":\"AddClient\",\"QueueId\":\"" + xcqh.getGuidanceId() + "\",\"IsPrint\":\"false\",\"Name\":\"" + xcqh.getApplyForName() + "\"}";
        String s = HttpUtils.sendGet("http://123.182.230.242:1888/getno", param);
        JSONObject jsonObject = JSONObject.parseObject(s);
        String clientId = jsonObject.getString("ClientId");
        xcqh.setSerNum(clientId);
        xcqh.setStatus("1");
        xcqh.setSubmitTime(new Date());
        String s1 = sendMsg(xcqh.getOpenid(), xcqh.getSerNum());
        return xcqhMapper.insertXcqh(xcqh);
    }


    /**
     * 删除现场取号列表对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteXcqhByIds(String ids) {
        return xcqhMapper.deleteXcqhByIds(Convert.toStrArray(ids));
    }


    public void sendWxMsg(String openid, String msg, String waitCount) {

    }

    public String sendMsg(String openid, String text) throws IOException {
        //https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN
        TestMsg testMsg = new TestMsg();
        testMsg.setTouser(openid);
        testMsg.setMsgtype("text");
        Map map = new HashMap();
        map.put("content", text);
        testMsg.setText(map);
        String accessToken = wxService.getToken().getAccessToken();
        String s = HttpUtils.sendPostJson("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accessToken, JSON.toJSONString(testMsg));
        return s;
    }
}
