package autoreply import ( "strconv" "time" "wechat-conf/models" "wechat-conf/models/model" "wechat-conf/utils" "github.com/go-xorm/xorm" ) // AutoreplyDAO 当前数据库操作对象 type AutoreplyDAO struct { ctx *utils.Context db *xorm.Session } // NewAutoreplyDAO New Inst func NewAutoreplyDAO(ctx *utils.Context) *AutoreplyDAO { return &AutoreplyDAO{ ctx: ctx, db: ctx.DB, } } func (m *AutoreplyDAO) GetAutoReplyList(orgId, autoType string, page, pagesize int) ([]model.TaAutoReply, error) { var autoreply []model.TaAutoReply sql := `SELECT * FROM ta_auto_reply WHERE org_id = '` + orgId + `' AND auto_type = '` + autoType + `' and status >` + strconv.Itoa(models.STATUS_DEL) sql += ` order by create_date desc limit ` + strconv.Itoa((page-1)*pagesize) + `, ` + strconv.Itoa(pagesize) err := m.db.Sql(sql).Find(&autoreply) return autoreply, err } func (m *AutoreplyDAO) GetAutoReplyListCount(orgId, autoType string) (int, error) { var autoreply []model.TaAutoReply sql := `SELECT * FROM ta_auto_reply WHERE org_id = '` + orgId + `' AND auto_type = '` + autoType + `'` err := m.db.Sql(sql).Find(&autoreply) return len(autoreply), err } func (m *AutoreplyDAO) AddAutoReply(auto model.TaAutoReply) (*model.TaAutoReply, error) { auto.CreateDate = time.Now() auto.Status = models.STATUS_NORMAL auto.AutoReplyId = utils.GetGUID() _, err := m.db.Insert(auto) return &auto, err } func (m *AutoreplyDAO) AddKeyword(keyword model.TaAutoReplyKeywords) error { keyword.KeywordsId = utils.GetGUID() keyword.Status = models.STATUS_NORMAL _, err := m.db.Insert(keyword) return err } func (m *AutoreplyDAO) UpdateAutoRelpy(auto model.TaAutoReply) error { var cols = []string{ "auto_type", "rule_name", "pair_type", "message_type", "message_paragraph", "message_img", "message_content", "keywords", } _, err := m.db.Cols(cols...).Where("auto_reply_id = ?", auto.AutoReplyId).Update(auto) return err } func (m *AutoreplyDAO) DeleteKeywords(autoReplyId string) error { _, err := m.db.Delete(&model.TaAutoReplyKeywords{AutoReplyId: autoReplyId}) return err } func (m *AutoreplyDAO) DeleteAutoReply(autoReplyId string) error { var auto = model.TaAutoReply{ AutoReplyId: autoReplyId, Status: models.STATUS_DEL, } var cols = []string{ "status", } _, err := m.db.Cols(cols...).Where("auto_reply_id = ?", auto.AutoReplyId).Update(auto) return err } func (m *AutoreplyDAO) GetAutoReplyById(autoReplyId string) (*model.TaAutoReply, error) { var autoReply []model.TaAutoReply sql := `SELECT * FROM ta_auto_reply WHERE auto_reply_id = '` + autoReplyId + `'` err := m.db.Sql(sql).Find(&autoReply) if len(autoReply) > 0 { return &autoReply[0], err } return nil, err } // GetAutoReplayByAppID 获取微信对应的自动回复信息 func (m *AutoreplyDAO) GetAutoReplayByAppID(appid, val string) (*model.TaAutoReply, error) { var reply []model.TaAutoReply // 先查询精确查询 sql := `SELECT a.* FROM ta_auto_reply a INNER JOIN sys_org b ON a.org_id = b.org_id INNER JOIN sys_wechat_conf c ON b.wechat_id = c.conf_id INNER JOIN ta_auto_reply_keywords d ON a.auto_reply_id = d.auto_reply_id WHERE c.appid = ? AND a.is_use = ? AND a.status > ? AND b.status > ? AND a.auto_type = ? AND a.pair_type = ? AND d.keywords = ? order by a.create_date desc` err := m.db.Sql(sql, appid, models.AUTOREPLY_IS_USE_ON, models.STATUS_DEL, models.STATUS_DEL, models.AUTOREPLY_KEYWORDS, models.PAIR_TYPE_ACU, val).Find(&reply) if err != nil { return nil, err } if len(reply) > 0 { return &reply[0], nil } // 模糊查询 sql = `SELECT DISTINCT a.* FROM ta_auto_reply a INNER JOIN sys_org b ON a.org_id = b.org_id INNER JOIN sys_wechat_conf c ON b.wechat_id = c.conf_id INNER JOIN ta_auto_reply_keywords d ON a.auto_reply_id = d.auto_reply_id WHERE c.appid = ? AND a.is_use = ? AND a.status > ? AND b.status > ? AND a.auto_type = ? AND a.pair_type = ? AND d.keywords like CONCAT('%',?, '%') order by a.create_date desc` err = m.db.Sql(sql, appid, models.AUTOREPLY_IS_USE_ON, models.STATUS_DEL, models.STATUS_DEL, models.AUTOREPLY_KEYWORDS, models.PAIR_TYPE_BLUR, val).Find(&reply) if err != nil { return nil, err } if len(reply) > 0 { return &reply[0], nil } return nil, nil } // GetSubscribeByAppID 获取微信关注自动回复 func (m *AutoreplyDAO) GetSubscribeByAppID(appid string) (*model.TaAutoReply, error) { var replys []model.TaAutoReply sql := `select a.* from ta_auto_reply a inner join sys_org b on a.org_id=b.org_id inner join sys_wechat_conf c on b.wechat_id = c.conf_id where c.appid=? and a.is_use=? and a.auto_type=? and a.status>? and b.status>? order by a.create_date desc` err := m.db.Sql(sql, appid, models.AUTOREPLY_IS_USE_ON, models.AUTOREPLY_SUBSCRIBE, models.STATUS_DEL, models.STATUS_DEL).Find(&replys) if err != nil { return nil, err } if len(replys) > 0 { return &replys[0], nil } return nil, nil } func (m *AutoreplyDAO) DisableAutoreply(autoType, orgId, isUse string) error { var auto = model.TaAutoReply{ AutoType: autoType, OrgId: orgId, IsUse: isUse, } var cols = []string{ "is_use", } _, err := m.db.Cols(cols...).Where("auto_type = ?", auto.AutoType).And("org_id = ?", auto.OrgId).Update(auto) return err }