package com.cnd3b.restcontroller.enterprise.webclient.tagents;

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

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

    /**
     * 数据统计
     *
     * @return
     */
    public String query_analysis() {
        SQLFactory sqlFactory = new SQLFactory(this, "经销商账号激活统计");
        sqlFactory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 企业档案经销商列表查询
     *
     * @return
     */
    public String query_enterpriseAgentsList() {
        /**
         * 过滤条件设置
         */
        StringBuffer where = new StringBuffer(" 1=1 ");
        if (content.containsKey("where")) {
            JSONObject whereObject = content.getJSONObject("where");
            if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
                where.append(" and(");
                where.append("t1.fagentshortname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.fagentname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.fphonenumber like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.fcontact like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.ftype like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }
            if (whereObject.containsKey("faccountstatus")) {
                if ("已付费".equals(whereObject.getString("faccountstatus"))) {
                    where.append("and(t4.fenddate >= GETDATE())");
                } else if ("未付费".equals(whereObject.getString("faccountstatus"))) {
                    where.append("and(t4.createdate IS null or t4.fenddate < GETDATE())");
                }
            }
            if (whereObject.containsKey("fsaler") && !"".equals(whereObject.getString("fsaler"))) {
                where.append("and(t2.fname='").append(whereObject.getString("fsaler")).append("')");
            }
            if (whereObject.containsKey("ftype") && !"".equals(whereObject.getString("ftype"))) {
                where.append(" and(");
                where.append("t1.ftype like'%").append(whereObject.getString("ftype")).append("%' ");
                where.append(")");
            }
        }
        SQLFactory sql = new SQLFactory(this, "经销商列表查询", pageSize, pageNumber, "t1.tagentsid");
        sql.addParameter("siteid", siteid);
        sql.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(sql.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 企业档案经销商选择列表查询
     *
     * @return
     */
    public String query_AgentsListselect() {
        /**
         * 过滤条件设置
         */
        StringBuffer where = new StringBuffer(" 1=1 ");
        if (content.containsKey("where")) {
            JSONObject whereObject = content.getJSONObject("where");
            if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
                where.append(" and(");
                where.append("t1.fagentshortname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.fagentname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.ftype like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }
            if (whereObject.containsKey("fsaler") && !"".equals(whereObject.getString("fsaler"))) {
                where.append(" and(");
                where.append("t2.fname like'%").append(whereObject.getString("fsaler")).append("%' ");
                where.append(")");
            }
            if (whereObject.containsKey("ftype") && !"".equals(whereObject.getString("ftype"))) {
                where.append(" and(");
                where.append("t1.ftype like'%").append(whereObject.getString("ftype")).append("%' ");
                where.append(")");
            }
        }
        SQLFactory sql = new SQLFactory(this, "经销商选择列表查询", pageSize, pageNumber, "t1.tagentsid");
        sql.addParameter("siteid", siteid);
        sql.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(sql.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(0, 1);
    }


    /**
     * 企业档案经销商主界面查询
     *
     * @return
     */
    public String query_enterpriseAgentsMain() {
        SQLFactory sql = new SQLFactory(this, "经销商主界面查询");
        sql.addParameter("siteid", siteid);
        sql.addParameter("tagentsid", content.getString("tagentsid"));
        Rows rows = dbConnect.runSqlQuery(sql.getSQL());
        return getSucReturnObject().setData(rows).toString();
    }

    /**
     * 企业经销商档案新增、修改
     *
     * @return
     */
    public String insert_modify_enterpriseAgent() throws D3bException, P2Exception {
        long tagentsid = content.getLongValue("tagentsid");//经销商ID，新增时默认传0
        String fagentnum = content.getString("fagentnum", "tagents.fagentnum", "经销商编号");//经销商编号
        String fagentname = content.getString("fagentname", "tagents.fagentname", "经销商名称");//经销商名称
        String fagentshortname = content.getString("fagentshortname", "tagents.fagentshortname", "经销商简称");//经销商简称
        String faddress = content.getString("faddress", "tagents.faddress", "详细地址");//地址
        String fcontact = content.getString("fcontact", "tagents.fcontact", "联系人");//联系人
        String fbirthdate = nullToStr(content.getString("fbirthdate"));//生日
        String fphonenumber = content.getString("fphonenumber", "tagents.fphonenumber", "联系方式");//手机号
        String fwechatno = content.getString("fwechatno", "tagents.fwechatno", "微信号");//微信号
        String femail = content.getString("femail", "tagents.femail", "邮箱");//邮箱
        //String fbegdate = nullToStr(content.getString("fbegdate"));//启用时间
        String fprovince = content.getString("fprovince");//省
        String fcity = content.getString("fcity");//市
        String fcounty = content.getString("fcounty");//县
        String fdutyparagraph = content.getString("fdutyparagraph", "tagents.fdutyparagraph", "统一社会代码");//统一社会信用代码
        //String fenddate = nullToStr(content.getString("fenddate"));//到期时间
        String fjoindate = nullToStr(content.getString("fjoindate"));//加盟日期
        //String fpayment = content.getString("fpayment");//付费方式
        long fsaleruserid = content.getLong("fsaleruserid");//专属业务员ID
        int fteamsize = content.getIntValue("fteamsize");//团队人数
        String fcooperationstatus = content.getString("fcooperationstatus");//合作状态
        String ftype = content.getString("ftype");//经销商类型

        PaoSetRemote tagentsSet = getP2ServerSystemPaoSet("tagents", "siteid='" + siteid + "' and tagentsid='" + tagentsid + "'");
        PaoRemote tagents = null;
        if (tagentsid <= 0 || tagentsSet.isEmpty()) {
            tagents = tagentsSet.addAtEnd();
            tagents.setValue("siteid", siteid, 11L);//企业ID
            tagents.setValue("createby", username, 11L);//录入人
            tagents.setValue("createdate", sysdate, 11L);//录入时间

        } else {
            tagents = tagentsSet.getPao(0);
        }
        tagents.setValue("fagentnum", fagentnum, 11L);
        tagents.setValue("fagentname", fagentname, 11L);
        tagents.setValue("fagentshortname", fagentshortname, 11L);
        tagents.setValue("faddress", faddress, 11L);
        tagents.setValue("fcontact", fcontact, 11L);
        tagents.setValue("fbirthdate", fbirthdate, 11L);
        tagents.setValue("fphonenumber", fphonenumber, 11L);
        tagents.setValue("fwechatno", fwechatno, 11L);
        tagents.setValue("femail", femail, 11L);
        //tagents.setValue("fbegdate", fbegdate, 11L);
        tagents.setValue("fprovince", fprovince, 11L);
        tagents.setValue("fcity", fcity, 11L);
        tagents.setValue("fcounty", fcounty, 11L);
        tagents.setValue("fdutyparagraph", fdutyparagraph, 11L);
        //tagents.setValue("fenddate", fenddate, 11L);
        tagents.setValue("fjoindate", fjoindate, 11L);
        //tagents.setValue("fpayment", fpayment, 11L);
        tagents.setValue("fsaleruserid", fsaleruserid, 11L);
        tagents.setValue("fteamsize", fteamsize, 11L);
        tagents.setValue("fcooperationstatus", fcooperationstatus, 11L);
        tagents.setValue("ftype", ftype, 11L);

        tagents.setValue("changeby", username, 11L);//修改人
        tagents.setValue("changedate", sysdate, 11L);//修改时间

        /**
         * 将手机号自动绑定到主账号中
         */
        PaoSetRemote tenterprise_users = tagents.getPaoSet("$tenterprise_users", "tenterprise_users", "siteid='" + siteid + "' and tagentsid=" + tagents.getUniqueIDValue() + " and fisadministrator=1");
        if (!tenterprise_users.isEmpty()) {
            PaoRemote tenterprise_user = tenterprise_users.getPao(0);
            tenterprise_user.setValue("fname", fcontact, 11L);
            tenterprise_user.setValue("fphonenumber", fphonenumber, 11L);
            tenterprise_user.setValue("fbirthdate", fbirthdate, 11L);
            tenterprise_user.setValue("femail", femail, 11L);
            tenterprise_user.setValue("fwechatno", fwechatno, 11L);
        }
        /**
         * 保存修改日志
         */
        saveDataLog(tagents);
        tagentsSet.save();

        return getSucReturnObject().toString();
    }


    /**
     * 企业经销商档案删除
     *
     * @return
     */
    public String delete_enterpriseAgent() {
        SQLFactory sqlFactory = new SQLFactory(this, "经销商档案删除");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", content.getString("tagentsid"));
        String result = dbConnect.runSqlUpdate(sqlFactory.getSQL());
        if ("true".equals(result)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(result).toString();
        }
    }

    /**
     * 企业档案经销商员工账号信息查询
     *
     * @return
     */
    public String query_enterpriseAgentUsers() {
        SQLFactory sql = new SQLFactory(this, "经销商账号列表查询");
        sql.addParameter("siteid", siteid);
        sql.addParameter("tagentsid", content.getString("tagentsid"));
        Rows rows = dbConnect.runSqlQuery(sql.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 企业档案经销商专属业务员选择查询
     *
     * @return
     */
    public String query_saleruserid() {
        SQLFactory sql = new SQLFactory(this, "经销商专属业务员选择查询");
        sql.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(sql.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 经销商客户档案列表查询
     *
     * @return
     */
    public String query_customer() {
        /**
         * 过滤条件设置
         */
        StringBuffer where = new StringBuffer(" 1=1 ");
        if (content.containsKey("where")) {
            JSONObject whereObject = content.getJSONObject("where");
            if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
                where.append(" and(");
                where.append("t2.fagentshortname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t2.fagentname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.fname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.fphonenumber like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t3.fstorename like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }
        }

        SQLFactory sql = new SQLFactory(this, "经销商客户档案列表查询", pageSize, pageNumber, "t1.tcustomerid desc");
        sql.addParameter("siteid", siteid);
        sql.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(sql.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 接待销售记录查询
     *
     * @return
     */
    public String query_customer_reception() {
        SQLFactory recepitionSQl = new SQLFactory(this, "经销商客户档案接待记录列表查询");
        recepitionSQl.addParameter("siteid", siteid);
        recepitionSQl.addParameter("tagentsid", content.getString("tagentsid"));
        recepitionSQl.addParameter("tcustomerid", content.getString("tcustomerid"));
        Rows recepitionrows = dbConnect.runSqlQuery(recepitionSQl.getSQL());

        SQLFactory orderSQl = new SQLFactory(this, "经销商客户档案销售记录列表查询");
        orderSQl.addParameter("siteid", siteid);
        orderSQl.addParameter("tagentsid", content.getString("tagentsid"));
        orderSQl.addParameter("tcustomerid", content.getString("tcustomerid"));
        Rows orderrows = dbConnect.runSqlQuery(orderSQl.getSQL());
        for (Row orderrow : orderrows) {
            String tcustomer_receptid = orderrow.getString("tcustomer_receptid");
            SQLFactory ordermxSQl = new SQLFactory(this, "经销商客户档案销售记录商品列表查询");
            ordermxSQl.addParameter("siteid", siteid);
            ordermxSQl.addParameter("tagentsid", content.getString("tagentsid"));
            ordermxSQl.addParameter("tcustomerid", content.getString("tcustomerid"));
            ordermxSQl.addParameter("tcustomer_receptid", tcustomer_receptid);
            Rows ordermxrows = dbConnect.runSqlQuery(ordermxSQl.getSQL());
            orderrow.put("orders", ordermxrows);
        }
        JSONObject object = new JSONObject();
        object.put("recepitions", recepitionrows.toJsonArray());
        object.put("orders", orderrows.toJsonArray());
        return getSucReturnObject().setData(object).saveToDataPool().toString();
    }


    /**
     * 经销商类型选择
     *
     * @return
     */
    public String selectAgenttype() {
        SQLFactory sqlFactory = new SQLFactory(this, "经销商类型选择");
        sqlFactory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        String result = rows.toJsonArray("ftype").toString();
        DataPool.put(content.toString(), result);
        return result;
    }
}
