package com.cnd3b.restcontroller.publicmethod.custorder;

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;

import com.alibaba.fastjson.JSONObject;
public class custorder extends Controller {
    public custorder(JSONObject content) {
        super(content);
    }

    /**
     * 客户历史订单列表查询
     *
     * @return
     */
    public String query_orderList() {
        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();

        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.fdate desc"};
        String sort = getSort(sortfield, "t1.fdate desc");

        SQLFactory sqlFactory = new SQLFactory(this, "客户历史订单列表查询", pageSize, pageNumber, sort);
        sqlFactory.addParameter("wechat_openid", wechat_openid == null ? "no" : wechat_openid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setDataByPaging(rows, sortfield).toString();
    }

    /**
     * 客户历史订单详情查询
     *
     * @return
     */
    public String query_orderMain() {
        String siteid = content.getString("siteid");
        String tagentsid = content.getString("tagentsid");
        String tcustomer_receptid = content.getString("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());
//            Rows signurlrows = getAttachmentUrl("tagents", tagentsid, "signet");
//            if (signurlrows.isEmpty()) {
//                row.put("signeturl", "");
//            } else {
//                row.put("signeturl", getAttachmentUrl("tagents", tagentsid, "signet").get(0).getString("fobsurl"));//签章url
//            }
        }
        return getSucReturnObject().setData(rows).toString();
    }


}
