package com.cnd3b.restcontroller.enterprise.agentwebclient.mainpage.action.saleeventinput;

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


    /**
     * 经销商营销事件列表查询
     *
     * @return
     */
    public String query_saleEventList() {
        /**
         *排序条件设置
         */
        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.fsaleevent 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("'");
            }
        }
        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).setFinalDo(DataPool.defaultdatalife, 1);
    }


    /**
     * 经销商营销事件主界面查询
     *
     * @return
     */
    public String query_saleEventMain() {
        SQLFactory sqlFactory = new SQLFactory(this, "经销商营销事件主界面查询");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        sqlFactory.addParameter("tsaleeventid", content.getString("tsaleeventid"));
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        for (Row row : rows) {
            row.put("datalog", getDataLog("tsaleevent", row.getString("tsaleeventid")));
        }
        return getSucReturnObject().setData(rows).toString();
    }

    /**
     * 经销商营销事件新增、修改
     *
     * @return
     */
    public String saleEventInsertOrModify() throws D3bException, P2Exception {
        long tsaleeventid = content.getLongValue("tsaleeventid");//营销事件ID，新增时默认传0
        String fsaleevent = content.getString("fsaleevent", "tsaleevent.fsaleevent", "事件标题");//营销事件名称
        Date fbegdate = content.getDate("fbegdate");//执行日期
        Date fenddate = content.getDate("fenddate");//完成时间

        PaoSetRemote tsaleeventSet = getP2ServerSystemPaoSet("tsaleevent", "siteid='" + siteid + "' and tagentsid='" + tagentsid + "' and tsaleeventid='" + tsaleeventid + "'");
        PaoRemote tsaleevent = null;
        if (tsaleeventid <= 0 || tsaleeventSet.isEmpty()) {
            tsaleevent = tsaleeventSet.addAtEnd();
            tsaleevent.setValue("siteid", siteid, 11L);//企业ID
            tsaleevent.setValue("tagentsid", tagentsid, 11L);//经销商ID
            tsaleevent.setValue("createby", username, 11L);//录入人
            tsaleevent.setValue("createdate", sysdate, 11L);//录入时间
            addActionScore(ActionScore.fsourcetype.事件录入, tsaleevent.getUniqueIDValue(), "销售事件录入");
        } else {
            tsaleevent = tsaleeventSet.getPao(0);
        }
        tsaleevent.setValue("fsaleevent", fsaleevent, 11L);
        tsaleevent.setValue("fbegdate", fbegdate, 11L);
        tsaleevent.setValue("fenddate", fenddate, 11L);

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

        saveDataLog(tsaleevent);
        content.put("tsaleeventid", tsaleevent.getString("tsaleeventid"));
        tsaleeventSet.save();

        return query_saleEventMain();
    }


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

        String result = dbConnect.runSqlUpdate(sqlFactory.getSQL());
        if ("true".equals(result)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(result).toString();
        }
    }

}
