import { Provide } from '@midwayjs/decorator'; import { InjectEntityModel } from '@midwayjs/typegoose'; import { ReturnModelType } from '@typegoose/typegoose'; import { BaseService } from 'free-midway-component'; import { OrderDetail } from '../entity/OrderDetail.entity'; import { Good } from '../entity/Good.entity'; import { Specs } from '../entity/Specs.entity'; import { User } from '../entity/User.entity'; import _ = require('lodash'); type modelType = ReturnModelType; @Provide() export class OrderDetailService extends BaseService { @InjectEntityModel(OrderDetail) model: modelType; @InjectEntityModel(Good) goodModel: ReturnModelType; @InjectEntityModel(Specs) specModel: ReturnModelType; @InjectEntityModel(User) userModel: ReturnModelType; async queryInfo(filter): Promise { const { skip = 0, limit, s_leader, s_accounting, c_leader, c_accounting, goods, status, ...info } = filter; let list: any = []; let total: any = 0; if (s_leader) info.s_leader = { $elemMatch: { _id: s_leader } }; else if (s_accounting) info.s_accounting = { $elemMatch: { _id: s_accounting } }; if (c_leader) info.c_leader = { $elemMatch: { _id: c_leader } }; else if (c_accounting) info.c_accounting = { $elemMatch: { _id: c_accounting } }; if (goods) { const arr = await this.goodModel .findOne({ name: { $regex: goods, }, }) .lean(); if (arr) info.good = arr._id; } if (status) { if (status === '0' && s_accounting) info.$or = [{ status: '0' }, { status: '20' }]; else if (status === '2' && s_leader) info.$or = [{ status: '2' }, { status: '30' }]; else if (status === '4' && info.supplier) info.$or = [{ status: '4' }, { status: '5' }, { status: '6' }]; else if (status === '6' && c_accounting) info.$or = [{ status: '6' }, { status: '40' }]; else if (status === '7' && c_leader) info.$or = [{ status: '7' }, { status: '50' }]; else info.status = status; } list = await this.model.find(info).skip(skip).limit(limit).lean(); total = await this.model.count(info); for (const val of list) { let res; res = await this.goodModel.findById(val.good).lean(); val.good_name = res.name; val.good_file = res.file[0]; res = await this.specModel.findById(val.spec).lean(); val.spec_name = res.name; val.spec_file = res.file[0]; res = await this.userModel.findById(val.user).lean(); val.user_name = res.name; val.user_role = res.role; res = await this.userModel.findById(val.supplier).lean(); val.supplier_name = res.name; } return { list, total }; } async money(filter): Promise { const { skip = 0, limit, goods, ...info } = filter; let list: any = []; let total: any = 0; if (goods) { const arr = await this.goodModel .findOne({ name: { $regex: goods, }, }) .lean(); if (arr) info.good = arr._id; } info.$or = [ { status: '6' }, { status: '7' }, { status: '-7' }, { status: '8' }, { status: '-8' }, ]; list = await this.model.find(info).skip(skip).limit(limit).lean(); total = await this.model.count(info); for (const val of list) { let res; res = await this.goodModel.findById(val.good).lean(); val.good_name = res.name; val.good_file = res.file[0]; res = await this.specModel.findById(val.spec).lean(); val.spec_name = res.name; val.spec_file = res.file[0]; res = await this.userModel.findById(val.user).lean(); val.user_name = res.name; val.user_role = res.role; res = await this.userModel.findById(val.supplier).lean(); val.supplier_name = res.name; } return { list, total }; } // 详情 async detail(id) { const res = await this.model.findById(id).lean(); let arr; const info: any = {}; arr = await this.goodModel.findById(res.good).lean(); info.good_name = arr.name; info.good_type = arr.type; info.good_file = arr.file; arr = await this.specModel.findById(res.spec).lean(); info.spec_name = arr.name; info.spec_file = arr.file; arr = await this.userModel.findById(res.user).lean(); info.user_name = arr.name; info.user_tel = arr.tel; info.user_role = arr.role; arr = await this.userModel.findById(res.supplier).lean(); info.supplier_name = arr.name; if (res.s_leader && res.s_leader.length > 0) { const sleader = []; for (const val of res.s_leader) { arr = await this.userModel.findById(val._id).lean(); sleader.push(arr.name); } info.sleader_name = sleader.toString(); } if (res.s_accounting && res.s_accounting.length > 0) { const saccounting = []; for (const val of res.s_accounting) { arr = await this.userModel.findById(val._id).lean(); saccounting.push(arr.name); } info.saccounting_name = saccounting.toString(); } if (res.c_leader && res.c_leader.length > 0) { const cleader = []; for (const val of res.c_leader) { arr = await this.userModel.findById(val._id).lean(); cleader.push(arr.name); } info.cleader_name = cleader.toString(); } if (res.c_accounting && res.c_accounting.length > 0) { const caccounting = []; for (const val of res.c_accounting) { arr = await this.userModel.findById(val._id).lean(); caccounting.push(arr.name); } info.caccounting_name = caccounting.toString(); } const result = _.assign(info, res); return result; } // 修改 async update(id, body): Promise { const { c_accounting, s_accounting, c_leader, s_leader, status, ...info } = body; const res = await this.model.findById(id); let arr; let result; let yes; let no; if (s_accounting) { const saccounting = []; for (const val of res.s_accounting) { const form: any = { _id: val._id }; if (val._id === s_accounting) { if (status === '2') form.status = '1'; else form.status = '-1'; } else form.status = val.status || '0'; saccounting.push(form); } info.s_accounting = saccounting; await this.model.updateOne({ _id: id }, info); arr = await this.model.findById(id); yes = arr.s_accounting.every(f => f.status === '1'); no = arr.s_accounting.every(f => f.status === '-1'); if (yes) { result = this.model.updateOne({ _id: id }, { status: '2' }); } else { if (no) result = this.model.updateOne({ _id: id }, { status: '-2' }); else result = this.model.updateOne({ _id: id }, { status: '20' }); } } else if (c_accounting) { const caccounting = []; for (const val of res.c_accounting) { const form: any = { _id: val._id }; if (val._id === c_accounting) { if (status === '7') form.status = '1'; else form.status = '-1'; } else form.status = val.status || '0'; caccounting.push(form); } info.c_accounting = caccounting; await this.model.updateOne({ _id: id }, info); arr = await this.model.findById(id); yes = arr.c_accounting.every(f => f.status === '1'); no = arr.c_accounting.every(f => f.status === '-1'); if (yes) { result = this.model.updateOne({ _id: id }, { status: '7' }); } else { if (no) result = this.model.updateOne({ _id: id }, { status: '-7' }); else result = this.model.updateOne({ _id: id }, { status: '40' }); } } else if (s_leader) { const sleader = []; for (const val of res.s_leader) { const form: any = { _id: val._id }; if (val._id === s_leader) { if (status === '4') form.status = '1'; else form.status = '-1'; } else form.status = val.status || '0'; sleader.push(form); } info.s_leader = sleader; await this.model.updateOne({ _id: id }, info); arr = await this.model.findById(id); yes = arr.s_leader.every(f => f.status === '1'); no = arr.s_leader.every(f => f.status === '-1'); if (yes) { result = this.model.updateOne({ _id: id }, { status: '4' }); } else { if (no) result = this.model.updateOne({ _id: id }, { status: '-3' }); else result = this.model.updateOne({ _id: id }, { status: '30' }); } } else if (c_leader) { const cleader = []; for (const val of res.c_leader) { const form: any = { _id: val._id }; if (val._id === c_leader) { if (status === '8') form.status = '1'; else form.status = '-1'; } else form.status = val.status || '0'; cleader.push(form); } info.c_leader = cleader; await this.model.updateOne({ _id: id }, info); arr = await this.model.findById(id); yes = arr.c_leader.every(f => f.status === '1'); no = arr.c_leader.every(f => f.status === '-1'); if (yes) { result = this.model.updateOne({ _id: id }, { status: '8' }); } else { if (no) result = this.model.updateOne({ _id: id }, { status: '-8' }); else result = this.model.updateOne({ _id: id }, { status: '50' }); } } else result = this.model.updateOne({ _id: id }, { status: status }); return result; } }