4
0

samplessourceService.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package samplessource
  2. import (
  3. "fmt"
  4. "github.com/tealeg/xlsx"
  5. "os"
  6. "time"
  7. //"fmt"
  8. "strconv"
  9. . "dashoo.cn/backend/api/mydb"
  10. "dashoo.cn/utils"
  11. . "dashoo.cn/utils/db"
  12. "github.com/go-xorm/xorm"
  13. )
  14. type SamplesSourceService struct {
  15. MyServiceBase
  16. }
  17. func GetSamplesSourceService(xormEngine *xorm.Engine) *SamplesSourceService {
  18. s := new(SamplesSourceService)
  19. s.DBE = xormEngine
  20. return s
  21. }
  22. //获取扩展字段列
  23. func (s *SamplesSourceService) QueryZBackList(acccode string) []String_Id {
  24. List := make([]String_Id, 0)
  25. sql := "select distinct FieldName Id from DonorsNoteItem where FieldNo>10 and AccCode ='" + acccode + "' order by Id "
  26. utils.DBE.Sql(sql).Find(&List)
  27. return List
  28. }
  29. //判断委托单号是否存在,返回总数
  30. func (s *SamplesSourceService) GetInnerNo(maintable string, where string) int64 {
  31. var sql string
  32. var total int64
  33. sql = `select count(*) from ` + maintable + ` where ` + where
  34. resultsSlice, err := s.DBE.Query(sql)
  35. LogError(err)
  36. if len(resultsSlice) > 0 {
  37. results := resultsSlice[0]
  38. for _, value := range results {
  39. total, err = strconv.ParseInt(string(value), 10, 64)
  40. LogError(err)
  41. break
  42. }
  43. }
  44. return total
  45. }
  46. //获取样本来源信息用于导出
  47. func (s *SamplesSourceService) GetSampleSourceInfo(donors, testmain, donorsdetail, where string) []DonorsInfoWithDetail {
  48. if where == "" {
  49. where = " 1=1 "
  50. }
  51. var sql string
  52. sql = `select * from ( select a.AccCode,a.Id,a.IdCard,a.InnerNo,a.Name,a.Sex,a.CompanyName,a.Duty,a.Birthday,a.Age,a.Telephone,a.Email,a.Nation,a.HomeAddress,a.IllnessName,a.Remark, b.InspectionNum, group_concat(c.PathologicalNum) as PathologicalNum
  53. from ` + donors + ` a
  54. left join ` + testmain + ` b on a.Id=b.SourceId
  55. left join ` + donorsdetail + ` c on c.ParentId=a.Id
  56. where ` + where + ` group by a.Id,a.IdCard,a.Name,b.InspectionNum) as m `
  57. List := make([]DonorsInfoWithDetail, 0)
  58. utils.DBE.Sql(sql).Find(&List)
  59. return List
  60. }
  61. //样本源导入--动物
  62. func (s *SamplesSourceService) InsertExcelAnimal(excelpath, tablename, accode, createby string, createuserid int) (err error, warn string) {
  63. //svcdnote := extend.GetExtendService(utils.DBE)
  64. //notecount := svcdnote.GetNoteCount(accode) //获取扩展项目count
  65. //wherenote := " AccCode='" + accode + "'"
  66. //notelist := svcdnote.GetDNoteItemList(wherenote) //获取扩展项目List
  67. dir, _ := os.Getwd()
  68. excelpath = dir + excelpath
  69. xlFile, _ := xlsx.OpenFile(excelpath)
  70. var warn1 string
  71. fmt.Println("ssdsdsd",xlFile.Sheets)
  72. for sheetindex, sheet := range xlFile.Sheets { //循环页数
  73. sheetrows := len(xlFile.Sheets[sheetindex].Rows)
  74. for rowindex, row := range sheet.Rows { //循环行数
  75. notefieldstr := ""
  76. notevaluestr := ""
  77. if rowindex == 0 {
  78. continue
  79. }
  80. if row.Cells == nil || len(row.Cells) < 2 { //少于两列退出姓名不能为空
  81. break
  82. }
  83. basestation := new(AnimalInfo)
  84. for cellindex, cell := range row.Cells { //循环列数
  85. if cell.String() != "" {
  86. if cellindex == 0 {
  87. basestation.ProvinceName = cell.String()
  88. } else if cellindex == 1 {
  89. if cell.String() == "" {
  90. basestation.Name = "其他"
  91. basestation.Genus = "其他"
  92. } else {
  93. basestation.Name = cell.String()
  94. basestation.Genus = cell.String()
  95. }
  96. } else if cellindex == 2 {
  97. basestation.CityName = cell.String()
  98. } else if cellindex == 3 {
  99. basestation.StreetName = cell.String()
  100. } else if cellindex == 4 {
  101. basestation.Address = cell.String()
  102. } else if cellindex == 5 {
  103. aa, _ := utils.StrTo(cell.Value).Float64()
  104. basestation.SurveyDate = xlsx.TimeFromExcelTime(aa, false)
  105. } else if cellindex == 6 {
  106. basestation.InnerNo = cell.String()
  107. } else if cellindex == 7 {
  108. basestation.Amount, _ = utils.StrTo(cell.String()).Int()
  109. } else if cellindex == 8 {
  110. basestation.Longitude = cell.String()
  111. } else if cellindex == 9 {
  112. basestation.Latitude = cell.String()
  113. } else if cellindex == 10 {
  114. basestation.Altitude = cell.String()
  115. } else if cellindex == 11 {
  116. basestation.AreaCode = cell.String()
  117. } else if cellindex == 12 {
  118. basestation.Province = cell.String()
  119. } else if cellindex == 13 {
  120. basestation.City = cell.String()
  121. } else if cellindex == 14 {
  122. basestation.Street = cell.String()
  123. } else if cellindex == 15 {
  124. basestation.ZBack17 = cell.String()
  125. } else if cellindex == 16 {
  126. basestation.ZBack11 = cell.String()
  127. }
  128. }
  129. }
  130. basestation.AccCode = accode
  131. basestation.Unit = "群"
  132. basestation.ProjectId = 0
  133. basestation.ProjectName = "技术体系"
  134. basestation.CreateOn = time.Now()
  135. basestation.CreateBy = createby
  136. basestation.CreateUserId = createuserid
  137. sqlfield := "AccCode,Genus,InnerNo,Name,Amount,Unit,Province,ProvinceName,City,CityName,"
  138. sqlfield = sqlfield + "Street,StreetName,Address,AreaCode,Longitude,Latitude,Altitude,SurveyDate"
  139. sqlfield = sqlfield + "ProjectId,ProjectName,"
  140. sqlfield = sqlfield + "CreateUserId,CreateBy,CreateOn"
  141. sqlfield = sqlfield + notefieldstr
  142. sqlvalue := "'" + basestation.AccCode + "','" + basestation.Genus + "','" + basestation.InnerNo + "','" + basestation.Name + "'," + utils.ToStr(basestation.Amount) + ",'" + basestation.Unit + "',"
  143. sqlvalue = sqlvalue + "'" + basestation.Province + "','" + basestation.ProvinceName + "','" + basestation.City + "','" + basestation.CityName + "','"+ basestation.Street + "',"
  144. sqlvalue = sqlvalue + "'" + basestation.StreetName + "','" + basestation.Address + "','" + basestation.AreaCode + "','" + basestation.Longitude + "','" + basestation.Latitude + "',"
  145. sqlvalue = sqlvalue + "'" + basestation.Altitude + "','" + basestation.SurveyDate.Format("2006-1-2 15:4:5") + "'," + utils.ToStr(basestation.ProjectId) + ",'" + basestation.ProjectName + "',"
  146. sqlvalue = sqlvalue + "" + utils.ToStr(basestation.CreateUserId) + ",'" + basestation.CreateBy + "','" + "now(),now()'"
  147. sqlvalue = sqlvalue + notevaluestr
  148. s.AddLine(tablename, sqlfield, sqlvalue)
  149. if rowindex == (sheetrows) {
  150. s.AddLine(tablename, sqlfield, sqlvalue)
  151. }
  152. }
  153. }
  154. return err, warn1
  155. }
  156. //数据写入数据库
  157. func (s *SamplesSourceService) AddLine(tableName, fieldName, fieldValue string) error {
  158. sql := "insert into " + tableName + "(" + fieldName + ") values(" + fieldValue + ")"
  159. fmt.Println(sql)
  160. _, err := s.DBE.Exec(sql)
  161. return err
  162. }