OrderDetail.service.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import { Provide } from '@midwayjs/decorator';
  2. import { InjectEntityModel } from '@midwayjs/typegoose';
  3. import { ReturnModelType } from '@typegoose/typegoose';
  4. import { BaseService } from 'free-midway-component';
  5. import { OrderDetail } from '../entity/OrderDetail.entity';
  6. import { Good } from '../entity/Good.entity';
  7. import { Specs } from '../entity/Specs.entity';
  8. import { User } from '../entity/User.entity';
  9. import _ = require('lodash');
  10. type modelType = ReturnModelType<typeof OrderDetail>;
  11. @Provide()
  12. export class OrderDetailService extends BaseService<modelType> {
  13. @InjectEntityModel(OrderDetail)
  14. model: modelType;
  15. @InjectEntityModel(Good)
  16. goodModel: ReturnModelType<typeof Good>;
  17. @InjectEntityModel(Specs)
  18. specModel: ReturnModelType<typeof Specs>;
  19. @InjectEntityModel(User)
  20. userModel: ReturnModelType<typeof User>;
  21. async queryInfo(filter): Promise<object> {
  22. const {
  23. skip = 0,
  24. limit,
  25. s_leader,
  26. s_accounting,
  27. c_leader,
  28. c_accounting,
  29. goods,
  30. status,
  31. ...info
  32. } = filter;
  33. let list: any = [];
  34. let total: any = 0;
  35. if (s_leader) info.s_leader = { $elemMatch: { _id: s_leader } };
  36. else if (s_accounting)
  37. info.s_accounting = { $elemMatch: { _id: s_accounting } };
  38. if (c_leader) info.c_leader = { $elemMatch: { _id: c_leader } };
  39. else if (c_accounting)
  40. info.c_accounting = { $elemMatch: { _id: c_accounting } };
  41. if (goods) {
  42. const arr = await this.goodModel
  43. .findOne({
  44. name: {
  45. $regex: goods,
  46. },
  47. })
  48. .lean();
  49. if (arr) info.good = arr._id;
  50. }
  51. if (status) {
  52. if (status === '0' && s_accounting)
  53. info.$or = [{ status: '0' }, { status: '20' }];
  54. else if (status === '2' && s_leader)
  55. info.$or = [{ status: '2' }, { status: '30' }];
  56. else if (status === '4' && info.supplier)
  57. info.$or = [{ status: '4' }, { status: '5' }, { status: '6' }];
  58. else if (status === '6' && c_accounting)
  59. info.$or = [{ status: '6' }, { status: '40' }];
  60. else if (status === '7' && c_leader)
  61. info.$or = [{ status: '7' }, { status: '50' }];
  62. else info.status = status;
  63. }
  64. list = await this.model.find(info).skip(skip).limit(limit).lean();
  65. total = await this.model.count(info);
  66. for (const val of list) {
  67. let res;
  68. res = await this.goodModel.findById(val.good).lean();
  69. val.good_name = res.name;
  70. val.good_file = res.file[0];
  71. res = await this.specModel.findById(val.spec).lean();
  72. val.spec_name = res.name;
  73. val.spec_file = res.file[0];
  74. res = await this.userModel.findById(val.user).lean();
  75. val.user_name = res.name;
  76. val.user_role = res.role;
  77. res = await this.userModel.findById(val.supplier).lean();
  78. val.supplier_name = res.name;
  79. }
  80. return { list, total };
  81. }
  82. async money(filter): Promise<object> {
  83. const { skip = 0, limit, goods, ...info } = filter;
  84. let list: any = [];
  85. let total: any = 0;
  86. if (goods) {
  87. const arr = await this.goodModel
  88. .findOne({
  89. name: {
  90. $regex: goods,
  91. },
  92. })
  93. .lean();
  94. if (arr) info.good = arr._id;
  95. }
  96. info.$or = [
  97. { status: '6' },
  98. { status: '7' },
  99. { status: '-7' },
  100. { status: '8' },
  101. { status: '-8' },
  102. ];
  103. list = await this.model.find(info).skip(skip).limit(limit).lean();
  104. total = await this.model.count(info);
  105. for (const val of list) {
  106. let res;
  107. res = await this.goodModel.findById(val.good).lean();
  108. val.good_name = res.name;
  109. val.good_file = res.file[0];
  110. res = await this.specModel.findById(val.spec).lean();
  111. val.spec_name = res.name;
  112. val.spec_file = res.file[0];
  113. res = await this.userModel.findById(val.user).lean();
  114. val.user_name = res.name;
  115. val.user_role = res.role;
  116. res = await this.userModel.findById(val.supplier).lean();
  117. val.supplier_name = res.name;
  118. }
  119. return { list, total };
  120. }
  121. // 详情
  122. async detail(id) {
  123. const res = await this.model.findById(id).lean();
  124. let arr;
  125. const info: any = {};
  126. arr = await this.goodModel.findById(res.good).lean();
  127. info.good_name = arr.name;
  128. info.good_type = arr.type;
  129. info.good_file = arr.file;
  130. arr = await this.specModel.findById(res.spec).lean();
  131. info.spec_name = arr.name;
  132. info.spec_file = arr.file;
  133. arr = await this.userModel.findById(res.user).lean();
  134. info.user_name = arr.name;
  135. info.user_tel = arr.tel;
  136. info.user_role = arr.role;
  137. arr = await this.userModel.findById(res.supplier).lean();
  138. info.supplier_name = arr.name;
  139. if (res.s_leader && res.s_leader.length > 0) {
  140. const sleader = [];
  141. for (const val of res.s_leader) {
  142. arr = await this.userModel.findById(val._id).lean();
  143. sleader.push(arr.name);
  144. }
  145. info.sleader_name = sleader.toString();
  146. }
  147. if (res.s_accounting && res.s_accounting.length > 0) {
  148. const saccounting = [];
  149. for (const val of res.s_accounting) {
  150. arr = await this.userModel.findById(val._id).lean();
  151. saccounting.push(arr.name);
  152. }
  153. info.saccounting_name = saccounting.toString();
  154. }
  155. if (res.c_leader && res.c_leader.length > 0) {
  156. const cleader = [];
  157. for (const val of res.c_leader) {
  158. arr = await this.userModel.findById(val._id).lean();
  159. cleader.push(arr.name);
  160. }
  161. info.cleader_name = cleader.toString();
  162. }
  163. if (res.c_accounting && res.c_accounting.length > 0) {
  164. const caccounting = [];
  165. for (const val of res.c_accounting) {
  166. arr = await this.userModel.findById(val._id).lean();
  167. caccounting.push(arr.name);
  168. }
  169. info.caccounting_name = caccounting.toString();
  170. }
  171. const result = _.assign(info, res);
  172. return result;
  173. }
  174. // 修改
  175. async update(id, body): Promise<string> {
  176. const { c_accounting, s_accounting, c_leader, s_leader, status, ...info } =
  177. body;
  178. const res = await this.model.findById(id);
  179. let arr;
  180. let result;
  181. let yes;
  182. let no;
  183. if (s_accounting) {
  184. const saccounting = [];
  185. for (const val of res.s_accounting) {
  186. const form: any = { _id: val._id };
  187. if (val._id === s_accounting) {
  188. if (status === '2') form.status = '1';
  189. else form.status = '-1';
  190. } else form.status = val.status || '0';
  191. saccounting.push(form);
  192. }
  193. info.s_accounting = saccounting;
  194. await this.model.updateOne({ _id: id }, info);
  195. arr = await this.model.findById(id);
  196. yes = arr.s_accounting.every(f => f.status === '1');
  197. no = arr.s_accounting.every(f => f.status === '-1');
  198. if (yes) {
  199. result = this.model.updateOne({ _id: id }, { status: '2' });
  200. } else {
  201. if (no) result = this.model.updateOne({ _id: id }, { status: '-2' });
  202. else result = this.model.updateOne({ _id: id }, { status: '20' });
  203. }
  204. } else if (c_accounting) {
  205. const caccounting = [];
  206. for (const val of res.c_accounting) {
  207. const form: any = { _id: val._id };
  208. if (val._id === c_accounting) {
  209. if (status === '7') form.status = '1';
  210. else form.status = '-1';
  211. } else form.status = val.status || '0';
  212. caccounting.push(form);
  213. }
  214. info.c_accounting = caccounting;
  215. await this.model.updateOne({ _id: id }, info);
  216. arr = await this.model.findById(id);
  217. yes = arr.c_accounting.every(f => f.status === '1');
  218. no = arr.c_accounting.every(f => f.status === '-1');
  219. if (yes) {
  220. result = this.model.updateOne({ _id: id }, { status: '7' });
  221. } else {
  222. if (no) result = this.model.updateOne({ _id: id }, { status: '-7' });
  223. else result = this.model.updateOne({ _id: id }, { status: '40' });
  224. }
  225. } else if (s_leader) {
  226. const sleader = [];
  227. for (const val of res.s_leader) {
  228. const form: any = { _id: val._id };
  229. if (val._id === s_leader) {
  230. if (status === '4') form.status = '1';
  231. else form.status = '-1';
  232. } else form.status = val.status || '0';
  233. sleader.push(form);
  234. }
  235. info.s_leader = sleader;
  236. await this.model.updateOne({ _id: id }, info);
  237. arr = await this.model.findById(id);
  238. yes = arr.s_leader.every(f => f.status === '1');
  239. no = arr.s_leader.every(f => f.status === '-1');
  240. if (yes) {
  241. result = this.model.updateOne({ _id: id }, { status: '4' });
  242. } else {
  243. if (no) result = this.model.updateOne({ _id: id }, { status: '-3' });
  244. else result = this.model.updateOne({ _id: id }, { status: '30' });
  245. }
  246. } else if (c_leader) {
  247. const cleader = [];
  248. for (const val of res.c_leader) {
  249. const form: any = { _id: val._id };
  250. if (val._id === c_leader) {
  251. if (status === '8') form.status = '1';
  252. else form.status = '-1';
  253. } else form.status = val.status || '0';
  254. cleader.push(form);
  255. }
  256. info.c_leader = cleader;
  257. await this.model.updateOne({ _id: id }, info);
  258. arr = await this.model.findById(id);
  259. yes = arr.c_leader.every(f => f.status === '1');
  260. no = arr.c_leader.every(f => f.status === '-1');
  261. if (yes) {
  262. result = this.model.updateOne({ _id: id }, { status: '8' });
  263. } else {
  264. if (no) result = this.model.updateOne({ _id: id }, { status: '-8' });
  265. else result = this.model.updateOne({ _id: id }, { status: '50' });
  266. }
  267. } else result = this.model.updateOne({ _id: id }, { status: status });
  268. return result;
  269. }
  270. }