package com.nanometer.smartlab.controller;
|
|
import com.nanometer.smartlab.entity.BaseMeta;
|
import com.nanometer.smartlab.service.BaseMetaService;
|
import com.nanometer.smartlab.util.Constants;
|
import com.nanometer.smartlab.util.FacesUtils;
|
import org.apache.log4j.Logger;
|
import org.primefaces.context.RequestContext;
|
import org.primefaces.model.LazyDataModel;
|
import org.primefaces.model.SortOrder;
|
import org.springframework.context.annotation.Scope;
|
import org.springframework.stereotype.Controller;
|
|
import javax.annotation.Resource;
|
import java.util.ArrayList;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* Created by johnny on 17/8/21.
|
*/
|
@Controller
|
@Scope("session")
|
public class BaseMetaController extends BaseController {
|
|
private static Logger logger = Logger.getLogger(BaseMetaController.class);
|
|
@Resource
|
private BaseMetaService baseMetaService;
|
|
private String groupCode;
|
private String keyword;
|
private LazyDataModel<BaseMeta> dataModel;
|
private BaseMeta baseMeta=new BaseMeta();
|
private List<BaseMeta> selectedList;
|
|
private int action;
|
|
public void onNewBtnClick() {
|
this.baseMeta = new BaseMeta();
|
this.action = Constants.ACTION_ADD;
|
}
|
|
public void onEditBtnClick() {
|
if (this.selectedList == null
|
|| this.selectedList.size() == 0) {
|
FacesUtils.warn("请选择数据。");
|
return;
|
}
|
if (this.selectedList.size() > 1) {
|
FacesUtils.warn("只能选择一个数据进行修改。");
|
return;
|
}
|
this.baseMeta = this.baseMetaService.getBaseMeta(this.selectedList.get(0).getId());
|
this.action = Constants.ACTION_EDIT;
|
RequestContext.getCurrentInstance().execute("PF('dialog').show()");
|
}
|
|
public void onSaveBtnClick() {
|
try {
|
// 新建
|
if (this.action == Constants.ACTION_ADD) {
|
if (this.baseMeta == null) {
|
FacesUtils.warn("新建对象为空。");
|
return;
|
}
|
|
if (this.baseMetaService.isBaseMetaExist(this.baseMeta.getGroupCode(), this.baseMeta.getMetaKey(), null)) {
|
FacesUtils.warn("种类下存在相同的字典编码。");
|
return;
|
}
|
|
this.baseMetaService.insertBaseMeta(baseMeta);
|
|
FacesUtils.info("新建成功。");
|
RequestContext.getCurrentInstance().execute("PF('dialog').hide()");
|
// 修改
|
} else if (this.action == Constants.ACTION_EDIT) {
|
if (this.baseMeta == null) {
|
FacesUtils.warn("修改对象为空。");
|
return;
|
}
|
|
if (this.baseMetaService.isBaseMetaExist(this.baseMeta.getGroupCode(), this.baseMeta.getMetaKey(), this.baseMeta.getId())) {
|
FacesUtils.warn("种类下存在相同的字典编码。");
|
return;
|
}
|
|
this.baseMetaService.updateBaseMeta(baseMeta);
|
|
FacesUtils.info("修改成功。");
|
RequestContext.getCurrentInstance().execute("PF('dialog').hide()");
|
}
|
} catch (Exception e) {
|
logger.error("操作失败。", e);
|
FacesUtils.warn("操作失败。");
|
}
|
}
|
|
public void onDeleteBtnClick() {
|
try {
|
if (this.selectedList == null
|
|| this.selectedList.size() == 0) {
|
FacesUtils.warn("请选择数据。");
|
return;
|
}
|
List<Long> ids = new ArrayList<Long>();
|
for (BaseMeta bm : this.selectedList) {
|
ids.add(bm.getId());
|
}
|
|
this.baseMetaService.deleteBaseMeta(ids);
|
|
FacesUtils.info("删除成功。");
|
} catch (Exception e) {
|
logger.error("操作失败。", e);
|
FacesUtils.warn("操作失败。");
|
}
|
}
|
|
public LazyDataModel<BaseMeta> getDataModel() {
|
if (this.dataModel == null) {
|
this.dataModel = new LazyDataModel<BaseMeta>() {
|
@Override
|
public List<BaseMeta> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
|
List<BaseMeta> list = null;
|
try {
|
int count = baseMetaService.getBaseMetaTotalCount(groupCode, keyword);
|
this.setRowCount(count);
|
if (count > 0) {
|
list = baseMetaService.getBaseMetaList(groupCode, keyword, first, pageSize);
|
}
|
selectedList = new ArrayList<>();
|
} catch (Exception e) {
|
logger.error(e);
|
}
|
return list;
|
}
|
|
@Override
|
public BaseMeta getRowData(String rowKey) {
|
// Iterator<BaseMeta> iterator = this.iterator();
|
// if (iterator != null) {
|
// BaseMeta bm = null;
|
// while (iterator.hasNext()) {
|
// bm = iterator.next();
|
// if (rowKey.equals(bm.getId())) {
|
// return bm;
|
// }
|
// }
|
// }
|
Long id=Long.valueOf(rowKey);
|
return baseMetaService.getBaseMeta(id);
|
}
|
};
|
}
|
return dataModel;
|
}
|
|
public String getGroupCode() {
|
return groupCode;
|
}
|
|
public void setGroupCode(String groupCode) {
|
this.groupCode = groupCode;
|
}
|
|
public String getKeyword() {
|
return keyword;
|
}
|
|
public void setKeyword(String keyword) {
|
this.keyword = keyword;
|
}
|
|
public BaseMeta getBaseMeta() {
|
return baseMeta;
|
}
|
|
public void setBaseMeta(BaseMeta baseMeta) {
|
this.baseMeta = baseMeta;
|
}
|
|
public int getAction() {
|
return action;
|
}
|
|
public List<BaseMeta> getSelectedList() {
|
return selectedList;
|
}
|
|
public void setSelectedList(List<BaseMeta> selectedList) {
|
this.selectedList = selectedList;
|
}
|
}
|