//添加数据 func insert(entity Entity, model *Model, elements []element) (id int, err error) { sql := "insert into " + entity.Id + "(" valueSql := " values (" params := []interface{}{} for _, element := range elements { if element.Type != "image" { sql += element.Field + "," valueSql += "?," params = append(params, GetPropValue(model, element.Field)) } } valueSql = commonlib.Substr(valueSql, 0, len(valueSql)-1) sql = commonlib.Substr(sql, 0, len(sql)-1) sql += ")" valueSql += ")" sql += valueSql Log.Debug(sql) db := GetMySQL() defer db.Close() stmt, err := db.Prepare(sql) if err != nil { Log.Warn(err.Error()) return 0, err } res, err := stmt.Exec(params...) if err != nil { Log.Warn(err.Error()) return 0, err } id64, err := res.LastInsertId() if err != nil { Log.Warn(err.Error()) return 0, err } return int(id64), nil }
func updateCourse(tx *sql.Tx, courseDataMap map[string]interface{}, id string) error { sql := "update course set %v where cid=?" params := []interface{}{} setSql := "" for key, value := range courseDataMap { setSql += key + "=?," params = append(params, value) } params = append(params, id) setSql = commonlib.Substr(setSql, 0, len(setSql)-1) sql = fmt.Sprintf(sql, setSql) lessgo.Log.Debug(sql) stmt, err := tx.Prepare(sql) if err != nil { lessgo.Log.Error(err.Error()) return err } _, err = stmt.Exec(params...) if err != nil { lessgo.Log.Error(err.Error()) return err } return nil }
/***** * 获取上传图片的随机不重复文件名 */ func findRandomFileName(sourceFileName string) string { suffix := commonlib.Substr(sourceFileName, strings.LastIndex(sourceFileName, "."), len(sourceFileName)) r := rand.New(rand.NewSource(time.Now().UnixNano())) str := "" for i := 0; i < 4; i++ { str += fmt.Sprint(r.Intn(10)) } return fmt.Sprint(time.Now().UnixNano(), str, suffix) }
//构建查询的sql语句 func bulidSelectSql(entity Entity, colmuns []column) (string, string) { dataSql := "select " + entity.Id + "." + entity.Pk + "," if len(colmuns) > 0 { for _, column := range colmuns { if strings.Contains(column.Field, ".") { dataSql += column.Field + "," } else { dataSql += entity.Id + "." + column.Field + "," } } } else { for _, field := range entity.Fields { dataSql += entity.Id + "." + field.Name + "," } for _, ref := range entity.Refs { for _, field := range ref.Fields { if ref.Alias != "" { dataSql += ref.Alias + "." + field.Name + "," } else { dataSql += ref.Entity + "." + field.Name + "," } } } } dataSql = commonlib.Substr(dataSql, 0, len(dataSql)-1) dataSql += " from " + entity.Id + " " + entity.Id for _, ref := range entity.Refs { if ref.Alias != "" { dataSql += fmt.Sprintf(" left join %v %v on %v.%v=%v.%v", ref.Entity, ref.Alias, entity.Id, ref.Field, ref.Alias, ref.RefEntityField) } else { dataSql += fmt.Sprintf(" left join %v %v on %v.%v=%v.%v", ref.Entity, ref.Entity, entity.Id, ref.Field, ref.Entity, ref.RefEntityField) } } countSql := "select count(1) from " + entity.Id return countSql, dataSql }
func NewsImageUplaodAction(w http.ResponseWriter, r *http.Request) { m := make(map[string]interface{}) newsImgDir, _ := lessgo.Config.GetValue("wooyou", "newsImgDir") _, err := os.Stat(newsImgDir) if err != nil && os.IsNotExist(err) { lessgo.Log.Info(newsImgDir, ",创建") os.Mkdir(newsImgDir, 0777) } fn, header, err := r.FormFile("imgFile") if err != nil && os.IsNotExist(err) { m["error"] = 1 m["message"] = err.Error() lessgo.Log.Error("获取上传图片发生错误,信息如下:", err.Error()) commonlib.OutputJson(w, m, " ") return } suffix := commonlib.Substr(header.Filename, strings.LastIndex(header.Filename, ".")+1, len(header.Filename)) newFileName := findRandomFileName(header.Filename) + "." + suffix f, err := os.Create(newsImgDir + "/" + newFileName) if err != nil { m["error"] = 1 m["message"] = err.Error() lessgo.Log.Error("获取上传图片发生错误,信息如下:", err.Error()) commonlib.OutputJson(w, m, " ") return } defer f.Close() io.Copy(f, fn) m["error"] = 0 m["url"] = "/newsimg/" + newFileName commonlib.OutputJson(w, m, " ") }
//处理实体的保存页请求 func dealEntitySave(_entity Entity, w http.ResponseWriter, r *http.Request) { m := make(map[string]interface{}) employee := GetCurrentEmployee(r) if employee.UserId == "" { Log.Warn("用户未登陆") m["success"] = false m["code"] = 100 m["msg"] = "用户未登陆" commonlib.OutputJson(w, m, " ") return } Log.Debug("路径:", r.URL.Path, "访问实体", _entity.Id, "的保存ajax请求") err := r.ParseForm() //异步请求绑定的组件Id componentId := r.FormValue("componentId") formpanel := runtimeComponentContain[componentId].(formPanel) if err != nil { m["success"] = false m["code"] = 100 m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error() Log.Warn(err.Error()) commonlib.OutputJson(w, m, " ") return } idString := r.FormValue(_entity.Pk) _model := new(Model) _model.Props = []*Prop{} imgElements := []element{} for _, formElement := range formpanel.Elements { _prop := new(Prop) if formElement.Type == "image" { //图片类型需要做多表处理 imgElements = append(imgElements, formElement) } else if formElement.Type == "currentTime" { //当前时间,一般用于createTime _prop.Name = formElement.Field if formElement.Char14 == "true" { _prop.Value = time.Now().Format("20060102150405") } else { _prop.Value = time.Now().Format("2006-01-02 15:04:05") } _model.Props = append(_model.Props, _prop) } else { _prop.Name = formElement.Field _prop.Value = r.FormValue(formElement.Field) _model.Props = append(_model.Props, _prop) } } if idString != "" { //修改的情况 id, err := strconv.Atoi(idString) if err != nil { m["success"] = false m["code"] = 100 m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error() Log.Warn(err.Error()) commonlib.OutputJson(w, m, " ") return } _model.Id = fmt.Sprint(id) err = modify(_entity, _model, formpanel.Elements) if err != nil { m["success"] = false m["code"] = 100 m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error() commonlib.OutputJson(w, m, " ") return } m["success"] = true m["code"] = 200 commonlib.OutputJson(w, m, " ") return } else { id, err := insert(_entity, _model, formpanel.Elements) for _, imgElment := range imgElements { filePath := r.FormValue(imgElment.Field) tmpFileName := "" if filePath != "" { imgEntity := getEntity(imgElment.ImageEntity) imageModel := new(Model) imageModel.Props = []*Prop{} fileNameProp := new(Prop) fileNameProp.Name = "filename" fileNameProp.Value = commonlib.SubstrByStEd(filePath, strings.LastIndex(filePath, "/")+1, strings.LastIndex(filePath, ".")) tmpFileName = commonlib.Substr(filePath, strings.LastIndex(filePath, "/")+1, len(filePath)) suffixProp := new(Prop) suffixProp.Name = "suffix" suffixProp.Value = commonlib.Substr(filePath, strings.LastIndex(filePath, ".")+1, len(filePath)) refProp := new(Prop) refProp.Name = imgElment.Field refProp.Value = fmt.Sprint(id) imageModel.Props = append(imageModel.Props, fileNameProp) imageModel.Props = append(imageModel.Props, suffixProp) imageModel.Props = append(imageModel.Props, refProp) _, err := insert(imgEntity, imageModel, []element{ element{ Field: "filename", }, element{ Field: "suffix", }, element{ Field: imgElment.Field, }, }) if err != nil { m["success"] = false m["code"] = 100 m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error() Log.Warn(err.Error()) commonlib.OutputJson(w, m, " ") return } tmpFile, err := os.OpenFile("../tmp/"+tmpFileName, os.O_RDWR, 0777) if err != nil { m["success"] = false m["code"] = 100 m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error() Log.Warn(err.Error()) commonlib.OutputJson(w, m, " ") return } _, err = os.Stat(imgElment.ImagePath) if err != nil && os.IsNotExist(err) { Log.Info(imgElment.ImagePath, "文件夹不存在,创建") os.MkdirAll(imgElment.ImagePath, 0777) } disFile, err := os.Create(imgElment.ImagePath + "/" + tmpFileName) if err != nil { m["success"] = false m["code"] = 100 m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error() Log.Warn(err.Error()) commonlib.OutputJson(w, m, " ") return } io.Copy(disFile, tmpFile) os.Remove("../tmp/" + tmpFileName) } } if err != nil { m["success"] = false m["code"] = 100 m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error() commonlib.OutputJson(w, m, " ") return } m["success"] = true m["code"] = 200 commonlib.OutputJson(w, m, " ") return } }
func NewsFormAction(w http.ResponseWriter, r *http.Request, actionType string) { m := make(map[string]interface{}) err := r.ParseForm() if err != nil { lessgo.Log.Error(err.Error()) return } news := new(News) news.Id, _ = strconv.Atoi(r.FormValue("id")) news.Title = r.FormValue("title") news.NewsContent = r.FormValue("news_content") previewImage := r.FormValue("preview_image") news.PreviewImage = commonlib.Substr(previewImage, strings.LastIndex(previewImage, "/")+1, len(previewImage)) news.PreviewText = r.FormValue("preview_text") news.IsCarousel = r.FormValue("is_carousel") carouselImage := r.FormValue("carousel_image") news.CarouselImage = commonlib.Substr(carouselImage, strings.LastIndex(carouselImage, "/")+1, len(carouselImage)) msg := "" switch actionType { case FORM_ACTION_TYPE.CREATE.Key: msg, err = NewsSave(news) case FORM_ACTION_TYPE.UPDATE.Key: msg, err = NewsUpdate(news) } newsImgDir, _ := lessgo.Config.GetValue("wooyou", "newsImgDir") // 图片文件处理 if news.PreviewImage != "" { tmpFile, err := os.OpenFile("../tmp/"+news.PreviewImage, os.O_RDWR, 0777) if err != nil { m["success"] = false m["code"] = 100 m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error() lessgo.Log.Error(err.Error()) commonlib.OutputJson(w, m, " ") return } _, err = os.Stat(newsImgDir) if err != nil && os.IsNotExist(err) { lessgo.Log.Error(newsImgDir, "文件夹不存在,创建") os.MkdirAll(newsImgDir, 0777) } disFile, err := os.Create(newsImgDir + "/" + news.PreviewImage) if err != nil { m["success"] = false m["code"] = 100 m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error() lessgo.Log.Error(err.Error()) commonlib.OutputJson(w, m, " ") return } io.Copy(disFile, tmpFile) os.Remove("../tmp/" + news.PreviewImage) } // 图片文件处理 if news.CarouselImage != "" { tmpFile, err := os.OpenFile("../tmp/"+news.CarouselImage, os.O_RDWR, 0777) if err != nil { m["success"] = false m["code"] = 100 m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error() lessgo.Log.Error(err.Error()) commonlib.OutputJson(w, m, " ") return } _, err = os.Stat(newsImgDir) if err != nil && os.IsNotExist(err) { lessgo.Log.Error(newsImgDir, "文件夹不存在,创建") os.MkdirAll(newsImgDir, 0777) } disFile, err := os.Create(newsImgDir + "/" + news.CarouselImage) if err != nil { m["success"] = false m["code"] = 100 m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error() lessgo.Log.Error(err.Error()) commonlib.OutputJson(w, m, " ") return } io.Copy(disFile, tmpFile) os.Remove("../tmp/" + news.CarouselImage) } if err != nil { lessgo.Log.Error(err.Error()) m["sucess"] = false m["msg"] = msg } else { m["success"] = true m["datas"] = news } commonlib.OutputJson(w, m, "") return }
func GetImageFileSuffix(imageName string) string { return commonlib.Substr(imageName, strings.LastIndex(imageName, ".")+1, len(imageName)) }
func (log *MyLogger) Warn(arg0 ...interface{}) { _, file, line, _ := runtime.Caller(1) file = commonlib.Substr(file, strings.LastIndex(file, "/")+1, 100) tmplog.Warn(fmt.Sprint("[文件:", file, ",行:", line, "] ", fmt.Sprint(arg0...))) }
//处理实体的保存页请求 func dealEntitySave(entity Entity, w http.ResponseWriter, r *http.Request) { Log.Debug("路径:", r.URL.Path, "访问实体", entity.Id, "的保存ajax请求") err := r.ParseForm() //异步请求绑定的组件Id componentId := r.FormValue("componentId") formpanel := runtimeComponentContain[componentId].(FormPanel) m := make(map[string]interface{}) if err != nil { m["success"] = false m["code"] = 100 m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error() Log.Warn(err.Error()) commonlib.OutputJson(w, m, " ") return } idString := r.FormValue(entity.Pk) model := new(Model) model.Props = []*Prop{} for _, element := range formpanel.Elements { prop := new(Prop) if element.Type == "image" { //图片类型需要做多表处理 filePath := r.FormValue(element.Field) tmpFileName := "" if filePath != "" { imgEntity := GetEntity(element.ImageEntity) imageModel := new(Model) imageModel.Props = []*Prop{} fileNameProp := new(Prop) fileNameProp.Name = "filename" fileNameProp.Value = commonlib.SubstrByStEd(filePath, strings.LastIndex(filePath, "/")+1, strings.LastIndex(filePath, ".")) tmpFileName = commonlib.Substr(filePath, strings.LastIndex(filePath, "/")+1, len(filePath)) suffixProp := new(Prop) suffixProp.Name = "suffix" suffixProp.Value = commonlib.Substr(filePath, strings.LastIndex(filePath, ".")+1, len(filePath)) imageModel.Props = append(imageModel.Props, fileNameProp) imageModel.Props = append(imageModel.Props, suffixProp) imgId, err := Insert(imgEntity, imageModel, []Element{ Element{ Field: "filename", }, Element{ Field: "suffix", }, }) if err != nil { m["success"] = false m["code"] = 100 m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error() Log.Warn(err.Error()) commonlib.OutputJson(w, m, " ") return } prop.Name = element.Field prop.Value = fmt.Sprint(imgId) model.Props = append(model.Props, prop) tmpFile, err := os.OpenFile("../tmp/"+tmpFileName, os.O_RDWR, 0777) if err != nil { m["success"] = false m["code"] = 100 m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error() Log.Warn(err.Error()) commonlib.OutputJson(w, m, " ") return } _, err = os.Stat(element.ImagePath) if err != nil && os.IsNotExist(err) { Log.Info(element.ImagePath, "文件夹不存在,创建") os.Mkdir(element.ImagePath, 0777) } disFile, err := os.Create(element.ImagePath + "/" + tmpFileName) if err != nil { m["success"] = false m["code"] = 100 m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error() Log.Warn(err.Error()) commonlib.OutputJson(w, m, " ") return } io.Copy(disFile, tmpFile) os.Remove("../tmp/" + tmpFileName) } else { prop.Name = element.Field prop.Value = fmt.Sprint(0) model.Props = append(model.Props, prop) } } else { prop.Name = element.Field prop.Value = r.FormValue(element.Field) model.Props = append(model.Props, prop) } } if idString != "" { //修改的情况 id, err := strconv.Atoi(idString) if err != nil { m["success"] = false m["code"] = 100 m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error() Log.Warn(err.Error()) commonlib.OutputJson(w, m, " ") return } model.Id = id err = Modify(entity, model, formpanel.Elements) if err != nil { m["success"] = false m["code"] = 100 m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error() commonlib.OutputJson(w, m, " ") return } m["success"] = true m["code"] = 200 commonlib.OutputJson(w, m, " ") return } else { _, err = Insert(entity, model, formpanel.Elements) if err != nil { m["success"] = false m["code"] = 100 m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error() commonlib.OutputJson(w, m, " ") return } m["success"] = true m["code"] = 200 commonlib.OutputJson(w, m, " ") return } }
func ContractOfChildAction(w http.ResponseWriter, r *http.Request) { m := make(map[string]interface{}) err := r.ParseForm() if err != nil { m["success"] = false m["code"] = 100 m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error() commonlib.OutputJson(w, m, " ") return } childId := r.FormValue("childId") sql := "select contr.id,contr.contract_no,cour.name,contr.apply_time from contract contr left join course cour on cour.cid=contr.course_id where contr.child_id=? " lessgo.Log.Debug(sql) db := lessgo.GetMySQL() defer db.Close() rows, err := db.Query(sql, childId) if err != nil { lessgo.Log.Warn(err.Error()) m["success"] = false m["code"] = 100 m["msg"] = "系统发生错误,请联系IT部门" commonlib.OutputJson(w, m, " ") return } type Result struct { Value string `json:"value"` Desc string `json:"desc"` } objects := []*Result{} for rows.Next() { model := new(Result) var contractNo, courseName, applyTime string err = commonlib.PutRecord(rows, &model.Value, &contractNo, &courseName, &applyTime) model.Desc = contractNo + "-" + courseName + "-" + commonlib.Substr(applyTime, 0, 8) if err != nil { lessgo.Log.Warn(err.Error()) m["success"] = false m["code"] = 100 m["msg"] = "系统发生错误,请联系IT部门" commonlib.OutputJson(w, m, " ") return } objects = append(objects, model) } m["success"] = true m["datas"] = objects commonlib.OutputJson(w, m, " ") return }