package com.cnd3b.common.actionscore;

import com.cnd3b.common.BaseClass;
import p2.pao.PaoRemote;
import p2.pao.PaoSetRemote;

import java.util.HashMap;

/**
 * 行动力计算系统
 */
public class ActionScore extends BaseClass {

    String siteid = null;
    String createby = null;
    long userid = 0;
    String tagentsid = null;
    String fusertype = null;
    long fsourceid = 0L;
    String fnotes = null;
    fsourcetype fsourcetype = null;
    /**
     * 经销商行动力值 key:tagentsid,value:tactionscoreid
     */
    protected static HashMap<String, Long> agentactionscoremap = new HashMap<>();
    /**
     * 企业用户行动力值 key:userid,value:tactionscoreid
     */
    protected static HashMap<Long, Long> usertactionscoremap = new HashMap<>();


    /**
     * 创建行动力值
     */
    protected long getActionScoreID() {
        if ("企业".equals(fusertype) && usertactionscoremap.containsKey(userid)) {
            return usertactionscoremap.get(userid);
        } else if ("经销商".equals(fusertype) && agentactionscoremap.containsKey(tagentsid)) {
            return agentactionscoremap.get(tagentsid);
        }

        PaoSetRemote tactionscoreSet = null;
        PaoRemote tactionscore = null;
        try {
            if ("企业".equals(fusertype)) {
                tactionscoreSet = getP2ServerSystemPaoSet("tactionscore", "siteid='" + siteid + "' and fusertype='企业' and tenterprise_userid='" + userid + "'");
                if (tactionscoreSet.isEmpty()) {
                    tactionscore = tactionscoreSet.addAtEnd();
                    tactionscore.setValue("tenterprise_userid", userid, 11L);
                    tactionscore.setValue("fscore", 0, 11L);
                    tactionscore.setValue("fsitescore", 0, 11L);
                    tactionscore.setValue("fsysscore", 0, 11L);
                    tactionscore.setValue("fusertype", fusertype, 11L);
                    tactionscore.setValue("siteid", siteid, 11L);
                    tactionscoreSet.save();
                } else {
                    tactionscore = tactionscoreSet.getPao(0);
                }
                usertactionscoremap.put(userid, tactionscore.getUniqueIDValue());
            } else if ("经销商".equals(fusertype)) {
                tactionscoreSet = getP2ServerSystemPaoSet("tactionscore", "siteid='" + siteid + "' and fusertype='经销商' and tagentsid='" + tagentsid + "'");
                if (tactionscoreSet.isEmpty()) {
                    tactionscore = tactionscoreSet.addAtEnd();
                    tactionscore.setValue("tagentsid", tagentsid, 11L);
                    tactionscore.setValue("fscore", 0, 11L);
                    tactionscore.setValue("fsitescore", 0, 11L);
                    tactionscore.setValue("fsysscore", 0, 11L);
                    tactionscore.setValue("fusertype", fusertype, 11L);
                    tactionscore.setValue("siteid", siteid, 11L);
                    tactionscoreSet.save();
                } else {
                    tactionscore = tactionscoreSet.getPao(0);
                }
                agentactionscoremap.put(tagentsid, tactionscore.getUniqueIDValue());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                tactionscoreSet.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if ("企业".equals(fusertype)) {
            return usertactionscoremap.get(userid);
        } else if ("经销商".equals(fusertype)) {
            return agentactionscoremap.get(tagentsid);
        }
        return 0L;
    }


    /**
     * 增加经销商行动力
     *
     * @param fscore 分值
     */
    protected void actionScore_Add(int fscore) {
        if (fscore == 0) {
            return;
        }
        long tactionscoreid = getActionScoreID();
        PaoSetRemote tactionscoreSet = null;
        try {
            tactionscoreSet = getP2ServerSystemPaoSet("tactionscore", "siteid='" + siteid + "' and tactionscoreid=" + tactionscoreid);
            if (!tactionscoreSet.isEmpty()) {
                PaoRemote tactionscore = tactionscoreSet.getPao(0);
                tactionscore.setValue("fscore", tactionscore.getInt("fscore") + fscore, 11L);
                tactionscore.setValue("fsysscore", tactionscore.getInt("fsysscore") + fscore, 11L);
                tactionscore.setValue("fsitescore", tactionscore.getInt("fsitescore") + fscore, 11L);
                PaoSetRemote tactionscoremxSet = tactionscore.getPaoSet("$tactionscoremx", "tactionscoremx");
                String[] ftypemxs = new String[]{"个人", "企业", "平台"};
                for (String ftypemx : ftypemxs) {
                    PaoRemote tactionscoremx = tactionscoremxSet.addAtEnd();
                    tactionscoremx.setValue("siteid", siteid, 11L);
                    tactionscoremx.setValue("ftype", "收入", 11L);
                    tactionscoremx.setValue("ftypemx", ftypemx, 11L);
                    tactionscoremx.setValue("fscore", fscore, 11L);
                    tactionscoremx.setValue("fnotes", fnotes, 11L);
                    tactionscoremx.setValue("tactionscoreid", tactionscoreid, 11L);
                    tactionscoremx.setValue("createby", createby, 11L);
                    tactionscoremx.setValue("createdate", getDateTime(), 11L);
                    tactionscoremx.setValue("fsourcetype", fsourcetype.toString(), 11L);
                    tactionscoremx.setValue("fsourceid", fsourceid, 11L);
                }
                tactionscoreSet.save();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (tactionscoreSet != null) {
                try {
                    tactionscoreSet.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * 扣减行动力
     *
     * @param ftypemx  企业、平台
     * @param fscore   分值
     * @param fnotes   收支说明
     * @param fremarks 备注
     */
    protected boolean actionScore_Sub(String ftypemx, int fscore, String fnotes, String fremarks) {
        long tactionscoreid = getActionScoreID();
        PaoSetRemote tactionscoreSet = null;
        try {
            tactionscoreSet = getP2ServerSystemPaoSet("tactionscore", "siteid='" + siteid + "' and tactionscoreid=" + tactionscoreid);
            if (!tactionscoreSet.isEmpty()) {
                PaoRemote tactionscore = tactionscoreSet.getPao(0);
                switch (ftypemx) {
                    case "个人":
                        tactionscore.setValue("fscore", tactionscore.getInt("fscore") - fscore, 11L);
                        break;
                    case "企业":
                        tactionscore.setValue("fsitescore", tactionscore.getInt("fsitescore") - fscore, 11L);
                        break;
                    case "平台":
                        tactionscore.setValue("fsysscore", tactionscore.getInt("fsysscore") - fscore, 11L);
                        break;
                    default:
                        break;
                }

                PaoSetRemote tactionscoremxSet = tactionscore.getPaoSet("$tactionscoremx", "tactionscoremx");

                PaoRemote tactionscoremx = tactionscoremxSet.addAtEnd();
                tactionscoremx.setValue("siteid", siteid, 11L);
                tactionscoremx.setValue("ftype", "支出", 11L);
                tactionscoremx.setValue("ftypemx", ftypemx, 11L);
                tactionscoremx.setValue("fscore", fscore, 11L);
                tactionscoremx.setValue("fnotes", fnotes, 11L);
                tactionscoremx.setValue("fremarks", fremarks, 11L);
                tactionscoremx.setValue("tactionscoreid", tactionscoreid, 11L);
                tactionscoremx.setValue("createby", createby, 11L);
                tactionscoremx.setValue("createdate", getDateTime(), 11L);

                tactionscoreSet.save();
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (tactionscoreSet != null) {
                try {
                    tactionscoreSet.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
        return false;
    }

    public enum fsourcetype {
        /**
         * fsourceid为经销商档案ID
         */
        每日签到,
        /**
         * fsourceid为客户接待录入ID
         */
        意向客户录入,
        /**
         * fsourceid为客户销售录入ID
         */
        销售录入,
        /**
         * fsourceid为费用录入ID
         */
        费用录入,
        /**
         * fsourceid为事件录入ID
         */
        事件录入,
        /**
         * fsourceid为智库ID
         */
        智库分享,
        /**
         * fsourceid为通告ID
         */
        阅读通告,
        /**
         * fsourceid为通告ID
         */
        通告评分,
        /**
         * fsourceid为通告ID
         */
        下载通告附件,
        /**
         * fsourceid为智库ID
         */
        下载营销智库,
        标准工作录入,
        自选工作录入,
        行程评优,
        店铺录入
    }

    public int getScore() {
        if ("经销商".equals(fusertype)) {
            switch (fsourcetype) {
                case 每日签到:
                    return 10;
                case 意向客户录入:
                    return 5;
                case 销售录入:
                    return 10;
                case 费用录入:
                    return 5;
                case 事件录入:
                    return 50;
                case 智库分享:
                    return 50;
                case 阅读通告:
                    return 5;
                case 通告评分:
                    return 10;
                case 下载通告附件:
                    return 5;
                case 下载营销智库:
                    return 5;

                default:
                    break;
            }
        } else if ("企业".equals(fusertype)) {
            switch (fsourcetype) {
                case 每日签到:
                    return 10;
                case 标准工作录入:
                    return 10;
                case 自选工作录入:
                    return 5;
                case 行程评优:
                    return 50;
                case 销售录入:
                    return 50;
                case 店铺录入:
                    return 10;
                case 阅读通告:
                    return 5;
                case 通告评分:
                    return 10;
                case 下载通告附件:
                    return 5;
                case 下载营销智库:
                    return 5;
                default:
                    break;
            }
        }

        return 0;
    }
}
