package com.cnd3b.restcontroller.publicmethod.custorder;

import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.Controller;
import com.cnd3b.common.data.Row;
import com.cnd3b.common.data.Rows;
import com.cnd3b.common.data.SQLFactory;
import com.cnd3b.utility.wechatdock.WechatDock_gzh;
import com.cnd3b.utility.wechatdock.WechatUser;

public class custorderpreview extends Controller {
    public custorderpreview(JSONObject content) {
        super(content);
    }

    /**
     * 客户H5订单预览查询
     *
     * @return
     */
    public String query_orderMainPreview() {
        String tcustomer_receptid = content.getString("tcustomer_receptid");
        String siteid = content.getString("siteid");
        String tagentsid = content.getString("tagentsid");
        String wechat_code = content.getString("wechat_code");

        WechatDock_gzh wechatDock_gzh = new WechatDock_gzh();
        WechatUser wechatUser = wechatDock_gzh.getCustMsg(wechat_code);
        String wechat_openid = wechatUser.getOpenid();
        if (wechat_openid != null) {
            Rows orderrow = dbConnect.runSqlQuery("select isnull(wechat_openid,'') as wechat_openid from tcustomer_reception where siteid='" + siteid + "' and tagentsid='" + tagentsid + "'and tcustomer_receptid='" + tcustomer_receptid + "'");
            if (!orderrow.isEmpty()) {
                String orderopenid = orderrow.get(0).getString("wechat_openid");
                if (!orderopenid.equals("") && !orderopenid.equals(wechat_openid)) {
                    return getErrReturnObject().setErrMsg("订单已被绑定").toString();
                }
            }
            dbConnect.runSqlUpdate("update tcustomer_reception set wechat_openid='" + wechat_openid + "' where siteid='" + siteid + "' and tagentsid='" + tagentsid + "'and tcustomer_receptid='" + tcustomer_receptid + "'");

            SQLFactory sqlFactory = new SQLFactory(this, "客户订单预览主界面查询");
            sqlFactory.addParameter("siteid", siteid);
            sqlFactory.addParameter("tagentsid", tagentsid);
            sqlFactory.addParameter("tcustomer_receptid", tcustomer_receptid);
            Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
            for (Row row : rows) {
                SQLFactory orderFactory = new SQLFactory(this, "客户订单预览商品明细查询");
                orderFactory.addParameter("siteid", siteid);
                orderFactory.addParameter("tagentsid", tagentsid);
                orderFactory.addParameter("tcustomerid", row.getString("tcustomerid"));
                orderFactory.addParameter("tcustomer_receptid", row.getString("tcustomer_receptid"));

                Rows orderRows = dbConnect.runSqlQuery(orderFactory.getSQL());
                row.put("orderlist", orderRows.toJsonArray());
                row.put("wechat_openid", wechat_openid);
            }
            return getSucReturnObject().setData(rows).toString();
        } else {
            return getErrReturnObject().setErrMsg("订单查询失败").toString();
        }
    }


    public String sendVipCard() {
        String wechat_openid = content.getString("wechat_openid");
        WechatDock_gzh wechatDock_gzh = new WechatDock_gzh();
        return wechatDock_gzh.sendVipCard(wechat_openid);
    }
}
