package com.awspaas.user.apps.donenow_sys.controller;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.bind.annotation.Controller;
import com.actionsoft.bpms.server.bind.annotation.Mapping;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.sql.SQLException;

@Controller
public class accountClassController {
    public String getAccountClass() {
        return "accountClass";
    }

    /**
     * 删除账类
     * @param uc
     * @param ids
     * @return
     * @throws SQLException
     */
    @Mapping(value = "com.awspaas.user.apps.donenow_sys_delAccountClass")
    public ResponseObject delAccountClass(UserContext uc, String ids) throws SQLException {
        if (StringUtils.isBlank(ids))
            return ResponseObject.newErrResponse("请选择要删除的客户分类");

        Connection conn = null;
        try {
            conn = DBSql.open();
            conn.setAutoCommit(false);

            for (String id : ids.split(",")) {
                BO bo = SDK.getBOAPI().get("BO_EU_DND_ACCOUNT_CLASSIFICATION", id);
                if (bo.getString("IS_SYSTEM").equals("1"))
                    throw new Exception("系统内置的分类不能删除");
                bo.set("CLOSED", "1");
                SDK.getBOAPI().update("BO_EU_DND_ACCOUNT_CLASSIFICATION", bo, conn);
            }
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            conn.rollback();
            return ResponseObject.newErrResponse(e.getMessage());
        } catch (Exception e) {
            return ResponseObject.newErrResponse(e.getMessage());
        } finally {
            if (conn != null)
                DBSql.close(conn);
        }
        return ResponseObject.newOkResponse();
    }

    /**
     * 复活账号分类
     */
    @Mapping(value = "com.awspaas.user.apps.donenow_sys_reviveAccountClass")
    public ResponseObject reviveAccountClass(UserContext uc, String ids) throws SQLException {
        if (StringUtils.isBlank(ids))
            return ResponseObject.newErrResponse("请选择要复活的账号分类");
        int count = 0;
        Connection conn = null;
        try {
            conn = DBSql.open();
            conn.setAutoCommit(false);

            for (String id : ids.split(",")) {
                BO bo = SDK.getBOAPI().get("BO_EU_DND_ACCOUNT_CLASSIFICATION", id);
                if (bo.getString("CLOSED").equals("1")) {
                    count++;
                    bo.set("CLOSED", "0");
                    SDK.getBOAPI().update("BO_EU_DND_ACCOUNT_CLASSIFICATION", bo, conn);
                }
            }
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            conn.rollback();
            return ResponseObject.newErrResponse(e.getMessage());
        } catch (Exception e) {
            return ResponseObject.newErrResponse(e.getMessage());
        } finally {
            if (conn != null)
                DBSql.close(conn);
        }
        if (count == 0) {
            return ResponseObject.newWarnResponse("没有需要激活的账号分类");
        }
        return ResponseObject.newOkResponse();
    }

}
