zs hai 1 ano
pai
achega
23ab6b8e12
Modificáronse 2 ficheiros con 125 adicións e 11 borrados
  1. 3 3
      src/controller/OrderDetail.controller.ts
  2. 122 8
      src/service/OrderDetail.service.ts

+ 3 - 3
src/controller/OrderDetail.controller.ts

@@ -16,7 +16,7 @@ import {
   FVO_OrderDetail,
   QDTO_OrderDetail,
   QVO_OrderDetail,
-  UDTO_OrderDetail,
+  // UDTO_OrderDetail,
   UVAO_OrderDetail,
 } from '../interface/OrderDetail.interface';
 import { ApiResponse, ApiTags, ApiQuery } from '@midwayjs/swagger';
@@ -89,8 +89,8 @@ export class OrderDetailController extends BaseController {
   @Post('/:id')
   @Validate()
   @ApiResponse({ type: UVAO_OrderDetail })
-  async update(@Param('id') id: string, @Body() body: UDTO_OrderDetail) {
-    const result = await this.service.updateOne(id, body);
+  async update(@Param('id') id: string, @Body() body: any) {
+    const result = await this.service.update(id, body);
     return result;
   }
 

+ 122 - 8
src/service/OrderDetail.service.ts

@@ -31,14 +31,17 @@ export class OrderDetailService extends BaseService<modelType> {
       c_leader,
       c_accounting,
       goods,
+      status,
       ...info
     } = filter;
     let list: any = [];
     let total: any = 0;
-    if (s_leader) info.s_leader = { $in: [s_leader] };
-    else if (s_accounting) info.s_accounting = { $in: [s_accounting] };
-    if (c_leader) info.c_leader = { $in: [c_leader] };
-    else if (c_accounting) info.c_accounting = { $in: [c_accounting] };
+    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({
@@ -49,6 +52,20 @@ export class OrderDetailService extends BaseService<modelType> {
         .lean();
       if (arr) info.good = arr._id;
     }
+    if (status) {
+      if (status === '0') {
+        info.$or = [{ status: '0' }, { status: '20' }];
+      }
+      if (status === '2') {
+        info.$or = [{ status: '2' }, { status: '30' }];
+      }
+      if (status === '6') {
+        info.$or = [{ status: '6' }, { status: '40' }];
+      }
+      if (status === '7') {
+        info.$or = [{ status: '7' }, { status: '50' }];
+      }
+    }
     list = await this.model.find(info).skip(skip).limit(limit).lean();
     total = await this.model.count(info);
     for (const val of list) {
@@ -127,7 +144,7 @@ export class OrderDetailService extends BaseService<modelType> {
     if (res.s_leader && res.s_leader.length > 0) {
       const sleader = [];
       for (const val of res.s_leader) {
-        arr = await this.userModel.findById(val).lean();
+        arr = await this.userModel.findById(val._id).lean();
         sleader.push(arr.name);
       }
       info.sleader_name = sleader.toString();
@@ -135,7 +152,7 @@ export class OrderDetailService extends BaseService<modelType> {
     if (res.s_accounting && res.s_accounting.length > 0) {
       const saccounting = [];
       for (const val of res.s_accounting) {
-        arr = await this.userModel.findById(val).lean();
+        arr = await this.userModel.findById(val._id).lean();
         saccounting.push(arr.name);
       }
       info.saccounting_name = saccounting.toString();
@@ -143,7 +160,7 @@ export class OrderDetailService extends BaseService<modelType> {
     if (res.c_leader && res.c_leader.length > 0) {
       const cleader = [];
       for (const val of res.c_leader) {
-        arr = await this.userModel.findById(val).lean();
+        arr = await this.userModel.findById(val._id).lean();
         cleader.push(arr.name);
       }
       info.cleader_name = cleader.toString();
@@ -151,7 +168,7 @@ export class OrderDetailService extends BaseService<modelType> {
     if (res.c_accounting && res.c_accounting.length > 0) {
       const caccounting = [];
       for (const val of res.c_accounting) {
-        arr = await this.userModel.findById(val).lean();
+        arr = await this.userModel.findById(val._id).lean();
         caccounting.push(arr.name);
       }
       info.caccounting_name = caccounting.toString();
@@ -159,4 +176,101 @@ export class OrderDetailService extends BaseService<modelType> {
     const result = _.assign(info, res);
     return result;
   }
+
+  // 修改
+  async update(id, body): Promise<string> {
+    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;
+  }
 }