package com.cnd3b.restcontroller.enterprise.salerwebclient.mainpage.action.saleinput;

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

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

    /**
     * 销售记录列表
     *
     * @return
     */
    public String query_saleList() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.tagents_purchaseid"};
        String sort = getSort(sortfield, "t1.tagents_purchaseid 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("t2.fagentname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }
        }
        SQLFactory costhead = new SQLFactory(this, "销售记录列表查询", pageSize, pageNumber, sort);
        costhead.addParameter("siteid", siteid);
        costhead.addParameter_in("fsaleruserid", getSubUsers());
        costhead.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(costhead.getSQL());
        return getSucReturnObject().setDataByPaging(rows, sortfield).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 销售记录主界面查询
     *
     * @return
     */
    public String query_saleMain() {
        SQLFactory costhead = new SQLFactory(this, "销售记录主界面查询");
        costhead.addParameter("siteid", siteid);
        costhead.addParameter("tagents_purchaseid", content.getString("tagents_purchaseid"));

        Rows rows = dbConnect.runSqlQuery(costhead.getSQL());
        for (Row row : rows) {
            row.put("datalog", getDataLog("tagents_purchase", row.getString("tagents_purchaseid")));
        }
        return getSucReturnObject().setData(rows).toString();
    }

    /**
     * 销售记录新增、修改
     *
     * @return
     */
    public String saleInsertOrModify() throws P2Exception {
        long tagentsid = content.getLongValue("tagentsid");//经销商ID
        long tagents_purchaseid = content.getLongValue("tagents_purchaseid");//销售记录ID，新增时默认为0
        int fyear = content.getIntValue("fyear");//年
        int fmonth = content.getIntValue("fmonth");//月
        double famount = content.getDoubleValue("famount");//金额

        PaoSetRemote tagents_purchaseSet = getP2ServerSystemPaoSet("tagents_purchase", "siteid='" + siteid + "' and tagents_purchaseid='" + tagents_purchaseid + "'");
        PaoRemote tagents_purchase = null;
        if (tagents_purchaseid <= 0 || tagents_purchaseSet.isEmpty()) {
            tagents_purchase = tagents_purchaseSet.addAtEnd();
            tagents_purchase.setValue("siteid", siteid, 11L);//企业ID
            tagents_purchase.setValue("createby", username, 11L);//录入人
            tagents_purchase.setValue("createdate", sysdate, 11L);//录入时间

            addActionScore(ActionScore.fsourcetype.销售录入, tagents_purchase.getUniqueIDValue(), "销售录入");
        } else {
            tagents_purchase = tagents_purchaseSet.getPao(0);
        }
        tagents_purchase.setValue("tagentsid", tagentsid, 11L);//经销商ID
        tagents_purchase.setValue("fyear", fyear, 11L);
        tagents_purchase.setValue("fmonth", fmonth, 11L);
        tagents_purchase.setValue("famount", famount, 11L);

        content.put("tagents_purchaseid", tagents_purchase.getUniqueIDValue());
        saveDataLog(tagents_purchase);
        tagents_purchaseSet.save();

        return query_saleMain();
    }


    /**
     * 销售记录删除
     *
     * @return
     */
    public String saleDelete() {
        SQLFactory sqlFactory = new SQLFactory(this, "销售记录删除");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagents_purchaseid", content.getLongValue("tagents_purchaseid"));

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

    /**
     * 经销商选择列表查询
     *
     * @return
     */
    public String query_agentList() {
        SQLFactory sqlFactory = new SQLFactory(this, "经销商列表查询");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter_in("fsaleruserid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }
}
