converseService.go 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  1. package converseService
  2. import (
  3. "crypto/md5"
  4. "dashoo.cn/genepoint_srv/business/arrangeService"
  5. "dashoo.cn/genepoint_srv/business/common"
  6. "fmt"
  7. "github.com/gogf/gf/os/gtime"
  8. "github.com/gogf/gf/util/gconv"
  9. "github.com/unknwon/com"
  10. "github.com/gogf/gf/os/glog"
  11. "log"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "xorm.io/xorm"
  16. "dashoo.cn/base_common/utils"
  17. . "dashoo.cn/base_common/utils/db"
  18. )
  19. const CU = 1
  20. const EE = 1
  21. const POS = 1
  22. const LTU = 1
  23. const GROUP = 1
  24. const TABLE_TAST = "sample_storage_task"
  25. const TABLE_SAMPLE_APPLY = "bank_apply_main"
  26. const TABLE_DETAIL = "bank_apply_detail"
  27. const TABLE_SHELF = "bank_shelf"
  28. const TABLE_BOX = "bank_box"
  29. const TABLE_EQUIPMENT = "bank_equipment"
  30. const TABLE_DEVICE_DATA = "GenePoint_Device_Report_Data"
  31. const TABLE_SAMPLE = "bank_sample"
  32. const TABLE_APPLY_DETAIL = "bank_apply_detail"
  33. const STATUS_LOCKED = "1"
  34. const STATUS_UNLOCK = "0"
  35. const RK_PASS = "2"
  36. const CK_PASS_ONHOLD = "3" //保留位置
  37. const CK_PASS_DONE = "4" //已归档
  38. const LOGIC_ID = "0" // 逻辑分区Id
  39. type ConverseService struct {
  40. ServiceBase
  41. }
  42. type ConverseSessionService struct {
  43. SessionBase
  44. }
  45. func GetConverseService(xormEngine *xorm.Engine) *ConverseService {
  46. s := new(ConverseService)
  47. s.DBE = xormEngine
  48. return s
  49. }
  50. func GetConverseSessionService(session *xorm.Session) *ConverseSessionService {
  51. s := new(ConverseSessionService)
  52. s.Session = session
  53. return s
  54. }
  55. //建立会话
  56. func (this *ConverseService) GetSessionSetupRequest(utc string) *RequestEntity {
  57. var request = "session_setup"
  58. var reqData RequestData
  59. reqData.Key = this.GetKeyBaseOnTime(utc)
  60. reqData.Client = "lims"
  61. var entity = this.GetRequestEntity(request, utc, reqData)
  62. return entity
  63. }
  64. //生成请求报文
  65. func (this *ConverseService) GetRequestEntity(request string, utc string, data RequestData) *RequestEntity {
  66. var entity RequestEntity
  67. entity.Request = request
  68. entity.Time = utc
  69. entity.Data = data
  70. return &entity
  71. }
  72. // 获取身份验证key
  73. // key的信息按照生物样本存储系统分配的私钥字符串+请求报文中的time字段的值进行MD5加密
  74. func (this *ConverseService) GetKeyBaseOnTime(utc string) string {
  75. privateKey, _ := utils.Cfg.GetValue("key", "privateKey")
  76. log.Println("身份验证Key", privateKey)
  77. //privateKey := "ZGlzdHJp23Yn4V06b3I6OGQ5NjllZWY2ZWNhZDNjMjlhM2E2MjkyODBlNjg2Y2YwYzNmNWQ1YTg2YWZmM2Nh3MTIwMjB3454jOTIzYWRjNmM5M4g"
  78. unencoded := privateKey + utc
  79. byte := md5.Sum([]byte(unencoded))
  80. encode := fmt.Sprintf("%x", byte)
  81. resultKey := strings.ToUpper(encode)
  82. return resultKey
  83. }
  84. //查询库存冻存盒信息
  85. func (this *ConverseService) CheckStockRackInfo(cu int, rack_id string, utc string) *RequestEntity {
  86. var request = "stock_rack"
  87. var data RequestData
  88. data.Rack_id = rack_id
  89. data.Cu = cu
  90. entity := this.GetRequestEntity(request, utc, data)
  91. return entity
  92. }
  93. //查询库存冻存管信息
  94. func (this *ConverseService) CheckStockTubeInfo(cu int, rack_id, tube_id, utc string) *RequestEntity {
  95. var request = "stock_rack_tube"
  96. var data RequestData
  97. data.Rack_id = rack_id
  98. data.Tube_id = tube_id
  99. data.Cu = cu
  100. entity := this.GetRequestEntity(request, utc, data)
  101. return entity
  102. }
  103. //任务撤销
  104. func (this *ConverseService) CancelTast(utc string, task_id string) *RequestEntity {
  105. var request = "task_change"
  106. var data RequestData
  107. data.Task_id = task_id
  108. data.Status = 1
  109. entity := this.GetRequestEntity(request, utc, data)
  110. return entity
  111. }
  112. //扫描新任务
  113. func (this *ConverseService) ScanNewTaskDetail() []Sample_Storage_Task {
  114. var tasks []Sample_Storage_Task
  115. where := "statuscode = 0"
  116. this.GetEntitysByWhere(TABLE_TAST, where, &tasks)
  117. return tasks
  118. }
  119. // 获取申请单创建人
  120. func (this *ConverseService) GetOperUser(EntryNo string) (user string) {
  121. // 获取当前任务的操作人
  122. sql := " select a.UserName from base_user a left join bank_apply_main b on a.id = b.CreateUserId where b.EntryNo ='" + EntryNo + "' "
  123. this.DBE.SQL(sql).Get(&user)
  124. return user
  125. }
  126. //新任务数量
  127. func (this *ConverseService) GetNewTaskBasic() []Sample_Storage_Task {
  128. //var count64 int64
  129. var tasks []Sample_Storage_Task
  130. //sql := "SELECT COUNT(DISTINCT(task_id)) FROM sample_storage_task WHERE status = 0"
  131. sql := "SELECT task_id, task_type, statuscode FROM " + TABLE_TAST + " GROUP BY task_id, task_type HAVING statuscode = 0"
  132. err := this.DBE.SQL(sql).Find(&tasks)
  133. if err != nil {
  134. log.Println(err)
  135. }
  136. return tasks
  137. }
  138. ////冻存管位置格式转换
  139. //func TransPositionToNo(boxType int, position string) int{
  140. // co := strings.Split(position,";")
  141. // x, _ := strconv.Atoi(co[0])
  142. // y, _ := strconv.Atoi(co[1])
  143. // var no int
  144. // switch boxType {
  145. // case 101,102,103:
  146. // no = (y-1)*14+x
  147. // break
  148. // case 0 :
  149. // no = (y-1)*10+x
  150. // }
  151. // return no
  152. //}
  153. // 生成请求实体,根据任务类型获取requestName
  154. func (this *ConverseService) GenerateRequestEntity(taskType int, task_id, user string) *RequestEntity {
  155. var requestName string
  156. switch taskType {
  157. case 1:
  158. requestName = "rack_storing"
  159. break
  160. case 2:
  161. requestName = "rack_retrieving"
  162. break
  163. case 3, 8:
  164. requestName = "tube_storing"
  165. break
  166. case 4:
  167. requestName = "tube_retrieving"
  168. break
  169. case 5:
  170. requestName = "stock_rack"
  171. break
  172. case 6:
  173. requestName = "stock_rack_tube"
  174. break
  175. case 7:
  176. requestName = "task_change"
  177. break
  178. case 9:
  179. // add by 徐春林 添加库内整理任务
  180. requestName = "moving"
  181. }
  182. utc := time.Now().UTC().Format(time.RFC3339)
  183. var data RequestData
  184. if requestName != "stock_rack" && requestName != "stock_rack_tube" {
  185. data.Task_id = task_id
  186. data.Type = "begin"
  187. // 自动化设备 必须要穿 Operation_user 用户信息
  188. data.Operation_user = user
  189. // 半自动化设备,不需要传 Operation_user 用户信息
  190. //data.Operation_user = ""
  191. }
  192. entity := this.GetRequestEntity(requestName, utc, data)
  193. return entity
  194. }
  195. // 按类型进行任务处理
  196. func (this *ConverseService) HandleTaskDetail(taskType int, entity *RequestEntity, detail *Sample_Storage_Task) {
  197. switch taskType {
  198. case 1: //冻存盒 入库
  199. handleRackStore(entity, detail)
  200. break
  201. case 2: //冻存盒出库
  202. handleRackRetrieve(entity, detail)
  203. break
  204. case 3, 8: //冻存管入库
  205. handleTubeStore(entity, detail)
  206. break
  207. case 4: // 冻存管出库
  208. handleTubeRetrieve(entity, detail)
  209. break
  210. case 5: // 获取设备库存情况及冻存盒信息
  211. handleQueryRack(entity, detail)
  212. break
  213. case 6: // 根据冻存盒编号查询冻存管信息
  214. handleQueryTube(entity, detail)
  215. break
  216. case 7: // 任务变更通知
  217. handleTaskChange(entity, detail)
  218. break
  219. case 9: // add by 徐春林 添加库内整理任务 手动库内整理
  220. handleArrange(entity, detail)
  221. }
  222. }
  223. //处理冻存盒入库任务信息
  224. func handleRackStore(entity *RequestEntity, detail *Sample_Storage_Task) {
  225. var ones = make([]Single, 0)
  226. var exist bool
  227. if entity.Data.Task_data != nil {
  228. ones = entity.Data.Task_data.([]Single)
  229. for idx, value := range ones {
  230. if value.Target.Cu == detail.Cu && value.Target.Unit == detail.Unit && value.Target.Pos == detail.Pos && value.Target.Ltu == detail.Ltu && value.Target.Group == detail.Group && value.Rack_id == detail.BoxBarcode {
  231. var tube Tube
  232. tube.Id = detail.BarCode
  233. ones[idx].Tubes = append(ones[idx].Tubes, tube)
  234. exist = true
  235. }
  236. }
  237. }
  238. if !exist {
  239. var one Single
  240. one.Rack = detail.BoxType
  241. one.Tube = detail.TubeType
  242. one.Rack_id = detail.BoxBarcode
  243. //one.Source.Cu = CU
  244. one.Source.Cu = detail.Cu
  245. one.Source.Ee = EE
  246. one.Source.Pos = POS
  247. //one.Target.Cu = CU
  248. one.Target.Cu = detail.Cu
  249. one.Target.Unit = detail.Unit
  250. one.Target.Pos = detail.Pos
  251. one.Target.Ltu = detail.Ltu
  252. //one.Target.Group = 1
  253. one.Target.Group = detail.Group
  254. if detail.BarCode != "" {
  255. var tube Tube
  256. tube.Id = detail.BarCode
  257. one.Tubes = append(one.Tubes, tube)
  258. } else {
  259. one.Tubes = make([]Tube, 0) //tubes不能为空, 否则Genepoint系统报错
  260. }
  261. ones = append(ones, one)
  262. }
  263. entity.Data.Task_data = ones
  264. }
  265. //处理冻存盒出库任务信息
  266. func handleRackRetrieve(entity *RequestEntity, detail *Sample_Storage_Task) {
  267. var ones []Single
  268. var one Single
  269. one.Tubes = make([]Tube, 0) //tubes不能为空, 否则Genepoint系统报错
  270. if entity.Data.Task_data != nil {
  271. // ones = make([]Single, 0)
  272. //}else{
  273. ones = entity.Data.Task_data.([]Single)
  274. }
  275. one.Rack_id = detail.BoxBarcode
  276. one.Target.Cu = detail.Cu //todo
  277. one.Target.Ee = EE
  278. one.Target.Pos = POS
  279. ones = append(ones, one)
  280. entity.Data.Task_data = ones
  281. }
  282. //处理冻存管入库任务信息
  283. func handleTubeStore(entity *RequestEntity, detail *Sample_Storage_Task) {
  284. var ones []Single
  285. var exist bool
  286. if entity.Data.Operation_mode == "" {
  287. entity.Data.Operation_mode = detail.OperaMode
  288. }
  289. // 盒子存在追加样本
  290. if entity.Data.Task_data != nil {
  291. ones = entity.Data.Task_data.([]Single)
  292. for idx, value := range ones {
  293. // 若目标盒一致,则追加样本
  294. isSampleTarget := value.Target.Cu == detail.Cu && value.Target.Ltu == detail.Ltu && value.Target.Group == detail.Group && value.Target.Unit == detail.Unit && value.Target.Pos == detail.Pos && value.Target.Rack_id == detail.BoxBarcode
  295. if isSampleTarget {
  296. var tube Tube
  297. if detail.OperaMode != "auto" {
  298. //tube.T_no = TransPositionToNo(detail.BoxType,detail.Position)
  299. //实际采用,暂时注释掉
  300. tube.T_no = detail.No
  301. }
  302. tube.Id = detail.BarCode
  303. ones[idx].Tubes = append(ones[idx].Tubes, tube)
  304. //value.Tubes = append(value.Tubes,tube)
  305. exist = true
  306. goto label
  307. }
  308. }
  309. }
  310. // 盒子不存在新加盒子及管信息
  311. if !exist {
  312. var one Single
  313. //one.Rack = 101 // 测试临时使用
  314. //one.Tube = 201 // 测试临时使用
  315. one.Rack = detail.BoxType // 样本库系统,没有添加该字段信息逻辑,临时注释
  316. one.Tube = detail.TubeType // 样本库系统,没有添加该字段信息逻辑,临时注释
  317. one.Rack_id = detail.SourceBox // 此处需要添加源盒编码 SourceBox
  318. //one.Source.Cu = CU // 测试临时使用
  319. one.Source.Cu = detail.Cu
  320. one.Source.Ee = EE
  321. one.Source.Pos = POS
  322. var tube Tube
  323. if detail.OperaMode != "auto" {
  324. one.Target.Cu = detail.Cu
  325. one.Target.Ltu = detail.Ltu
  326. one.Target.Group = detail.Group
  327. one.Target.Unit = detail.Unit
  328. one.Target.Pos = detail.Pos
  329. one.Target.Rack_id = detail.BoxBarcode
  330. //tube.T_no = TransPositionToNo(detail.BoxType,detail.Position)
  331. tube.T_no = detail.No //实际采用,暂时注释掉
  332. }
  333. tube.Id = detail.BarCode
  334. one.Tubes = append(one.Tubes, tube)
  335. ones = append(ones, one)
  336. }
  337. label:
  338. entity.Data.Task_data = ones
  339. }
  340. //处理冻存管出库任务信息
  341. func handleTubeRetrieve(entity *RequestEntity, detail *Sample_Storage_Task) {
  342. var one OutSingle
  343. //var exist bool
  344. if entity.Data.Task_data != nil {
  345. one = entity.Data.Task_data.(OutSingle)
  346. //exist = true
  347. }
  348. //if !exist {
  349. // //one.Target.Cu = CU
  350. // one.Target.Cu = detail.Cu
  351. // one.Target.Ee = EE
  352. // one.Target.Pos = POS
  353. //}
  354. var tube Tube
  355. tube.Id = detail.BarCode
  356. one.Tubes = append(one.Tubes, tube)
  357. entity.Data.Task_data = one
  358. }
  359. //任务撤销
  360. func handleTaskChange(entity *RequestEntity, detail *Sample_Storage_Task) {
  361. entity.Data.Status = detail.Status
  362. }
  363. //查询冻存盒库存信息
  364. func handleQueryRack(entity *RequestEntity, detail *Sample_Storage_Task) {
  365. entity.Data.Cu = detail.Cu
  366. entity.Data.Rack_id = detail.BoxBarcode
  367. }
  368. //查询冻存管库存信息
  369. func handleQueryTube(entity *RequestEntity, detail *Sample_Storage_Task) {
  370. entity.Data.Rack_id = detail.BoxBarcode
  371. entity.Data.Tube_id = detail.BarCode
  372. }
  373. /**
  374. * @Author 徐春林
  375. * @Description // 手动选择位置 库内整理
  376. * @Date 20:01 2021/3/29
  377. * @Param
  378. * @return
  379. **/
  380. func handleArrange(entity *RequestEntity, detail *Sample_Storage_Task) {
  381. var ones []Arrange
  382. if entity.Data.Task_data != nil {
  383. ones = entity.Data.Task_data.([]Arrange)
  384. }
  385. if entity.Data.Operation_mode == "" {
  386. entity.Data.Operation_mode = detail.OperaMode
  387. }
  388. position := Position{
  389. Cu: detail.Cu,
  390. Ltu: detail.Ltu,
  391. Group: detail.Group,
  392. Unit: detail.Unit,
  393. Pos: detail.Pos,
  394. }
  395. one := Arrange{
  396. Logic_id: 0,
  397. Position: position,
  398. }
  399. ones = append(ones, one)
  400. entity.Data.Operation_mode = detail.OperaMode
  401. entity.Data.Initiator = "genepoint"
  402. entity.Data.Name = "库内整理"
  403. entity.Data.Task_data = ones
  404. }
  405. /**
  406. * 卢传敏
  407. * 响应报文,任务执行提示
  408. */
  409. func handleTaskActivate(entity *RequestEntity, detail *Sample_Storage_Task) {
  410. }
  411. // 查询申请单详情下状态,存在一条子任务失败的情况,则整个申请单状态为失败
  412. func (this *ConverseService) StatusModifyWithDetail(task_id string, res ResponseEntity) error {
  413. status := SUCCESS
  414. // 关联查询申请详情表,申请主表中指定申请单下执行状态不为success的申请详情数量
  415. sql := "SELECT count(*) FROM `" + TABLE_APPLY_DETAIL + "` a JOIN `" + TABLE_SAMPLE_APPLY + "` b ON a.parentid = b.Id WHERE (b.entryno = '" + task_id + "' and a.taskstatus <> " + strconv.Itoa(SUCCESS) + ")"
  416. //total, err := this.DBE.Table(TABLE_APPLY_DETAIL ).Join("", TABLE_SAMPLE_APPLY ,"a.parentid = b.Id").Where(" a.parentid = b.Id and b.entryno = '" + task_id + "' and a.taskstatus <> " + strconv.Itoa(SUCCESS)).Count()
  417. //if err != nil && err != sql.ErrNoRows {
  418. // return err
  419. //}
  420. var total int
  421. //_, err := this.DBE.Exec(sql)
  422. this.DBE.SQL(sql).Get(&total)
  423. //total ,_ := result.RowsAffected()
  424. fmt.Println("数量:", total)
  425. // 存在未执行成功的子任务
  426. if total > 0 {
  427. status = FAILED
  428. }
  429. if res.Data.Type == "abnormal_end" {
  430. status = EXCEPTION
  431. }
  432. // 更新任务状态
  433. //updTaskSql := "UPDATE " + TABLE_TAST + " SET statuscode = '" + strconv.Itoa(status) + "' WHERE task_id = '" + task_id + "'"
  434. //_, err = this.DBE.Exec(updTaskSql)
  435. set := ""
  436. if res.Data.Type == "end" {
  437. set = " ,exception='' "
  438. }
  439. // 更新申请主表状态
  440. updApplySql := "UPDATE " + TABLE_SAMPLE_APPLY + " SET taskstatus = '" + strconv.Itoa(status) + "' " + set + " WHERE entryno = '" + task_id + "'"
  441. _, err := this.DBE.Exec(updApplySql)
  442. return err
  443. }
  444. //修改任务表状态码
  445. func (this *ConverseService) TaskStatusModify(statusCode int, task_id string) error {
  446. sql := "UPDATE " + TABLE_TAST + " SET statuscode = '" + strconv.Itoa(statusCode) + "' WHERE task_id = '" + task_id + "'"
  447. _, err := this.DBE.Exec(sql)
  448. return err
  449. }
  450. // 修改出入库单状态
  451. func (this *ConverseService) SampleApplyStatusModify(statusCode int, entryNo, msg string) error {
  452. set := " "
  453. if statusCode == 1 || statusCode == 2 || statusCode == 3 || statusCode == 5 {
  454. set = " ,exception='' "
  455. if statusCode == 5 {
  456. // 任务执行成功以后,需要记录执行时间
  457. set = " ,exception='', ExecutionTime = " + gconv.String(gtime.Now()) + ", ModifiedOn = " + gconv.String(gtime.Now())
  458. }
  459. } else {
  460. if msg != "" {
  461. set = " ,exception='" + msg + "' "
  462. } else {
  463. set = " ,exception='' "
  464. }
  465. }
  466. sql := "UPDATE " + TABLE_SAMPLE_APPLY + " SET taskstatus = '" + strconv.Itoa(statusCode) + "' " + set + " WHERE entryno = '" + entryNo + "'"
  467. _, err := this.DBE.Exec(sql)
  468. return err
  469. }
  470. //2020/12/18新增 根据当前任务报文返回状态批量更新当前任务下所有的样本状态
  471. func (this *ConverseService) SampleApplyDetailStatusModify(statusCode int, entryNo string) error {
  472. //获取出入库单Id
  473. getId := "SELECT id FROM " + TABLE_SAMPLE_APPLY + " WHERE entryno = '" + entryNo + "'"
  474. var idList []Id
  475. err := this.DBE.SQL(getId).Find(&idList)
  476. //批量更新全部成功或失败的任务样本状态
  477. sql := "UPDATE " + TABLE_APPLY_DETAIL + " SET taskstatus = '" + strconv.Itoa(statusCode) + "' WHERE parentid = '" + strconv.Itoa(idList[0].Id) + "' and taskstatus !=5"
  478. _, err = this.DBE.Exec(sql)
  479. return err
  480. }
  481. //任务执行驳回, 修改全部样本失败状态
  482. func (this *ConverseService) ModifySampleAllRejectStatus(res ResponseEntity) error {
  483. //获取出入库单Id
  484. getId := "SELECT id FROM " + TABLE_SAMPLE_APPLY + " WHERE entryno = '" + res.Data.Task_id + "'"
  485. var idList []Id
  486. err := this.DBE.SQL(getId).Find(&idList)
  487. //批量更新全部成功或失败的任务样本状态
  488. sql := "UPDATE " + TABLE_APPLY_DETAIL + " SET taskstatus = '" + strconv.Itoa(REJECT) + "' WHERE parentid = '" + strconv.Itoa(idList[0].Id) + "'"
  489. _, err = this.DBE.Exec(sql)
  490. return err
  491. }
  492. //修改样本失败状态
  493. func (this *ConverseService) handleSampleFailed(idsStr string, response string, parentId int) {
  494. if strings.Contains(response, "tube_") {
  495. // 更新任务明细信息
  496. updatesql := "UPDATE " + TABLE_APPLY_DETAIL + " SET taskstatus = '" + strconv.Itoa(FAILED) + "' WHERE barcode IN (" + idsStr + ")" + " AND parentid = '" + strconv.Itoa(parentId) + "'"
  497. this.DBE.Exec(updatesql)
  498. // 更新任务状态
  499. updTaskSql := "UPDATE " + TABLE_TAST + " SET statuscode = '" + strconv.Itoa(FAILED) + "' WHERE barcode IN (" + idsStr + ")"
  500. this.DBE.Exec(updTaskSql)
  501. }
  502. }
  503. //处理样本执行成功状态
  504. func (this *ConverseService) handleSampleSuccess(idsStr string, response string, applyMainId int) {
  505. if strings.Contains(response, "tube_storing") {
  506. this.sampleRKSuccessDBModify(idsStr, applyMainId)
  507. } else if strings.Contains(response, "tube_retrieving") {
  508. this.sampleCKSuccessDBModify(idsStr, applyMainId)
  509. }
  510. }
  511. //入库成功, 修改样本数据库状态信息
  512. func (this *ConverseService) sampleRKSuccessDBModify(idsStr string, parentId int) error {
  513. fmt.Println("开始更新样本入库信息, 包括更新容量,状态, 锁定状态")
  514. // add by 徐春林 基点入库成功, 根据样本入库详情表中 更新 样本的 剩余容量
  515. var list []Bank_Apply_Detail
  516. // add by 徐春林 20211224 添加主单Id 作为条件
  517. err := this.DBE.Where("barcode IN (" + idsStr + ") and ParentId = '" + strconv.Itoa(parentId) + "'").Find(&list)
  518. for _, one := range list {
  519. fmt.Println("更新入库容量:", one.BarCode)
  520. sql := "UPDATE " + TABLE_SAMPLE + " a SET Capacity = (SELECT Capacity from " + TABLE_APPLY_DETAIL + " b where b.BarCode = '" + one.BarCode + "' and b.ParentId = '" + strconv.Itoa(parentId) + "' ) Where barcode = '" + one.BarCode + "'"
  521. _, err = this.DBE.Exec(sql)
  522. }
  523. updatesql := "UPDATE " + TABLE_APPLY_DETAIL + " SET IsLocked = '" + STATUS_UNLOCK + "', taskstatus = '" + strconv.Itoa(SUCCESS) + "' WHERE barcode IN (" + idsStr + ")" + " AND parentid = '" + strconv.Itoa(parentId) + "'"
  524. // 更新样本状态解锁,原盒子id置为0
  525. updatesql2 := "UPDATE " + TABLE_SAMPLE + " SET IsLocked = '" + STATUS_UNLOCK + "', IState = '" + RK_PASS + "',SourceBoxId = 0 WHERE barcode IN (" + idsStr + ")"
  526. //更新每个样本的孔位信息
  527. _, err = this.DBE.Exec(updatesql)
  528. _, err = this.DBE.Exec(updatesql2)
  529. return err
  530. }
  531. //出库成功, 修改样本数据库状态信息
  532. func (this *ConverseService) sampleCKSuccessDBModify(idsStr string, parentId int) error {
  533. var list []Bank_Apply_Detail
  534. var onHold []string
  535. var done []string
  536. // add by 徐春林 20211224 添加主单Id 作为条件
  537. err := this.DBE.Where("barcode IN (" + idsStr + ") and ParentId = '" + strconv.Itoa(parentId) + "'").Find(&list)
  538. for _, one := range list {
  539. // add by 记录冻融次数
  540. //beforeSql := "UPDATE "+TABLE_SAMPLE+" a SET a.FreezingNum = ( select FreezingNum from "+TABLE_APPLY_DETAIL+" b where b.BarCode = '"+one.BarCode+"' and b.ParentId = '"+ strconv.Itoa(parentId)+"' ) Where barcode = '"+one.BarCode+"'"
  541. beforeSql := "UPDATE " + TABLE_SAMPLE + " a SET a.FreezingNum = (a.FreezingNum +1 ) Where barcode = '" + one.BarCode + "'"
  542. _, err = this.DBE.Exec(beforeSql)
  543. if strconv.Itoa(one.RecoveryId) == CK_PASS_ONHOLD {
  544. // 查询样本出库详情, 更新 取出帶放回
  545. onHold = append(onHold, one.BarCode)
  546. } else if strconv.Itoa(one.RecoveryId) == CK_PASS_DONE {
  547. // 查询样本出库详情, 更新 取出归档
  548. done = append(done, one.BarCode)
  549. }
  550. }
  551. // 基点出库取出待放回, 清空样本位置信息, 但是状态保留为 取出待放回
  552. // sql := "UPDATE " + TABLE_SAMPLE + " SET istate = '" + CK_PASS_ONHOLD + "', islocked = '" + STATUS_UNLOCK + "' WHERE barcode IN ('" + strings.Join(onHold, "','") + "')"
  553. sql := "UPDATE " + TABLE_SAMPLE + " SET istate = '" + CK_PASS_ONHOLD + "', islocked = '" + STATUS_UNLOCK + "',equipmentid = '0', shelfid = '0',boxid= '0', position = '', positioninfo = '' WHERE barcode IN ('" + strings.Join(onHold, "','") + "')"
  554. sql2 := "UPDATE " + TABLE_SAMPLE + " SET istate = '" + CK_PASS_DONE + "', islocked = '" + STATUS_UNLOCK + "', equipmentid = '0', shelfid = '0',boxid= '0', position = '', positioninfo = '' WHERE barcode IN ('" + strings.Join(done, "','") + "')"
  555. sql3 := "UPDATE " + TABLE_APPLY_DETAIL + " SET IsLocked = '" + STATUS_UNLOCK + "', taskstatus = '" + strconv.Itoa(SUCCESS) + "' WHERE barcode IN (" + idsStr + ")" + " AND parentid = '" + strconv.Itoa(parentId) + "'"
  556. _, err = this.DBE.Exec(sql)
  557. _, err = this.DBE.Exec(sql2)
  558. _, err = this.DBE.Exec(sql3)
  559. return err
  560. }
  561. //获取出入库单信息
  562. func (this *ConverseService) GetApplyMain(entryNo string) Bank_Apply_Main {
  563. where := "entryNo = '" + entryNo + "'"
  564. var entity Bank_Apply_Main
  565. this.GetEntity(&entity, where)
  566. return entity
  567. }
  568. //根据出入库单信息判断任务信息, 并修改样本状态
  569. func (this *ConverseService) ModifySampleStatusByApplyMainInfo(entryNo string, resp string, entity ResponseEntity) {
  570. applyMain := this.GetApplyMain(entryNo)
  571. fmt.Println("resp:", resp)
  572. var operaIds, abnormalIds, taskType string
  573. // 此处判断,如果是冻存盒整盒入库,需要更新盒子位置信息 盒 冻存盒当前的 所在的冻存架的id
  574. if resp == "rack_storing" {
  575. fmt.Println("冻存盒入库")
  576. this.UpdateBoxInfo(entity, applyMain.Id)
  577. }
  578. if applyMain.ApplyType == TASK_TUBE_STORING && strings.Contains(resp, "_storing") {
  579. fmt.Println("开始入库1", entity)
  580. taskType = "tube_storing" // 冻存管入库
  581. // 获取返回报文中成功的样本 和失败的样本
  582. operaIds, abnormalIds = this.getOperaSampleIdsStr(applyMain.ApplyType, applyMain.Id, resp, entity)
  583. for _, value := range entity.Data.Actual_data {
  584. //获取报文中的 盒子信息
  585. var Rack_id string = value.Target.Rack_id
  586. fmt.Println("冻存盒盒子编号:", Rack_id)
  587. // 获取报文中的 位置信息和 样本编码
  588. var tubes = value.Tubes
  589. fmt.Println("样本位置信息:", tubes)
  590. //新增方法 更新位置信息
  591. this.UpdatePosition(Rack_id, tubes, entity, applyMain.Id)
  592. }
  593. } else if applyMain.ApplyType == TASK_TUBE_RETREIVING && strings.Contains(resp, "_storing") {
  594. fmt.Println("开始入库2")
  595. //lite设备 部分管子出库
  596. taskType = "tube_retrieving"
  597. operaIds, abnormalIds = this.getOperaSampleIdsStr(applyMain.ApplyType, applyMain.Id, resp, entity)
  598. } else if applyMain.ApplyType == TASK_TUBE_RETREIVING && strings.Contains(resp, "_retrieving") /*resp == "rack_retrieving"*/ /*&& entity.Data.Is_end*/ {
  599. fmt.Println("开始出库3")
  600. //lite设备 整盒出库
  601. taskType = "tube_retrieving"
  602. //operaIds, abnormalIds = this.getAllSampleIdsFromResEntity(entity)
  603. operaIds, abnormalIds = this.getOperaSampleIdsStr(applyMain.ApplyType, applyMain.Id, resp, entity)
  604. // 管出库需要 更新 转运空盒 为 源盒
  605. this.UpdateBoxInfoCK(entity, applyMain.Id)
  606. }
  607. // 此处判断,如果是冻存盒整盒入库,需要更新盒子位置信息 盒 冻存盒当前的 所在的冻存架的id
  608. if resp == "rack_retrieving" {
  609. fmt.Println("冻存盒出库")
  610. //rack_retrieving
  611. // 冻存盒出库任务完成以后,需要清空冻存盒中的冻存架信息,同时清空
  612. this.UpdateBoxInfoCK(entity, applyMain.Id)
  613. }
  614. // 根据管操作 或 盒操作 判断是否执行以下更新
  615. if (applyMain.ApplyType == TASK_TUBE_STORING && strings.Contains(resp, "_storing")) || (applyMain.ApplyType == TASK_TUBE_RETREIVING && strings.Contains(resp, "_retrieving")) {
  616. this.handleSampleSuccess(operaIds, taskType, applyMain.Id)
  617. if entity.Data.Type == "abnormal_end" {
  618. this.handleSampleFailed(abnormalIds, taskType, applyMain.Id)
  619. }
  620. }
  621. }
  622. //2021/01/09 by 卢传敏
  623. // 根据入库返回报文,更新 冻存盒的信息盒
  624. func (this *ConverseService) UpdateBoxInfo(entity ResponseEntity, parentId int) (err error) {
  625. //1. 获取当前返回的 冻存架信息,
  626. var shelf Shelf
  627. var equipment Bank_Equipment
  628. glog.Info("entity.Data.Actual_data::", entity.Data.Actual_data)
  629. // 判断是否存在处理成功的数据
  630. if len(entity.Data.Actual_data) > 0 {
  631. // 循环处理返回的数据,如果出现多盒存入的分批处理
  632. for i := range entity.Data.Actual_data {
  633. glog.Info("操作地 ", i, " 条数据")
  634. boxBarcode := entity.Data.Actual_data[i].Rack_id // 冻存盒编号
  635. Cu := entity.Data.Actual_data[i].Target.Cu // 设备编号
  636. Ltu := entity.Data.Actual_data[i].Target.Ltu //
  637. Unit := entity.Data.Actual_data[i].Target.Unit
  638. Pos := entity.Data.Actual_data[i].Target.Pos
  639. Group := entity.Data.Actual_data[i].Target.Group
  640. fmt.Println("保存返回的冻存盒位置信息:cu:", Cu, ",Ltu", Ltu, ",Unit", Unit, ",Group", Group, ",Pos", Pos)
  641. // 判断是否是冷库,如果是冷库的话,查询容器架子信息时不需要组装 unit
  642. lengSql := " select * from bank_equipment where cu = '" + utils.ToStr(Cu) + "' and Ltu = '" + utils.ToStr(Ltu) + "' "
  643. this.DBE.SQL(lengSql).Get(&equipment)
  644. sql := "select b.id EquipmentId,b.code,b.name,a.id,a.XStation,a.YStation from bank_shelf a left join bank_equipment b on a.EquipmentId = b.id where b.cu = '" + utils.ToStr(Cu) + "' " +
  645. "and a.Ltu = '" + utils.ToStr(Ltu) + "' and a.Group ='" + utils.ToStr(Group) + "'"
  646. // 冷库设备
  647. YStation := " "
  648. if equipment.Ltu == 0 {
  649. sql += " and a.Unit ='" + utils.ToStr(Unit) + "' "
  650. YStation = " , YStation =1 "
  651. fmt.Println("保存返回的冻存盒位置信息:YStation1:1 ", YStation)
  652. } else {
  653. YStation = " , YStation = " + utils.ToStr(Unit)
  654. fmt.Println("保存返回的冻存盒位置信息:cYStation1:2 ", YStation)
  655. }
  656. this.DBE.SQL(sql).Get(&shelf)
  657. fmt.Println("冻存架信息:", shelf.Code, shelf.Name, shelf.Id, shelf.XStation, shelf.YStation)
  658. // 更新盒所在的冻存架的信息 计算位置信息,并更新
  659. // 临时处理逻辑,默认 pos 为 盒子的 XStation
  660. pos := shelf.Code + "-" + common.Boxlinename(shelf.YStation) + utils.ToStr(shelf.XStation) + "-" +
  661. "" + common.Boxlinename(Unit) + utils.ToStr(utils.ToStr(Pos))
  662. // 冻存盒入库需要先校验,当前盒子是否存在样本,如果不存在,则证明盒子为空盒,修改冻存盒状态为空盒
  663. IsEmptysql := "update bank_box a set a.IsEmpty = (case when (select count(b.Id) from bank_sample b where b.DeletionStateCode!=1 and a.Id = b.BoxId ) > 0 then 0 else 1 end) where a.Barcode = '" + boxBarcode + "'"
  664. this.DBE.Exec(IsEmptysql)
  665. possql := "update bank_box set XStation ='" + utils.ToStr(Pos) + "' " + YStation + "," +
  666. " EquipmentId ='" + com.ToStr(shelf.EquipmentId) + "'," +
  667. " shelfId =" + utils.ToStr(shelf.Id) + ",IsLocked = 0 ," +
  668. " Position ='" + pos + "'" +
  669. " where Barcode ='" + boxBarcode + "' "
  670. // 冷库设备
  671. _, err = this.DBE.Exec(possql)
  672. // 更新 当前冰箱冻存盒容量 信息
  673. _, err = this.DBE.Exec(" update bank_currboxcapacity set `A" + utils.ToStr(Pos) + "`= -1 where shelfId =" + utils.ToStr(shelf.Id) + "")
  674. }
  675. }
  676. err = this.UpdateBoxAbnormalInfo(entity, parentId)
  677. return err
  678. }
  679. func (this *ConverseService) UpdateBoxAbnormalInfo(entity ResponseEntity, parentId int) (err error) {
  680. glog.Info("entity.Data.Abnormal_data::", entity.Data.Abnormal_data)
  681. AbnormalRack := ""
  682. // 判断是否存在处理失败的数据
  683. rackEntity := entity.Data.Abnormal_data.Racks
  684. if rackEntity != nil {
  685. // 获取处理失败的 数据 判断失败的盒是否为空
  686. if len(rackEntity) > 0 {
  687. for _, v := range rackEntity {
  688. // 不为空则需要将数据取出
  689. AbnormalRack += v.Rack_id + ","
  690. }
  691. AbnormalRack = AbnormalRack[0 : len(AbnormalRack)-1]
  692. // 更新盒 的任务状态
  693. // 处理异常任务数据
  694. updatesql := "UPDATE " + TABLE_SAMPLE_APPLY + " SET taskstatus = '" + strconv.Itoa(FAILED) + "' WHERE parentid = '" + strconv.Itoa(parentId) + "'"
  695. _, err = this.DBE.Exec(updatesql)
  696. if err != nil {
  697. return err
  698. }
  699. // 更新任务状态
  700. updTaskSql := "UPDATE " + TABLE_TAST + " SET statuscode = '" + strconv.Itoa(FAILED) + "' WHERE BoxBarcode IN (" + AbnormalRack + ")"
  701. _, err = this.DBE.Exec(updTaskSql)
  702. if err != nil {
  703. return err
  704. }
  705. this.UpdateBoxIsLocked(utils.ToStr(parentId))
  706. }
  707. }
  708. return err
  709. }
  710. //2021/01/09 by 卢传敏
  711. // 根据出库返回报文,更新 冻存盒的信息盒
  712. func (this *ConverseService) UpdateBoxInfoCK(entity ResponseEntity, parentId int) (err error) {
  713. //1. 获取当前返回的 冻存架信息,
  714. var shelf Shelf
  715. // 循环处理返回的数据,如果出现多盒存入的分批处理
  716. for i := range entity.Data.Actual_data {
  717. TaskId := entity.Data.Task_id
  718. boxBarcode := entity.Data.Actual_data[i].Rack_id // 冻存盒编号
  719. Pos := entity.Data.Actual_data[i].Target.Pos
  720. sql := " select shelfId , EquipmentId from bank_box where code = '" + boxBarcode + "'"
  721. this.DBE.SQL(sql).Get(&shelf)
  722. fmt.Println("冻存架信息:", shelf.Id, shelf.XStation, shelf.YStation)
  723. // 更新盒所在的冻存架的信息 计算位置信息,并更新
  724. // 临时处理逻辑,默认 pos 为 盒子的 XStation
  725. _, err = this.DBE.Exec("update bank_box set EquipmentId=null,shelfId=null,XStation =null,YStation =null,IsLocked = 0,Position =null where Barcode ='" + boxBarcode + "' ")
  726. // 更新 当前冰箱冻存盒容量 信息
  727. _, err = this.DBE.Exec(" update bank_currboxcapacity set `A" + utils.ToStr(Pos) + "`= -2 where shelfId =" + utils.ToStr(shelf.Id) + "")
  728. updatesql := "UPDATE " + TABLE_DETAIL + " SET taskstatus = '" + strconv.Itoa(SUCCESS) + "' WHERE parentid = '" + strconv.Itoa(parentId) + "'"
  729. _, err = this.DBE.Exec(updatesql)
  730. if err != nil {
  731. return err
  732. }
  733. // 更新任务状态
  734. updTaskSql := "UPDATE " + TABLE_TAST + " SET statuscode = '" + strconv.Itoa(SUCCESS) + "' WHERE BoxBarcode = '" + boxBarcode + "' and task_id = '" + TaskId + "'"
  735. _, err = this.DBE.Exec(updTaskSql)
  736. if err != nil {
  737. return err
  738. }
  739. }
  740. err = this.UpdateBoxAbnormalInfo(entity, parentId)
  741. return err
  742. }
  743. /**
  744. * @Author 徐春林
  745. * @Description // 盒任务执行失败 , 更新 盒锁定状态为 解锁
  746. * @Date 19:17 2021/3/22
  747. * @Param
  748. * @return
  749. **/
  750. func (this *ConverseService) UpdateBoxIsLocked(taskId string) (err error) {
  751. var task Sample_Storage_Task
  752. sql := "SELECT id FROM " + TABLE_TAST + " WHERE taskId = '" + taskId + "'"
  753. err = this.DBE.SQL(sql).Find(&task)
  754. _, err = this.DBE.Exec("update bank_box set IsLocked = 0 where Barcode ='" + task.BoxId + "' ")
  755. return err
  756. }
  757. /**
  758. 修改detail 信息
  759. */
  760. func (this *ConverseService) UpdateDetail(BarCode string, entity ResponseEntity, parentId int, rack_id string) {
  761. glog.Info("修改detail:", entity)
  762. var shelf Shelf
  763. var equipment Bank_Equipment
  764. var equipmentid string
  765. var ShelfId string
  766. for i := range entity.Data.Actual_data {
  767. Cu := entity.Data.Actual_data[i].Target.Cu // 设备编号
  768. Ltu := entity.Data.Actual_data[i].Target.Ltu //
  769. Unit := entity.Data.Actual_data[i].Target.Unit
  770. Group := entity.Data.Actual_data[i].Target.Group
  771. //当 检测到数据 与当前操作盒子的数据一直时,更新 detail 和sample 信息
  772. if entity.Data.Actual_data[i].Target.Rack_id == rack_id {
  773. fmt.Println("保存返回的冻存盒位置信息:cu:", Cu, ",Ltu", Ltu, ",Unit", Unit, ",Group", Group)
  774. // 判断是否是冷库,如果是冷库的话,查询容器架子信息时不需要组装 unit
  775. lengSql := " select * from bank_equipment where cu = '" + utils.ToStr(Cu) + "' and Ltu = '" + utils.ToStr(Ltu) + "' "
  776. this.DBE.SQL(lengSql).Get(&equipment)
  777. sql := "select b.id EquipmentId,a.id,a.XStation,a.YStation from bank_shelf a left join bank_equipment b on a.EquipmentId = b.id where b.cu = '" + utils.ToStr(Cu) + "' " +
  778. "and a.Ltu = '" + utils.ToStr(Ltu) + "' and a.Group ='" + utils.ToStr(Group) + "'"
  779. // 冷库设备
  780. if equipment.Ltu == 0 {
  781. sql += " and a.Unit ='" + utils.ToStr(Unit) + "' "
  782. }
  783. this.DBE.SQL(sql).Get(&shelf)
  784. equipmentid = strconv.Itoa(shelf.EquipmentId)
  785. ShelfId = strconv.Itoa(shelf.Id)
  786. sql = "Update bank_apply_detail Set EquipmentId=" + equipmentid + ",ShelfId=" + ShelfId + " where BarCode='" + BarCode + "' and ParentId=" + strconv.Itoa(parentId)
  787. this.DBE.Exec(sql)
  788. sql = "update bank_sample set EquipmentId=" + equipmentid + ",ShelfId=" + ShelfId + " where barcode ='" + BarCode + "' "
  789. this.DBE.Exec(sql)
  790. }
  791. }
  792. }
  793. // 20201026 卢传敏新增根据返回报文,更新位置信息
  794. /**
  795. * rack_id 盒子编号
  796. * tubes 报文中返回的 位置和样本条码
  797. */
  798. func (this *ConverseService) UpdatePosition(rack_id string, tubes []Tube, entity ResponseEntity, parentId int) {
  799. // 根据返回的报文中的盒子标识,获取盒子类型
  800. sql := "select Id,RowNum,ColumnNum,EquipmentId,ShelfId,XStation,YStation from bank_box where Barcode ='" + rack_id + "'"
  801. var box Box
  802. this.DBE.SQL(sql).Get(&box)
  803. glog.Info("盒信息: ", box)
  804. glog.Info("容器Id: ", box.EquipmentId, ";字符串:", utils.ToStr(box.EquipmentId))
  805. // 根据盒子信息获取容器和冻存架信息, 用于更新样本孔位信息
  806. // 获取容器信息
  807. equipmentsql := " select Id,Code,Name from bank_equipment where id = '" + box.EquipmentId + "' "
  808. var equipmentInfo EquipmentInfo
  809. this.DBE.SQL(equipmentsql).Get(&equipmentInfo)
  810. // 获取架子信息
  811. shelfsql := " select Id,XStation,YStation from bank_shelf where id = '" + box.ShelfId + "' "
  812. var shelf Shelf
  813. this.DBE.SQL(shelfsql).Get(&shelf)
  814. RowNum := box.RowNum
  815. ColumnNum := box.ColumnNum
  816. glog.Info("冻存盒的行数:", RowNum, ";冻存盒的列数:", ColumnNum)
  817. for i := 0; i < len(tubes); i++ {
  818. no := tubes[i].No
  819. id := tubes[i].Id
  820. glog.Info("样本在盒子中的位置:", no, ";样本条码: ", id)
  821. //计算管子在盒子中的坐标
  822. var box_x, box_y int
  823. if no%ColumnNum == 0 { // 如果 取余数为0 则 证明该数是能被 10 整除的数 为该行 最后一个孔位
  824. box_y = ColumnNum
  825. box_x = no / ColumnNum
  826. } else {
  827. box_y = no % ColumnNum
  828. box_x = (no / ColumnNum) + 1
  829. }
  830. glog.Info("管子在盒子中的位置坐标为:", box_y, ";", box_x)
  831. //var position = utils.NumberToLetter(box_x) + utils.ToStr(box_y)
  832. position := equipmentInfo.Code + "-" + common.Boxlinename(shelf.YStation) + utils.ToStr(shelf.XStation) + "-" +
  833. "" + common.Boxlinename(box.YStation) + utils.ToStr(box.XStation) + "-" + common.Boxlinename(box_x) + utils.ToStr(box_y)
  834. sql = "update bank_sample set BoxId = '" + utils.ToStr(box.Id) + "',Position = '" + utils.ToStr(box_y) + ";" +
  835. "" + utils.ToStr(box_x) + "',PositionInfo = '" + position + "' " +
  836. // " , unit = '"+ utils.ToStr( entity.Data.Actual_data[i].Target.Unit)+"'" +
  837. " where barcode ='" + id + "' "
  838. //执行sql 更新位置和坐标信息
  839. this.DBE.Exec(sql)
  840. this.UpdateDetail(id, entity, parentId, rack_id)
  841. //卢传敏 新增更新盒子位置信息操作,因为新的业务处理,冻存管必须放在冻存盒中才能入库
  842. this.UpdateBoxInfo(entity, parentId)
  843. }
  844. }
  845. //获取当前操作样本的id, 正常和异常的
  846. func (this *ConverseService) getOperaSampleIdsStr(applyType, parentId int, resp string, entity ResponseEntity) (actualIds string, abnormalIds string) {
  847. //获取出入库单中所有样本信息
  848. var list []Bank_Apply_Detail
  849. where := " parentid = '" + strconv.Itoa(parentId) + "'"
  850. this.GetEntitysByWhere(TABLE_DETAIL, where, &list)
  851. var sampleIds []string
  852. for _, detail := range list {
  853. sampleIds = append(sampleIds, detail.BarCode)
  854. }
  855. //获取反馈报文中的所有成功样本Id列表
  856. var actualTubeIds []string
  857. for _, single := range entity.Data.Actual_data {
  858. for _, tube := range single.Tubes {
  859. actualTubeIds = append(actualTubeIds, tube.Id)
  860. }
  861. }
  862. //获取反馈报文中的所有异常样本Id列表
  863. var abnormalTubeIds []string
  864. for _, single := range entity.Data.Abnormal_data.Tubes {
  865. abnormalTubeIds = append(abnormalTubeIds, single.Id)
  866. }
  867. var operaTubeIds []string
  868. var failTubeIds []string
  869. if applyType == TASK_TUBE_STORING {
  870. for _, sampleId := range sampleIds {
  871. //操作成功样本Id
  872. for _, tubeId := range actualTubeIds {
  873. if sampleId == tubeId {
  874. operaTubeIds = append(operaTubeIds, tubeId)
  875. break
  876. }
  877. }
  878. //操作失败样本Id
  879. for _, tubeId := range abnormalTubeIds {
  880. if sampleId == tubeId {
  881. failTubeIds = append(failTubeIds, tubeId)
  882. break
  883. }
  884. }
  885. }
  886. }
  887. if applyType == TASK_TUBE_RETREIVING {
  888. for _, sampleId := range sampleIds {
  889. //操作成功样本Id
  890. for _, tubeId := range actualTubeIds {
  891. if sampleId == tubeId {
  892. operaTubeIds = append(operaTubeIds, sampleId)
  893. }
  894. }
  895. //操作失败样本Id
  896. for _, tubeId := range abnormalTubeIds {
  897. if sampleId == tubeId {
  898. failTubeIds = append(failTubeIds, tubeId)
  899. break
  900. }
  901. }
  902. }
  903. }
  904. actualIds = "'" + strings.Join(operaTubeIds, "','") + "'"
  905. abnormalIds = "'" + strings.Join(failTubeIds, ",") + "'"
  906. glog.Info("处理成功数据:", actualIds)
  907. glog.Info("处理失败数据:", abnormalIds)
  908. return
  909. }
  910. //当任务为rack_retrieving 且 end 时, 获取所有样本Ids
  911. func (this *ConverseService) getAllSampleIdsFromResEntity(entity ResponseEntity) (actualIds string, abnormalIds string) {
  912. //获取反馈报文中的所有成功样本Id列表
  913. var actualTubeIds []string
  914. for _, single := range entity.Data.Actual_data {
  915. for _, tube := range single.Tubes {
  916. actualTubeIds = append(actualTubeIds, tube.Id)
  917. }
  918. }
  919. actualIds = "'" + strings.Join(actualTubeIds, "','") + "'"
  920. abnormalIds = ""
  921. return
  922. }
  923. //保存异常信息
  924. func (this *ConverseService) SaveTaskExcepMsg(msg, taskId string) error {
  925. sql := "UPDATE " + TABLE_TAST + " SET exception = '" + msg + "' WHERE task_id = '" + taskId + "'"
  926. _, err := this.DBE.Exec(sql)
  927. return err
  928. }
  929. //保存异常信息
  930. func (this *ConverseService) SaveSampleExcepMsg(msg, entryNo string) error {
  931. sql := "UPDATE " + TABLE_SAMPLE_APPLY + " SET exception = '" + msg + "' WHERE entryno = '" + entryNo + "'"
  932. _, err := this.DBE.Exec(sql)
  933. return err
  934. }
  935. //根据条件删除数据,不记录日志
  936. func (this *ConverseService) DeleteEntityBytbl(tablename string, where string) (err error) {
  937. if where == "" {
  938. where = "1=2"
  939. }
  940. sql := `delete from ` + tablename + ` where ` + where
  941. _, err = this.DBE.Exec(sql)
  942. return
  943. }
  944. //样本库分表
  945. func (s *ConverseSessionService) InsertEntityBytbl(tablename string, entity interface{}) (affected int64, err error) {
  946. affected, err = s.Session.Table(tablename).Insert(entity)
  947. LogError(err)
  948. return
  949. }
  950. //获取设备名称
  951. func (s *ConverseService) GetEquipmentName(cu int) string {
  952. sql := "SELECT name from " + TABLE_EQUIPMENT + " where cu = '" + strconv.Itoa(cu) + "'"
  953. var entity Equipment
  954. s.DBE.SQL(sql).Get(&entity)
  955. return entity.Name
  956. }
  957. //异常信息转换
  958. func (s *ConverseService) HandleCauses(causes []Cause) string {
  959. var exceptInfo string
  960. for _, cause := range causes {
  961. eName := s.GetEquipmentName(cause.Cu)
  962. exceptInfo += s.GetReason(cause, eName)
  963. }
  964. return exceptInfo
  965. }
  966. //根据异常码获取异常原因
  967. func (s *ConverseService) GetReason(cause Cause, equipmentName string) string {
  968. var reasonStr string
  969. switch cause.Reason {
  970. case 1:
  971. reasonStr = "存储位不够;"
  972. break
  973. case 2:
  974. reasonStr = "单次任务涉及的冻存盒数量超出限制;"
  975. break
  976. case 3:
  977. reasonStr = "指定的位置已经被占用或者位置不正确,无法存入;"
  978. break
  979. case 4:
  980. reasonStr = "没有权限对指定位置进行操作;"
  981. break
  982. case 5:
  983. reasonStr = "指定的编号不正确,无法完成操作;"
  984. break
  985. case 6:
  986. reasonStr = "报文数据不完整,缺少冻存管数据;"
  987. break
  988. case 7:
  989. reasonStr = "相同的冻存盒位置只允许有一组报文数据;"
  990. break
  991. case 8:
  992. reasonStr = "半自动设备在不配置转运机器人的情况下,一个存管任务只允许操作一台设备;"
  993. default:
  994. reasonStr = cause.Msg
  995. }
  996. if cause.Cu != 0 {
  997. //reasonStr = strconv.Itoa(cause.Cu)+"号设备, " + equipmentName + "," + reasonStr
  998. reasonStr = "设备: " + equipmentName + "," + reasonStr
  999. }
  1000. return reasonStr
  1001. }
  1002. //处理Exceptions异常信息
  1003. func (s *ConverseService) HandleExceptions(excepts []Exception) string {
  1004. var exceptStr string
  1005. for _, except := range excepts {
  1006. eName := s.GetEquipmentName(except.Cu)
  1007. for _, code := range except.Codes {
  1008. //var tempStr = string(except.Cu)+"号设备," + eName + ","
  1009. var tempStr = "设备: " + eName + ","
  1010. switch code {
  1011. case 40200:
  1012. tempStr += "移动盒子过程中设备故障,动作失败;"
  1013. break
  1014. case 40201:
  1015. tempStr += "移动盒子过程中目标位置不为空,动作取消;"
  1016. break
  1017. case 40202:
  1018. tempStr += "移动盒子过程中起始位置为空,动作取消;"
  1019. break
  1020. case 40300:
  1021. tempStr += "移动管子过程中设备故障,动作失败;"
  1022. break
  1023. case 40301:
  1024. tempStr = "移动管子过程中目标位置不为空,动作取消;"
  1025. break
  1026. case 40302:
  1027. tempStr = "移动管子过程中起始位置为空,动作取消;"
  1028. }
  1029. exceptStr += tempStr
  1030. }
  1031. }
  1032. return exceptStr
  1033. }
  1034. //处理返回设备状态数据, 并存储到数据表里
  1035. func RecordDeviceData(list []List_Data) {
  1036. t := time.Now()
  1037. var data []GenePoint_Device_Report_Data
  1038. for _, one := range list {
  1039. var piece GenePoint_Device_Report_Data
  1040. piece.Cu = one.Cu
  1041. piece.Addr = one.Addr
  1042. piece.Type = one.Type
  1043. piece.Value = one.Value
  1044. piece.RecordTime = t
  1045. data = append(data, piece)
  1046. }
  1047. svc := GetConverseService(utils.DBE)
  1048. svc.InsertEntity(data)
  1049. }
  1050. // 更新异常信息
  1051. func (s *ConverseService) SaveErrInfo(err, taskid string, reqType string) {
  1052. // 任务发送失败,,,修改任务状态,,,,提示任务执行失败
  1053. // 请求发送失败
  1054. svc := GetConverseService(utils.DBE)
  1055. // add by 徐春林 库内整理错误信息保存
  1056. asvc := arrangeService.GetArrangeService(utils.DBE)
  1057. svc.SaveTaskExcepMsg(err, taskid)
  1058. if reqType == "moving" {
  1059. asvc.SaveArrangeExcepMsg(err, taskid)
  1060. } else {
  1061. svc.SaveSampleExcepMsg(err, taskid)
  1062. }
  1063. }
  1064. // 修改同步申请库单状态
  1065. func (this *ConverseService) ApplyStatusModify(statusCode int, entryNo, msg string) error {
  1066. set := " "
  1067. if statusCode == 1 || statusCode == 2 || statusCode == 3 || statusCode == 5 {
  1068. set = " ,exception='' "
  1069. } else {
  1070. if msg != "" {
  1071. set = " ,exception='" + msg + "' "
  1072. } else {
  1073. set = " ,exception='' "
  1074. }
  1075. }
  1076. sql := "UPDATE " + "bank_synchronization_main" + " SET taskstatus = '" + strconv.Itoa(statusCode) + "' " + set + " " +
  1077. " WHERE entryno = '" + entryNo + "' "
  1078. _, err := this.DBE.Exec(sql)
  1079. return err
  1080. }
  1081. //修改任务表状态码
  1082. func (this *ConverseService) ApplyTaskStatusModify(statusCode int, task_id, rack_id string) error {
  1083. sql := "UPDATE " + TABLE_TAST + " SET statuscode = '" + strconv.Itoa(statusCode) + "' WHERE " +
  1084. " task_id = '" + task_id + "' and BoxBarcode = '" + rack_id + "' "
  1085. _, err := this.DBE.Exec(sql)
  1086. return err
  1087. }
  1088. // 同步更新盒子位置信息
  1089. func (this *ConverseService) UpdateBoxPosition(res ResponseEntity) {
  1090. for k, v := range res.Data.List {
  1091. glog.Info("操作地 ", k, " 条数据")
  1092. boxBarcode := v.Rack_id // 冻存盒编号
  1093. Cu := res.Data.Cu // 设备编号
  1094. Ltu := v.Ltu //
  1095. Unit := v.Unit
  1096. Pos := v.Pos
  1097. Group := v.Group
  1098. fmt.Println("保存返回的冻存盒位置信息:cu:", Cu, ",Ltu", Ltu, ",Unit", Unit, ",Group", Group, ",Pos", Pos)
  1099. var shelf Shelf
  1100. var equipment Bank_Equipment
  1101. // 判断是否是冷库,如果是冷库的话,查询容器架子信息时不需要组装 unit
  1102. lengSql := " select * from bank_equipment where cu = '" + utils.ToStr(res.Data.Cu) + "' and Ltu = '" + utils.ToStr(Ltu) + "' "
  1103. this.DBE.SQL(lengSql).Get(&equipment)
  1104. sql := "select b.id EquipmentId,b.code,b.name,a.id,a.XStation,a.YStation from bank_shelf a left join bank_equipment b on a.EquipmentId = b.id where b.cu = '" + utils.ToStr(Cu) + "' " +
  1105. "and a.Ltu = '" + utils.ToStr(Ltu) + "' and a.Group ='" + utils.ToStr(Group) + "'"
  1106. // 冷库设备
  1107. YStation := " "
  1108. if equipment.Ltu == 0 {
  1109. sql += " and a.Unit ='" + utils.ToStr(Unit) + "' "
  1110. YStation = " , YStation =1 "
  1111. fmt.Println("保存返回的冻存盒位置信息:YStation1:1 ", YStation)
  1112. } else {
  1113. YStation = " , YStation = " + utils.ToStr(Unit)
  1114. fmt.Println("保存返回的冻存盒位置信息:cYStation1:2 ", YStation)
  1115. }
  1116. this.DBE.SQL(sql).Get(&shelf)
  1117. fmt.Println("冻存架信息:", shelf.Code, shelf.Name, shelf.Id, shelf.XStation, shelf.YStation)
  1118. // 更新盒所在的冻存架的信息 计算位置信息,并更新
  1119. // 临时处理逻辑,默认 pos 为 盒子的 XStation
  1120. pos := shelf.Code + "-" + common.Boxlinename(shelf.YStation) + utils.ToStr(shelf.XStation) + "-" +
  1121. "" + common.Boxlinename(Unit) + utils.ToStr(utils.ToStr(Pos))
  1122. possql := "update bank_box set XStation ='" + utils.ToStr(Pos) + "' " + YStation + "," +
  1123. " EquipmentId ='" + com.ToStr(shelf.EquipmentId) + "'," +
  1124. " shelfId =" + utils.ToStr(shelf.Id) + ",IsLocked = 0 ," +
  1125. " Position ='" + pos + "'" +
  1126. " where Barcode ='" + boxBarcode + "' "
  1127. // 冷库设备
  1128. this.DBE.Exec(possql)
  1129. // 更新 当前冰箱冻存盒容量 信息
  1130. this.DBE.Exec(" update bank_currboxcapacity set `A" + utils.ToStr(Pos) + "`= -1 where shelfId =" + utils.ToStr(shelf.Id) + "")
  1131. }
  1132. var task Sample_Storage_Task
  1133. sql := " select * from Sample_Storage_Task where " +
  1134. " cu = '" + gconv.String(res.Data.Cu) + "' " + " and Task_type =5 and StatusCode =1 "
  1135. this.DBE.SQL(sql).Get(&task)
  1136. // 跟新任务状态
  1137. this.DBE.Exec(" update sample_storage_task set StatusCode =5 where " +
  1138. " cu = '" + gconv.String(res.Data.Cu) + "' " + " and Task_type =5 and StatusCode =1 ")
  1139. this.ApplyStatusModify(SUCCESS, task.Task_id, "")
  1140. }
  1141. // 更新同步返回的位置信息
  1142. func (this *ConverseService) UpdateSamplePosition(res SamplePostion) {
  1143. boxBarcode := res.Data.Rack_id // 冻存盒编号
  1144. Cu := res.Data.Cu // 设备编号
  1145. Ltu := res.Data.Ltu //
  1146. Unit := res.Data.Unit
  1147. Pos := res.Data.Pos
  1148. Group := res.Data.Group
  1149. fmt.Println("保存返回的冻存盒位置信息:cu:", Cu, ",Ltu", Ltu, ",Unit", Unit, ",Group", Group, ",Pos", Pos)
  1150. var shelf Shelf
  1151. var equipment Bank_Equipment
  1152. // 判断是否是冷库,如果是冷库的话,查询容器架子信息时不需要组装 unit
  1153. lengSql := " select * from bank_equipment where cu = '" + utils.ToStr(res.Data.Cu) + "' and Ltu = '" + utils.ToStr(Ltu) + "' "
  1154. this.DBE.SQL(lengSql).Get(&equipment)
  1155. sql := "select b.id EquipmentId,b.code,b.name,a.id,a.XStation,a.YStation from bank_shelf a left join bank_equipment b on a.EquipmentId = b.id where b.cu = '" + utils.ToStr(Cu) + "' " +
  1156. "and a.Ltu = '" + utils.ToStr(Ltu) + "' and a.Group ='" + utils.ToStr(Group) + "'"
  1157. // 冷库设备
  1158. YStation := " "
  1159. if equipment.Ltu == 0 {
  1160. sql += " and a.Unit ='" + utils.ToStr(Unit) + "' "
  1161. YStation = " , YStation =1 "
  1162. fmt.Println("保存返回的冻存盒位置信息:YStation1:1 ", YStation)
  1163. } else {
  1164. YStation = " , YStation = " + utils.ToStr(Unit)
  1165. fmt.Println("保存返回的冻存盒位置信息:cYStation1:2 ", YStation)
  1166. }
  1167. this.DBE.SQL(sql).Get(&shelf)
  1168. fmt.Println("冻存架信息:", shelf.Code, shelf.Name, shelf.Id, shelf.XStation, shelf.YStation)
  1169. // 更新盒所在的冻存架的信息 计算位置信息,并更新
  1170. // 临时处理逻辑,默认 pos 为 盒子的 XStation
  1171. pos := shelf.Code + "-" + common.Boxlinename(shelf.YStation) + utils.ToStr(shelf.XStation) + "-" +
  1172. "" + common.Boxlinename(Unit) + utils.ToStr(utils.ToStr(Pos))
  1173. possql := "update bank_box set XStation ='" + utils.ToStr(Pos) + "' " + YStation + "," +
  1174. " EquipmentId ='" + com.ToStr(shelf.EquipmentId) + "'," +
  1175. " shelfId =" + utils.ToStr(shelf.Id) + ",IsLocked = 0 ," +
  1176. " Position ='" + pos + "'" +
  1177. " where Barcode ='" + boxBarcode + "' "
  1178. // 冷库设备
  1179. this.DBE.Exec(possql)
  1180. // 更新 当前冰箱冻存盒容量 信息
  1181. this.DBE.Exec(" update bank_currboxcapacity set `A" + utils.ToStr(Pos) + "`= -1 where shelfId =" + utils.ToStr(shelf.Id) + "")
  1182. //根据返回的报文中的盒子标识,获取盒子类型
  1183. boxsql := "select Id,RowNum,ColumnNum,EquipmentId,ShelfId,XStation,YStation from bank_box where Barcode ='" + res.Data.Rack_id + "'"
  1184. var box Box
  1185. this.DBE.SQL(boxsql).Get(&box)
  1186. glog.Info("盒信息: ", box)
  1187. glog.Info("容器Id: ", box.EquipmentId, ";字符串:", utils.ToStr(box.EquipmentId))
  1188. // 根据盒子信息获取容器和冻存架信息, 用于更新样本孔位信息
  1189. RowNum := box.RowNum
  1190. ColumnNum := box.ColumnNum
  1191. glog.Info("冻存盒的行数:", RowNum, ";冻存盒的列数:", ColumnNum)
  1192. // 因为每次只会同步一个盒子的数据,所以按照一个盒子的来
  1193. for _, v := range res.Data.List {
  1194. no := v.No
  1195. id := v.Id
  1196. glog.Info("样本在盒子中的位置:", no, ";样本条码: ", id)
  1197. //计算管子在盒子中的坐标
  1198. var box_x, box_y int
  1199. if no%ColumnNum == 0 { // 如果 取余数为0 则 证明该数是能被 10 整除的数 为该行 最后一个孔位
  1200. box_y = ColumnNum
  1201. box_x = no / ColumnNum
  1202. } else {
  1203. box_y = no % ColumnNum
  1204. box_x = (no / ColumnNum) + 1
  1205. }
  1206. glog.Info("管子在盒子中的位置坐标为:", box_y, ";", box_x)
  1207. //var position = utils.NumberToLetter(box_x) + utils.ToStr(box_y)
  1208. position := shelf.Code + "-" + common.Boxlinename(shelf.YStation) + utils.ToStr(shelf.XStation) + "-" +
  1209. "" + common.Boxlinename(box.YStation) + utils.ToStr(box.XStation) + "-" + common.Boxlinename(box_x) + utils.ToStr(box_y)
  1210. sql = "update bank_sample set BoxId = '" + utils.ToStr(box.Id) + "',Position = '" + utils.ToStr(box_y) + ";" +
  1211. "" + utils.ToStr(box_x) + "',PositionInfo = '" + position + "' " +
  1212. // " , unit = '"+ utils.ToStr( entity.Data.Actual_data[i].Target.Unit)+"'" +
  1213. " where barcode ='" + id + "' "
  1214. //执行sql 更新位置和坐标信息
  1215. this.DBE.Exec(sql)
  1216. }
  1217. var task Sample_Storage_Task
  1218. tasksql := " select * from Sample_Storage_Task where " +
  1219. " boxBarcode = '" + res.Data.Rack_id + "' " + " and Task_type =6 and StatusCode =1 "
  1220. this.DBE.SQL(tasksql).Get(&task)
  1221. // 跟新任务状态
  1222. this.DBE.Exec(" update sample_storage_task set StatusCode =5 where " +
  1223. " id = " + gconv.String(task.Id) + " ")
  1224. // 判断所有任务是否全部执行完成
  1225. count, _ := this.DBE.Table(" sample_storage_task ").Where(" task_id = '" + task.Task_id + "'").
  1226. Where(" StatusCode !=5 ").Count()
  1227. glog.Info(" count ", count)
  1228. if count == 0 {
  1229. this.ApplyStatusModify(SUCCESS, task.Task_id, "")
  1230. }
  1231. }