package com.cnd3b.restcontroller.enterprise.agentwebclient.usercenter.actionscore;

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

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

    /**
     * 行动力总值查询
     *
     * @return
     */
    public String query_total() {
        SQLFactory sqlFactory = new SQLFactory(this, "行动力总值查询");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        if(!rows.isEmpty()){
            return getSucReturnObject().setData(rows.get(0)).saveToDataPool().toString();
        }else{
            return getSucReturnObject().setData(new Row()).saveToDataPool().toString();
        }

    }

    /**
     * 行动力日志
     *
     * @return
     */
    public String query_scorelog() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.submitdate"};
        String sort = getSort(sortfield, "t1.createdate desc");

        SQLFactory sqlFactory = new SQLFactory(this, "行动力值收支明细查询", pageSize, pageNumber, sort);
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tactionscoreid", content.getString("tactionscoreid"));
        sqlFactory.addParameter("ftypemx", content.getString("ftypemx"));
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());

        return getSucReturnObject().setDataByPaging(rows, sortfield).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 经销商个人行动力排名
     *
     * @return
     */
    public String query_rank() {
        SQLFactory sqlFactory = new SQLFactory(this, "行动力名次查询");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 经销商行动力排行榜
     *
     * @return
     */
    public String query_rankinglist() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.fscore desc"};
        String sort = getSort(sortfield, "t1.fscore desc");

        SQLFactory sqlFactory = new SQLFactory(this, "行动力排行榜", pageSize, pageNumber, sort);
        sqlFactory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setDataByPaging(rows, sortfield).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 经销商任务完成情况
     *
     * @return
     */
    public String query_taskstatus() {
        SQLFactory sqlFactory = new SQLFactory(this, "今日已完成的任务查询");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).toString();
    }
}
