Beispiel #1
0
func dbSearchEnquires(t string, p int) ([]Enquire, int) {
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	offset := enquirePageLimit * (p - 1)
	querySql := fmt.Sprintf("select id, status, customer_id, customer_name, subject, message, followup, create_time, last_modify_time from enquires where subject like '%%%s%%' or customer_name like '%%%s%%' order by id desc limit %d offset %d", t, t, enquirePageLimit, offset)

	stmt, err := dbHandler.Prepare(querySql)
	if err != nil {
		log.Debug("querySql: %s", querySql)
		log.Error("Prepare failed : %v", err)
		return nil, http.StatusInternalServerError
	}

	defer stmt.Close()
	rows, err := stmt.Query()
	if err != nil {
		log.Fatal("Query products failed, something changed on db schema? : %v ", err)
		return nil, http.StatusNotFound
	}
	defer rows.Close()

	es := make([]Enquire, 0)
	for rows.Next() {
		var enquire_id, status, customer_id sql.NullInt64
		var customer_name, subject, message, followup sql.NullString
		var create_time, last_modify_time time.Time
		rows.Scan(&enquire_id, &status, &customer_id, &customer_name, &subject, &message, &followup, &create_time, &last_modify_time)
		es = append(es, Enquire{auth.Encode(strconv.FormatInt(enquire_id.Int64, 10)), status.Int64, customer_id.Int64, customer_name.String, subject.String, message.String, 1, followup.String, nil, create_time.Format(timeLayout), last_modify_time.Format(timeLayout)})
	}
	return es, http.StatusOK
}
Beispiel #2
0
func addEnquire(req *restful.Request, resp *restful.Response) {
	e := new(Enquire)
	err := req.ReadEntity(&e)
	if err == nil {
		e.CustomerId, err = strconv.ParseInt(req.Attribute("agsuserid").(string), 10, 64)
		ret := dbAddEnquire(e)
		if ret == http.StatusOK {
			resp.WriteHeader(http.StatusCreated)
			e.Id = auth.Encode(e.Id)
			resp.WriteEntity(e)
		} else {
			resp.WriteErrorString(ret, http.StatusText(ret))
		}
	} else {
		resp.WriteError(http.StatusInternalServerError, err)
	}
}
Beispiel #3
0
func followupEnquire(req *restful.Request, resp *restful.Response) {
	id := req.PathParameter("EnquireId")

	e := new(Enquire)
	err := req.ReadEntity(&e)
	if err == nil {
		e.EmployeeId, err = strconv.ParseInt(req.Attribute("agsemployeeid").(string), 10, 64)
		e.Id = auth.Decode(id)
		ret := dbFollowupEnquire(e)
		if ret == http.StatusOK {
			resp.WriteHeader(http.StatusCreated)
			e.Id = auth.Encode(e.Id)
			resp.WriteEntity(e)
		} else {
			resp.WriteErrorString(ret, http.StatusText(ret))
		}
	} else {
		resp.WriteError(http.StatusInternalServerError, err)
	}
}
Beispiel #4
0
func dbFindEnquiresByCond(cond string) ([]Enquire, int) {
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	sqlCond, limit, offset := buildSqlCond(cond)

	log.Debug("get enquires for %s, %d, %d", sqlCond, limit, offset)
	querySql := fmt.Sprintf("SELECT id, status, customer_id, customer_name, subject, message, employee_id, followup, create_time, last_modify_time FROM enquires WHERE %s ORDER BY id DESC LIMIT %d OFFSET %d ", sqlCond, limit, offset)

	stmt, err := dbHandler.Prepare(querySql)
	if err != nil {
		log.Debug("querySql: %s", querySql)
		log.Error("Prepare failed : %v", err)
		return nil, http.StatusInternalServerError
	}
	defer stmt.Close()
	rows, err := stmt.Query()
	if err != nil {
		log.Fatal("Query enquire failed, something changed on db schema? : %v ", err)
		return nil, http.StatusNotFound
	}
	defer rows.Close()

	es := make([]Enquire, 0)
	for rows.Next() {
		var enquire_id, status, customer_id, employee_id sql.NullInt64
		var customer_name, subject, message, followup sql.NullString
		var create_time, last_modify_time time.Time
		rows.Scan(&enquire_id, &status, &customer_id, &customer_name, &subject, &message, &employee_id, &followup, &create_time, &last_modify_time)
		es = append(es, Enquire{auth.Encode(strconv.FormatInt(enquire_id.Int64, 10)), status.Int64, customer_id.Int64, customer_name.String, subject.String, message.String, employee_id.Int64, followup.String, nil, create_time.Format(timeLayout), last_modify_time.Format(timeLayout)})
	}
	return es, http.StatusOK
}