package com.cnd3b.restcontroller.enterprise.agentwebclient.mainpage.workers;

import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.Controller;
import com.cnd3b.common.D3bException;
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 submitedit extends Controller {

    public submitedit(JSONObject content) {
        super(content);
    }

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

        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 t2.fname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.fphonenumber like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.fcommunity like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }
            if (whereObject.containsKey("fiscooperation") && !"".equals(whereObject.getString("fiscooperation"))) {
                where.append(" and t2.fiscooperation ='").append(whereObject.getString("fiscooperation")).append("' ");
            }
        }
        if (!fisadministrator) {
            where.append(" and (t2.createuserid is null or t2.createuserid='").append(userid).append("' or t2.createuserid in ( select fsubid from tenterprise_userdetail where siteid='").append(siteid).append("' and fparentid='").append(userid).append("' )) ");
        }
        SQLFactory queryworker = new SQLFactory(this, "合伙人提报列表查询", pageSize, pageNumber, sort);
        queryworker.addParameter("siteid", siteid);
        queryworker.addParameter("tagentsid", tagentsid);
        queryworker.addParameter_SQL("where", where);
        Rows workerrows = dbConnect.runSqlQuery(queryworker.getSQL());

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

    /**
     * 合伙人提报修改
     *
     * @return
     */
    public String update_submitedit() throws D3bException, P2Exception {
        long tagents_workersubmitid = content.getLongValue("tagents_workersubmitid");
        long tenterprise_userid = content.getLongValue("tenterprise_userid");
        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", "备注");

        PaoSetRemote tagents_workersubmitSet = getP2ServerSystemPaoSet("tagents_workersubmit", "fismodify=0 and siteid='" + siteid + "' and tagentsid='" + tagentsid + "' and tenterprise_userid='" + tenterprise_userid + "' and tagents_workersubmitid='" + tagents_workersubmitid + "'");
        if (tagents_workersubmitSet.isEmpty()) {
            return getErrReturnObject().setErrMsg("找不到原提报信息，或提报信息已修改").toString();
        }
        PaoRemote tagents_workersubmit = null;
        if (tenterprise_userid == userid) {
            /**
             * 如果修改对是之前已经创建的数据副本，则直接修改
             */
            tagents_workersubmit = tagents_workersubmitSet.getPao(0);
        } else {
            tagents_workersubmit = tagents_workersubmitSet.addAtEnd();
            PaoRemote tagents_workersubmit_old = tagents_workersubmitSet.getPao(0);
            tagents_workersubmit_old.setValue("fismodify", true, 11L);

            tagents_workersubmit.setValue("tenterprise_userid", tenterprise_userid, 11L);
            tagents_workersubmit.setValue("siteid", siteid, 11L);//企业ID
            tagents_workersubmit.setValue("tagentsid", tagentsid, 11L);//经销商ID
            tagents_workersubmit.setValue("fsourceid", tagents_workersubmitid, 11L);
            tagents_workersubmit.setValue("fsourceobject", tagents_workersubmit.getName(), 11L);
            tagents_workersubmit.setValue("createby", tagents_workersubmit_old.getString("createby"), 11L);//录入人
            tagents_workersubmit.setValue("createdate", tagents_workersubmit_old.getDate("createdate"), 11L);//录入时间
            tagents_workersubmit.setValue("changeby", username, 11L);//修改人
            tagents_workersubmit.setValue("changedate", sysdate, 11L);//修改时间
        }
        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_workersubmitSet.save();
        return getSucReturnObject().toString();
    }


    /**
     * 合伙人积分兑换规则查询
     *
     * @return
     */
    public String query_workerscorerule() {
        SQLFactory sqlFactory = new SQLFactory(this, "合伙人积分兑换规则查询");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).toString();
    }

    /**
     * 合伙人积分兑换规则修改
     *
     * @return
     */
    public String update_workerscorerule() throws P2Exception {
        if (!fisadministrator) {
            return getErrReturnObject().setErrMsg("您无此操作权限").toString();
        }
        int fmsgscore = content.getIntValue("fmsgscore");
        int fnamescore = content.getIntValue("fnamescore");
        int fphonenumberscore = content.getIntValue("fphonenumberscore");

        String fgrade1name = content.getString("fgrade1name");
        String fgrade2name = content.getString("fgrade2name");
        String fgrade3name = content.getString("fgrade3name");
        String fgrade4name = content.getString("fgrade4name");
        String fgrade5name = content.getString("fgrade5name");

        String fgrade1score = content.getString("fgrade1score");
        String fgrade2score = content.getString("fgrade2score");
        String fgrade3score = content.getString("fgrade3score");
        String fgrade4score = content.getString("fgrade4score");
        String fgrade5score = content.getString("fgrade5score");

        PaoSetRemote tagents_workerscoreruleSet = getP2ServerSystemPaoSet("tagents_workerscorerule", "siteid='" + siteid + "' and tagentsid='" + tagentsid + "'");
        PaoRemote tagents_workerscorerule = null;
        if (tagents_workerscoreruleSet.isEmpty()) {
            tagents_workerscorerule = tagents_workerscoreruleSet.addAtEnd();
            tagents_workerscorerule.setValue("siteid", siteid, 11L);//企业ID
            tagents_workerscorerule.setValue("tagentsid", tagentsid, 11L);//经销商ID
            tagents_workerscorerule.setValue("createby", username, 11L);//录入人
            tagents_workerscorerule.setValue("createdate", sysdate, 11L);//录入时间
        } else {
            tagents_workerscorerule = tagents_workerscoreruleSet.getPao(0);
        }
        tagents_workerscorerule.setValue("fmsgscore", fmsgscore, 11L);
        tagents_workerscorerule.setValue("fnamescore", fnamescore, 11L);
        tagents_workerscorerule.setValue("fphonenumberscore", fphonenumberscore, 11L);
        tagents_workerscorerule.setValue("fgrade1name", fgrade1name, 11L);
        tagents_workerscorerule.setValue("fgrade2name", fgrade2name, 11L);
        tagents_workerscorerule.setValue("fgrade3name", fgrade3name, 11L);
        tagents_workerscorerule.setValue("fgrade4name", fgrade4name, 11L);
        tagents_workerscorerule.setValue("fgrade5name", fgrade5name, 11L);

        tagents_workerscorerule.setValue("fgrade1score", fgrade1score, 11L);
        tagents_workerscorerule.setValue("fgrade2score", fgrade2score, 11L);
        tagents_workerscorerule.setValue("fgrade3score", fgrade3score, 11L);
        tagents_workerscorerule.setValue("fgrade4score", fgrade4score, 11L);
        tagents_workerscorerule.setValue("fgrade5score", fgrade5score, 11L);

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

        tagents_workerscoreruleSet.save();

        return query_workerscorerule();
    }
}
