<commit_before>// Copyright 2016 The StudyGolang Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// http://studygolang.com
// Author:polaris	<EMAIL>

package logic

import (
	. "db"
	"model"
	"net/url"
	"strconv"
	"strings"

	"github.com/polaris1119/logger"
	"golang.org/x/net/context"
)

type ReadingLogic struct{}

var DefaultReading = ReadingLogic{}

func (ReadingLogic) FindLastList(beginTime string) ([]*model.MorningReading, error) {
	readings := make([]*model.MorningReading, 0)
	err := MasterDB.Where("ctime>? AND rtype=0", beginTime).OrderBy("id DESC").Find(&readings)

	return readings, err
}

// 获取晨读列表（分页）
func (ReadingLogic) FindBy(ctx context.Context, limit, rtype int, lastIds ...int) []*model.MorningReading {
	objLog := GetLogger(ctx)

	dbSession := MasterDB.Where("rtype=?", rtype)
	if len(lastIds) > 0 && lastIds[0] > 0 {
		dbSession.And("id<?", lastIds[0])
	}

	readingList := make([]*model.MorningReading, 0)
	err := dbSession.OrderBy("id DESC").Limit(limit).Find(&readingList)
	if err != nil {
		objLog.Errorln("ResourceLogic FindReadings Error:", err)
		return nil
	}

	return readingList
}

// 【我要晨读】
func (ReadingLogic) IReading(ctx context.Context, id int) string {
	objLog := GetLogger(ctx)

	reading := &model.MorningReading{}
	_, err := MasterDB.Id(id).Get(reading)
	if err != nil {
		objLog.Errorln("reading logic IReading error:", err)
		return "/readings"
	}

	if reading.Id == 0 {
		return "/readings"
	}

	go MasterDB.Id(id).Incr("clicknum", 1).Update(reading)

	if reading.Inner == 0 {
		return "/wr?u=" + reading.Url
	}

	return "/articles/" + strconv.Itoa(reading.Inner)
}

// FindReadingByPage 获取晨读列表（分页）
func (ReadingLogic) FindReadingByPage(ctx context.Context, conds map[string]string, curPage, limit int) ([]*model.MorningReading, int) {
	objLog := GetLogger(ctx)

	session := MasterDB.NewSession()

	for k, v := range conds {
		session.And(k+"=?", v)
	}

	totalSession := session.Clone()

	offset := (curPage - 1) * limit
	readingList := make([]*model.MorningReading, 0)
	err := session.OrderBy("id DESC").Limit(limit, offset).Find(&readingList)
	if err != nil {
		objLog.Errorln("reading find error:", err)
		return nil, 0
	}

	total, err := totalSession.Count(new(model.MorningReading))
	if err != nil {
		objLog.Errorln("reading find count error:", err)
		return nil, 0
	}

	return readingList, int(total)
}

// SaveReading 保存晨读
func (ReadingLogic) SaveReading(ctx context.Context, form url.Values, username string) (errMsg string, err error) {
	reading := &model.MorningReading{}
	err = schemaDecoder.Decode(reading, form)
	if err != nil {
		logger.Errorln("reading SaveReading error", err)
		errMsg = err.Error()
		return
	}

	reading.Moreurls = strings.TrimSpace(reading.Moreurls)
	if strings.Contains(reading.Moreurls, "\n") {
		reading.Moreurls = strings.Join(strings.Split(reading.Moreurls, "\n"), ",")
	}

	reading.Username = username

	logger.Debugln(reading.Rtype, "id=", reading.Id)
	if reading.Id != 0 {
		_, err = MasterDB.Update(reading)
	} else {
		_, err = MasterDB.Insert(reading)
	}

	if err != nil {
		errMsg = "内部服务器错误"
		logger.Errorln("reading save:", errMsg, ":", err)
		return
	}

	return
}

// FindById 获取单条晨读
func (ReadingLogic) FindById(ctx context.Context, id int) *model.MorningReading {
	reading := &model.MorningReading{}
	_, err := MasterDB.Id(id).Get(reading)
	if err != nil {
		logger.Errorln("reading logic FindReadingById Error:", err)
		return nil
	}

	if reading.Id == 0 {
		return nil
	}

	return reading
}
<commit_msg>晨读修改 bugfix<commit_after>// Copyright 2016 The StudyGolang Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// http://studygolang.com
// Author:polaris	<EMAIL>

package logic

import (
	. "db"
	"model"
	"net/url"
	"strconv"
	"strings"

	"github.com/polaris1119/logger"
	"golang.org/x/net/context"
)

type ReadingLogic struct{}

var DefaultReading = ReadingLogic{}

func (ReadingLogic) FindLastList(beginTime string) ([]*model.MorningReading, error) {
	readings := make([]*model.MorningReading, 0)
	err := MasterDB.Where("ctime>? AND rtype=0", beginTime).OrderBy("id DESC").Find(&readings)

	return readings, err
}

// 获取晨读列表（分页）
func (ReadingLogic) FindBy(ctx context.Context, limit, rtype int, lastIds ...int) []*model.MorningReading {
	objLog := GetLogger(ctx)

	dbSession := MasterDB.Where("rtype=?", rtype)
	if len(lastIds) > 0 && lastIds[0] > 0 {
		dbSession.And("id<?", lastIds[0])
	}

	readingList := make([]*model.MorningReading, 0)
	err := dbSession.OrderBy("id DESC").Limit(limit).Find(&readingList)
	if err != nil {
		objLog.Errorln("ResourceLogic FindReadings Error:", err)
		return nil
	}

	return readingList
}

// 【我要晨读】
func (ReadingLogic) IReading(ctx context.Context, id int) string {
	objLog := GetLogger(ctx)

	reading := &model.MorningReading{}
	_, err := MasterDB.Id(id).Get(reading)
	if err != nil {
		objLog.Errorln("reading logic IReading error:", err)
		return "/readings"
	}

	if reading.Id == 0 {
		return "/readings"
	}

	go MasterDB.Id(id).Incr("clicknum", 1).Update(reading)

	if reading.Inner == 0 {
		return "/wr?u=" + reading.Url
	}

	return "/articles/" + strconv.Itoa(reading.Inner)
}

// FindReadingByPage 获取晨读列表（分页）
func (ReadingLogic) FindReadingByPage(ctx context.Context, conds map[string]string, curPage, limit int) ([]*model.MorningReading, int) {
	objLog := GetLogger(ctx)

	session := MasterDB.NewSession()

	for k, v := range conds {
		session.And(k+"=?", v)
	}

	totalSession := session.Clone()

	offset := (curPage - 1) * limit
	readingList := make([]*model.MorningReading, 0)
	err := session.OrderBy("id DESC").Limit(limit, offset).Find(&readingList)
	if err != nil {
		objLog.Errorln("reading find error:", err)
		return nil, 0
	}

	total, err := totalSession.Count(new(model.MorningReading))
	if err != nil {
		objLog.Errorln("reading find count error:", err)
		return nil, 0
	}

	return readingList, int(total)
}

// SaveReading 保存晨读
func (ReadingLogic) SaveReading(ctx context.Context, form url.Values, username string) (errMsg string, err error) {
	reading := &model.MorningReading{}
	err = schemaDecoder.Decode(reading, form)
	if err != nil {
		logger.Errorln("reading SaveReading error", err)
		errMsg = err.Error()
		return
	}

	if reading.Inner != 0 {
		reading.Url = ""
	}

	reading.Moreurls = strings.TrimSpace(reading.Moreurls)
	if strings.Contains(reading.Moreurls, "\n") {
		reading.Moreurls = strings.Join(strings.Split(reading.Moreurls, "\n"), ",")
	}

	reading.Username = username

	logger.Debugln(reading.Rtype, "id=", reading.Id)
	if reading.Id != 0 {
		_, err = MasterDB.Id(reading.Id).Update(reading)
	} else {
		_, err = MasterDB.Insert(reading)
	}

	if err != nil {
		errMsg = "内部服务器错误"
		logger.Errorln("reading save:", errMsg, ":", err)
		return
	}

	return
}

// FindById 获取单条晨读
func (ReadingLogic) FindById(ctx context.Context, id int) *model.MorningReading {
	reading := &model.MorningReading{}
	_, err := MasterDB.Id(id).Get(reading)
	if err != nil {
		logger.Errorln("reading logic FindReadingById Error:", err)
		return nil
	}

	if reading.Id == 0 {
		return nil
	}

	return reading
}

{'fraction_non_alphanumeric': 0.15457936179030252, 'fraction_numerical': 0.006768890730763918, 'mean_word_length': 4.30791788856305, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1417874', 'n_tokens_mistral': 2866, 'n_tokens_neox': 2621, 'n_words': 607}