4
0

samplesinfoService.go 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  1. package samplesinfo
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/astaxie/beego"
  10. "dashoo.cn/backend/api/business/baseUser"
  11. "dashoo.cn/backend/api/business/donorsinfo"
  12. "dashoo.cn/backend/api/business/sampleimporttemplate"
  13. // "dashoo.cn/backend/api/business/samplenoteitem"
  14. "dashoo.cn/backend/api/business/samplesbusiness"
  15. "dashoo.cn/backend/api/business/sampletype"
  16. "dashoo.cn/backend/api/business/shelfset"
  17. . "dashoo.cn/backend/api/models"
  18. . "dashoo.cn/backend/api/mydb"
  19. "dashoo.cn/business2/userRole"
  20. "dashoo.cn/utils"
  21. . "dashoo.cn/utils/db"
  22. "github.com/go-xorm/xorm"
  23. "github.com/tealeg/xlsx"
  24. )
  25. type SamplesInfoService struct {
  26. MyServiceBase
  27. }
  28. func GetSamplesInfoService(xormEngine *xorm.Engine) *SamplesInfoService {
  29. s := new(SamplesInfoService)
  30. s.DBE = xormEngine
  31. return s
  32. }
  33. func (s *SamplesInfoService) GetPagingEntitiesWithOrderSearch(acccode string, pageIndex, itemsPerPage int64, order, where string) (int64, []SamplesInfoList) {
  34. var err error
  35. var total int64
  36. if order != "" {
  37. order = " order by " + order
  38. }
  39. //获取总记录数
  40. if where == "" {
  41. where = "1=1"
  42. }
  43. tbldetail := acccode + SamplesDetailtbName
  44. tblmain := acccode + SamplesMaintbName
  45. tbldonor := acccode + DonorstbName
  46. groupdetail := acccode + GroupDetailName
  47. sqlCount := " select count(*) from " + tbldetail + " a left join " + tblmain + " b on a.SampleCode = b.SampleCode left join " + tbldonor + " c on b.SourceId = c.Id left join Shelf d on a.ShelfId=d.Id left join Box e on a.BoxId=e.Id left join Equipment f on a.EquipmentId=f.Id left join " + groupdetail + " g on g.SampleId=a.Id where " + where
  48. fmt.Println(sqlCount)
  49. var sql string
  50. sql = ` select a.*,b.*,c.FamilyState,c.ClinicNum,d.XStation ShelfX,d.YStation ShelfY,d.Code ShelfCode,e.XStation BoxX,e.YStation BoxY,e.Code BoxCode,f.Id Eid,f.Name EName,f.Code ECode,g.GroupId,g.GroupName from ` + tbldetail + ` a
  51. left join ` + tblmain + ` b on a.SampleCode = b.SampleCode
  52. left join ` + tbldonor + ` c on b.SourceId = c.Id
  53. left join Shelf d on a.ShelfId=d.Id
  54. left join Box e on a.BoxId=e.Id
  55. left join Equipment f on a.EquipmentId=f.Id
  56. left join ` + groupdetail + ` g on g.SampleId=a.Id
  57. where ` + where + order + `
  58. limit ` + utils.ToStr((pageIndex-1)*itemsPerPage) + "," + utils.ToStr(itemsPerPage) + ""
  59. fmt.Println(sql)
  60. List := make([]SamplesInfoList, 0)
  61. utils.DBE.Sql(sql).Find(&List)
  62. resultsSlice, err := s.DBE.Query(sqlCount)
  63. LogError(err)
  64. if len(resultsSlice) > 0 {
  65. results := resultsSlice[0]
  66. for _, value := range results {
  67. total, err = strconv.ParseInt(string(value), 10, 64)
  68. LogError(err)
  69. break
  70. }
  71. }
  72. return total, List
  73. }
  74. func (s *SamplesInfoService) GetPagingEntitiesWithTb(acccode string, pageIndex, itemsPerPage int64, tbldonor, order, where string) (int64, []SamplesInfoList) {
  75. var err error
  76. var total int64
  77. if order != "" {
  78. order = " order by " + order
  79. }
  80. //获取总记录数
  81. if where == "" {
  82. where = "1=1"
  83. }
  84. tbldetail := acccode + SamplesDetailtbName
  85. tblmain := acccode + SamplesMaintbName
  86. groupdetail := acccode + GroupDetailName
  87. sqlCount := " select count(*) from " + tbldetail + " a left join " + tblmain + " b on a.SampleCode = b.SampleCode left join " + tbldonor + " c on b.SourceId = c.Id left join Shelf d on a.ShelfId=d.Id left join Box e on a.BoxId=e.Id left join Equipment f on a.EquipmentId=f.Id left join " + groupdetail + " g on g.SampleId=a.Id where " + where
  88. fmt.Println(sqlCount)
  89. var sql string
  90. sql = ` select a.*,b.*,c.Genus,concat(c.ProvinceName,c.CityName,c.StreetName,c.Address) as AddressName,c.SurveyDate,d.XStation ShelfX,d.YStation ShelfY,d.Code ShelfCode,e.XStation BoxX,e.YStation BoxY,e.Code BoxCode,f.Id Eid,f.Name EName,f.Code ECode,g.GroupId,g.GroupName from ` + tbldetail + ` a
  91. left join ` + tblmain + ` b on a.SampleCode = b.SampleCode
  92. left join ` + tbldonor + ` c on b.SourceId = c.Id
  93. left join Shelf d on a.ShelfId=d.Id
  94. left join Box e on a.BoxId=e.Id
  95. left join Equipment f on a.EquipmentId=f.Id
  96. left join ` + groupdetail + ` g on g.SampleId=a.Id
  97. where ` + where + order + `
  98. limit ` + utils.ToStr((pageIndex-1)*itemsPerPage) + "," + utils.ToStr(itemsPerPage) + ""
  99. fmt.Println(sql)
  100. List := make([]SamplesInfoList, 0)
  101. utils.DBE.Sql(sql).Find(&List)
  102. resultsSlice, err := s.DBE.Query(sqlCount)
  103. LogError(err)
  104. if len(resultsSlice) > 0 {
  105. results := resultsSlice[0]
  106. for _, value := range results {
  107. total, err = strconv.ParseInt(string(value), 10, 64)
  108. LogError(err)
  109. break
  110. }
  111. }
  112. return total, List
  113. }
  114. func (s *SamplesInfoService) GetPagingEntitiesWithDonorsTb(acccode string, pageIndex, itemsPerPage int64, tbldonor, order, where string) (int64, []SamplesInfoList) {
  115. var err error
  116. var total int64
  117. if order != "" {
  118. order = " order by " + order
  119. }
  120. //获取总记录数
  121. if where == "" {
  122. where = "1=1"
  123. }
  124. tbldetail := acccode + SamplesDetailtbName
  125. tblmain := acccode + SamplesMaintbName
  126. groupdetail := acccode + GroupDetailName
  127. sqlCount := " select count(*) from " + tbldetail + " a left join " + tblmain + " b on a.SampleCode = b.SampleCode left join " + tbldonor + " c on b.SourceId = c.Id left join Shelf d on a.ShelfId=d.Id left join Box e on a.BoxId=e.Id left join Equipment f on a.EquipmentId=f.Id left join " + groupdetail + " g on g.SampleId=a.Id where " + where
  128. fmt.Println(sqlCount)
  129. var sql string
  130. sql = ` select a.*,b.*,c.*,d.XStation ShelfX,d.YStation ShelfY,d.Code ShelfCode,e.XStation BoxX,e.YStation BoxY,e.Code BoxCode,f.Id Eid,f.Name EName,f.Code ECode,g.GroupId,g.GroupName from ` + tbldetail + ` a
  131. left join ` + tblmain + ` b on a.SampleCode = b.SampleCode
  132. left join ` + tbldonor + ` c on b.SourceId = c.Id
  133. left join Shelf d on a.ShelfId=d.Id
  134. left join Box e on a.BoxId=e.Id
  135. left join Equipment f on a.EquipmentId=f.Id
  136. left join ` + groupdetail + ` g on g.SampleId=a.Id
  137. where ` + where + order + `
  138. limit ` + utils.ToStr((pageIndex-1)*itemsPerPage) + "," + utils.ToStr(itemsPerPage) + ""
  139. fmt.Println(sql)
  140. List := make([]SamplesInfoList, 0)
  141. utils.DBE.Sql(sql).Find(&List)
  142. resultsSlice, err := s.DBE.Query(sqlCount)
  143. LogError(err)
  144. if len(resultsSlice) > 0 {
  145. results := resultsSlice[0]
  146. for _, value := range results {
  147. total, err = strconv.ParseInt(string(value), 10, 64)
  148. LogError(err)
  149. break
  150. }
  151. }
  152. return total, List
  153. }
  154. //质控随机显示
  155. func (s *SamplesInfoService) GetPagingEntitiesSearch(acccode string, page, size string, order, where string) (int64, []SamplesInfoList) {
  156. var err error
  157. var total int64
  158. if order != "" {
  159. order = " order by " + order
  160. }
  161. //获取总记录数
  162. if where == "" {
  163. where = "1=1"
  164. }
  165. tblmain := acccode + SamplesMaintbName
  166. tbldetail := acccode + SamplesDetailtbName
  167. tbldonor := acccode + DonorstbName
  168. sqlCount := " select count(*) from " + tbldetail + " a left join " + tblmain + " b on a.SampleCode = b.SampleCode left join " + tbldonor + " c on b.SourceId = c.Id left join Shelf d on a.ShelfId=d.Id left join Box e on a.BoxId=e.Id left join Equipment f on a.EquipmentId=f.Id where " + where
  169. fmt.Println(sqlCount)
  170. var sql string
  171. sql = ` select a.*,b.*,c.FamilyState,c.ClinicNum,d.XStation ShelfX,d.YStation ShelfY,d.Code ShelfCode,e.XStation BoxX,e.YStation BoxY,e.Code BoxCode,f.Id Eid,f.Name EName,f.Code ECode from ` + tbldetail + ` a
  172. left join ` + tblmain + ` b on a.SampleCode = b.SampleCode
  173. left join ` + tbldonor + ` c on b.SourceId = c.Id
  174. left join Shelf d on a.ShelfId=d.Id
  175. left join Box e on a.BoxId=e.Id
  176. left join Equipment f on a.EquipmentId=f.Id where ` + where + order + `
  177. limit ` + page + `,` + size
  178. fmt.Println(sql)
  179. List := make([]SamplesInfoList, 0)
  180. utils.DBE.Sql(sql).Find(&List)
  181. resultsSlice, err := s.DBE.Query(sqlCount)
  182. LogError(err)
  183. if len(resultsSlice) > 0 {
  184. results := resultsSlice[0]
  185. for _, value := range results {
  186. total, err = strconv.ParseInt(string(value), 10, 64)
  187. LogError(err)
  188. break
  189. }
  190. }
  191. return total, List
  192. }
  193. func (s *SamplesInfoService) GetSamplesInfoListAllData(acccode string, order, where string) []SamplesInfoList {
  194. if order != "" {
  195. order = " order by " + order
  196. }
  197. //获取总记录数
  198. if where == "" {
  199. where = "1=1"
  200. }
  201. tblmain := acccode + SamplesMaintbName
  202. tbldetail := acccode + SamplesDetailtbName
  203. tbldonor := acccode + DonorstbName
  204. groupdetail := acccode + GroupDetailName
  205. var sql string
  206. sql = ` select a.*,b.*,c.FamilyState,c.ClinicNum,d.XStation ShelfX,d.YStation ShelfY,d.Code ShelfCode,e.XStation BoxX,e.YStation BoxY,e.Code BoxCode,f.Id Eid,f.Name EName,f.Code ECode,g.GroupName from ` + tbldetail + ` a
  207. left join ` + tblmain + ` b on a.SampleCode = b.SampleCode
  208. left join ` + tbldonor + ` c on b.SourceId = c.Id
  209. left join Shelf d on a.ShelfId=d.Id
  210. left join Box e on a.BoxId=e.Id
  211. left join Equipment f on a.EquipmentId=f.Id
  212. left join ` + groupdetail + ` g on a.Id = g.SampleId where ` + where + order
  213. fmt.Println(sql)
  214. List := make([]SamplesInfoList, 0)
  215. utils.DBE.Sql(sql).Find(&List)
  216. return List
  217. }
  218. func (s *SamplesInfoService) GetArchivedSamplesInfoListAllData(acccode string, order, where string) []SamplesInfoList {
  219. if order != "" {
  220. order = " order by " + order
  221. }
  222. //获取总记录数
  223. if where == "" {
  224. where = "1=1"
  225. }
  226. tblmain := acccode + SamplesMaintbName
  227. tbldetail := acccode + SamplesDetailFiletbName
  228. tbldonor := acccode + DonorstbName
  229. var sql string
  230. sql = ` select a.*,b.*,c.FamilyState from ` + tbldetail + ` a left join ` + tblmain +
  231. ` b on a.SampleCode = b.SampleCode left join ` + tbldonor + ` c on b.SourceId = c.Id where ` + where + order
  232. fmt.Println(sql)
  233. List := make([]SamplesInfoList, 0)
  234. utils.DBE.Sql(sql).Find(&List)
  235. return List
  236. }
  237. func (s *SamplesInfoService) QuerySampleEntity(acccode string, where string) (entity SamplesInfoShow) {
  238. tblmain := acccode + SamplesMaintbName
  239. tbldetail := acccode + SamplesDetailtbName
  240. sql := "select a.*,b.* from " + tbldetail + " a left join " + tblmain +
  241. " b on a.SampleCode = b.SampleCode where " + where
  242. s.DBE.Sql(sql).Get(&entity)
  243. return
  244. }
  245. //获取样本详情 --动物
  246. func (s *SamplesInfoService) QueryAnimalSampleEntity(acccode string, where string) (entity AnimalSamplesInfoShow) {
  247. tblmain := acccode + SamplesMaintbName
  248. tbldetail := acccode + SamplesDetailtbName
  249. sourcetb := acccode + AnimalInfoName
  250. sql := `select a.*,b.*,c.Genus,c.GenusName,concat(c.ProvinceName,c.CityName,c.StreetName,c.Address) as AddressName,c.InnerNo as SourceInner,c.Amount,c.Unit as SourceUni,c.ProjectName,c.Longitude,c.Latitude,c.Altitude,c.SurveyDate
  251. from ` + tbldetail + ` a left join
  252. ` + tblmain + ` b on a.SampleCode = b.SampleCode
  253. left join ` + sourcetb + ` c on b.SourceId = c.Id
  254. where ` + where
  255. s.DBE.Sql(sql).Get(&entity)
  256. return
  257. }
  258. //获取样本详情 2017-10-31新增,增加设备名等信息
  259. func (s *SamplesInfoService) QuerySampleDetailEntity(acccode string, where string) (entity SamplesInDetailfoShow) {
  260. tblmain := acccode + SamplesMaintbName
  261. tbldetail := acccode + SamplesDetailtbName
  262. sql := `select a.*,b.*,c.Name EName,c.Code ECode,d.Code ShelfCode,e.Code BoxCode,
  263. d.XStation ShelfX,d.YStation ShelfY,e.XStation BoxX,e.YStation BoxY
  264. from ` + tbldetail + ` a left join
  265. ` + tblmain + ` b on a.SampleCode = b.SampleCode
  266. inner join Equipment c on a.EquipmentId=c.Id
  267. inner join Shelf d on a.ShelfId=d.Id
  268. inner join Box e on a.BoxId = e.Id
  269. where ` + where
  270. s.DBE.Sql(sql).Get(&entity)
  271. return
  272. }
  273. func (s *SamplesInfoService) DeleteDetail(tbldetail, where string) error {
  274. sql := "update " + tbldetail + " set DeletionStateCode=1 where " + where
  275. _, err := s.DBE.Exec(sql)
  276. return err
  277. }
  278. func (s *SamplesInfoService) InsertExcelSamplesInput(excelpath, acccode, createby string, createuserid int) (err error, warn string) {
  279. //公共扩展list
  280. //svcdnote := samplenoteitem.GetSampleNoteItemService(utils.DBE)
  281. //wherenote := " AccCode='" + acccode + "'"
  282. //notecount := svcdnote.GetNoteCount(acccode) //获取扩展项目count
  283. //notelist := svcdnote.GetDNoteItemList(wherenote)
  284. tblmain := acccode + SamplesMaintbName
  285. tbldetail := acccode + SamplesDetailtbName
  286. tblfile := acccode + SamplesDetailFiletbName
  287. tblbuss := acccode + SamplesBusstbName
  288. //tbldonor := acccode + AnimalInfoName
  289. //tbldonor := acccode + DonorstbName
  290. //当是蜜蜂所用户时,取acccode + AnimalInfoName 动物表数据
  291. tbldonor := ""
  292. if(strings.EqualFold(acccode,"sBBo4")){
  293. tbldonor = acccode + AnimalInfoName
  294. }else{
  295. tbldonor = acccode + DonorstbName
  296. }
  297. var datadetail SamplesDetail
  298. var databuss samplesbusiness.SamplesBusiness //业务记录
  299. dir, _ := os.Getwd()
  300. flag := 0
  301. excelpath = dir + excelpath
  302. xlFile, err := xlsx.OpenFile(excelpath)
  303. if err != nil {
  304. beego.Error(err)
  305. return
  306. }
  307. svcdonors := donorsinfo.GetDonorsInfoService(utils.DBE)
  308. var donors_data donorsinfo.DonorsInfo
  309. var sampletype_data sampletype.SampleType
  310. var base_userrole userRole.Base_User
  311. svc := sampletype.GetSampleTypeService(utils.DBE)
  312. svc1 := baseUser.GetBaseUserService(utils.DBE)
  313. var warn1 string
  314. for sheetindex, sheet := range xlFile.Sheets { //循环页数
  315. sheetrows := len(xlFile.Sheets[sheetindex].Rows) //每一页的行数
  316. templateName := sheet.Name
  317. if templateName != "默认模板" {
  318. svc2 := sampleimporttemplate.GetSampleImporttemplateService(utils.DBE)
  319. where := " AccCode = '" + acccode + "'"
  320. where = where + " and Name='" + templateName + "'"
  321. var list []sampleimporttemplate.SampleImporttemplate
  322. svc2.GetPagingEntitiesWithSortCode(0, 1, "Id desc", &list, where)
  323. if len(list) == 0 {
  324. beego.Error("not found template:", templateName)
  325. continue
  326. }
  327. fields := strings.Split(list[0].Content, ",")
  328. for rowindex, row := range sheet.Rows { //循环行数
  329. notefieldstr := ""
  330. notevaluestr := ""
  331. if rowindex == 0 { //第一行是表头,跳过
  332. continue
  333. }
  334. if row.Cells == nil || len(row.Cells) < 8 { //小于必填项的19列,不允许导入(改为跳过)
  335. continue
  336. }
  337. basestation := new(SamplesInfoShow)
  338. for cellindex, cell := range row.Cells { //循环列数
  339. if cellindex >= len(fields) {
  340. break
  341. }
  342. field := fields[cellindex]
  343. if len(cell.String()) == 0 && (field == "SampleCode" || field == "SampleType") {
  344. //不能为空
  345. flag = 1
  346. break
  347. }
  348. if cell.String() != "" && len(cell.String()) != 0 {
  349. if field == "EquipmentId" {
  350. basestation.EquipmentId, _ = utils.StrTo(cell.String()).Int()
  351. } else if field == "ShelfId" {
  352. basestation.ShelfId, _ = utils.StrTo(cell.String()).Int()
  353. } else if field == "BoxId" {
  354. basestation.BoxId, _ = utils.StrTo(cell.String()).Int()
  355. } else if field == "Position" {
  356. spos := cell.String()
  357. sposs := StrSplit(spos, ";")
  358. basestation.Position = sposs[1] + ";" + utils.ToStr(Boxlineumber(sposs[0]))
  359. whereposition := " DeletionStateCode=0 and IState in (1,5) and BoxId=" + utils.ToStr(basestation.BoxId) + " and Position='" + basestation.Position + "'"
  360. have := s.QuerySampleInfoHas(tbldetail, whereposition)
  361. if have {
  362. flag = 1
  363. warn1 += cell.String() + ","
  364. break
  365. }
  366. } else if field == "SampleCode" { //样本编码
  367. basestation.SampleCode = cell.String()
  368. } else if field == "BarCode" { //样本条码
  369. whereposition := " DeletionStateCode=0 and BarCode = '" + cell.String() + "' "
  370. have := s.QuerySampleInfoHas(tbldetail, whereposition)
  371. where := " DeletionStateCode=0 and BarCode = '" + cell.String() + "'"
  372. has := s.QuerySampleInfoHas(tblfile, where)
  373. if have || has {
  374. flag = 1
  375. warn1 += cell.String() + ","
  376. break
  377. }
  378. basestation.BarCode = cell.String()
  379. } else if field == "SampleType" {
  380. basestation.SampleTypeName = cell.String()
  381. where2 := " AccCode='" + acccode + "' and Name='" + cell.String() + "'"
  382. sampletype_data = svc.GetSampleType(where2)
  383. if sampletype_data.Id == 0 {
  384. flag = 1
  385. warn1 = " "
  386. break
  387. }
  388. basestation.SampleType = sampletype_data.Id
  389. } else if field == "Name" {
  390. basestation.Name = cell.String()
  391. } else if field == "Unit" {
  392. basestation.Unit = cell.String()
  393. } else if field == "ValidityDate" {
  394. stime, err := utils.TimeParse(cell.Value, "2006-1-2 15:4:5")
  395. if err != nil {
  396. aa, _ := utils.StrTo(cell.Value).Float64()
  397. basestation.ValidityDate = xlsx.TimeFromExcelTime(aa, false)
  398. } else {
  399. basestation.ValidityDate = stime
  400. }
  401. } else if field == "ReceiveDate" {
  402. stime, err := utils.TimeParse(cell.Value, "2006-1-2 15:4:5")
  403. if err != nil {
  404. aa, _ := utils.StrTo(cell.Value).Float64()
  405. basestation.ReceiveDate = xlsx.TimeFromExcelTime(aa, false)
  406. } else {
  407. basestation.ReceiveDate = stime
  408. }
  409. } else if field == "CreateBy" {
  410. base_userrole = svc1.GetidByRealname(cell.String())
  411. basestation.CreateBy = cell.String()
  412. basestation.CreateUserId = base_userrole.Id
  413. } else if field == "SourceName" {
  414. basestation.SourceName = cell.String()
  415. } else if field == "SourceIdCard" {
  416. basestation.SourceIdCard = cell.String()
  417. } else if field == "Remark" {
  418. basestation.Remark = cell.String()
  419. } else if field == "ParentBarCode" {
  420. basestation.ParentBarCode = cell.String()
  421. } else if field == "InnerCode" {
  422. basestation.InnerCode = cell.String()
  423. } else {
  424. if strings.Contains(field, "ZBack") {
  425. notefieldstr = notefieldstr + "," + field //扩展字段
  426. notevaluestr = notevaluestr + ",'" + cell.String() + "'" //扩展名称
  427. }
  428. }
  429. // else if field == "SamplingOrgan" {
  430. // basestation.SamplingOrgan = cell.String()
  431. // } else if field == "SamplingOrganName" {
  432. // basestation.SamplingOrganName = cell.String()
  433. // } else if field == "SamplingSite" {
  434. // basestation.SamplingSite = cell.String()
  435. // } else if field == "SamplingSiteName" {
  436. // basestation.SamplingSiteName = cell.String()
  437. // } else if field == "Capacity" {
  438. // basestation.Capacity, _ = utils.StrTo(cell.String()).Float32()
  439. // basestation.InitCapacity = basestation.Capacity
  440. // }
  441. }
  442. }
  443. // if flag == 1 {
  444. // flag = 0
  445. // continue
  446. // }
  447. if basestation.CreateBy == "" {
  448. basestation.CreateBy = createby
  449. basestation.CreateUserId = createuserid
  450. }
  451. // if basestation.SourceName != "" || basestation.SourceIdCard != "" {
  452. if basestation.SourceName != "" {
  453. //wheredonors := " IdCard = '" + basestation.SourceIdCard + "' and Name ='" + basestation.SourceName + "' "
  454. wheredonors := " Name ='" + basestation.SourceName + "' "
  455. donors_data = svcdonors.QueryDonorEntity(tbldonor, wheredonors)
  456. basestation.SourceId = donors_data.Id
  457. }
  458. basestation.AccCode = acccode
  459. basestation.CreateOn = time.Now()
  460. basestation.IState = 3
  461. basestation.IType = 0
  462. basestation.FreezingNum = 0
  463. wheremain := " SampleCode='" + basestation.SampleCode + "' "
  464. ishasmain := s.QuerySampleInfoHas(tblmain, wheremain)
  465. if !ishasmain { //没有主信息,全部添加
  466. //添加主表
  467. sqlfield := " AccCode,SampleCode,Name,SampleType,SampleTypeName,SourceId,"
  468. sqlfield = sqlfield + "SamplingOrgan,SamplingOrganName,SamplingSite,SamplingSiteName,ReceiveDate,"
  469. sqlfield = sqlfield + "SourceIdCard,SourceName,MCreateUserId,MCreateBy,MCreateOn,MModifiedOn"
  470. sqlfield = sqlfield + notefieldstr
  471. sqlvalue := "'" + basestation.AccCode + "','" + basestation.SampleCode + "','" + basestation.Name + "'," + utils.ToStr(basestation.SampleType) + ",'" + basestation.SampleTypeName + "',"
  472. sqlvalue = sqlvalue + "" + utils.ToStr(basestation.SourceId) + ","
  473. sqlvalue = sqlvalue + "'" + basestation.SamplingOrgan + "','" + basestation.SamplingOrganName + "','" + basestation.SamplingSite + "',"
  474. sqlvalue = sqlvalue + "'" + basestation.SamplingSiteName + "','" + basestation.ReceiveDate.Format("2006-1-2 15:4:5") + "','" + basestation.SourceIdCard + "','" + basestation.SourceName + "',"
  475. sqlvalue = sqlvalue + "" + utils.ToStr(basestation.CreateUserId) + ",'" + basestation.CreateBy + "',now(),now()"
  476. sqlvalue = sqlvalue + notevaluestr
  477. s.AddLine(tblmain, sqlfield, sqlvalue)
  478. }
  479. // if basestation.BarCode == "" {
  480. // basestation.BarCode = s.AutoGetBarCode(acccode, basestation.SampleCode)
  481. // }
  482. datadetail.SampleCode = basestation.SampleCode
  483. datadetail.BarCode = basestation.BarCode
  484. datadetail.IState = 1
  485. datadetail.EquipmentId = basestation.EquipmentId
  486. datadetail.ShelfId = basestation.ShelfId
  487. datadetail.BoxId = basestation.BoxId
  488. datadetail.Position = basestation.Position
  489. datadetail.ParentBarCode = basestation.ParentBarCode
  490. datadetail.IType = basestation.IType
  491. datadetail.Capacity = basestation.Capacity
  492. datadetail.InitCapacity = basestation.Capacity
  493. datadetail.Unit = basestation.Unit
  494. datadetail.FreezingNum = 0
  495. datadetail.ValidityDate = basestation.ValidityDate
  496. datadetail.InnerCode = basestation.InnerCode
  497. datadetail.CreateUserId = basestation.CreateUserId
  498. datadetail.CreateBy = basestation.CreateBy
  499. datadetail.Remark = basestation.Remark
  500. fmt.Println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee3", tbldetail, &datadetail)
  501. affected, _ := s.InsertEntityBytbl(tbldetail, &datadetail)
  502. if affected > 0 {
  503. databuss.OpType = 7
  504. databuss.AccCode = basestation.AccCode
  505. databuss.BarCode = datadetail.BarCode
  506. databuss.CapacityUsed = datadetail.Capacity
  507. databuss.CapacityRest = datadetail.Capacity
  508. databuss.SampleCode = datadetail.SampleCode
  509. databuss.Unit = datadetail.Unit
  510. databuss.SampleType = basestation.SampleType
  511. databuss.EquipmentId = datadetail.EquipmentId
  512. databuss.ShelfId = datadetail.ShelfId
  513. databuss.BoxId = datadetail.BoxId
  514. databuss.Position = datadetail.Position
  515. databuss.PositionDesc = s.GetPostiondescByPosId(datadetail.EquipmentId, datadetail.ShelfId, datadetail.BoxId, datadetail.Position)
  516. databuss.CreateUserId = datadetail.CreateUserId
  517. databuss.CreateBy = datadetail.CreateBy
  518. s.InsertEntityBytbl(tblbuss, &databuss)
  519. }
  520. if rowindex == (sheetrows) {
  521. //s.AddLine(tbldetail, sqlfield, sqlvalue)
  522. }
  523. }
  524. } else {
  525. for rowindex, row := range sheet.Rows { //循环行数
  526. notefieldstr := ""
  527. notevaluestr := ""
  528. if rowindex == 0 { //第一行是表头,跳过
  529. continue
  530. }
  531. if row.Cells == nil || len(row.Cells) < 13 { //小于必填项的19列,不允许导入(改为跳过)
  532. continue
  533. }
  534. basestation := new(SamplesInfoShow)
  535. for cellindex, cell := range row.Cells { //循环列数
  536. if cellindex == 0 && len(cell.String()) == 0 { //冰箱ID不能为空
  537. flag = 1
  538. break
  539. }
  540. if cellindex == 2 && len(cell.String()) == 0 { //冻存架不能为空
  541. flag = 1
  542. break
  543. }
  544. if cellindex == 4 && len(cell.String()) == 0 { //冻存盒Id不能为空
  545. flag = 1
  546. break
  547. }
  548. if cellindex == 6 && len(cell.String()) == 0 { //位置不能为空
  549. flag = 1
  550. break
  551. }
  552. if cellindex == 7 && len(cell.String()) == 0 { //样本编码不能为空
  553. flag = 1
  554. break
  555. }
  556. if cellindex == 9 && len(cell.String()) == 0 { //样本类型不能为空
  557. flag = 1
  558. break
  559. }
  560. if cellindex == 17 && len(cell.String()) == 0 { //有效日期不能为空
  561. flag = 1
  562. break
  563. }
  564. if cellindex == 18 && len(cell.String()) == 0 { //接收日期不能为空
  565. flag = 1
  566. break
  567. }
  568. if cell.String() != "" && len(cell.String()) != 0 {
  569. if cellindex == 0 {
  570. basestation.EquipmentId, _ = utils.StrTo(cell.String()).Int()
  571. } else if cellindex == 2 {
  572. basestation.ShelfId, _ = utils.StrTo(cell.String()).Int()
  573. } else if cellindex == 4 {
  574. basestation.BoxId, _ = utils.StrTo(cell.String()).Int()
  575. } else if cellindex == 6 {
  576. spos := cell.String()
  577. sposs := StrSplit(spos, ";")
  578. basestation.Position = sposs[1] + ";" + utils.ToStr(Boxlineumber(sposs[0]))
  579. whereposition := " DeletionStateCode=0 and IState in (1,5) and BoxId=" + utils.ToStr(basestation.BoxId) + " and Position='" + basestation.Position + "'"
  580. have := s.QuerySampleInfoHas(tbldetail, whereposition)
  581. if have {
  582. flag = 1
  583. warn1 += cell.String() + ","
  584. break
  585. }
  586. } else if cellindex == 7 { //样本编码
  587. basestation.SampleCode = cell.String()
  588. } else if cellindex == 8 { //样本条码
  589. whereposition := " DeletionStateCode=0 and BarCode = '" + cell.String() + "' "
  590. have := s.QuerySampleInfoHas(tbldetail, whereposition)
  591. where := " DeletionStateCode=0 and BarCode = '" + cell.String() + "'"
  592. has := s.QuerySampleInfoHas(tblfile, where)
  593. if have || has {
  594. flag = 1
  595. warn1 += cell.String() + ","
  596. break
  597. }
  598. basestation.BarCode = cell.String()
  599. } else if cellindex == 9 {
  600. basestation.SampleTypeName = cell.String()
  601. where2 := " AccCode='" + acccode + "' and Name='" + cell.String() + "'"
  602. sampletype_data = svc.GetSampleType(where2)
  603. if sampletype_data.Id == 0 {
  604. flag = 1
  605. warn1 = " "
  606. break
  607. }
  608. basestation.SampleType = sampletype_data.Id
  609. } else if cellindex == 10 {
  610. basestation.Name = cell.String()
  611. } else if cellindex == 11 {
  612. basestation.SamplingOrgan = cell.String()
  613. } else if cellindex == 12 {
  614. basestation.SamplingOrganName = cell.String()
  615. } else if cellindex == 13 {
  616. basestation.SamplingSite = cell.String()
  617. } else if cellindex == 14 {
  618. basestation.SamplingSiteName = cell.String()
  619. } else if cellindex == 15 {
  620. basestation.Capacity, _ = utils.StrTo(cell.String()).Float32()
  621. basestation.InitCapacity = basestation.Capacity
  622. } else if cellindex == 16 {
  623. basestation.Unit = cell.String()
  624. } else if cellindex == 17 {
  625. stime, err := utils.TimeParse(cell.Value, "2006-1-2 15:4:5")
  626. if err != nil {
  627. aa, _ := utils.StrTo(cell.Value).Float64()
  628. basestation.ValidityDate = xlsx.TimeFromExcelTime(aa, false)
  629. } else {
  630. basestation.ValidityDate = stime
  631. }
  632. } else if cellindex == 18 {
  633. stime, err := utils.TimeParse(cell.Value, "2006-1-2 15:4:5")
  634. if err != nil {
  635. aa, _ := utils.StrTo(cell.Value).Float64()
  636. basestation.ReceiveDate = xlsx.TimeFromExcelTime(aa, false)
  637. } else {
  638. basestation.ReceiveDate = stime
  639. }
  640. } else if cellindex == 19 {
  641. base_userrole = svc1.GetidByRealname(cell.String())
  642. basestation.CreateBy = cell.String()
  643. basestation.CreateUserId = base_userrole.Id
  644. } else if cellindex == 20 {
  645. basestation.SourceName = cell.String()
  646. } else if cellindex == 21 {
  647. basestation.SourceIdCard = cell.String()
  648. } else if cellindex == 22 {
  649. basestation.Remark = cell.String()
  650. } else if cellindex == 23 {
  651. basestation.ParentBarCode = cell.String()
  652. } else if cellindex == 24 {
  653. basestation.InnerCode = cell.String()
  654. }
  655. // else if cellindex > 24 && cellindex < 25+notecount {
  656. // for i := 1; i < notecount+1; i++ {
  657. // if cellindex == 24+i {
  658. // notefieldstr = notefieldstr + "," + notelist[i-1].FieldName //扩展字段 因为cellindex可能小于21
  659. // notevaluestr = notevaluestr + ",'" + cell.String() + "'" //扩展名称
  660. // }
  661. // }
  662. // }
  663. }
  664. }
  665. if flag == 1 {
  666. flag = 0
  667. //continue
  668. }
  669. if basestation.CreateBy == "" {
  670. basestation.CreateBy = createby
  671. basestation.CreateUserId = createuserid
  672. }
  673. if basestation.SourceName != "" || basestation.SourceIdCard != "" {
  674. wheredonors := " IdCard = '" + basestation.SourceIdCard + "' and Name ='" + basestation.SourceName + "' "
  675. donors_data = svcdonors.QueryDonorEntity(tbldonor, wheredonors)
  676. basestation.SourceId = donors_data.Id
  677. }
  678. basestation.AccCode = acccode
  679. basestation.CreateOn = time.Now()
  680. basestation.IState = 3
  681. basestation.IType = 0
  682. basestation.FreezingNum = 0
  683. wheremain := " SampleCode='" + basestation.SampleCode + "' "
  684. ishasmain := s.QuerySampleInfoHas(tblmain, wheremain)
  685. if !ishasmain { //没有主信息,全部添加
  686. //添加主表
  687. sqlfield := " AccCode,SampleCode,Name,SampleType,SampleTypeName,SourceId,"
  688. sqlfield = sqlfield + "SamplingOrgan,SamplingOrganName,SamplingSite,SamplingSiteName,ReceiveDate,"
  689. sqlfield = sqlfield + "SourceIdCard,SourceName,MCreateUserId,MCreateBy,MCreateOn,MModifiedOn"
  690. sqlfield = sqlfield + notefieldstr
  691. sqlvalue := "'" + basestation.AccCode + "','" + basestation.SampleCode + "','" + basestation.Name + "'," + utils.ToStr(basestation.SampleType) + ",'" + basestation.SampleTypeName + "',"
  692. sqlvalue = sqlvalue + "" + utils.ToStr(basestation.SourceId) + ","
  693. sqlvalue = sqlvalue + "'" + basestation.SamplingOrgan + "','" + basestation.SamplingOrganName + "','" + basestation.SamplingSite + "',"
  694. sqlvalue = sqlvalue + "'" + basestation.SamplingSiteName + "','" + basestation.ReceiveDate.Format("2006-1-2 15:4:5") + "','" + basestation.SourceIdCard + "','" + basestation.SourceName + "',"
  695. sqlvalue = sqlvalue + "" + utils.ToStr(basestation.CreateUserId) + ",'" + basestation.CreateBy + "',now(),now()"
  696. sqlvalue = sqlvalue + notevaluestr
  697. fmt.Println(sqlfield)
  698. fmt.Println(sqlvalue)
  699. s.AddLine(tblmain, sqlfield, sqlvalue)
  700. }
  701. if basestation.BarCode == "" {
  702. basestation.BarCode = s.AutoGetBarCode(acccode, basestation.SampleCode)
  703. }
  704. datadetail.SampleCode = basestation.SampleCode
  705. datadetail.BarCode = basestation.BarCode
  706. datadetail.IState = 1
  707. datadetail.EquipmentId = basestation.EquipmentId
  708. datadetail.ShelfId = basestation.ShelfId
  709. datadetail.BoxId = basestation.BoxId
  710. datadetail.Position = basestation.Position
  711. datadetail.ParentBarCode = basestation.ParentBarCode
  712. datadetail.IType = basestation.IType
  713. datadetail.Capacity = basestation.Capacity
  714. datadetail.InitCapacity = basestation.Capacity
  715. datadetail.Unit = basestation.Unit
  716. datadetail.FreezingNum = 0
  717. datadetail.ValidityDate = basestation.ValidityDate
  718. datadetail.InnerCode = basestation.InnerCode
  719. datadetail.CreateUserId = basestation.CreateUserId
  720. datadetail.CreateBy = basestation.CreateBy
  721. datadetail.Remark = basestation.Remark
  722. affected, _ := s.InsertEntityBytbl(tbldetail, &datadetail)
  723. if affected > 0 {
  724. databuss.OpType = 7
  725. databuss.AccCode = basestation.AccCode
  726. databuss.BarCode = datadetail.BarCode
  727. databuss.CapacityUsed = datadetail.Capacity
  728. databuss.CapacityRest = datadetail.Capacity
  729. databuss.SampleCode = datadetail.SampleCode
  730. databuss.Unit = datadetail.Unit
  731. databuss.SampleType = basestation.SampleType
  732. databuss.EquipmentId = datadetail.EquipmentId
  733. databuss.ShelfId = datadetail.ShelfId
  734. databuss.BoxId = datadetail.BoxId
  735. databuss.Position = datadetail.Position
  736. databuss.PositionDesc = s.GetPostiondescByPosId(datadetail.EquipmentId, datadetail.ShelfId, datadetail.BoxId, datadetail.Position)
  737. databuss.CreateUserId = datadetail.CreateUserId
  738. databuss.CreateBy = datadetail.CreateBy
  739. s.InsertEntityBytbl(tblbuss, &databuss)
  740. }
  741. if rowindex == (sheetrows) {
  742. //s.AddLine(tbldetail, sqlfield, sqlvalue)
  743. }
  744. }
  745. }
  746. }
  747. return err, warn1
  748. }
  749. //判断是否存在此样本
  750. func (s *SamplesInfoService) QuerySampleInfoHas(tablename, where string) bool {
  751. var num MyInt_Id
  752. sql := "select count(1) Id from " + tablename
  753. if where != "" {
  754. sql = sql + " where " + where + " limit 1 "
  755. }
  756. s.DBE.Sql(sql).Get(&num)
  757. total := num.Id
  758. if total > 0 {
  759. return true
  760. } else {
  761. return false
  762. }
  763. }
  764. func (s *SamplesInfoService) AddLine(tablename, fieldName, fieldValue string) error {
  765. sql := "insert into " + tablename + "(" + fieldName + ") values(" + fieldValue + ")"
  766. _, err := s.DBE.Exec(sql)
  767. return err
  768. }
  769. func (s *SamplesInfoService) AutoGetBarCode(acccode, samplecode string) string {
  770. var num MyInt_Id
  771. tbldetail := acccode + SamplesDetailtbName
  772. tblfile := acccode + SamplesDetailFiletbName
  773. slikestring := samplecode
  774. // slikestring := samplecode + "-"
  775. sql := "select MAX(REPLACE(BarCode,'" + slikestring + "','')+1) Id from "
  776. sql = sql + "( select BarCode from " + tbldetail
  777. sql = sql + " where DeletionStateCode=0 and BarCode like '" + slikestring + "%' "
  778. sql = sql + " union select BarCode from " + tblfile
  779. sql = sql + " where DeletionStateCode=0 and BarCode like '" + slikestring + "%') as A "
  780. s.DBE.Sql(sql).Get(&num)
  781. total := num.Id
  782. if total == 0 {
  783. total = 1
  784. }
  785. if total < 10 {
  786. slikestring = samplecode + "0"
  787. }
  788. return slikestring + utils.ToStr(total)
  789. }
  790. func (s *SamplesInfoService) QueryZBackList(acccode string) []String_Id {
  791. List := make([]String_Id, 0)
  792. sql := "select FieldName Id from STypeNoteItem where FieldNo>10 and AccCode ='" + acccode + "' union select FieldName Id from SamplesNoteItem where FieldNo>10 and AccCode ='" + acccode + "'"
  793. utils.DBE.Sql(sql).Find(&List)
  794. return List
  795. }
  796. func (s *SamplesInfoService) DelSDetailInfo(acccode string, id int) error {
  797. tbldetail := acccode + SamplesDetailtbName
  798. sql := " delete from " + tbldetail + " where Id=" + utils.ToStr(id) + ""
  799. _, err := s.DBE.Exec(sql)
  800. return err
  801. }
  802. func (s *SamplesInfoService) DelSMainInfo(acccode string, id int) error {
  803. tbldetail := acccode + SamplesMaintbName
  804. sql := " delete from " + tbldetail + " where MId=" + utils.ToStr(id) + ""
  805. _, err := s.DBE.Exec(sql)
  806. return err
  807. }
  808. func (s *SamplesInfoService) QuerySampleList(acccode string, where string) []SamplesInfoShow {
  809. tblmain := acccode + SamplesMaintbName
  810. tbldetail := acccode + SamplesDetailtbName
  811. sql := "select a.*,b.* from " + tbldetail + " a left join " + tblmain +
  812. " b on a.SampleCode = b.SampleCode where " + where + " order by a.Id desc"
  813. List := make([]SamplesInfoShow, 0)
  814. utils.DBE.Sql(sql).Find(&List)
  815. return List
  816. }
  817. func (s *SamplesInfoService) QueryAnimalSampleList(acccode string, where string) []AnimalSamplesInfoShow {
  818. tblmain := acccode + SamplesMaintbName
  819. tbldetail := acccode + SamplesDetailtbName
  820. animaltb := acccode + AnimalInfoName
  821. sql := "select a.*,b.*,c.Genus,c.GenusName,concat(c.ProvinceName,c.CityName,c.StreetName,c.Address) as AddressName,c.InnerNo as SourceInner,c.Amount,c.Unit as SourceUni,c.ProjectName,c.Longitude,c.Latitude,c.Altitude,c.SurveyDate,c.ZBack11 as BeeKeepers,c.ZBack17 as TakeAway from " + tbldetail + " a left join " + tblmain +
  822. " b on a.SampleCode = b.SampleCode left join " + animaltb +
  823. " c on b.SourceId = c.Id where " + where + " order by a.Id desc"
  824. List := make([]AnimalSamplesInfoShow, 0)
  825. utils.DBE.Sql(sql).Find(&List)
  826. for i := 0; i < len(List); i++ {
  827. //List[i].AllbarCode = "蜂种:" + List[i].Genus + "&&蜂种备注:" + List[i].GenusName + "&&项目属性:" + List[i].ProjectName + "&&采样地址:" + List[i].AddressName + "&&采样时间:" + List[i].SurveyDate.Format("2006-01-02") + "&&群数:" + strconv.Itoa(List[i].Amount) + "&&经度:" + List[i].Longitude + "&&纬度:" + List[i].Latitude + "&&海拔:" + List[i].Altitude + "&&采集人:" + List[i].TakeAway + "&&供蜂人:" + List[i].BeeKeepers + "&&样本编码:" + List[i].BarCode
  828. List[i].ShortbarCode = List[i].BarCode[0:14]
  829. List[i].AllbarCode = List[i].BarCode + "·" + List[i].Genus + "-" + List[i].GenusName + "·" + List[i].AddressName + "·" + List[i].SurveyDate.Format("2006-01-02") + "·" + List[i].Altitude + "米·" + List[i].ProjectName
  830. }
  831. return List
  832. }
  833. type SampleCount struct {
  834. IState int
  835. SCount int
  836. }
  837. //获取数量
  838. func (s *SamplesInfoService) GetSampleOwnCount(tablename, where string) int {
  839. var num MyInt_Id
  840. sql := "select count(Id) Id from " + tablename
  841. if where != "" {
  842. sql = sql + " where " + where
  843. }
  844. s.DBE.Sql(sql).Get(&num)
  845. total := num.Id
  846. return total
  847. }
  848. //获取数量
  849. func (s *SamplesInfoService) GetSampleOwnCountBySourceId(acccode, where string) int {
  850. tblmain := acccode + SamplesMaintbName
  851. tbldetail := acccode + SamplesDetailtbName
  852. var num MyInt_Id
  853. sql := "select count(Id) Id from " + tbldetail + " a left join " + tblmain +
  854. " b on a.SampleCode = b.SampleCode "
  855. if where != "" {
  856. sql = sql + " where " + where
  857. }
  858. s.DBE.Sql(sql).Get(&num)
  859. total := num.Id
  860. return total
  861. }
  862. //获取样本数量分样本类型
  863. type SampleCountType struct {
  864. SampleType int
  865. SCount int
  866. }
  867. func (s *SamplesInfoService) GetSampleCountbySampleType(tablename, where, groupstr string) []SampleCountType {
  868. sql := " select SampleType,Count(Id) SCount from " + tablename
  869. if where != "" {
  870. sql = sql + " where " + where
  871. }
  872. if groupstr != "" {
  873. sql = sql + groupstr
  874. }
  875. List := make([]SampleCountType, 0)
  876. utils.DBE.Sql(sql).Find(&List)
  877. return List
  878. }
  879. func (s *SamplesInfoService) GetPreSampleCountbySampleType(acccode, where, groupstr string) []SampleCountType {
  880. tblmain := acccode + SamplesMaintbName
  881. tbldetail := acccode + SamplesDetailtbName
  882. sql := " select SampleType,Count(Id) SCount from " + tbldetail + " a left join " + tblmain +
  883. " b on a.SampleCode = b.SampleCode "
  884. if where != "" {
  885. sql = sql + " where " + where
  886. }
  887. if groupstr != "" {
  888. sql = sql + groupstr
  889. }
  890. List := make([]SampleCountType, 0)
  891. utils.DBE.Sql(sql).Find(&List)
  892. return List
  893. }
  894. //获取样本数量分样本类型
  895. type SampleOutCountOpType struct {
  896. SampleType int
  897. OpType int
  898. SCount int
  899. }
  900. func (s *SamplesInfoService) GetSampleOutCountbyOpType(tablename, where, groupstr string) []SampleOutCountOpType {
  901. sql := " select SampleType,OpType,Count(Id) SCount from " + tablename
  902. if where != "" {
  903. sql = sql + " where " + where
  904. }
  905. if groupstr != "" {
  906. sql = sql + groupstr
  907. }
  908. List := make([]SampleOutCountOpType, 0)
  909. utils.DBE.Sql(sql).Find(&List)
  910. return List
  911. }
  912. func (s *SamplesInfoService) GetSumSampleLogCountbyOpType(tablename, where, groupstr string) []SampleOutCountOpType {
  913. sql := " select OpType,Count(Id) SCount from " + tablename
  914. if where != "" {
  915. sql = sql + " where " + where
  916. }
  917. if groupstr != "" {
  918. sql = sql + groupstr
  919. }
  920. List := make([]SampleOutCountOpType, 0)
  921. utils.DBE.Sql(sql).Find(&List)
  922. return List
  923. }
  924. func (s *SamplesInfoService) InsertExcelSampleBox(excelpath string, boxentity shelfset.Box, stypeid int, stypename string, capacity float32, unit string) (err error, warn string) {
  925. tblmain := boxentity.AccCode + SamplesMaintbName
  926. tbldetail := boxentity.AccCode + SamplesDetailtbName
  927. tblfile := boxentity.AccCode + SamplesDetailFiletbName
  928. boxrow := boxentity.RowNum
  929. positionitem := 2
  930. var datamain SamplesMain
  931. var datadetail SamplesDetail
  932. dir, _ := os.Getwd()
  933. flag := 0
  934. excelpath = dir + excelpath
  935. xlFile, _ := xlsx.OpenFile(excelpath)
  936. var warn1 string
  937. for sheetindex, sheet := range xlFile.Sheets { //循环页数
  938. sheetrows := len(xlFile.Sheets[sheetindex].Rows) //每一页的行数
  939. fmt.Println("sheetindex", sheetindex)
  940. for rowindex, row := range sheet.Rows { //循环行数
  941. scellrow := ""
  942. scolumn := ""
  943. srealrow := ""
  944. realrow := 0
  945. if rowindex == 0 { //第一行是表头,跳过
  946. continue
  947. }
  948. if row.Cells == nil || len(row.Cells) < 3 { //小于必填项的3列,不允许导入
  949. break
  950. }
  951. basestation := new(SamplesInfoShow)
  952. for cellindex, cell := range row.Cells { //循环列数
  953. if cellindex == 0 && len(cell.String()) == 0 { //条码不能为空
  954. flag = 1
  955. break
  956. }
  957. if cellindex == 1 && len(cell.String()) == 0 { //行数不能为空
  958. flag = 1
  959. break
  960. }
  961. if cellindex == 2 && len(cell.String()) == 0 { //列数不能为空
  962. flag = 1
  963. break
  964. }
  965. if cell.String() != "" && len(cell.String()) != 0 {
  966. if cellindex == 0 {
  967. if cell.String() == "notfound" || cell.String() == "notpipe" {
  968. flag = 1
  969. break
  970. }
  971. basestation.BarCode = cell.String()
  972. } else if cellindex == 1 {
  973. scellrow = cell.String()
  974. if positionitem == 2 {
  975. rowint, _ := utils.StrTo(scellrow).Int()
  976. realrow = boxrow + 1 - rowint
  977. srealrow = utils.ToStr(realrow)
  978. } else {
  979. srealrow = scellrow
  980. }
  981. } else if cellindex == 2 {
  982. scolumn = cell.String()
  983. basestation.Position = utils.ToStr(Boxlineumber(scolumn)) + ";" + srealrow
  984. }
  985. }
  986. }
  987. fmt.Println("qqq---", basestation.Position, positionitem)
  988. if flag == 1 {
  989. flag = 0
  990. continue
  991. }
  992. if basestation.CreateBy == "" {
  993. basestation.CreateBy = boxentity.CreateBy
  994. basestation.CreateUserId = boxentity.CreateUserId
  995. }
  996. basestation.SampleCode = basestation.BarCode
  997. basestation.Capacity = capacity
  998. basestation.InitCapacity = basestation.Capacity
  999. basestation.Unit = unit
  1000. basestation.SampleTypeName = stypename
  1001. basestation.SampleType = stypeid
  1002. basestation.AccCode = boxentity.AccCode
  1003. basestation.CreateOn = time.Now()
  1004. basestation.IType = 0
  1005. basestation.FreezingNum = 0
  1006. wheremain := " SampleCode='" + basestation.SampleCode + "' "
  1007. ishasmain := s.QuerySampleInfoHas(tblmain, wheremain)
  1008. where := " DeletionStateCode=0 and BarCode = '" + basestation.BarCode + "'"
  1009. have := s.QuerySampleInfoHas(tbldetail, where)
  1010. has := s.QuerySampleInfoHas(tblfile, where)
  1011. if !ishasmain && !has && !have {
  1012. datamain.SampleCode = basestation.SampleCode
  1013. datamain.SampleType = basestation.SampleType
  1014. datamain.SampleTypeName = basestation.SampleTypeName
  1015. datamain.AccCode = basestation.AccCode
  1016. datamain.MCreateUserId = basestation.CreateUserId
  1017. datamain.MCreateBy = basestation.CreateBy
  1018. s.InsertEntityBytbl(tblmain, &datamain)
  1019. datadetail.EquipmentId = boxentity.EquipmentId
  1020. datadetail.ShelfId = boxentity.ShelfId
  1021. datadetail.BoxId = boxentity.Id
  1022. datadetail.SampleCode = basestation.SampleCode
  1023. datadetail.BarCode = basestation.BarCode
  1024. datadetail.IState = 11
  1025. datadetail.Position = basestation.Position
  1026. datadetail.IType = basestation.IType
  1027. datadetail.Capacity = basestation.Capacity
  1028. datadetail.InitCapacity = basestation.Capacity
  1029. datadetail.Unit = basestation.Unit
  1030. datadetail.FreezingNum = 0
  1031. datadetail.CreateUserId = basestation.CreateUserId
  1032. datadetail.CreateBy = basestation.CreateBy
  1033. datadetail.Remark = basestation.Remark
  1034. s.InsertEntityBytbl(tbldetail, &datadetail)
  1035. }
  1036. if rowindex == (sheetrows) {
  1037. //s.AddLine(tbldetail, sqlfield, sqlvalue)
  1038. }
  1039. }
  1040. }
  1041. return err, warn1
  1042. }
  1043. //扫描导入无冻存盒导入冻存管
  1044. func (s *SamplesInfoService) ExportSampleBox(dongcun string, boxentity shelfset.Box, stypeid int, stypename string, capacity float32, unit string) (err error, issuess int) {
  1045. dongcuns := strings.Split(dongcun, ",")
  1046. if len(dongcuns) == 48 || len(dongcuns) == 49 {
  1047. tblmain := boxentity.AccCode + SamplesMaintbName
  1048. tbldetail := boxentity.AccCode + SamplesDetailtbName
  1049. tblfile := boxentity.AccCode + SamplesDetailFiletbName
  1050. var datamain SamplesMain
  1051. var datadetail SamplesDetail
  1052. //添加冻存管数据
  1053. for i := 1; i <= 12; i++ { //盒子有12列,8行,交替显示
  1054. for j := 1; j <= 4; j++ { //实际每列只有4个
  1055. guanx, guany := 0, 0
  1056. if i%2 == 1 { //奇数列从第2行开始2,4,6,8
  1057. guanx, guany = i, 2*j
  1058. } else {
  1059. guanx, guany = i, 2*j-1
  1060. }
  1061. basestation := new(SamplesInfoShow)
  1062. basestation.BarCode = dongcuns[4*(i-1)+(j-1)]
  1063. //无冻存管是字符串:No Tube
  1064. if strings.Index(basestation.BarCode, "No Tube") > -1 {
  1065. continue
  1066. }
  1067. basestation.Position = fmt.Sprintf("%v;%v", guanx, guany) //位置
  1068. basestation.SampleCode = basestation.BarCode
  1069. if basestation.CreateBy == "" {
  1070. basestation.CreateBy = boxentity.CreateBy
  1071. basestation.CreateUserId = boxentity.CreateUserId
  1072. }
  1073. basestation.Capacity = capacity
  1074. basestation.InitCapacity = basestation.Capacity
  1075. basestation.Unit = unit
  1076. basestation.SampleTypeName = stypename
  1077. basestation.SampleType = stypeid
  1078. basestation.AccCode = boxentity.AccCode
  1079. basestation.CreateOn = time.Now()
  1080. basestation.IType = 0
  1081. basestation.FreezingNum = 0
  1082. wheremain := " SampleCode='" + basestation.SampleCode + "' "
  1083. ishasmain := s.QuerySampleInfoHas(tblmain, wheremain)
  1084. where := " DeletionStateCode=0 and BarCode = '" + basestation.BarCode + "'"
  1085. have := s.QuerySampleInfoHas(tbldetail, where)
  1086. has := s.QuerySampleInfoHas(tblfile, where)
  1087. if !ishasmain && !has && !have {
  1088. datamain.SampleCode = basestation.SampleCode
  1089. datamain.SampleType = basestation.SampleType
  1090. datamain.SampleTypeName = basestation.SampleTypeName
  1091. datamain.AccCode = basestation.AccCode
  1092. datamain.MCreateUserId = basestation.CreateUserId
  1093. datamain.MCreateBy = basestation.CreateBy
  1094. s.InsertEntityBytbl(tblmain, &datamain)
  1095. datadetail.EquipmentId = boxentity.EquipmentId
  1096. datadetail.ShelfId = boxentity.ShelfId
  1097. datadetail.BoxId = boxentity.Id
  1098. datadetail.SampleCode = basestation.SampleCode
  1099. datadetail.BarCode = basestation.BarCode
  1100. datadetail.IState = 1
  1101. datadetail.Position = basestation.Position
  1102. datadetail.IType = basestation.IType
  1103. datadetail.Capacity = basestation.Capacity
  1104. datadetail.InitCapacity = basestation.Capacity
  1105. datadetail.Unit = basestation.Unit
  1106. datadetail.FreezingNum = 0
  1107. datadetail.CreateUserId = basestation.CreateUserId
  1108. datadetail.CreateBy = basestation.CreateBy
  1109. datadetail.Remark = basestation.Remark
  1110. s.InsertEntityBytbl(tbldetail, &datadetail)
  1111. }
  1112. }
  1113. }
  1114. //添加补充位置的数据
  1115. for i := 1; i <= 12; i++ { //盒子有12列,8行,交替显示
  1116. for j := 1; j <= 4; j++ { //实际每列只有4个
  1117. guanx, guany := 0, 0
  1118. if i%2 == 1 { //奇数列从第2行开始2,4,6,8
  1119. guanx, guany = i, 2*j-1
  1120. } else {
  1121. guanx, guany = i, 2*j
  1122. }
  1123. basestation := new(SamplesInfoShow)
  1124. basestation.BarCode = "-99" //特殊,不需要
  1125. basestation.Position = fmt.Sprintf("%v;%v", guanx, guany) //位置
  1126. basestation.SampleCode = basestation.BarCode
  1127. if basestation.CreateBy == "" {
  1128. basestation.CreateBy = boxentity.CreateBy
  1129. basestation.CreateUserId = boxentity.CreateUserId
  1130. }
  1131. basestation.Capacity = 0
  1132. basestation.InitCapacity = basestation.Capacity
  1133. basestation.Unit = unit
  1134. basestation.SampleTypeName = stypename
  1135. basestation.SampleType = stypeid
  1136. basestation.AccCode = boxentity.AccCode
  1137. basestation.CreateOn = time.Now()
  1138. basestation.IType = 0
  1139. basestation.FreezingNum = 0
  1140. where := " DeletionStateCode=0 and BarCode = '" + basestation.BarCode + "' and Position = '" +
  1141. basestation.Position + "' and EquipmentId=" + utils.ToStr(boxentity.EquipmentId) + " and ShelfId = " +
  1142. utils.ToStr(boxentity.ShelfId) + " and BoxId = " + utils.ToStr(boxentity.Id)
  1143. have := s.QuerySampleInfoHas(tbldetail, where)
  1144. if !have {
  1145. datadetail.EquipmentId = boxentity.EquipmentId
  1146. datadetail.ShelfId = boxentity.ShelfId
  1147. datadetail.BoxId = boxentity.Id
  1148. datadetail.SampleCode = basestation.SampleCode
  1149. datadetail.BarCode = basestation.BarCode
  1150. datadetail.IState = 99
  1151. datadetail.Position = basestation.Position
  1152. datadetail.IType = basestation.IType
  1153. datadetail.Capacity = basestation.Capacity
  1154. datadetail.InitCapacity = basestation.Capacity
  1155. datadetail.Unit = basestation.Unit
  1156. datadetail.FreezingNum = 0
  1157. datadetail.CreateUserId = basestation.CreateUserId
  1158. datadetail.CreateBy = basestation.CreateBy
  1159. datadetail.Remark = basestation.Remark
  1160. s.InsertEntityBytbl(tbldetail, &datadetail)
  1161. }
  1162. }
  1163. }
  1164. return err, 1
  1165. } else {
  1166. return nil, 0
  1167. }
  1168. }
  1169. //扫描导入有冻存盒导入冻存管
  1170. func (s *SamplesInfoService) ExportSampleBoxFull(dongcun string, boxentity shelfset.Box, stypeid int, stypename string, capacity float32, unit string) (err error, issuess int) {
  1171. dongcuns := strings.Split(dongcun, ",")
  1172. if len(dongcuns) == 48 || len(dongcuns) == 49 {
  1173. tblmain := boxentity.AccCode + SamplesMaintbName
  1174. tbldetail := boxentity.AccCode + SamplesDetailtbName
  1175. tblfile := boxentity.AccCode + SamplesDetailFiletbName
  1176. var datamain SamplesMain
  1177. var datadetail SamplesDetail
  1178. //添加冻存管数据
  1179. for i := 1; i <= 12; i++ { //盒子有12列,8行,交替显示
  1180. for j := 1; j <= 4; j++ { //实际每列只有4个
  1181. guanx, guany := 0, 0
  1182. if i%2 == 1 { //奇数列从第2行开始2,4,6,8
  1183. guanx, guany = i, 2*j
  1184. } else {
  1185. guanx, guany = i, 2*j-1
  1186. }
  1187. basestation := new(SamplesInfoShow)
  1188. basestation.BarCode = dongcuns[4*(i-1)+(j-1)]
  1189. //无冻存管是字符串:No Tube
  1190. if strings.Index(basestation.BarCode, "No Tube") > -1 {
  1191. continue
  1192. }
  1193. basestation.Position = fmt.Sprintf("%v;%v", guanx, guany) //位置
  1194. basestation.SampleCode = basestation.BarCode
  1195. if basestation.CreateBy == "" {
  1196. basestation.CreateBy = boxentity.CreateBy
  1197. basestation.CreateUserId = boxentity.CreateUserId
  1198. }
  1199. basestation.Capacity = capacity
  1200. basestation.InitCapacity = basestation.Capacity
  1201. basestation.Unit = unit
  1202. basestation.SampleTypeName = stypename
  1203. basestation.SampleType = stypeid
  1204. basestation.AccCode = boxentity.AccCode
  1205. basestation.CreateOn = time.Now()
  1206. basestation.IType = 0
  1207. basestation.FreezingNum = 0
  1208. wheremain := " SampleCode='" + basestation.SampleCode + "' "
  1209. ishasmain := s.QuerySampleInfoHas(tblmain, wheremain)
  1210. where := " DeletionStateCode=0 and BarCode = '" + basestation.BarCode + "'"
  1211. have := s.QuerySampleInfoHas(tbldetail, where)
  1212. has := s.QuerySampleInfoHas(tblfile, where)
  1213. //当前位置为空
  1214. whereempty := " Position = '" + basestation.Position +
  1215. "' and EquipmentId=" + utils.ToStr(boxentity.EquipmentId) + " and ShelfId = " +
  1216. utils.ToStr(boxentity.ShelfId) + " and BoxId = " + utils.ToStr(boxentity.Id)
  1217. haveempty := s.QuerySampleInfoHas(tbldetail, whereempty)
  1218. if !ishasmain && !has && !have && !haveempty {
  1219. datamain.SampleCode = basestation.SampleCode
  1220. datamain.SampleType = basestation.SampleType
  1221. datamain.SampleTypeName = basestation.SampleTypeName
  1222. datamain.AccCode = basestation.AccCode
  1223. datamain.MCreateUserId = basestation.CreateUserId
  1224. datamain.MCreateBy = basestation.CreateBy
  1225. s.InsertEntityBytbl(tblmain, &datamain)
  1226. datadetail.EquipmentId = boxentity.EquipmentId
  1227. datadetail.ShelfId = boxentity.ShelfId
  1228. datadetail.BoxId = boxentity.Id
  1229. datadetail.SampleCode = basestation.SampleCode
  1230. datadetail.BarCode = basestation.BarCode
  1231. datadetail.IState = 1
  1232. datadetail.Position = basestation.Position
  1233. datadetail.IType = basestation.IType
  1234. datadetail.Capacity = basestation.Capacity
  1235. datadetail.InitCapacity = basestation.Capacity
  1236. datadetail.Unit = basestation.Unit
  1237. datadetail.FreezingNum = 0
  1238. datadetail.CreateUserId = basestation.CreateUserId
  1239. datadetail.CreateBy = basestation.CreateBy
  1240. datadetail.Remark = basestation.Remark
  1241. s.InsertEntityBytbl(tbldetail, &datadetail)
  1242. }
  1243. }
  1244. }
  1245. return err, 1
  1246. } else {
  1247. return nil, 0
  1248. }
  1249. }
  1250. // 获取统计数据按样本类型分组
  1251. func (s *SamplesInfoService) GetTJBysampletype(acccode string, where string) (list []TJGroupModel) {
  1252. tblmain := acccode + SamplesMaintbName
  1253. tbldetail := acccode + SamplesDetailtbName
  1254. donorsinfo := acccode + DonorstbName
  1255. // sql := `select * from (
  1256. // select b.SampleTypeName Name,count(1) Num from ` + tbldetail + ` a left join
  1257. // ` + tblmain + ` b on a.SampleCode = b.SampleCode
  1258. // where a.DeletionStateCode=0 group by b.SampleTypeName) m order by Num desc`
  1259. sql := `select * from (
  1260. select b.SampleTypeName Name,count(1) Num from ` + tbldetail + ` a
  1261. left join ` + tblmain + ` b on a.SampleCode = b.SampleCode
  1262. left join ` + donorsinfo + ` c on b.SourceId=c.Id
  1263. where ` + where + ` group by b.SampleTypeName) m order by Num desc`
  1264. s.DBE.Sql(sql).Find(&list)
  1265. return
  1266. }
  1267. // 获取统计数据按设备分组
  1268. func (s *SamplesInfoService) GetTJBydevice(acccode, where string) (list []TJGroupModel) {
  1269. tbldetail := acccode + SamplesDetailtbName
  1270. sql := `select Name,sum(num) Num from (
  1271. select a.Id,a.Name,case when b.Id is null then 0 else 1 end num from Equipment a left join
  1272. ` + tbldetail + ` b on a.Id=b.EquipmentId where ` + where + `
  1273. ) m group by Id`
  1274. s.DBE.Sql(sql).Find(&list)
  1275. return
  1276. }
  1277. //多中心管理分组统计
  1278. func (s *SamplesInfoService) GetTJBygroup(acccode string, where string) (list []GroupSamples) {
  1279. tbldetail := acccode + SamplesDetailtbName
  1280. tbgroup := acccode + GroupDetailName
  1281. sql := `select * from (
  1282. select a.IState ,c.GroupName as Name,count(1) Num from ` + tbldetail + ` a
  1283. left join ` + tbgroup + ` c on c.SampleId = a.Id
  1284. where ` + where + ` group by IState,Name) m order by IState desc`
  1285. s.DBE.Sql(sql).Find(&list)
  1286. return
  1287. }
  1288. func (s *SamplesInfoService) GetStationByCode(acccode string, where string) SamplesInfoList {
  1289. if where == "" {
  1290. where = "1=1"
  1291. }
  1292. tblmain := acccode + SamplesMaintbName
  1293. tbldetail := acccode + SamplesDetailtbName
  1294. sql := ` select a.*,b.*,d.XStation ShelfX,d.YStation ShelfY,d.Code ShelfCode,e.XStation BoxX,e.YStation BoxY,e.Code BoxCode,f.Id Eid,f.Name EName,f.Code ECode from ` + tbldetail + ` a left join ` + tblmain +
  1295. ` b on a.SampleCode = b.SampleCode left join Shelf d on a.ShelfId=d.Id left join Box e on a.BoxId=e.Id left join Equipment f on a.EquipmentId=f.Id where ` + where + `
  1296. limit 1`
  1297. var model SamplesInfoList
  1298. utils.DBE.Sql(sql).Get(&model)
  1299. return model
  1300. }
  1301. func (s *SamplesInfoService) QuerySampleBoxDataList(tablename, where string) []SampleBoxCount {
  1302. sql := "select BoxId,Count(Id) CurrCount,100 AllCount from " + tablename + where + " group by BoxId "
  1303. List := make([]SampleBoxCount, 0)
  1304. utils.DBE.Sql(sql).Find(&List)
  1305. return List
  1306. }
  1307. //查询日志数据列表
  1308. func (s *SamplesInfoService) GetContantById(tablename, id string) *SamplesLog {
  1309. entity := new(SamplesLog)
  1310. sql := "select * from " + tablename + " where Id=" + id + ""
  1311. s.DBE.Sql(sql).Get(entity)
  1312. return entity
  1313. }
  1314. //获取主表修改记录的下一条
  1315. func (s *SamplesInfoService) GetNextContantById(logtablename, id, tblmain, mid string, model interface{}) string {
  1316. entity := new(SamplesLog)
  1317. sql := "select * from " + logtablename + " where TableName='SamplesMain' and MId = '" + mid + "' and Id > '" + id + "' order by Id limit 1"
  1318. has, _ := s.DBE.Sql(sql).Get(entity)
  1319. if !has {
  1320. sql = "select * from " + tblmain + " where MId = '" + mid + "' limit 1"
  1321. has, _ := s.DBE.Sql(sql).Get(model)
  1322. if has {
  1323. j, _ := json.Marshal(model)
  1324. fmt.Println(j)
  1325. return string(j)
  1326. }
  1327. }
  1328. return entity.RecordData
  1329. }
  1330. //查询下一条数据列表
  1331. func (s *SamplesInfoService) GetNextContantShowInfoById(logtablename, id, recordId string, model interface{}, acccode string) string {
  1332. entity := new(SamplesLog)
  1333. sql := "select * from " + logtablename + " where RecordId = '" + recordId + "' and Id > '" + id + "' order by Id limit 1"
  1334. has, _ := s.DBE.Sql(sql).Get(entity)
  1335. if !has {
  1336. tblmain := acccode + SamplesMaintbName
  1337. tbldetail := acccode + SamplesDetailtbName
  1338. sql := "select a.*,b.* from " + tbldetail + " a left join " + tblmain +
  1339. " b on a.SampleCode = b.SampleCode where Id=" + recordId + " and DeletionStateCode =0 limit 1"
  1340. has, _ := s.DBE.Sql(sql).Get(model)
  1341. if has {
  1342. j, _ := json.Marshal(model)
  1343. return string(j)
  1344. }
  1345. }
  1346. return entity.RecordData
  1347. }
  1348. func NumtoUpChar(x int) string {
  1349. return string(64 + x)
  1350. }
  1351. func (s *SamplesInfoService) GetPostiondescByPosId(eid, sid, bid int, posti string) string {
  1352. var entity SamplesInDetailfoShow
  1353. sql := `select c.Code ECode,b.XStation ShelfX,b.YStation ShelfY,a.XStation BoxX,a.YStation BoxY from Box a
  1354. left join Shelf b on a.ShelfId=b.Id
  1355. left join Equipment c on a.EquipmentId=c.Id
  1356. where a.Id=` + utils.ToStr(bid) + ` and a.ShelfId=` + utils.ToStr(sid) + ` and a.EquipmentId=` + utils.ToStr(eid)
  1357. s.DBE.Sql(sql).Get(&entity)
  1358. pos := strings.Split(posti, ";")
  1359. position := posti
  1360. if len(pos) == 2 {
  1361. posyint, _ := utils.StrTo(pos[1]).Int()
  1362. position = NumtoUpChar(posyint) + pos[0]
  1363. }
  1364. return fmt.Sprintf("%v-%v%v-%v%v-%v", entity.ECode, NumtoUpChar(entity.ShelfY), entity.ShelfX, NumtoUpChar(entity.BoxY), entity.BoxX, position)
  1365. }
  1366. func (s *SamplesInfoService) UpsertSearchTemplate(tablename, name, template string) error {
  1367. var num MyInt_Id
  1368. sql := "select count(1) Id from " + tablename
  1369. sql = sql + " where Name='" + name + "' limit 1 "
  1370. s.DBE.Sql(sql).Get(&num)
  1371. total := num.Id
  1372. var err error
  1373. if total > 0 {
  1374. // 更新
  1375. sql := fmt.Sprintf("update %s set Template='%s' where name='%s'", tablename, template, name)
  1376. _, err = s.DBE.Exec(sql)
  1377. } else {
  1378. //插入
  1379. //sql := "insert into " + tablename + "(Name, Template) values('" + name + "','" + template + "')"
  1380. //_, err = s.DBE.Exec(sql)
  1381. _, err = s.DBE.Table(tablename).Cols("Id", "Name", "Template").Insert(&SamplesSearchTemplate{
  1382. Name: name,
  1383. Template: template,
  1384. })
  1385. }
  1386. return err
  1387. }
  1388. func (s *SamplesInfoService) GetAllSearchTemplate(tablename string) ([]*SamplesSearchTemplate, error) {
  1389. templates := make([]*SamplesSearchTemplate, 0)
  1390. err := s.DBE.Table(tablename).Find(&templates)
  1391. return templates, err
  1392. }
  1393. func (s *SamplesInfoService) GetSearchTemplateByName(tablename, name string) (*SamplesSearchTemplate, error) {
  1394. var template SamplesSearchTemplate
  1395. _, err := s.DBE.Table(tablename).Where("Name='" + name + "'").Get(&template)
  1396. return &template, err
  1397. }
  1398. func (s *SamplesInfoService) DeleteSearchTemplate(tablename, name string) error {
  1399. sql := fmt.Sprintf("delete from %s where name='%s'", tablename, name)
  1400. _, err := s.DBE.Exec(sql)
  1401. return err
  1402. }
  1403. func (s *SamplesInfoService) CreateTemplateTable(tablename string) error {
  1404. sql := fmt.Sprintf(`create table %s (Id int(10) NOT NULL AUTO_INCREMENT, Name varchar(25), Template Text, PRIMARY KEY (Id), UNIQUE (Name))`, tablename)
  1405. _, err := s.DBE.Exec(sql)
  1406. return err
  1407. }