|  | @@ -0,0 +1,153 @@
 | 
	
		
			
				|  |  | +/**
 | 
	
		
			
				|  |  | + * Created by MyPC on 2019/11/5.
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  | +'use strict';
 | 
	
		
			
				|  |  | +const hash= require('../class/hash');
 | 
	
		
			
				|  |  | +const Sequelize = require('sequelize');
 | 
	
		
			
				|  |  | +const Op = Sequelize.Op;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +module.exports = (sequelize, DataTypes) => {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    const serviceLog = sequelize.define('CLD_service_log', {
 | 
	
		
			
				|  |  | +        slid   : {type: DataTypes.INTEGER(11), primaryKey: true, autoIncrement: true},
 | 
	
		
			
				|  |  | +        status: DataTypes.INTEGER,
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        mark: DataTypes.STRING,
 | 
	
		
			
				|  |  | +        date: DataTypes.DATE,
 | 
	
		
			
				|  |  | +        category: DataTypes.STRING,
 | 
	
		
			
				|  |  | +        clientid: DataTypes.INTEGER,
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        staffname: DataTypes.STRING,
 | 
	
		
			
				|  |  | +        staffid: DataTypes.INTEGER,
 | 
	
		
			
				|  |  | +    }, {});
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /*
 | 
	
		
			
				|  |  | +     * 根据客户ID合集获得服务信息
 | 
	
		
			
				|  |  | +     * */
 | 
	
		
			
				|  |  | +    serviceLog.findAllByClientid=async function(clientid,attributes){
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if(clientid==undefined){
 | 
	
		
			
				|  |  | +            return [];
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        var condition={
 | 
	
		
			
				|  |  | +            raw:true,
 | 
	
		
			
				|  |  | +            where: {
 | 
	
		
			
				|  |  | +                clientid: clientid
 | 
	
		
			
				|  |  | +            },
 | 
	
		
			
				|  |  | +            order: [['date', 'desc']]
 | 
	
		
			
				|  |  | +        };
 | 
	
		
			
				|  |  | +        if(attributes!=undefined){
 | 
	
		
			
				|  |  | +            condition.attributes=attributes;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        var serviceLogList = await this.findAll(condition);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        serviceLogList.forEach(function(v,i){
 | 
	
		
			
				|  |  | +            serviceLogList[i].slidKey=hash.hashEncode(serviceLogList[i].slid.toString());
 | 
	
		
			
				|  |  | +        });
 | 
	
		
			
				|  |  | +        return serviceLogList;
 | 
	
		
			
				|  |  | +    };
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +/*
 | 
	
		
			
				|  |  | + * 根据ID获得客户信息
 | 
	
		
			
				|  |  | + * */
 | 
	
		
			
				|  |  | +serviceLog.findById=async function(cid,attributes){
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    if(!hash.isNotANumber(cid)){
 | 
	
		
			
				|  |  | +        cid=hash.hashDecode(cid);
 | 
	
		
			
				|  |  | +        if(!hash.isNotANumber(cid)){
 | 
	
		
			
				|  |  | +            return [];
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //console.log(cid);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    if(hash.isExistence(attributes)){
 | 
	
		
			
				|  |  | +        condition.attributes=attributes;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    var condition={
 | 
	
		
			
				|  |  | +        raw:true,
 | 
	
		
			
				|  |  | +        where: {
 | 
	
		
			
				|  |  | +            cid: cid
 | 
	
		
			
				|  |  | +        }};
 | 
	
		
			
				|  |  | +    var detail = await this.findOne(condition);
 | 
	
		
			
				|  |  | +    if(!hash.isExistence(detail)){
 | 
	
		
			
				|  |  | +        return [];
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    var staffDetail=[];
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    var sqlQuery = 'SELECT b.* FROM CLD_client_staff as a left join CLD_staff as b on (a.sid=b.sid) where a.cid=?';
 | 
	
		
			
				|  |  | +    await sequelize.query(sqlQuery,
 | 
	
		
			
				|  |  | +    		  { replacements: [cid], type: sequelize.QueryTypes.SELECT }
 | 
	
		
			
				|  |  | +    		).then(function(results) {
 | 
	
		
			
				|  |  | +    			staffDetail=results[0];
 | 
	
		
			
				|  |  | +    		})
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +//    await this.sequelize.query('SELECT b.* FROM CLD_client_staff as a left join CLD_staff as b on (a.sid=b.sid) where a.cid=7').spread((results, metadata) => {
 | 
	
		
			
				|  |  | +//        staffDetail=results[0];
 | 
	
		
			
				|  |  | +//    });
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    detail['staff']=staffDetail;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    detail.cidKey=hash.hashEncode(detail.cid.toString());
 | 
	
		
			
				|  |  | +    detail.cid=hash.hashEncode(detail.cid.toString());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    return detail;
 | 
	
		
			
				|  |  | +};
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +/*
 | 
	
		
			
				|  |  | + * 根据ID合集获得客户信息
 | 
	
		
			
				|  |  | + * */
 | 
	
		
			
				|  |  | +serviceLog.getClentListByClientname=async function(clientname,attributes){
 | 
	
		
			
				|  |  | +    if(!hash.isExistence(clientname)){
 | 
	
		
			
				|  |  | +        return [];
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +//
 | 
	
		
			
				|  |  | +    var condition={
 | 
	
		
			
				|  |  | +        raw:true,
 | 
	
		
			
				|  |  | +        limit:30,
 | 
	
		
			
				|  |  | +        where: {
 | 
	
		
			
				|  |  | +            clientname: {
 | 
	
		
			
				|  |  | +                [Op.like]: '%'+clientname+'%'
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    }};
 | 
	
		
			
				|  |  | +    if(hash.isExistence(attributes)){
 | 
	
		
			
				|  |  | +        condition.attributes=attributes;
 | 
	
		
			
				|  |  | +    };
 | 
	
		
			
				|  |  | +    var clientList = await this.findAll(condition);
 | 
	
		
			
				|  |  | +    clientList.forEach(function(v,i){
 | 
	
		
			
				|  |  | +        clientList[i].cidKey=hash.hashEncode(clientList[i].cid.toString());
 | 
	
		
			
				|  |  | +    });
 | 
	
		
			
				|  |  | +    return clientList;
 | 
	
		
			
				|  |  | +};
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +/*
 | 
	
		
			
				|  |  | + * 根据ID合集获得客户信息
 | 
	
		
			
				|  |  | + * */
 | 
	
		
			
				|  |  | +serviceLog.getClentListByPhone=async function(phone,attributes){
 | 
	
		
			
				|  |  | +    if(!hash.isExistence(phone)){
 | 
	
		
			
				|  |  | +        return [];
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +//
 | 
	
		
			
				|  |  | +    var condition={
 | 
	
		
			
				|  |  | +        raw:true,
 | 
	
		
			
				|  |  | +        limit:30,
 | 
	
		
			
				|  |  | +        where: {
 | 
	
		
			
				|  |  | +            telephone: {
 | 
	
		
			
				|  |  | +                [Op.like]: '%'+phone+'%'
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }};
 | 
	
		
			
				|  |  | +        if(hash.isExistence(attributes)){
 | 
	
		
			
				|  |  | +            condition.attributes=attributes;
 | 
	
		
			
				|  |  | +        };
 | 
	
		
			
				|  |  | +        var clientList = await this.findAll(condition);
 | 
	
		
			
				|  |  | +        clientList.forEach(function(v,i){
 | 
	
		
			
				|  |  | +            clientList[i].cidKey=hash.hashEncode(clientList[i].cid.toString());
 | 
	
		
			
				|  |  | +        });
 | 
	
		
			
				|  |  | +        return clientList;
 | 
	
		
			
				|  |  | +};
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    return serviceLog;
 | 
	
		
			
				|  |  | +};
 | 
	
		
			
				|  |  | +
 |