/**
 * Created by MyPC on 2019/11/5.
 */
'use strict';
const hash= require('../class/hash');
const Sequelize = require('sequelize');
const Op = Sequelize.Op;

var moment = require('moment');

/*var d = new Date();
var tableNameYear=d.getFullYear();
 +OPERATELOGTABLE*/


module.exports = (sequelize, DataTypes) => {

    const operateLog = sequelize.define('operate_log', {
        id : {type: DataTypes.INTEGER(11), primaryKey: true, autoIncrement: true},
        logType: DataTypes.INTEGER,

        dataId: DataTypes.INTEGER,
        cid: DataTypes.INTEGER,
        sid: DataTypes.STRING,

        username: DataTypes.STRING,
        category: DataTypes.STRING,
        operation: DataTypes.STRING,
        img: DataTypes.INTEGER(15),

        createDate: DataTypes.DATE,

    }, {tableName:'operate_log_'+OPERATELOGTABLE});


    /*
     * 根据ID合集获得客户信息
     * */
    operateLog.findByCondition=async function(logType,dataId){

        if(!hash.isExistence(logType)||!hash.isExistence(dataId)){
            return []
        }else if(!hash.isNotANumber(logType)||!hash.isNotANumber(dataId)){
            return [];
        }
        //var idList = await this.findAll(condition);//获得idList
        //var yearList= await this.findAll(condition);//获得yearList
        //console.log(logType+'d'+dataId);
        var list=[];
        await this.sequelize.query('select  CONVERT(GROUP_CONCAT(id) USING utf8)   as id,date_format(createDate,"%Y") as year FROM operate_log where ' +
            'logType='+logType+' and dataId='+dataId+' GROUP BY Year(createDate) ').spread((results, metadata) => {
            list=results;
        });

        var sqlArray=[];
        list.forEach(function(v,i){
            sqlArray.push(' SELECT *,DATE_FORMAT(createDate, "%Y-%m-%d %H:%m:%S") AS createDate FROM operate_log_'+v.year+' WHERE id in ('+v.id+')   ');

        });
        if(!hash.isExistence(list)){
            return [];
        }

        var sql=sqlArray.join(" UNION ");
        sql+=' ORDER BY id desc';

        await this.sequelize.query(sql).spread((results, metadata) => {
            list=results;
        });

        list.forEach(function(v,i){
            //list[i].createDate=moment(v.createDate, 'YYYY-MM-DD HH:mm');//moment.unix(v.createDate).format('YYYY-MM-DD HH:mm');
            list[i].id=hash.hashEncode(v.id.toString());
        });

        return list;
    };

/*
 * 新增操作日志
 *
 * */
operateLog.createOperateLog=async function(logType,dataId,operation){

    if(!hash.isExistence(logType)||!hash.isExistence(dataId)){
        return false;
    }

    var sid=hash.hashDecode(STAFF.sid);
    var createDate=moment(new Date()).format('YYYY-MM-DD HH:mm:ss');

    var sql='INSERT INTO operate_log (logType,sid,createDate,dataId,cid) values ('+logType+','+sid+',"'+createDate +'",'+dataId+','+STAFF.cid+')';
    var id='';
    await this.sequelize.query(sql).spread((results, metadata) => {
        id=results;
    });

    var obj = {
        id:id,
        logType: logType,
        username: STAFF.username,
        sid: sid,
        category: STAFF.category,
        createDate: createDate,
        operation: operation,
        img: STAFF.avatar,

        dataId: dataId,
        cid: STAFF.cid,
    };
    await this.create(obj);
    return true;
};








    return operateLog;
};

