package com.cnd3b.common.actionscore;

import com.cnd3b.common.Controller;
import com.cnd3b.common.data.SQLFactory;
import com.cnd3b.common.data.Rows;
import com.cnd3b.common.data.db.DBConnect;

/**
 * 经销商行动力计算系统
 */
public class AgentActionScore extends ActionScore {
    /**
     * 经销商构造函数
     *
     * @param siteid    站点
     * @param tagentsid
     * @param createby
     */
    public AgentActionScore(String siteid, String tagentsid, long userid, String createby, ActionScore.fsourcetype fsourcetype, long fsourceid, String fnotes) {
        this.siteid = siteid;
        this.createby = createby;
        this.tagentsid = tagentsid;
        this.userid = userid;
        this.fusertype = "经销商";
        this.fsourcetype = fsourcetype;
        this.fsourceid = fsourceid;
        this.fnotes = fnotes;
    }

    /**
     * 经销商构造函数1
     */
    public AgentActionScore(Controller controller, ActionScore.fsourcetype fsourcetype, long fsourceid, String fnotes) {
        this.siteid = controller.siteid;
        this.createby = controller.username;
        this.tagentsid = controller.tagentsid;
        this.userid = controller.userid;
        this.fusertype = "经销商";
        this.fsourcetype = fsourcetype;
        this.fsourceid = fsourceid;
        this.fnotes = fnotes;
    }


    /**
     * 增加行动值
     */
    public void add() {
        synchronized (AgentActionScore.class) {
            super.actionScore_Add(addValidate());
        }
    }

    /**
     * 对需要验证的行动值增加验证,并返回行动得分
     *
     * @return
     */
    private int addValidate() {

        boolean isaddscore = true;
        switch (fsourcetype) {
            case 每日签到: {
                /**
                 * 规避当日重复签到，重复记分
                 */
                SQLFactory sqlFactory = new SQLFactory(this, "经销商_今日签到查询");
                sqlFactory.addParameter("siteid", siteid);
                sqlFactory.addParameter("tagentsid", tagentsid);
                Rows rows = new DBConnect().runSqlQuery(sqlFactory.getSQL());
                isaddscore = rows.isEmpty();
                break;
            }
            case 意向客户录入: {
                /**
                 * 同一条数据不会重复录入，无需考虑重复记录
                 */
                isaddscore = true;
                break;
            }
            case 销售录入: {
                /**
                 * 同一条数据不会重复录入，无需考虑重复记录
                 */
                isaddscore = true;
                break;
            }
            case 费用录入: {
                /**
                 * 同一条数据不会重复录入，无需考虑重复记录
                 */
                isaddscore = true;
                break;
            }
            case 事件录入: {
                isaddscore = true;
                break;
            }
            case 智库分享: {
                SQLFactory sqlFactory = new SQLFactory(this, "经销商_智库分享查询");
                sqlFactory.addParameter("siteid", siteid);
                sqlFactory.addParameter("tagentsid", tagentsid);
                sqlFactory.addParameter("fsourceid", fsourceid);
                Rows rows = new DBConnect().runSqlQuery(sqlFactory.getSQL());
                isaddscore = rows.isEmpty();
                break;
            }
            case 阅读通告: {
                /**
                 * 规避重复阅读通告，重复记分
                 */
                SQLFactory sqlFactory = new SQLFactory(this, "经销商_通告阅读查询");
                sqlFactory.addParameter("siteid", siteid);
                sqlFactory.addParameter("tagentsid", tagentsid);
                sqlFactory.addParameter("fsourceid", fsourceid);
                Rows rows = new DBConnect().runSqlQuery(sqlFactory.getSQL());
                isaddscore = rows.isEmpty();
                break;
            }
            case 通告评分: {
                /**
                 * 规避重复评分通告，重复记分
                 */
                SQLFactory sqlFactory = new SQLFactory(this, "经销商_通告评分查询");
                sqlFactory.addParameter("siteid", siteid);
                sqlFactory.addParameter("tagentsid", tagentsid);
                sqlFactory.addParameter("fsourceid", fsourceid);
                Rows rows = new DBConnect().runSqlQuery(sqlFactory.getSQL());
                isaddscore = rows.isEmpty();
                break;
            }
            case 下载通告附件: {
                /**
                 * 规避重复下载通告附件，重复记分
                 */
                SQLFactory sqlFactory = new SQLFactory(this, "经销商_下载通告附件查询");
                sqlFactory.addParameter("siteid", siteid);
                sqlFactory.addParameter("tagentsid", tagentsid);
                sqlFactory.addParameter("fsourceid", fsourceid);
                Rows rows = new DBConnect().runSqlQuery(sqlFactory.getSQL());
                isaddscore = rows.isEmpty();
                break;
            }
            case 下载营销智库: {
                /**
                 * 规避重复下载通告附件，重复记分
                 */
                SQLFactory sqlFactory = new SQLFactory(this, "经销商_下载智库素材查询");
                sqlFactory.addParameter("siteid", siteid);
                sqlFactory.addParameter("tagentsid", tagentsid);
                sqlFactory.addParameter("fsourceid", fsourceid);
                Rows rows = new DBConnect().runSqlQuery(sqlFactory.getSQL());
                isaddscore = rows.isEmpty();
                break;
            }
            default:break;
        }
        if (isaddscore) {
            return getScore();
        } else {
            return 0;
        }
    }


}
