package cc.smtweb.biz.mztb.web.base.partyDataFill;

import cc.smtweb.biz.mztb.web.base.FillOut;
import cc.smtweb.biz.mztb.web.tool.MztbEnum;
import cc.smtweb.framework.core.common.R;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.EntityHelper;
import cc.smtweb.framework.core.mvc.service.AbstractHandler;
import cc.smtweb.framework.core.session.UserSession;

/**
 * 机构填报自定义请求类
 */
public class PartyDataCustomHandler extends AbstractHandler {
    // 逻辑删除
    public R logicDel(SwMap params, UserSession us) {
        DbEngine.getInstance().update(
                "update "+ EntityHelper.getSchemaTableName(FillOut.ENTITY_NAME) + " set fo_state = ? where fo_id = ? "
                , MztbEnum.FoState.LOGIC.value,params.readLong("id"));
        return R.success();
    }

    // 确认
    public R affirm(SwMap params, UserSession us) {
        DbEngine.getInstance().update(
                "update "+ EntityHelper.getSchemaTableName(FillOut.ENTITY_NAME) + " set fo_state = ? where fo_id = ? "
                , MztbEnum.FoState.AFFIRM.value,params.readLong("id"));
        return R.success();
    }

    // 取消确认
    public R cancelAffirm(SwMap params, UserSession us) {
        DbEngine.getInstance().update(
                "update "+ EntityHelper.getSchemaTableName(FillOut.ENTITY_NAME) + " set fo_state = ? where fo_id = ? "
                , MztbEnum.FoState.WAIT_AFFIRM.value,params.readLong("id"));
        return R.success();
    }

    // 推送数据到前置库
    public R pushData(SwMap params, UserSession us) {
        return null;
    }

    public R checkRepetition(SwMap params, UserSession us) {
        DbEngine instance = DbEngine.getInstance();
        String task = params.readString("task");
        if(task.length() == 4) task+="99";
        Integer cur_party_id = instance.queryInt("select count(*) from "+EntityHelper.getSchemaTableName(FillOut.ENTITY_NAME)
                        +" where fo_party_id = ? and fo_state != -1 and fo_task = ?"
                , us.getPartyId(),task);
        if(cur_party_id != 0) {
            return R.error("当前机构已有该任务的填报数据："+params.readString("task_text"));
        }
        return R.success();
    }
}
