|
|
@@ -0,0 +1,245 @@
|
|
|
+package company
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "strconv"
|
|
|
+
|
|
|
+ "dashoo.cn/utils"
|
|
|
+ . "dashoo.cn/utils/db"
|
|
|
+ "github.com/go-xorm/xorm"
|
|
|
+)
|
|
|
+
|
|
|
+type BaseCompanyService struct {
|
|
|
+ ServiceBase
|
|
|
+}
|
|
|
+
|
|
|
+func GetCompanyService(xormEngine *xorm.Engine) *BaseCompanyService {
|
|
|
+ s := new(BaseCompanyService)
|
|
|
+ s.DBE = xormEngine
|
|
|
+ return s
|
|
|
+}
|
|
|
+
|
|
|
+//提交 //传入f,s 两个参数 返回 comid
|
|
|
+func (s *BaseCompanyService) AddCompany(fullname, shortname string) (err error, comacccode string) {
|
|
|
+ model := Base_Company{Fullname: fullname, ShortName: shortname}
|
|
|
+ //model := Base_User{Username: user.Username}
|
|
|
+
|
|
|
+ has, _ := s.DBE.Get(&model)
|
|
|
+ if has {
|
|
|
+ err = utils.NewError("ErrorUserNameExist")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ for i := 0; i < 10000; i++ {
|
|
|
+ acccode := "s" + utils.GetRandomString(4)
|
|
|
+ model1 := Base_Company{AccCode: acccode}
|
|
|
+ has1, _ := s.DBE.Get(&model1)
|
|
|
+ if has1 {
|
|
|
+ continue
|
|
|
+ } else {
|
|
|
+ model.AccCode = acccode
|
|
|
+ _, err = s.DBE.Insert(&model)
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ comacccode = model.AccCode
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+//根据Domain,ID查企业ID
|
|
|
+func (s *BaseCompanyService) GetCompanyid(where string) (ids []string) {
|
|
|
+ var idList []Id_Str
|
|
|
+ s.DBE.Sql(` select Id from Base_Company ` + where).Find(&idList)
|
|
|
+ ids = s.GetIdsFromId_StrList(idList)
|
|
|
+ if len(ids) == 0 {
|
|
|
+ ids = append(ids, "-1")
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+//判断是否存在短域名
|
|
|
+func (s *BaseCompanyService) IsexistComcode(code string) bool {
|
|
|
+ company := new(Base_Company)
|
|
|
+ has, _ := s.DBE.Where("ShortName=?", code).Get(company)
|
|
|
+ return has
|
|
|
+}
|
|
|
+
|
|
|
+//获取acccode
|
|
|
+func (s *BaseCompanyService) GetAcccodeByComcode(code string) int {
|
|
|
+ company := new(Base_Company)
|
|
|
+ s.DBE.Where("ShortName=?", code).Get(company)
|
|
|
+ return company.Id
|
|
|
+}
|
|
|
+func (s *BaseCompanyService) GetFullnameByAccode(Accode string) string {
|
|
|
+ var BaseCompany Base_Company
|
|
|
+ s.DBE.Where("Id=?", Accode).Get(&BaseCompany) //都得到的是整条语句
|
|
|
+ return BaseCompany.Fullname //取其中一个字段
|
|
|
+}
|
|
|
+
|
|
|
+//获取公司列表
|
|
|
+func (s *BaseCompanyService) GetCompanyTree(companytitle string) []Base_Companytree {
|
|
|
+ var tree []Base_Companytree
|
|
|
+ s.DBE.Sql(`select
|
|
|
+ 0 Id,-1 ParentId,'` + companytitle + `' Fullname union all
|
|
|
+ select Id,0 ParentId,Fullname from Base_Company order by Id`).Find(&tree)
|
|
|
+ return tree
|
|
|
+}
|
|
|
+
|
|
|
+func (s *BaseCompanyService) GetPagingEntitiesWithOrderSearch(pageIndex, itemsPerPage int64, order string, where string) (int64, []UserCompany) {
|
|
|
+ var err error
|
|
|
+ var total int64
|
|
|
+ //获取总记录数
|
|
|
+ sqlCount := `select count(*)
|
|
|
+ from Base_User a left join
|
|
|
+ Base_Company b on a.AccCode=b.Id where` + where + ""
|
|
|
+ var sql string
|
|
|
+
|
|
|
+ sql = `select a.Realname Realname,a.UserName Username,b.Fullname Companyname,a.Mobile,a.Telephone,a.Email
|
|
|
+ from Base_User a left join
|
|
|
+ Base_Company b on a.AccCode=b.Id where ` + where + ` order by ` + order + ` limit ` + utils.ToStr((pageIndex-1)*itemsPerPage) + "," + utils.ToStr(itemsPerPage)
|
|
|
+
|
|
|
+ List := make([]UserCompany, 0)
|
|
|
+ utils.DBE.Sql(sql).Find(&List)
|
|
|
+ resultsSlice, err := s.DBE.Query(sqlCount)
|
|
|
+ LogError(err)
|
|
|
+
|
|
|
+ if len(resultsSlice) > 0 {
|
|
|
+ results := resultsSlice[0]
|
|
|
+ for _, value := range results {
|
|
|
+ total, err = strconv.ParseInt(string(value), 10, 64)
|
|
|
+ LogError(err)
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return total, List
|
|
|
+}
|
|
|
+
|
|
|
+func (s *BaseCompanyService) GetBindingTree(whereorg, wherecompany string) (tree []Base_Companytree) {
|
|
|
+ s.DBE.Sql(`select concat('o',Id) Id,concat('o',ParentId) ParentId,Fullname, 'true' IsParent,'false' Nocheck from Base_Organize
|
|
|
+ where ` + whereorg + `
|
|
|
+ union all
|
|
|
+ select Id,concat('o',OrganizeId) ParentId,Fullname, 'false' IsParent ,'false' Nocheck
|
|
|
+ from Base_Company where
|
|
|
+ ` + wherecompany).Find(&tree)
|
|
|
+ return
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+//撤销用户控制企业权限
|
|
|
+func (s *BaseCompanyService) GetEntityByUserid(userId string) (Base_Company, bool) {
|
|
|
+ var model Base_Company
|
|
|
+ sql := `select b.* from Base_User a
|
|
|
+ inner join Base_Company b on a.AccCode=b.Id
|
|
|
+ where a.Id=` + userId
|
|
|
+ has, _ := s.DBE.Sql(sql).Get(&model)
|
|
|
+ return model, has
|
|
|
+}
|
|
|
+
|
|
|
+func (s *BaseCompanyService) GetDevicesNumByOrg(whereorg, wherecompany string) int {
|
|
|
+ var num Id_Int
|
|
|
+ s.DBE.Sql(`select count(*) Id from Channels a
|
|
|
+ inner join Base_User b on a.CreateUserId=b.Id
|
|
|
+ inner join Base_Company c on b.AccCode=c.Id and ` + wherecompany + `
|
|
|
+ inner join Base_Organize d on c.OrganizeId=d.Id and
|
|
|
+ ` + whereorg).Get(&num)
|
|
|
+ return num.Id
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func (s *BaseCompanyService) GetDevicesNumByCompany(wherecompany string) int {
|
|
|
+ var num Id_Int
|
|
|
+ s.DBE.Sql(`select count(*) Id from Channels a
|
|
|
+ inner join Base_User b on a.CreateUserId=b.Id
|
|
|
+ inner join Base_Company c on b.AccCode=c.Id and ` + wherecompany).Get(&num)
|
|
|
+ return num.Id
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func (s *BaseCompanyService) GetIndexTree(whereorg, wherecompany string) (tree []Base_Indextree) {
|
|
|
+ s.DBE.Sql(`select concat('o',Id) Id,concat('o',ParentId) ParentId,Fullname, 'true' IsParent,'true' Nocheck,0 Longitude,0 Latitude,0 DeviceState from Base_Organize
|
|
|
+ where ` + whereorg + `
|
|
|
+ union all
|
|
|
+ select Id,concat('o',OrganizeId) ParentId,Fullname, 'false' IsParent ,'false' Nocheck,Longitude,Latitude,DeviceState
|
|
|
+ from Base_Company where
|
|
|
+ ` + wherecompany).Find(&tree)
|
|
|
+ return
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func (s *BaseCompanyService) GetIndexTree1(whereorg, wherecompany string) (tree []Base_Indextree1) {
|
|
|
+ s.DBE.Sql(`select concat('o',Id) Id,concat('o',ParentId) ParentId,Fullname from Base_Organize
|
|
|
+ where ` + whereorg + `
|
|
|
+ union all
|
|
|
+ select Id,concat('o',OrganizeId) ParentId,Fullname
|
|
|
+ from Base_Company where
|
|
|
+ ` + wherecompany).Find(&tree)
|
|
|
+ return
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func (s *BaseCompanyService) GetUidByCompanyid(companyid string) int {
|
|
|
+ var uid Id_Int
|
|
|
+ s.DBE.Sql("select Id from Base_User where CreateUserId='0' and AccCode=" + companyid + " limit 1").Get(&uid)
|
|
|
+ return uid.Id
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+// 表
|
|
|
+var (
|
|
|
+ biobank = []string{"DonorsInfo", "DonorsLog", "SamplesMain", "SamplesDetail",
|
|
|
+ "SamplesBusiness", "SamplesFileDetail", "SamplesLog", "SamplesAttachment",
|
|
|
+ "SampleSearchTemplate", "MaterialInfo", "MaterialBatchKC",
|
|
|
+ "MaterialStoreHouse", "MaterialCKDetail", "MaterialCKHead", "MaterialPDDetail",
|
|
|
+ "MaterialPDHead", "MaterialRKDetail", "MaterialRKHead", "DonorsInfoAttachment",
|
|
|
+ "DocumentInfo", "DocumentHistory"}
|
|
|
+ cellbank = []string{"DonorsInfo", "DonorsLog", "SamplesMain", "SamplesDetail",
|
|
|
+ "SamplesBusiness", "SamplesFileDetail", "SamplesLog", "SamplesAttachment",
|
|
|
+ "SampleSearchTemplate", "MaterialInfo", "MaterialBatchKC",
|
|
|
+ "MaterialStoreHouse", "MaterialCKDetail", "MaterialCKHead", "MaterialPDDetail",
|
|
|
+ "MaterialPDHead", "MaterialRKDetail", "MaterialRKHead", "Instrument",
|
|
|
+ "InstrumenMaintainLog", "InstrumenRunRecord", "Customer", "CellsContract", "CellsContractDetail",
|
|
|
+ "CellsCollection", "CellsCollectionDetail", "CellsCollectionSensors", "CellsPrepareTemplate",
|
|
|
+ "CellsPrepareTemplateStep", "CellsPrepareTemplateStepDetail",
|
|
|
+ "CellsPreparation", "CellsPreparationInfo", "CellsPreparationInfoDetail",
|
|
|
+ "DonorsInfoAttachment", "InstrumenMaintainLogAttachment", "CellsContractAttachment", "TestTemplate",
|
|
|
+ "CellsProductDelivery", "CellsProductDeliveryAttachment", "CellsProductDeliverySample", "CellsPreparationInfoDetailAttachment",
|
|
|
+ "CellsPreparationTrace", "DocumentInfo", "DocumentHistory"}
|
|
|
+ lims = []string{"TestSample", "TestSampleDetail", "TestSampleInfo",
|
|
|
+ "TestSampleRelation", "TestTransferOrders", "TestInfo", "TestInfoDetail",
|
|
|
+ "TestItemsList", "TestList", "TestListDetail", "TestLaboratory",
|
|
|
+ "TestQualityControl", "TestControlParameters", "TestControlParametersDetail", "TestApplication",
|
|
|
+ "TestPackageCategory", "TestPrintTemplate", "TestPackage", "TestPackageItems",
|
|
|
+ "Instrument", "InstrumenMaintainLog", "Instrumenstate", "DocumentInfo", "DocumentHistory"}
|
|
|
+)
|
|
|
+
|
|
|
+func (s *BaseCompanyService) CreateSampleDonorTable(acccode, source string) error {
|
|
|
+ var err error
|
|
|
+ switch source {
|
|
|
+ case "biobank":
|
|
|
+ for _, v := range biobank {
|
|
|
+ if v != "" {
|
|
|
+ table := acccode + v
|
|
|
+ sql := fmt.Sprintf("CREATE TABLE %v LIKE AAtemplate%v", table, v)
|
|
|
+ _, err = s.DBE.Exec(sql)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ case "cellbank":
|
|
|
+ for _, v := range cellbank {
|
|
|
+ if v != "" {
|
|
|
+ table := acccode + v
|
|
|
+ sql := fmt.Sprintf("CREATE TABLE %v LIKE AAtemplate%v", table, v)
|
|
|
+ _, err = s.DBE.Exec(sql)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ case "lims":
|
|
|
+ for _, v := range lims {
|
|
|
+ if v != "" {
|
|
|
+ table := acccode + v
|
|
|
+ sql := fmt.Sprintf("CREATE TABLE %v LIKE AAtemplate%v", table, v)
|
|
|
+ _, err = s.DBE.Exec(sql)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return err
|
|
|
+}
|