'use strict'; const Service = require('egg').Service; class CompanyService extends Service { async list() { return this.app.mysql.select("ins_company", {columns:["id", "full_name"]}); } async listProd( cmpId ) { return this.app.mysql.select("ins_company_prod", {where:{cmpy_id: cmpId}, columns:["id", "name","letter"]}); } async listPage(pageNo, pageSize, short_name ) { let mysql = this.app.mysql; let where = ""; if( short_name ) { where = ` where short_name like '%${short_name}%' OR full_name like '%${short_name}%'`; } let total = await mysql.query("select count(*) as count from ins_company" + where); total = total[0]['count']; let ret = { records: [], "total": total, "size": pageSize, "current":pageNo, "orders":[], "searchCount":true, "pages": Math.ceil(total * 1.0 / pageSize) }; let list = await mysql.query("select * from ins_company" + where + ` limit ${(pageNo-1)* pageSize},${pageSize}`); ret.records = list; return ret; } async listProdPage(pageNo, pageSize, name, compy_id) { let mysql = this.app.mysql; let where = ""; if( name ) { where = ` where name like '%${name}%'`; } if( compy_id ) { where = name ? where + ` and compy_id=${compy_id}`: ` where compy_id=${compy_id}`; } let total = await mysql.query("select count(*) as count from ins_company_prod" + where); total = total[0]['count']; let ret = { records: [], "total": total, "size": pageSize, "current":pageNo, "orders":[], "searchCount":true, "pages": Math.ceil(total * 1.0 / pageSize) }; 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}`); ret.records = list; return ret; } async deleteCompany( id ) { //判断有没有产品关联当前公司 let mysql = this.app.mysql; let prod = await mysql.get("ins_company_prod",{cmpy_id: id}); if( prod ) throw "有产品关联公司,不能删除"; return await mysql.delete("ins_company", {id}); } async deleteProd( id ) { //判断有没有产品关联当前公司 let mysql = this.app.mysql; let prod = await mysql.get("instance",{prod_id: id}); if( prod ) throw "有保单关联公司,不能删除"; return await mysql.delete("ins_company_prod", {id}); } async addCompany( data ) { let mysql = this.app.mysql; return await mysql.insert("ins_company",data); } async addProd( data ) { let mysql = this.app.mysql; return await mysql.insert("ins_company_prod",data); } async editCompany( data ) { let mysql = this.app.mysql; let ret = await mysql.update("ins_company",data, {id: data.id}); return ret; } async editProd( data ) { let mysql = this.app.mysql; return await mysql.update("ins_company_prod",data, {id: data.id}); } } module.exports = CompanyService;