service_log.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 serviceLog = sequelize.define('CLD_service_log', {
  10. slid : {type: DataTypes.INTEGER(11), primaryKey: true, autoIncrement: true},
  11. status: DataTypes.INTEGER,
  12. mark: DataTypes.STRING,
  13. date: DataTypes.DATE,
  14. category: DataTypes.STRING,
  15. clientid: DataTypes.INTEGER,
  16. staffname: DataTypes.STRING,
  17. staffid: DataTypes.INTEGER,
  18. }, {});
  19. /*
  20. * 根据客户ID合集获得服务信息
  21. * */
  22. serviceLog.findAllByClientid=async function(clientid,attributes){
  23. if(clientid==undefined){
  24. return [];
  25. }
  26. var condition={
  27. raw:true,
  28. where: {
  29. clientid: clientid
  30. },
  31. order: [['date', 'desc']]
  32. };
  33. if(attributes!=undefined){
  34. condition.attributes=attributes;
  35. }
  36. var serviceLogList = await this.findAll(condition);
  37. serviceLogList.forEach(function(v,i){
  38. serviceLogList[i].slidKey=hash.hashEncode(serviceLogList[i].slid.toString());
  39. });
  40. return serviceLogList;
  41. };
  42. /*
  43. * 根据ID获得客户信息
  44. * */
  45. serviceLog.findById=async function(cid,attributes){
  46. if(!hash.isNotANumber(cid)){
  47. cid=hash.hashDecode(cid);
  48. if(!hash.isNotANumber(cid)){
  49. return [];
  50. }
  51. }
  52. //console.log(cid);
  53. if(hash.isExistence(attributes)){
  54. condition.attributes=attributes;
  55. }
  56. var condition={
  57. raw:true,
  58. where: {
  59. cid: cid
  60. }};
  61. var detail = await this.findOne(condition);
  62. if(!hash.isExistence(detail)){
  63. return [];
  64. }
  65. var staffDetail=[];
  66. var sqlQuery = 'SELECT b.* FROM CLD_client_staff as a left join CLD_staff as b on (a.sid=b.sid) where a.cid=?';
  67. await sequelize.query(sqlQuery,
  68. { replacements: [cid], type: sequelize.QueryTypes.SELECT }
  69. ).then(function(results) {
  70. staffDetail=results[0];
  71. })
  72. // 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) => {
  73. // staffDetail=results[0];
  74. // });
  75. detail['staff']=staffDetail;
  76. detail.cidKey=hash.hashEncode(detail.cid.toString());
  77. detail.cid=hash.hashEncode(detail.cid.toString());
  78. return detail;
  79. };
  80. /*
  81. * 根据ID合集获得客户信息
  82. * */
  83. serviceLog.getClentListByClientname=async function(clientname,attributes){
  84. if(!hash.isExistence(clientname)){
  85. return [];
  86. }
  87. //
  88. var condition={
  89. raw:true,
  90. limit:30,
  91. where: {
  92. clientname: {
  93. [Op.like]: '%'+clientname+'%'
  94. }
  95. }};
  96. if(hash.isExistence(attributes)){
  97. condition.attributes=attributes;
  98. };
  99. var clientList = await this.findAll(condition);
  100. clientList.forEach(function(v,i){
  101. clientList[i].cidKey=hash.hashEncode(clientList[i].cid.toString());
  102. });
  103. return clientList;
  104. };
  105. /*
  106. * 根据ID合集获得客户信息
  107. * */
  108. serviceLog.getClentListByPhone=async function(phone,attributes){
  109. if(!hash.isExistence(phone)){
  110. return [];
  111. }
  112. //
  113. var condition={
  114. raw:true,
  115. limit:30,
  116. where: {
  117. telephone: {
  118. [Op.like]: '%'+phone+'%'
  119. }
  120. }};
  121. if(hash.isExistence(attributes)){
  122. condition.attributes=attributes;
  123. };
  124. var clientList = await this.findAll(condition);
  125. clientList.forEach(function(v,i){
  126. clientList[i].cidKey=hash.hashEncode(clientList[i].cid.toString());
  127. });
  128. return clientList;
  129. };
  130. return serviceLog;
  131. };