package com.cnd3b.restcontroller.enterprise.wechatclient.usercenter.promoters;

import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.Controller;
import com.cnd3b.common.data.Rows;
import com.cnd3b.common.data.SQLFactory;
import p2.pao.PaoRemote;
import p2.pao.PaoSetRemote;
import p2.util.P2Exception;

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

    /**
     * 二级推广人列表查询
     *
     * @return
     */
    public String query_promoterList() {
        SQLFactory sqlFactory = new SQLFactory(this, "二级推广人名单");
        sqlFactory.addParameter("userid", userid);
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 二级推广人合作商家查询
     *
     * @return
     */
    public String query_agentList() {
        SQLFactory sqlFactory = new SQLFactory(this, "合作商家");
        sqlFactory.addParameter("userid", userid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 商家合作
     *
     * @return
     */
    public String cooperation() throws P2Exception {
        String fshortcode = content.getString("fshortcode");
        Rows rows = dbConnect.runSqlQuery("select t1.tenterprise_userid,t1.fname,t2.tagentsid,t2.fagentname,t1.siteid from tenterprise_users t1 inner join tagents t2 on t1.siteid=t2.siteid and t1.tagentsid=t2.tagentsid where t1.fshortcode='" + fshortcode + "'");
        if (!rows.isEmpty()) {
            long fsaler1id = rows.get(0).getLong("tenterprise_userid");
            long tagentsid = rows.get(0).getLong("tagentsid");
            String siteid = rows.get(0).getString("siteid");

            PaoSetRemote tpromotersSet = getP2ServerSystemPaoSet("tpromoters", "siteid='" + siteid + "' and tagentsid='" + tagentsid + "' and fsaler2id='" + userid + "'", "createdate desc");
            boolean iscooperation = false;
            PaoRemote cooperationpao = null;
            int i = 0;
            while (tpromotersSet.getPao(i) != null) {
                if (tpromotersSet.getPao(i).getBoolean("fstatus")) {
                    iscooperation = true;
                } else if (fsaler1id == tpromotersSet.getPao(i).getLong("fsaler1id")) {
                    cooperationpao = tpromotersSet.getPao(i);
                }
                i++;
            }
            if (!iscooperation) {
                if (cooperationpao != null) {
                    cooperationpao.setValue("fstatus", true, 11L);
                } else {
                    cooperationpao = tpromotersSet.addAtEnd();
                    cooperationpao.setValue("createdate", getDateTime(), 11L);
                    cooperationpao.setValue("fsaler1id", fsaler1id, 11L);
                    cooperationpao.setValue("fsaler2id", userid, 11L);
                    cooperationpao.setValue("fstatus", true, 11L);
                    cooperationpao.setValue("siteid", siteid, 11L);
                    cooperationpao.setValue("tagentsid", tagentsid, 11L);
                }
                tpromotersSet.save();
            } else {
                return getErrReturnObject().setErrMsg("已和该经销商存在合作关系").toString();
            }
        } else {
            return getErrReturnObject().setErrMsg("无效的合作码").toString();
        }
        return getSucReturnObject().toString();
    }
}
