package com.cnd3b.restcontroller.enterprise.agentclient.usercenter.signin;

import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.Controller;
import com.cnd3b.common.actionscore.ActionScore;
import com.cnd3b.common.data.Rows;
import p2.pao.PaoRemote;
import p2.pao.PaoSetRemote;
import p2.util.P2Exception;

public class SignIn extends Controller {

    public SignIn(JSONObject content) {
        super(content);
    }

    /**
     * 签到
     *
     * @return
     */
    public String signin() throws P2Exception {
        PaoSetRemote tsigninSet = getP2ServerSystemPaoSet("tsignin", "siteid='" + siteid + "' and tenterprise_userid='" + userid + "' and convert(varchar(10),FDATE,120)=convert(varchar(10),getdate(),120)");
        if (tsigninSet.isEmpty()) {
            PaoRemote pao = tsigninSet.addAtEnd();
            pao.setValue("siteid", siteid, 11L);
            pao.setValue("tenterprise_userid", userid, 11L);
            pao.setValue("fdate", getDateTime(), 11L);
            tsigninSet.save();
            addActionScore(ActionScore.fsourcetype.每日签到, Long.parseLong(tagentsid), "每日签到");
        } else {
            return getErrReturnObject().setErrMsg("今日已签到！").toString();
        }
        return getSucReturnObject().setData("签到成功!").toString();
    }

    /**
     * 获取本月签到记录
     *
     * @return
     */
    public String query_signinlog() {
        String sql = "select convert(varchar(10),fdate,120)fdate from tsignin where siteid='" + siteid + "' and tenterprise_userid='" + userid + "' and MONTH(fdate)=MONTH(GETDATE())";
        Rows rows = dbConnect.runSqlQuery(sql);
        return getSucReturnObject().setData(rows.toJsonArray("fdate")).saveToDataPool().toString();
    }

}
