client.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 sqlQuery = 'SELECT b.* FROM CLD_client_staff as a left join CLD_staff as b on (a.sid=b.sid) where a.cid=?';
  93. // sequelize.query(sqlQuery,
  94. // { replacements: [cid], type: sequelize.QueryTypes.SELECT }
  95. // ).then(function(projects) {
  96. // console.log(projects)
  97. // })
  98. // await this.sequelize.query(sqlQuery,
  99. // type: sequelize.QueryTypes.SELECT,
  100. // bind: {
  101. // cid: cid
  102. // }
  103. // ).spread((results, metadata) => {
  104. // staffDetail=results[0];
  105. // });
  106. 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) => {
  107. staffDetail=results[0];
  108. });
  109. detail['staff']=staffDetail;
  110. detail.cidKey=hash.hashEncode(detail.cid.toString());
  111. detail.cid=hash.hashEncode(detail.cid.toString());
  112. return detail;
  113. };
  114. /*
  115. * 根据ID合集获得客户信息
  116. * */
  117. client.getClentListByClientname=async function(clientname,attributes){
  118. if(!hash.isExistence(clientname)){
  119. return [];
  120. }
  121. //
  122. var condition={
  123. raw:true,
  124. limit:30,
  125. where: {
  126. clientname: {
  127. [Op.like]: '%'+clientname+'%'
  128. }
  129. }};
  130. if(hash.isExistence(attributes)){
  131. condition.attributes=attributes;
  132. };
  133. var clientList = await this.findAll(condition);
  134. clientList.forEach(function(v,i){
  135. clientList[i].cidKey=hash.hashEncode(clientList[i].cid.toString());
  136. });
  137. return clientList;
  138. };
  139. return client;
  140. };