Commit 0c69d658 authored by 任国军's avatar 任国军

add tag & logo

parent e3708872
Pipeline #16461 passed with stage
in 8 seconds
'use strict';
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, DATE, ENUM } = app.Sequelize;
const CourseInstitutionToTag = app.classModel.define('course_institution_to_tag', {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true,
},
institution_id: INTEGER,
tag_id: INTEGER,
sort: INTEGER,
status: ENUM('offline', 'online'),
is_deleted: INTEGER,
created_time: {
type: DATE,
allowNull: true,
get() {
const date = this.getDataValue('created_time');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
updated_time: {
type: DATE,
allowNull: true,
get() {
const date = this.getDataValue('updated_time');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
}, {
timestamps: false,
tableName: 'course_institution_to_tag',
});
return CourseInstitutionToTag;
};
'use strict';
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, DATE, ENUM } = app.Sequelize;
const CourseTag = app.classModel.define('course_tag', {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: STRING,
sort: INTEGER,
status: ENUM('offline', 'online'),
is_deleted: INTEGER,
created_time: {
type: DATE,
allowNull: true,
get() {
const date = this.getDataValue('created_time');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
updated_time: {
type: DATE,
allowNull: true,
get() {
const date = this.getDataValue('updated_time');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
}, {
timestamps: false,
tableName: 'course_tag',
});
return CourseTag;
};
'use strict'; 'use strict';
'use strict';
const moment = require('moment'); const moment = require('moment');
module.exports = app => { module.exports = app => {
const { STRING, INTEGER, DATE, DECIMAL, TEXT, ENUM } = app.Sequelize; const { STRING, INTEGER, DATE, DECIMAL, TEXT, ENUM } = app.Sequelize;
const CourseArea = app.classModel.define('course_area'); const CourseArea = app.classModel.define('course_area');
const CourseInstitution = app.classModel.define('course_institution', { const CourseInstitution = app.classModel.define('course_institution', {
id: { id: {
type: INTEGER, type: INTEGER,
primaryKey: true, primaryKey: true,
autoIncrement: true autoIncrement: true,
}, },
name: STRING, name: STRING,
type: STRING, type: STRING,
// image: STRING, // image: STRING,
establishment_time: STRING, establishment_time: STRING,
class_type: STRING, class_type: STRING,
teacher_count: INTEGER, teacher_count: INTEGER,
teacher_experience: INTEGER, teacher_experience: INTEGER,
corner: STRING, logo: STRING,
min_age: INTEGER, corner: STRING,
max_age: INTEGER, min_age: INTEGER,
price: STRING, max_age: INTEGER,
characteristic: STRING, price: STRING,
description: TEXT, characteristic: STRING,
honor: STRING, description: TEXT,
point: STRING, honor: STRING,
status: ENUM('offline', 'online'), point: STRING,
is_deleted: INTEGER, status: ENUM('offline', 'online'),
created_time: { is_deleted: INTEGER,
type: DATE, created_time: {
allowNull: true, type: DATE,
get() { allowNull: true,
const date = this.getDataValue('created_time'); get() {
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined; const date = this.getDataValue('created_time');
}, return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
} },
}, { },
timestamps: false, }, {
tableName: 'course_institution', timestamps: false,
}); tableName: 'course_institution',
});
CourseInstitution.hasMany(CourseArea, { CourseInstitution.hasMany(CourseArea, {
foreignKey: { foreignKey: {
name: 'institution_id', name: 'institution_id',
allowNull: false allowNull: false,
} },
}); });
CourseInstitution.one = async (data) => { CourseInstitution.one = async data => {
const attributes = data.attributes ? data.attributes : {}; const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {}; const where = data.where ? data.where : {};
return await CourseInstitution.findOne({ return await CourseInstitution.findOne({
attributes: attributes, attributes,
where: where, where,
}); });
} };
CourseInstitution.all = async (data) => { CourseInstitution.all = async data => {
const attributes = data.attributes ? data.attributes : {}; const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {}; const where = data.where ? data.where : {};
const order = data.order ? data.order : []; const order = data.order ? data.order : [];
return await CourseInstitution.findAll({ return await CourseInstitution.findAll({
attributes: attributes, attributes,
where: where, where,
order, order,
}); });
} };
CourseInstitution.list = async (data = {}) => { CourseInstitution.list = async (data = {}) => {
const limit = data.limit ? Number(data.limit) : 10; const limit = data.limit ? Number(data.limit) : 10;
const page = data.page ? data.page : 1; const page = data.page ? data.page : 1;
const order = data.order ? data.order : []; const order = data.order ? data.order : [];
const attributes = data.attributes ? data.attributes : {}; const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {}; const where = data.where ? data.where : {};
const condition = { const condition = {
offset: (page - 1) * limit, offset: (page - 1) * limit,
limit, limit,
where: where, where,
order: order, order,
attributes: attributes, attributes,
}; };
const { count, rows } = await CourseInstitution.findAndCountAll(condition); const { count, rows } = await CourseInstitution.findAndCountAll(condition);
return { page, count, rows }; return { page, count, rows };
} };
CourseInstitution.add = async (data) => { CourseInstitution.add = async data => {
try { try {
//返回promise对象实力 instance // 返回promise对象实力 instance
const res = await CourseInstitution.create(data); const res = await CourseInstitution.create(data);
//从promise 实例中中获得需要的id号,id 必须是自增长,而且必须主键,否则返回null // 从promise 实例中中获得需要的id号,id 必须是自增长,而且必须主键,否则返回null
return res.id; return res.id;
} catch (error) { } catch (error) {
throw (error); throw (error);
}
} }
};
CourseInstitution.edit = async (data) => { CourseInstitution.edit = async data => {
const where = data.where; const where = data.where;
const params = data.params; const params = data.params;
try { try {
const res = await CourseInstitution.update(params, { where: where }) const res = await CourseInstitution.update(params, { where });
return res; return res;
} catch (error) { } catch (error) {
throw (error); throw (error);
}
} }
};
return CourseInstitution; return CourseInstitution;
}; };
\ No newline at end of file
...@@ -127,8 +127,22 @@ class InstitutionSubService extends Service { ...@@ -127,8 +127,22 @@ class InstitutionSubService extends Service {
// 是否已收藏 // 是否已收藏
const userCollect = await ctx.classModel.CourseUserCollection.findOne({ where: { institution_id, user_uuid: userUuid, is_deleted: 0 } }); const userCollect = await ctx.classModel.CourseUserCollection.findOne({ where: { institution_id, user_uuid: userUuid, is_deleted: 0 } });
// 获取所有标签
const allTags = await ctx.classModel.CourseTag.findAll({ where: { status: 'online', is_deleted: 0 } });
const tagList = [];
for (const v of allTags) {
tagList[v.id] = v;
}
let tags = await ctx.classModel.CourseInstitutionToTag.findAll({ where: { institution_id: institution.id, status: 'online', is_deleted: 0 } });
tags = _.orderBy(tags, [ 'sort' ], [ 'asc' ]);
institution.tags = [];
for (const v of tags) {
institution.tags.push(tagList[v.tag_id]);
}
institution.phone = areas.length > 0 ? areas[0].phone : ''; institution.phone = areas.length > 0 ? areas[0].phone : '';
institution.distance = areas.length > 0 ? areas[0].distance : ''; institution.distance = areas.length > 0 ? String(areas[0].distance) + 'km' : '无法计算';
institution.travel_method = areas.length > 0 ? areas[0].travel_method : ''; institution.travel_method = areas.length > 0 ? areas[0].travel_method : '';
institution.travel_tips = areas.length > 0 ? areas[0].travel_tips : ''; institution.travel_tips = areas.length > 0 ? areas[0].travel_tips : '';
...@@ -284,6 +298,13 @@ class InstitutionSubService extends Service { ...@@ -284,6 +298,13 @@ class InstitutionSubService extends Service {
} }
institutionCats = await ctx.classModel.CourseInstitutionToCat.findAll({ where: { status: 'online', is_deleted: 0, institution_id: { $in: institutionIds } } }); institutionCats = await ctx.classModel.CourseInstitutionToCat.findAll({ where: { status: 'online', is_deleted: 0, institution_id: { $in: institutionIds } } });
// 获取所有标签
const allTags = await ctx.classModel.CourseTag.findAll({ where: { status: 'online', is_deleted: 0 } });
const tagList = [];
for (const v of allTags) {
tagList[v.id] = v;
}
// 用户已收藏机构列表 // 用户已收藏机构列表
const userInstitutions = await ctx.classModel.CourseUserCollection.findAll({ where: { is_deleted: 0, user_uuid: userUuid } }); const userInstitutions = await ctx.classModel.CourseUserCollection.findAll({ where: { is_deleted: 0, user_uuid: userUuid } });
const userInstitutionIds = R.pluck('id', userInstitutions); const userInstitutionIds = R.pluck('id', userInstitutions);
...@@ -308,6 +329,14 @@ class InstitutionSubService extends Service { ...@@ -308,6 +329,14 @@ class InstitutionSubService extends Service {
institutionList[i].cats.push(catList[v]); institutionList[i].cats.push(catList[v]);
} }
// 标签
let tags = await ctx.classModel.CourseInstitutionToTag.findAll({ where: { institution_id: institutionList[i].id, status: 'online', is_deleted: 0 } });
tags = _.orderBy(tags, [ 'sort' ], [ 'asc' ]);
institutionList[i].tags = [];
for (const v of tags) {
institutionList[i].tags.push(tagList[v.tag_id]);
}
// 优先获取机构详情图 // 优先获取机构详情图
let institutionImages = await ctx.classModel.CourseImages.findAll({ where: { type: 1, type_id: institutionList[i].id, status: 'online', is_deleted: 0 }, order: [[ 'sort', 'asc' ]], limit: 3 }); let institutionImages = await ctx.classModel.CourseImages.findAll({ where: { type: 1, type_id: institutionList[i].id, status: 'online', is_deleted: 0 }, order: [[ 'sort', 'asc' ]], limit: 3 });
if (institutionImages.length < 3) { if (institutionImages.length < 3) {
...@@ -316,7 +345,7 @@ class InstitutionSubService extends Service { ...@@ -316,7 +345,7 @@ class InstitutionSubService extends Service {
} }
institutionList[i].images = institutionImages; institutionList[i].images = institutionImages;
institutionList[i].distance = (institutionList[i].distance / 1000).toFixed(1); institutionList[i].distance = String((institutionList[i].distance / 1000).toFixed(1)) + 'km';
institutionList[i].phone = institutionList[i].area.length > 0 ? institutionList[i].area[0].phone : ''; institutionList[i].phone = institutionList[i].area.length > 0 ? institutionList[i].area[0].phone : '';
// 是否已收藏 // 是否已收藏
...@@ -487,20 +516,20 @@ class InstitutionSubService extends Service { ...@@ -487,20 +516,20 @@ class InstitutionSubService extends Service {
} }
//搜索关联 // 搜索关联
async getSuggestSearch(input) { async getSuggestSearch(input) {
const {ctx} = this; const { ctx } = this;
const {search} = input; const { search } = input;
let results = []; let results = [];
if (!ctx.isEmpty(search)) { if (!ctx.isEmpty(search)) {
const institutions = await ctx.classModel.CourseInstitution.findAll({ where: { status: 'online', is_deleted: 0, name: { $like: '%' + search + '%' } }, limit: 10, attributes: [ 'id', 'name', 'status', 'is_deleted' ]}) const institutions = await ctx.classModel.CourseInstitution.findAll({ where: { status: 'online', is_deleted: 0, name: { $like: '%' + search + '%' } }, limit: 10, attributes: [ 'id', 'name', 'status', 'is_deleted' ] });
results = R.pluck('name', institutions); results = R.pluck('name', institutions);
} }
const ret = { const ret = {
results, results,
count: results.length, count: results.length,
} };
return ret; return ret;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment