client.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * Created by MyPC on 2019/11/5.
  3. */
  4. 'use strict';
  5. const hash= require('../class/hash');
  6. const Sequelize = require('sequelize');
  7. const Op = Sequelize.Op;
  8. module.exports = (sequelize, DataTypes) => {
  9. const client = sequelize.define('CLD_client', {
  10. cid : {type: DataTypes.INTEGER(11), primaryKey: true, autoIncrement: true},
  11. clientname: DataTypes.STRING,
  12. companyid: DataTypes.INTEGER,
  13. companyname: DataTypes.STRING,
  14. position: DataTypes.STRING,
  15. telephone: DataTypes.STRING,
  16. gender: DataTypes.STRING,
  17. nicename: DataTypes.STRING,
  18. qq: DataTypes.INTEGER(15),
  19. phone: DataTypes.STRING,
  20. email: DataTypes.STRING,
  21. area: DataTypes.STRING,
  22. keynum: DataTypes.STRING,
  23. unit: DataTypes.STRING,
  24. nature: DataTypes.STRING,
  25. local: DataTypes.STRING,
  26. fax: DataTypes.STRING,
  27. webservice: DataTypes.STRING,
  28. department: DataTypes.STRING,
  29. office: DataTypes.STRING,
  30. address: DataTypes.STRING,
  31. district: DataTypes.STRING,
  32. tag: DataTypes.STRING,
  33. //tooltip: DataTypes.STRING,
  34. ride: DataTypes.STRING,
  35. stay: DataTypes.STRING,
  36. landmarks: DataTypes.STRING,
  37. mark: DataTypes.STRING,
  38. priority: DataTypes.STRING,
  39. updatetime: DataTypes.INTEGER(11),
  40. createTime:DataTypes.DATE,
  41. servicetime: DataTypes.INTEGER(11),
  42. }, {});
  43. /*
  44. * 根据ID合集获得客户信息
  45. * */
  46. client.findAllInCid=async function(cid,attributes){
  47. if(cid==undefined){
  48. return [];
  49. }
  50. var condition={
  51. raw:true,
  52. where: {
  53. cid: {
  54. [Op.or]: [cid]
  55. }
  56. }};
  57. if(attributes!=undefined){
  58. condition.attributes=attributes;
  59. }
  60. var clientList = await this.findAll(condition);
  61. clientList.forEach(function(v,i){
  62. clientList[i].cidKey=hash.hashEncode(clientList[i].cid.toString());
  63. //console.log(clientList[i].dataValues.cidKey);
  64. //console.log(hash.hashDecode(clientList[i].dataValues.cidKey.toString()));
  65. });
  66. return clientList;
  67. };
  68. /*
  69. * 根据ID获得客户信息
  70. * */
  71. client.findById=async function(cid,attributes){
  72. if(!hash.isNotANumber(cid)){
  73. cid=hash.hashDecode(cid);
  74. if(!hash.isNotANumber(cid)){
  75. return [];
  76. }
  77. }
  78. //console.log(cid);
  79. if(hash.isExistence(attributes)){
  80. condition.attributes=attributes;
  81. }
  82. var condition={
  83. raw:true,
  84. where: {
  85. cid: cid
  86. }};
  87. var detail = await this.findOne(condition);
  88. if(!hash.isExistence(detail)){
  89. return [];
  90. }
  91. var staffDetail=[];
  92. var tblName1 = 'CLD_client_staff', tblName2='CLD_staff';
  93. var cid = '20160923 AND 1=1;-- hack';
  94. var sqlQuery = 'SELECT b.* FROM ${tblName1} as a left join ${tblName2} as b on (a.sid=b.sid) where a.cid=$cid';
  95. await this.sequelize.query(sqlQuery,
  96. type: sequelize.QueryTypes.SELECT,
  97. bind: {
  98. cid: cid
  99. }
  100. ).spread((results, metadata) => {
  101. staffDetail=results[0];
  102. });
  103. // 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) => {
  104. // staffDetail=results[0];
  105. // });
  106. detail['staff']=staffDetail;
  107. detail.cidKey=hash.hashEncode(detail.cid.toString());
  108. detail.cid=hash.hashEncode(detail.cid.toString());
  109. return detail;
  110. };
  111. /*
  112. * 根据ID合集获得客户信息
  113. * */
  114. client.getClentListByClientname=async function(clientname,attributes){
  115. if(!hash.isExistence(clientname)){
  116. return [];
  117. }
  118. //
  119. var condition={
  120. raw:true,
  121. limit:30,
  122. where: {
  123. clientname: {
  124. [Op.like]: '%'+clientname+'%'
  125. }
  126. }};
  127. if(hash.isExistence(attributes)){
  128. condition.attributes=attributes;
  129. };
  130. var clientList = await this.findAll(condition);
  131. clientList.forEach(function(v,i){
  132. clientList[i].cidKey=hash.hashEncode(clientList[i].cid.toString());
  133. });
  134. return clientList;
  135. };
  136. return client;
  137. };