/**
* Created by ldz..attack on 2016/11/23 0023.
* <p>
* 配合mybatis的预编译想用它做一个通用操作类
* <p>
* 目前来看只有:删除/权限修改两种操作用此方法比较简便
* <p>
* 如果是查询,大量的不同字段不易解决
*/
@Controller
@RequestMapping (value = "/general" )
public class ControllerGeneral extends BaseController {
public static final String shop = "ty_shop" ;
public static final String usr = "ty_member" ;
@Resource (name = "ServiceGeneral" )
private ServiceGeneral serviceGeneral;
/**
* 通用操作
* <p>
* 前端有属性type:
* <p>
* type:1.删除,2.禁用和启用
*/
@RequestMapping (value = "/operate" )
public void operate (HttpServletResponse response, HttpServletRequest request) throws Exception {
JSONObject jo = new JSONObject();
PageData pd = this .getPageData();
int i = 0 ;
try {
String ids = pd.get("ids" ) == null ? "" : pd.getString("ids" );
int type = pd.get("type" ) == null ? 0 : Transform.string_int(pd.getString("type" ));
List<String> list_id = ArrayTool.str2list(ids, "," );
for (String str_id : list_id) {
switch (type) {
case 1 :
pd = this .setVal(pd, type, str_id);
serviceGeneral.del(pd);
break ;
case 2 :
pd = this .setVal4Ban(pd, type, str_id, true );
int j = serviceGeneral.ban(pd);
if (j == 0 ) {
pd = this .setVal4Ban(pd, type, str_id, false );
serviceGeneral.allow(pd);
}
break ;
}
i++;
}
jo.put("code" , "success" );
jo.put("msg" , "成功操作" + i + "条记录" );
} catch (Exception e) {
jo.put("code" , "error" );
String msg = i == 0 ? "操作失败" : "操作未全部完成,修改" + i + "条记录" ;
jo.put("msg" , msg);
logger.error(e.toString(), e);
}
System.out.println(jo);
JsonRender8QC.renderObj(response, jo);
}
PageData setVal(PageData pd, int type, String id) {
String filter = pd.get("filter" ) == null ? "" : pd.getString("filter" );
if ("shop" .equals(filter)) {
pd.put("tableName" , shop);
this .setConditionAndValue(pd, "isDel" , "1" , "id" , id);
}
if ("usr" .equals(filter)) {
pd.put("tableName" , usr);
this .setConditionAndValue(pd, "isDel" , "1" , "id" , id);
}
return pd;
}
PageData setVal4Ban(PageData pd, int type, String id, boolean boo_ban) {
String filter = pd.get("filter" ) == null ? "" : pd.getString("filter" );
if ("shop" .equals(filter)) {
pd.put("tableName" , shop);
this .setConditionAndValue(pd, "isDisable" , boo_ban ? "1" : "0" , "id" , id);
}
if ("usr" .equals(filter)) {
pd.put("tableName" , usr);
this .setConditionAndValue(pd, "isDisabled" , boo_ban ? "1" : "0" , "id" , id);
}
return pd;
}
PageData setConditionAndValue(PageData pd, String editFieldName,
String editFieldValue,
String conditionFieldName,
String conditionFieldValue) {
pd.put("editFieldName" , editFieldName);
pd.put("editFieldValue" , editFieldValue);
pd.put("conditionFieldName" , conditionFieldName);
pd.put("conditionFieldValue" , conditionFieldValue);
return pd;
}
}
<update id ="del" parameterType ="pd" statementType ="STATEMENT" >
UPDATE ${tableName}
SET ${editFieldName}=${editFieldValue}
WHERE ${conditionFieldName} = ${conditionFieldValue}
</update >
<update id ="ban" parameterType ="pd" statementType ="STATEMENT" >
UPDATE ${tableName}
SET ${editFieldName}=${editFieldValue}
WHERE ${conditionFieldName} = ${conditionFieldValue}
/*更好的解决方案是,在操作之前,查一下*/
AND IFNULL(${editFieldName},0)=0
</update >
<update id ="allow" parameterType ="pd" statementType ="STATEMENT" >
UPDATE ${tableName}
SET ${editFieldName}=${editFieldValue}
WHERE ${conditionFieldName} = ${conditionFieldValue}
/*更好的解决方案是,在操作之前,查一下*/
AND IFNULL(${editFieldName},0)=1
</update >