company.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. class CompanyService extends Service {
  4. async list() {
  5. return this.app.mysql.select("ins_company", {columns:["id", "full_name"]});
  6. }
  7. async listProd( cmpId ) {
  8. return this.app.mysql.select("ins_company_prod", {where:{cmpy_id: cmpId}, columns:["id", "name","letter"]});
  9. }
  10. async listPage(pageNo, pageSize, short_name ) {
  11. let mysql = this.app.mysql;
  12. let where = "";
  13. if( short_name ) {
  14. where = ` where short_name like '%${short_name}%' OR full_name like '%${short_name}%'`;
  15. }
  16. let total = await mysql.query("select count(*) as count from ins_company" + where);
  17. total = total[0]['count'];
  18. let ret = {
  19. records: [],
  20. "total": total,
  21. "size": pageSize,
  22. "current":pageNo,
  23. "orders":[],
  24. "searchCount":true,
  25. "pages": Math.ceil(total * 1.0 / pageSize)
  26. };
  27. let list = await mysql.query("select * from ins_company" + where + ` limit ${(pageNo-1)* pageSize},${pageSize}`);
  28. ret.records = list;
  29. return ret;
  30. }
  31. async listProdPage(pageNo, pageSize, name, compy_id) {
  32. let mysql = this.app.mysql;
  33. let where = "";
  34. if( name ) {
  35. where = ` where name like '%${name}%'`;
  36. }
  37. if( compy_id ) {
  38. where = name ? where + ` and compy_id=${compy_id}`: ` where compy_id=${compy_id}`;
  39. }
  40. let total = await mysql.query("select count(*) as count from ins_company_prod" + where);
  41. total = total[0]['count'];
  42. let ret = {
  43. records: [],
  44. "total": total,
  45. "size": pageSize,
  46. "current":pageNo,
  47. "orders":[],
  48. "searchCount":true,
  49. "pages": Math.ceil(total * 1.0 / pageSize)
  50. };
  51. let list = await mysql.query("select prod.id, prod.name, prod.cmpy_id, cmp.short_name as cmpy_name from ins_company_prod as prod inner join ins_company as cmp on cmp.id=prod.cmpy_id" + where + ` limit ${(pageNo-1)* pageSize},${pageSize}`);
  52. ret.records = list;
  53. return ret;
  54. }
  55. async deleteCompany( id ) {
  56. //判断有没有产品关联当前公司
  57. let mysql = this.app.mysql;
  58. let prod = await mysql.get("ins_company_prod",{cmpy_id: id});
  59. if( prod ) throw "有产品关联公司,不能删除";
  60. return await mysql.delete("ins_company", {id});
  61. }
  62. async deleteProd( id ) {
  63. //判断有没有产品关联当前公司
  64. let mysql = this.app.mysql;
  65. let prod = await mysql.get("instance",{prod_id: id});
  66. if( prod ) throw "有保单关联公司,不能删除";
  67. return await mysql.delete("ins_company_prod", {id});
  68. }
  69. async addCompany( data ) {
  70. let mysql = this.app.mysql;
  71. return await mysql.insert("ins_company",data);
  72. }
  73. async addProd( data ) {
  74. let mysql = this.app.mysql;
  75. return await mysql.insert("ins_company_prod",data);
  76. }
  77. async editCompany( data ) {
  78. let mysql = this.app.mysql;
  79. let ret = await mysql.update("ins_company",data, {id: data.id});
  80. return ret;
  81. }
  82. async editProd( data ) {
  83. let mysql = this.app.mysql;
  84. return await mysql.update("ins_company_prod",data, {id: data.id});
  85. }
  86. }
  87. module.exports = CompanyService;