<commit_before>var Sequelize = require('sequelize'),
    sequelize = require('../db/sequelize');
var BaseDao = require('./baseDao'),
    goodDao = require('./goodDao');

class OrderDao extends BaseDao {

    /**
     * create order
     * @method create
     * @param  {Number} user_id     : user id
     * @param  {Number} orderId     : order id
     * @param  {Number} expressId   : express id
     * @param  {Number} expressCost : express cost
     * @param  {String} name        : buyer
     * @param  {Double} price       : order price
     * @param  {List} goodList      : good list
     * @param  {String} comment     : comment
     * @return {Promise}
     */
    create(user_id, orderId, expressId, expressCost, name, price, goodList, comment) {
        return sequelize.transaction((transaction) => {
            return this.model.Order.create({
                id: orderId,
                expressId: expressId,
                expressCost: expressCost,
                name: name,
                user_id: user_id,
                comment: comment,
                price: price
            }, {
                transaction: transaction
            }).then((order) => {
                if (order) {
                    var promises = [];
                    for (var i = 0; i < goodList.length; i++) {
                        var _good = goodList[i];
                        promises.push(goodDao.out(_good.id, _good.amount, price, _good.attr,
                            user_id));
                    }
                    return Promise.all(promises).then((result) => {
                        return this.ajaxModel(200, '创建成功!');
                    });
                }
            });
        });
    }

    /**
     * list order
     * @method list
     * @param  {Number} user_id  : user id
     * @param  {Number} page     : page index
     * @param  {Number} pageSize : page size
     * @return {Promise}
     */
    list(user_id, page, pageSize) {
        var limit = pageSize > 20 ? 20 : pageSize;
        var offset = page > 1 ? limit * (page - 1) : 0;

        return this.model.Order.findAndCountAll({
            limit: limit,
            offset: offset,
            attributes: [
                [Sequelize.fn('DATE_FORMAT', Sequelize.col('date'), '%Y-%m-%d'), 'date'],
                'id', 'expressCost', 'expressId', 'name', 'price', 'comment'
            ],
            where: {
                user_id: user_id
            },
            order: [
                ['expressDate', 'DESC']
            ]
        }).then((result) => {
            return this.ajaxModel(200, {
                count: result.count,
                content: result.rows
            });
        });
    }

    /**
     * order detail
     * @method orderDetail
     * @param  {Number} user_id  : user id
     * @param  {Number} order_id : order id
     * @return {Promise}
     */
    orderDetail(user_id, order_id) {
        return this.model.Records.findAll({
            include: {
                model: this.model.Goods,
                where: {
                    order_id: order_id,
                    user_id: user_id
                }
            }
        }).then((rows) => {
            return this.ajaxModel(200, rows);
        });<commit_msg>change for new order list<commit_after>var Sequelize = require('sequelize'),
    sequelize = require('../db/sequelize');
var BaseDao = require('./baseDao'),
    goodDao = require('./goodDao');

class OrderDao extends BaseDao {

    /**
     * create order
     * @method create
     * @param  {Number} user_id     : user id
     * @param  {Number} orderId     : order id
     * @param  {Number} expressId   : express id
     * @param  {Number} expressCost : express cost
     * @param  {String} name        : buyer
     * @param  {Double} price       : order price
     * @param  {List} goodList      : good list
     * @param  {String} comment     : comment
     * @return {Promise}
     */
    create(user_id, orderId, expressId, expressCost, name, price, goodList, comment) {
        return sequelize.transaction((transaction) => {
            return this.model.Order.create({
                id: orderId,
                expressId: expressId,
                expressCost: expressCost,
                name: name,
                user_id: user_id,
                comment: comment,
                price: price
            }, {
                transaction: transaction
            }).then((order) => {
                if (order) {
                    var promises = [];
                    for (var i = 0; i < goodList.length; i++) {
                        var _good = goodList[i];
                        promises.push(goodDao.out(_good.id, _good.amount, price, _good.attr,
                            user_id));
                    }
                    return Promise.all(promises).then((result) => {
                        return this.ajaxModel(200, '创建成功!');
                    });
                }
            });
        });
    }

    /**
     * list order
     * @method list
     * @param  {Number} user_id  : user id
     * @param  {Number} page     : page index
     * @param  {Number} pageSize : page size
     * @return {Promise}
     */
    list(user_id, page, pageSize) {
        var limit = pageSize > 20 ? 20 : pageSize;
        var offset = page > 1 ? limit * (page - 1) : 0;

        return this.model.Order.findAndCountAll({
            limit: limit,
            offset: offset,
            attributes: [
                [Sequelize.fn('DATE_FORMAT', Sequelize.col('expressDate'), '%Y-%m-%d'), 'date'],
                'id', 'expressCost', 'expressId', 'name', 'price', 'comment'
            ],
            where: {
                user_id: user_id
            },
            order: [
                ['expressDate', 'DESC']
            ]
        }).then((result) => {
            return this.ajaxModel(200, {
                count: result.count,
                content: result.rows
            });
        });
    }

    /**
     * order detail
     * @method orderDetail
     * @param  {Number} user_id  : user id
     * @param  {Number} order_id : order id
     * @return {Promise}
     */
    orderDetail(user_id, order_id) {
        return this.model.Record.findAll({
            include: [{
                model: this.model.Good,
                attributes: ['name'],
                where: {
                    user_id: user_id
                }
            }, {
                model: this.model.Attr,
                through: {
                    attributes: []
                }
            }],
            where: {
                order_id: order_id
            }
        }).then((rows) => {
            return this.ajaxModel(200, rows);
        });
{'fraction_non_alphanumeric': 0.1228355779191949, 'fraction_numerical': 0.005031818854521237, 'mean_word_length': 1.2931795045809298, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '3906258', 'n_tokens_mistral': 1916, 'n_tokens_neox': 1834, 'n_words': 433}