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 workers extends Controller {
    public workers(JSONObject content) {
        super(content);
    }

    /**
     * 合伙人列表
     *
     * @return
     */
    public String query_workerlist() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.changedate"};
        String sort = getSort(sortfield, "t1.changedate 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 t1.frole like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.fphonenumber like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }
            if (whereObject.containsKey("fiscooperation") && !"".equals(whereObject.getString("fiscooperation"))) {
                where.append(" and t1.fiscooperation ='").append(whereObject.getString("fiscooperation")).append("' ");
            }
        }
        if (!fisadministrator) {
            where.append(" and (t1.createuserid is null or t1.createuserid='").append(userid).append("' or t1.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 workerInsertOrModify() throws D3bException, P2Exception {
        long tagents_workersid = content.getLongValue("tagents_workersid");//工人ID，新增时默认传0
        String fname = content.getString("fname", "tagents_workers.fname", "姓名");//姓名
        String fphonenumber = content.getString("fphonenumber", "tagents_workers.fphonenumber", "手机");//手机号
        String frole = content.getString("frole", "tagents_workers.frole", "职业");//角色
        String fnotes = content.getString("fnotes", "tagents_workers.fnotes", "备注");//备注

        String fiscooperation = content.getString("fiscooperation");//合作状态

        PaoSetRemote tagents_workersSet = getP2ServerSystemPaoSet("tagents_workers", "siteid='" + siteid + "' and tagentsid='" + tagentsid + "' and tagents_workersid='" + tagents_workersid + "'");
        PaoRemote tagents_worker = null;
        if (tagents_workersid <= 0 || tagents_workersSet.isEmpty()) {
            tagents_worker = tagents_workersSet.addAtEnd();
            tagents_worker.setValue("siteid", siteid, 11L);//企业ID
            tagents_worker.setValue("tagentsid", tagentsid, 11L);//经销商ID
            tagents_worker.setValue("createuserid", userid, 11L);
            tagents_worker.setValue("createby", username, 11L);//录入人
            tagents_worker.setValue("createdate", sysdate, 11L);//录入时间
            tagents_worker.setValue("fscore", 0, 11L);
            tagents_worker.setValue("ftotalscore", 0, 11L);
            tagents_worker.setValue("fsubmitcount", 0, 11L);
        } else {
            tagents_worker = tagents_workersSet.getPao(0);
        }
        tagents_worker.setValue("fname", fname, 11L);
        tagents_worker.setValue("fphonenumber", fphonenumber, 11L);
        tagents_worker.setValue("frole", frole, 11L);
        tagents_worker.setValue("fnotes", fnotes, 11L);
        tagents_worker.setValue("fiscooperation", fiscooperation, 11L);

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

        long tenterprise_userid = tagents_worker.getLong("tenterprise_userid");

        PaoSetRemote tenterprise_usersSet = tagents_worker.getPaoSet("$tenterprise_users", "tenterprise_users", "siteid='" + siteid + "' and tagentsid='" + tagentsid + "' and tenterprise_userid='" + tenterprise_userid + "' and fusertype='工人'");
        PaoRemote tenterprise_user = null;
        if (tenterprise_usersSet.isEmpty()) {
            tenterprise_user = tenterprise_usersSet.addAtEnd();
            tenterprise_user.setValue("siteid", siteid, 11L);
            tenterprise_user.setValue("tagentsid", tagentsid, 11L);
            tenterprise_user.setValue("fusertype", "工人", 11L);
            tagents_worker.setValue("tenterprise_userid", tenterprise_user.getUniqueIDValue(), 11L);
        } else {
            tenterprise_user = tenterprise_usersSet.getPao(0);
        }
        tenterprise_user.setValue("fname", fname, 11L);
        tenterprise_user.setValue("fphonenumber", fphonenumber, 11L);
        tenterprise_user.setValue("frole", frole, 11L);

        tagents_workersSet.save();
        return getSucReturnObject().toString();
    }

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

        long tenterprise_userid = content.getLongValue("tenterprise_userid");
        SQLFactory queryworker = new SQLFactory(this, "合伙人详情历史提报列表查询", pageSize, pageNumber, sort);
        queryworker.addParameter("siteid", siteid);
        queryworker.addParameter("tagentsid", tagentsid);
        queryworker.addParameter("tenterprise_userid", tenterprise_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");

        long tenterprise_userid = content.getLongValue("tenterprise_userid");
        SQLFactory queryworker = new SQLFactory(this, "合伙人详情历史积分列表查询", pageSize, pageNumber, sort);
        queryworker.addParameter("siteid", siteid);
        queryworker.addParameter("tagentsid", tagentsid);
        queryworker.addParameter("tenterprise_userid", tenterprise_userid);
        Rows workerrows = dbConnect.runSqlQuery(queryworker.getSQL());

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

    /**
     * 合伙人积分调整
     *
     * @return
     */
    public String changeAgentScore() throws D3bException, P2Exception {
        long tenterprise_userid = content.getLongValue("tenterprise_userid");//工人账号ID
        String fnotes = content.getString("fnotes", "tagents_workerscoremx.fnotes", "备注");
        int fscore = content.getIntValue("fscore");
        if (fscore == 0) {
            return getErrReturnObject().setErrMsg("积分调整不可为0").toString();
        }

        PaoSetRemote tagents_workersSet = getP2ServerSystemPaoSet("tagents_workers", "siteid='" + siteid + "' and tagentsid='" + tagentsid + "' and tenterprise_userid=" + tenterprise_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);

            PaoSetRemote tagents_workerscoremxSet = tagents_worker.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", fnotes, 11L);
            tactionscoremx.setValue("tenterprise_userid", tenterprise_userid, 11L);

            tactionscoremx.setValue("fsourceobject", "TENTERPRISE_USER", 11L);
            tactionscoremx.setValue("fsourceid", userid, 11L);

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

            SQLFactory sqlFactory = new SQLFactory(this, "积分调整消息提醒");
            sqlFactory.addParameter("siteid", siteid);
            if (fscore > 0) {
                sqlFactory.addParameter("fmessage", "合伙人奖励积分+" + fscore + "，奖励原因：" + fnotes);
            } else {
                sqlFactory.addParameter("fmessage", "合伙人扣减积分-" + fscore + "，扣减原因：" + fnotes);
            }
            sqlFactory.addParameter("tenterprise_userid", tenterprise_userid);
            sqlFactory.addParameter("ftype", "系统");
            sqlFactory.addParameter("tagents_workerscoremxid", tagents_workerscoremxid);
            dbConnect.runSqlUpdate(sqlFactory.getSQL());
        } else {
            return getErrReturnObject().setErrMsg("没有此合伙人信息").toString();
        }
        return getSucReturnObject().toString();
    }

    /**
     * 工人角色查询
     *
     * @return
     */
    public String query_role() {
        SQLFactory sqlFactory = new SQLFactory(this, "工人角色选择");
        sqlFactory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }
}
