package com.cnd3b.restcontroller.enterprise.agentclient.mainpage.action.receptioninput;

import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.Controller;
import com.cnd3b.common.D3bException;
import com.cnd3b.common.actionscore.ActionScore;
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;

import java.util.Date;

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


    /**
     * 经销商接待记录列表查询
     *
     * @return
     */
    public String query_receptionList() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.changedate"};
        String sort = getSort(sortfield, "t1.changedate");

        /**
         * 过滤条件设置
         */
        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.fname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.fcommunity like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.fphonenumber like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }
            if (whereObject.containsKey("begdate") && !"".equals(whereObject.getString("begdate"))) {
                where.append(" and convert(varchar(10),t1.changedate,120)>=").append("'").append(whereObject.getString("begdate")).append("'");
            }
            if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
                where.append(" and convert(varchar(10),t1.changedate,120)<=").append("'").append(whereObject.getString("enddate")).append("'");
            }
            if (!fisadministrator) {
                where.append(" and (t1.tenterprise_userid is null or t1.tenterprise_userid='").append(userid).append("' or t1.tenterprise_userid in ( select fsubid from tenterprise_userdetail where siteid='").append(siteid).append("' and fparentid='").append(userid).append("' )) ");
            }
        }
        SQLFactory sqlFactory = new SQLFactory(this, "经销商接待记录列表查询", pageSize, pageNumber, sort);
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        sqlFactory.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setDataByPaging(rows, sortfield).addAppHelpNotes(getAppHelpNotes("接待系统")).setFinalDo(0, 1);
    }


    /**
     * 经销商接待记录主界面查询
     *
     * @return
     */
    public String query_receptionMain() {
        SQLFactory sqlFactory = new SQLFactory(this, "经销商接待记录主界面查询");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        sqlFactory.addParameter("tcustomer_receptid", content.getString("tcustomer_receptid"));
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        for (Row row : rows) {
            row.put("datalog", getDataLog("tcustomer_reception", row.getString("tcustomer_receptid")));
        }
        return getSucReturnObject().setData(rows).toString();
    }

    /**
     * 经销商接待记录新增、修改
     *
     * @return
     */
    public String receptionInsertOrModify() throws D3bException, P2Exception {
        long tcustomer_receptid = content.getLongValue("tcustomer_receptid");//接待记录ID，新增时默认传0
        String fname = content.getString("fname", "tcustomer.fname", "客户姓名");//顾客姓名
        String fphonenumber = content.getString("fphonenumber", "tcustomer.fphonenumber", "手机号码");//手机号
        String fchannel = content.getString("fchannel");//来源渠道
        String fcommunity = content.getString("fcommunity", "tcustomer.fcommunity", "小区名称");//小区名称
        String fnumber = content.getString("fnumber", "tcustomer.fnumber", "幢/单元/室");//幢/单元/室
        String frenovationplandate = content.getString("frenovationplandate");//计划装修时间
        String tstoreid = content.getString("tstoreid");//门店ID
        Date fdate = content.getDate("fdate");//接待日期

        String freception_address = content.getString("freception_address");//接待地点
        String fcustaction = content.getString("fcustaction");//客户动作
        String fnotes = content.getString("fnotes", "tcustomer_reception.fnotes", "接待情况");//接待情况

        PaoSetRemote tcustomerSet = getP2ServerSystemPaoSet("tcustomer", "siteid='" + siteid + "' and tagentsid='" + tagentsid + "' and fphonenumber='" + fphonenumber + "'");
        PaoRemote tcustomer = null;
        if (tcustomerSet.isEmpty()) {
            tcustomer = tcustomerSet.addAtEnd();
            tcustomer.setValue("fchannel", fchannel, 11L);//来源渠道
            tcustomer.setValue("siteid", siteid, 11L);//企业ID
            tcustomer.setValue("tagentsid", tagentsid, 11L);//经销商ID
            tcustomer.setValue("createby", username, 11L);//录入人
            tcustomer.setValue("createdate", sysdate, 11L);//录入时间
        } else {
            tcustomer = tcustomerSet.getPao(0);
        }
        tcustomer.setValue("fname", fname, 11L);
        tcustomer.setValue("fphonenumber", fphonenumber, 11L);//客户手机
        tcustomer.setValue("fcommunity", fcommunity, 11L);//小区名称
        tcustomer.setValue("fnumber", fnumber, 11L);//幢/单元/室
        tcustomer.setValue("frenovationplandate", frenovationplandate, 11L);//计划装修时间
        tcustomer.setValue("tstoreid", tstoreid, 11L);//门店ID

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

        long tcustomerid = tcustomer.getUniqueIDValue();
        /**
         * 查询销售记录，如不存在，则新增此销售记录
         */
        PaoSetRemote tcustomer_receptionSet = tcustomer.getPaoSet("$tcustomer_reception", "tcustomer_reception", "siteid=:siteid and tagentsid=:tagentsid and tcustomer_receptid='" + tcustomer_receptid + "'");
        PaoRemote tcustomer_reception = null;
        if (tcustomer_receptid <= 0 || tcustomer_receptionSet.isEmpty()) {
            tcustomer_reception = tcustomer_receptionSet.addAtEnd();
            tcustomer_reception.setValue("fwaiter", username, 11L);//接待员
            tcustomer_reception.setValue("tenterprise_userid", userid, 11L);//账号ID
            tcustomer_reception.setValue("siteid", siteid, 11L);//企业ID
            tcustomer_reception.setValue("tagentsid", tagentsid, 11L);//经销商ID
            tcustomer_reception.setValue("ftype", "接待", 11L);//记录类型
            tcustomer_reception.setValue("createby", username, 11L);//录入人
            tcustomer_reception.setValue("createdate", sysdate, 11L);//录入时间

            tcustomer.setValue("freceptiontimes", tcustomer.getInt("freceptiontimes") + 1, 11L);

            addActionScore(ActionScore.fsourcetype.意向客户录入, tcustomer_reception.getUniqueIDValue(), "意向客户录入");
        } else {
            tcustomer_reception = tcustomer_receptionSet.getPao(0);
        }

        tcustomer_reception.setValue("tcustomerid", tcustomerid, 11L);//客户档案ID
        tcustomer_reception.setValue("fname", fname, 11L);//顾客姓名
        tcustomer_reception.setValue("fphonenumber", fphonenumber, 11L);//客户手机
        tcustomer_reception.setValue("fchannel", fchannel, 11L);//来源渠道
        tcustomer_reception.setValue("fcommunity", fcommunity, 11L);//小区名称
        tcustomer_reception.setValue("fnumber", fnumber, 11L);//幢/单元/室
        tcustomer_reception.setValue("frenovationplandate", frenovationplandate, 11L);//计划装修时间
        tcustomer_reception.setValue("fdate", fdate, 11L);//接待日期
        tcustomer_reception.setValue("freception_address", freception_address, 11L);//接待地点
        tcustomer_reception.setValue("fcustaction", fcustaction, 11L);//客户动作
        tcustomer_reception.setValue("fnotes", fnotes, 11L);//接待情况
        tcustomer_reception.setValue("fstatus", "正常", 11L);//接待记录状态

        tcustomer_reception.setValue("tstoreid", tstoreid, 11L);//门店ID
        tcustomer_reception.setValue("changeby", username, 11L);//修改人
        tcustomer_reception.setValue("changedate", sysdate, 11L);//修改时间

        content.put("tcustomer_receptid", tcustomer_reception.getString("tcustomer_receptid"));

        saveDataLog(tcustomer_reception);
        tcustomerSet.save();
        tcustomerSet.close();

        return query_receptionMain();
    }


    /**
     * 经销商接待记录删除
     *
     * @return
     */
    public String receptionDelete() {
        SQLFactory sqlFactory = new SQLFactory(this, "经销商接待记录删除");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        sqlFactory.addParameter("tcustomer_receptid", content.getString("tcustomer_receptid"));

        String result = dbConnect.runSqlUpdate(sqlFactory.getSQL());

        if ("true".equals(result)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(result).toString();
        }
    }


    /**
     * 来源渠道选择
     *
     * @return
     */
    public String selectChannel() {
        SQLFactory sqlFactory = new SQLFactory(this, "来源渠道选择");
        sqlFactory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        String result = rows.toJsonArray("fchannel").toString();
        DataPool.put(content.toString(), result);
        return result;
    }


    /**
     * 接待门店选择
     *
     * @return
     */
    public String selectStore() {
        SQLFactory sqlFactory = new SQLFactory(this, "接待门店选择");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }


    /**
     * 销售动作选择
     *
     * @return
     */
    public String selectCustaction() {
        SQLFactory sqlFactory = new SQLFactory(this, "销售动作选择");
        sqlFactory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        String result = rows.toJsonArray("fcustaction").toString();
        DataPool.put(content.toString(), result);
        return result;
    }

    /**
     * 根据手机号匹配客户档案
     *
     * @return
     */
    public String queryCustomerByPhoneNumber() {
        if (content.getString("fphonenumber").length() >= 7) {
            SQLFactory sqlFactory = new SQLFactory(this, "手机号查询客户档案");
            sqlFactory.addParameter("siteid", siteid);
            sqlFactory.addParameter("tagentsid", tagentsid);
            sqlFactory.addParameter("fphonenumber", content.getString("fphonenumber") + "%");
            Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
            return getSucReturnObject().setData(rows).saveToDataPool().toString();
        } else {
            return getSucReturnObject().setData(new Rows()).toString();
        }
    }

}
