package com.skymobi.qc.admin.action;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import au.com.bytecode.opencsv.CSVReader;
import com.skymobi.qc.admin.dal.ProjectMapper;
import com.skymobi.qc.admin.dal.domain.CustomFields;
import com.skymobi.qc.admin.dal.domain.Project;
import com.skymobi.qc.admin.framework.base.BaseBO;
import com.skymobi.qc.admin.framework.util.Main;
import com.skymobi.qc.admin.framework.util.MainMenu;
import com.skymobi.qc.admin.framework.util.ParaMap;
public class ProjectBO extends BaseBO {
@Autowired
private FieldsBO fieldsBO;
@Autowired
private ProjectMapper projectMapper;
public String projectAdd(Project project) {
String msg = "";
// 判断name是否重复
if (projectMapper.isHasProjectName(project) > 0) {
// 返回错误信息
msg += "项目名已经存在<br/>";
}
// 保存
if (project.getId() > 0) {
projectMapper.projectUpdate(project);
} else {
//判断tag是否正确
Pattern pattern = Pattern.compile("[0-9a-z-]*");
Matcher matcher = pattern.matcher(project.getTag());
if( matcher.matches() ){
// 判断KEY是否重复
if (projectMapper.isHasProjectTag(project.getTag()) > 0) {
msg += "标识已经存在<br/>";
}
project.setTable_name("tbl_project_" + project.getTag().replace("-", "_"));
}else{
msg += "标识格式有误<br/>";
}
if (project.getTag().length()<3 || project.getTag().length()>20){
msg += "标识长度有误<br/>";
}
if (msg.equals("")){
projectMapper.projectAdd(project);
}
}
return msg;
}
public String projectDestroy(String tag){
//没有数据 、没有子项目才能被删除
Project project = getProjectByTag(tag);
ParaMap<String, Object> pm = new ParaMap<String, Object>();
pm.put("table_name", project.getTable_name());
int c = projectMapper.itemCount(pm);
//子项目
int child = projectMapper.getProjectChild(project.getId());
String msg = "";
if (c == 0 && child == 0){
projectMapper.projectDestroy(project);
}else{
if(c>0){
msg += "该项目还有"+c+"条数据,不能删除。<br/>";
}
if(child > 0){
msg += "该项目还有"+child+"个子项目,不能删除。";
}
}
return msg;
}
public Project getProjectByAttr(String name, Object value) {
HashMap<String, Object> m = new HashMap<String, Object>();
m.put("name", name);
m.put("value", value);
return projectMapper.getProjectByAttr(m);
}
public Project getProjectByTag(String tag) {
return projectMapper.getProjectByTag(tag);
}
public List<Project> getProjectList() {
List<Project> projectList = projectMapper.getProjectList();
List<Project> projectListNew = new ArrayList<Project>();
getProjectListSort(projectList, projectListNew, 0, 0);
return projectListNew;
}
public List<Project> getProjectList(Project project) {
List<Project> projectList = projectMapper.getProjectList();
List<Project> projectListNew = new ArrayList<Project>();
getProjectListSort(projectList, projectListNew, project.getId(),0, 0);
return projectListNew;
}
private void getProjectListSort(List<Project> projectList,
List<Project> projectListNew, int parent_id, int level) {
for (Project p : projectList) {
if (p.getParent_id() == parent_id) {
p.setParent_level(level);
projectListNew.add(p);
getProjectListSort(projectList, projectListNew, p.getId(),
level + 1);
}
}
}
private void getProjectListSort(List<Project> projectList,
List<Project> projectListNew,int projectId, int parent_id, int level) {
for (Project p : projectList) {
if (p.getParent_id() == parent_id && p.getId()!=projectId) {
p.setParent_level(level);
projectListNew.add(p);
getProjectListSort(projectList, projectListNew, projectId,p.getId(),
level + 1);
}
}
}
public void setMainMenu(Main main, String tag) {
main.getMainMenu().add(new MainMenu("/project/" + tag + "", "概要"));
main.getMainMenu()
.add(new MainMenu("/project/" + tag + "/items", "问题"));
main.getMainMenu().add(
new MainMenu("/project/" + tag + "/items/new", "新建问题"));
main.getMainMenu().add(
new MainMenu("/project/" + tag + "/settings", "配置"));
}
public List<HashMap<String, Object>> itemList(ParaMap<String, Object> m) {
return projectMapper.itemList(m);
}
public void itemAdd(Project project, ParaMap<String, Object> m) {
List<CustomFields> customFieldList = fieldsBO.customFieldList(project
.getId());
m.put("customFieldList", customFieldList);
if (m.getInt("id") == 0) {
projectMapper.itemAdd(m);
} else {
projectMapper.itemUpdate(m);
}
}
public HashMap<String,Object> getItem(Project project,int id){
HashMap<String,Object> h = new HashMap<String,Object>();
h.put("table_name", project.getTable_name());
h.put("id", id);
return projectMapper.getItem(h);
}
public void itemsImportCsv(CommonsMultipartFile mFile,Project project){
List<CustomFields> cfList = fieldsBO.customFieldList(project.getId());
HashMap<String,Integer> cfMap = new HashMap<String,Integer>();
for(CustomFields c:cfList){
cfMap.put(c.getField_name(), c.getId());
}
try {
CSVReader reader = new CSVReader(new InputStreamReader(mFile.getInputStream(),"GBK"));
// List<String[]> all = reader.readAll();
String[] title = reader.readNext();
String[] nextLine;
ParaMap<String, Object> m = new ParaMap<String, Object>();
m.put("customFieldList", cfList);
m.put("table_name", project.getTable_name());
while ((nextLine = reader.readNext()) != null) {
for(int i=0;i<nextLine.length;i++){
if (nextLine[i] != null)
m.put("field"+cfMap.get(title[i]), nextLine[i]);
}
projectMapper.itemAdd(m);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}