package com.cnd3b.restcontroller.enterprise.wechatclient.workers;

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 p2.pao.PaoRemote;
import p2.pao.PaoSetRemote;
import p2.util.P2Exception;

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

    /**
     * 首页简报
     *
     * @return
     */
    public String query_briefreport() {
        SQLFactory factory = new SQLFactory(this, "首页简报");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tagentsid", tagentsid);
        factory.addParameter("tenterprise_userid", userid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows.get(0)).saveToDataPool(1).toString();
    }

    /**
     * 历史提报列表
     *
     * @return
     */
    public String query_workersubmitList() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.createdate"};
        String sort = getSort(sortfield, "t1.createdate desc");

        SQLFactory queryworker = new SQLFactory(this, "历史提报列表查询", pageSize, pageNumber, sort);
        queryworker.addParameter("siteid", siteid);
        queryworker.addParameter("tagentsid", tagentsid);
        queryworker.addParameter("tenterprise_userid", userid);
        Rows workerrows = dbConnect.runSqlQuery(queryworker.getSQL());
        return getSucReturnObject().setDataByPaging(workerrows).saveToDataPool().preloading(1).toString();
    }


    /**
     * 积分记录列表
     *
     * @return
     */
    public String query_workerscoreList() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.createdate"};
        String sort = getSort(sortfield, "t1.createdate desc");

        SQLFactory queryworker = new SQLFactory(this, "积分历史列表查询", pageSize, pageNumber, sort);
        queryworker.addParameter("siteid", siteid);
        queryworker.addParameter("tagentsid", tagentsid);
        queryworker.addParameter("tenterprise_userid", userid);
        Rows workerrows = dbConnect.runSqlQuery(queryworker.getSQL());

        return getSucReturnObject().setDataByPaging(workerrows).saveToDataPool().preloading(1).toString();
    }

    /**
     * 合伙人提报
     *
     * @return
     */
    public String submiteditInsertOrModify() throws D3bException, P2Exception {
        long tagents_workersubmitid = content.getLongValue("tagents_workersubmitid");//新增时传0
        String fname = content.getString("fname", "tagents_workersubmit.fname", "客户姓名");
        String fphonenumber = content.getString("fphonenumber", "tagents_workersubmit.fphonenumber", "客户电话");
        String fcommunity = content.getString("fcommunity", "tagents_workersubmit.fcommunity", "小区");
        String fhouseunit = content.getString("fhouseunit", "tagents_workersubmit.fhouseunit", "单元");
        String fhousenumber = content.getString("fhousenumber", "tagents_workersubmit.fhousenumber", "门牌号");
        String fnotes = content.getString("fnotes", "tagents_workersubmit.fnotes", "备注");
        Rows checkrows = dbConnect.runSqlQuery("select *from tagents_workersubmit where siteid='" + siteid + "' and tagentsid='" + tagentsid + "' and fcommunity='" + fcommunity + "' and fhouseunit='" + fhouseunit + "' and fhousenumber='" + fhousenumber + "'");
        if (!checkrows.isEmpty()) {
            return getErrReturnObject().setErrMsg("系统已存在当前客户信息，无法提报！").toString();
        }
        PaoSetRemote tagents_workersubmitSet = getP2ServerSystemPaoSet("tagents_workersubmit", " siteid='" + siteid + "' and tagentsid='" + tagentsid + "' and tenterprise_userid='" + userid + "' and tagents_workersubmitid='" + tagents_workersubmitid + "'");
        PaoRemote tagents_workersubmit = null;
        if (tagents_workersubmitid <= 0 || tagents_workersubmitSet.isEmpty()) {
            tagents_workersubmit = tagents_workersubmitSet.addAtEnd();
            tagents_workersubmit.setValue("createby", username, 11L);//录入人
            tagents_workersubmit.setValue("createdate", sysdate, 11L);//录入时间
            tagents_workersubmit.setValue("tenterprise_userid", userid, 11L);
            tagents_workersubmit.setValue("siteid", siteid, 11L);//企业ID
            tagents_workersubmit.setValue("tagentsid", tagentsid, 11L);//经销商ID
        } else {
            tagents_workersubmit = tagents_workersubmitSet.getPao(0);
        }
        tagents_workersubmit.setValue("fname", fname, 11L);//客户姓名
        tagents_workersubmit.setValue("fphonenumber", fphonenumber, 11L);//客户手机号
        tagents_workersubmit.setValue("fcommunity", fcommunity, 11L);//客户小区
        tagents_workersubmit.setValue("fhouseunit", fhouseunit, 11L);//客户单元
        tagents_workersubmit.setValue("fhousenumber", fhousenumber, 11L);//客户门牌号
        tagents_workersubmit.setValue("fnotes", fnotes, 11L);//备注

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

        int fscore = 0;
        long tagents_workerscoremxid = 0L;
        if (tagents_workersubmit.toBeAdded()) {
            /**
             * 生成积分
             */
            Rows scorerows = dbConnect.runSqlQuery("select isnull(fmsgscore,0)as fmsgscore,isnull(fnamescore,0)as fnamescore,isnull(fphonenumberscore,0)as fphonenumberscore from tagents_workerscorerule where siteid='" + siteid + "' and tagentsid='" + tagentsid + "'");
            if (!scorerows.isEmpty()) {
                Row scorerow = scorerows.get(0);
                fscore = scorerow.getInteger("fmsgscore");
                int fnamescore = scorerow.getInteger("fnamescore");
                int fphonenumberscore = scorerow.getInteger("fphonenumberscore");
                if (!"".equals(fname)) {
                    fscore = fscore + fnamescore;
                }
                if (!"".equals(fphonenumber)) {
                    fscore = fscore + fphonenumberscore;
                }
            }
            if (fscore > 0) {
                PaoSetRemote tagents_workersSet = tagents_workersubmit.getPaoSet("$tagents_workers", "tagents_workers", "siteid='" + siteid + "' and tagentsid='" + tagentsid + "' and tenterprise_userid='" + userid + "'");
                if (!tagents_workersSet.isEmpty()) {
                    PaoRemote tagents_worker = tagents_workersSet.getPao(0);
                    tagents_worker.setValue("fscore", tagents_worker.getInt("fscore") + fscore, 11L);
                    tagents_worker.setValue("ftotalscore", tagents_worker.getInt("ftotalscore") + fscore, 11L);
                    tagents_worker.setValue("fsubmitcount", tagents_worker.getInt("fsubmitcount") + 1, 11L);
                } else {
                    return getErrReturnObject().setErrMsg("没有此合伙人信息").toString();
                }
                PaoSetRemote tagents_workerscoremxSet = tagents_workersubmit.getPaoSet("$tagents_workerscoremx", "tagents_workerscoremx");
                PaoRemote tactionscoremx = tagents_workerscoremxSet.addAtEnd();
                tactionscoremx.setValue("siteid", siteid, 11L);
                tactionscoremx.setValue("tagentsid", tagentsid, 11L);
                tactionscoremx.setValue("fscore", fscore, 11L);
                tactionscoremx.setValue("fnotes", "提报奖励", 11L);
                tactionscoremx.setValue("tenterprise_userid", userid, 11L);

                tactionscoremx.setValue("fsourceobject", tagents_workersubmit.getName(), 11L);
                tactionscoremx.setValue("fsourceid", tagents_workersubmit.getUniqueIDValue(), 11L);

                tactionscoremx.setValue("createby", username, 11L);
                tactionscoremx.setValue("createdate", getDateTime(), 11L);
                tagents_workerscoremxid = tactionscoremx.getUniqueIDValue();
            }
        }
        tagents_workersubmitSet.save();

        if (fscore > 0 && tagents_workerscoremxid > 0) {
            SQLFactory sqlFactory = new SQLFactory(this, "提报成功消息提醒");
            sqlFactory.addParameter("siteid", siteid);
            sqlFactory.addParameter("fmessage", "提报信息成功，积分奖励" + fscore + "!");
            sqlFactory.addParameter("tenterprise_userid", userid);
            sqlFactory.addParameter("ftype", "系统");
            sqlFactory.addParameter("tagents_workerscoremxid", tagents_workerscoremxid);
            dbConnect.runSqlUpdate(sqlFactory.getSQL());
        }
        return getSucReturnObject().toString();
    }
}
