manage_center_service.go 41KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. package service
  2. import (
  3. "strconv"
  4. "strings"
  5. "time"
  6. "XT_New/models"
  7. "github.com/jinzhu/gorm"
  8. )
  9. func GetDictionaryConfigList(orgID int64) (dataconfig interface{}, err error) {
  10. var configList []*models.DictionaryDataconfig
  11. err = readDb.Model(&models.DictionaryDataconfig{}).Where("(org_id in (0,?) and status = 1) or (status = 0 and delete_id_system > 0 and org_id = ?)", orgID, orgID).Order("orders asc, id asc").Find(&configList).Error
  12. if err != nil {
  13. return nil, err
  14. }
  15. // configResult := make([]*ConfigViewModel,0)
  16. childConfig := make(map[int64][]*models.DictionaryDataconfig)
  17. resultConfig := make(map[string][]*models.DictionaryConfigViewModel)
  18. deleteSystemChilds := make([]*models.DictionaryDataconfig, 0)
  19. editSystemChilds := make([]*models.DictionaryDataconfig, 0)
  20. for _, config := range configList {
  21. if config.Status == 1 && config.DeleteIdSystem > 0 {
  22. editSystemChilds = append(editSystemChilds, config)
  23. }
  24. if config.Status == 0 && config.DeleteIdSystem > 0 {
  25. deleteSystemChilds = append(deleteSystemChilds, config)
  26. }
  27. }
  28. continueFlag := false
  29. for _, config := range configList {
  30. continueFlag = false
  31. if config.ParentId == 0 {
  32. newConfig := &models.DictionaryConfigViewModel{
  33. ID: config.ID,
  34. ParentId: config.ParentId,
  35. Module: config.Module,
  36. OrgId: config.OrgId,
  37. Name: config.Name,
  38. FieldName: config.FieldName,
  39. Value: config.Value,
  40. CreateUserId: config.CreateUserId,
  41. Status: config.Status,
  42. Remark: config.Remark,
  43. Title: config.Title,
  44. Content: config.Content,
  45. }
  46. // configResult = append(configResult,newConfig)
  47. result := resultConfig[config.Module]
  48. if result == nil {
  49. result = make([]*models.DictionaryConfigViewModel, 0)
  50. }
  51. for _, vm := range editSystemChilds {
  52. if vm.DeleteIdSystem == config.ID {
  53. continueFlag = true
  54. break
  55. }
  56. }
  57. for _, _vm := range deleteSystemChilds {
  58. if _vm.DeleteIdSystem == config.ID {
  59. continueFlag = true
  60. break
  61. }
  62. }
  63. if config.OrgId != 0 && config.Status == 0 && config.DeleteIdSystem > 0 {
  64. continue
  65. }
  66. if continueFlag {
  67. continue
  68. }
  69. result = append(result, newConfig)
  70. resultConfig[config.Module] = result
  71. } else {
  72. childs := childConfig[config.ParentId]
  73. if childs == nil {
  74. childs = make([]*models.DictionaryDataconfig, 0)
  75. }
  76. continueFlag := false
  77. for _, vm := range editSystemChilds {
  78. if vm.DeleteIdSystem == config.ID {
  79. continueFlag = true
  80. break
  81. }
  82. }
  83. for _, _vm := range deleteSystemChilds {
  84. if _vm.DeleteIdSystem == config.ID {
  85. continueFlag = true
  86. break
  87. }
  88. }
  89. if config.OrgId != 0 && config.Status == 0 && config.DeleteIdSystem > 0 {
  90. continue
  91. }
  92. if continueFlag {
  93. continue
  94. }
  95. childs = append(childs, config)
  96. childConfig[config.ParentId] = childs
  97. }
  98. }
  99. for _, vm := range resultConfig {
  100. for _, _vm := range vm {
  101. _vm.Childs = childConfig[_vm.ID]
  102. }
  103. // vm.Childs = childConfig[vm.ID]
  104. }
  105. return resultConfig, err
  106. }
  107. func CreateDictionaryConfig(dataconfig *models.DictionaryDataconfig) (err error) {
  108. // readDb.Model(&models.Dataconfig{}).Where("module = ? and parent_id = 0 and org_id in (0,?)",dataconfig.Module,dataconfig.OrgId).Count(&total)
  109. err = readDb.Create(&dataconfig).Error
  110. return
  111. }
  112. func GetDictionaryChildValue(module string, parent_id int64, org_id int64) (value int) {
  113. readDb.Model(&models.DictionaryDataconfig{}).Where("module=? and parent_id=? and org_id in (0,?)", module, parent_id, org_id).Count(&value)
  114. return
  115. }
  116. func FindDictionaryConfigByTitle(module string, title string, org_id int64) (dataconfig models.DictionaryDataconfig, err error) {
  117. err = readDb.Model(&models.DictionaryDataconfig{}).Where("module=? and title=? and org_id in (0,?) and status = 1", module, title, org_id).First(&dataconfig).Error
  118. return
  119. }
  120. func UpdateDictionaryTemplate(dataconfig *models.DictionaryDataconfig) (err error) {
  121. err = readDb.Model(&models.DictionaryDataconfig{}).Where("id =?", dataconfig.ID).Update(map[string]interface{}{"title": dataconfig.Title, "content": dataconfig.Content, "update_time": dataconfig.UpdatedTime, "remark": dataconfig.Remark}).Error
  122. return
  123. }
  124. func UpdateDictionaryChildConfig(dataconfig *models.DictionaryDataconfig) (err error) {
  125. err = readDb.Model(&models.DictionaryDataconfig{}).Where("id =?", dataconfig.ID).Update(map[string]interface{}{"name": dataconfig.Name, "update_time": dataconfig.UpdatedTime, "remark": dataconfig.Remark, "orders": dataconfig.Order, "code": dataconfig.Code}).Error
  126. return
  127. }
  128. func DeleteDictionaryChildConfig(dataconfig *models.DictionaryDataconfig) (err error) {
  129. err = readDb.Model(&models.DictionaryDataconfig{}).Where("id =?", dataconfig.ID).Update(map[string]interface{}{"status": dataconfig.Status, "update_time": dataconfig.UpdatedTime}).Error
  130. return
  131. }
  132. func CreateBaseDrugLib(lib *models.BaseDrugLib) (err error) {
  133. err = writeDb.Create(&lib).Error
  134. return
  135. }
  136. func UpdateBaseDrugLib(lib *models.BaseDrugLib) (err error) {
  137. err = writeDb.Save(lib).Error
  138. return
  139. }
  140. func FindBaseDrugLibRecordCount(lib *models.BaseDrugLib) (count int64) {
  141. readDb.Model(&models.BaseDrugLib{}).Where("org_id = ? AND drug_name = ? AND drug_category = ? AND drug_spec=? AND drug_type=? AND status = 1", lib.OrgId, lib.DrugName, lib.DrugCategory, lib.DrugSpec, lib.DrugType).Count(&count)
  142. return
  143. }
  144. func FindBaseDrugLibRecordCountOne(lib *models.BaseDrugLib) (count int64) {
  145. readDb.Model(&models.BaseDrugLib{}).Where("org_id = ? and drug_name = ? and dose = ? and dose_unit = ? and min_number = ? and min_unit = ? and max_unit = ? and manufacturer = ? and status = 1", lib.OrgId, lib.DrugName, lib.Dose, lib.DoseUnit, lib.MinNumber, lib.MinUnit, lib.MaxUnit, lib.Manufacturer).Count(&count)
  146. return
  147. }
  148. func FindAllDrugLibRecordTotal(org_id int64) (count int64) {
  149. readDb.Model(&models.BaseDrugLib{}).Where("org_id = ? AND status = 1", org_id).Count(&count)
  150. return
  151. }
  152. func FindBaseDrugLibRecord(org_id int64, id int64) (lib models.BaseDrugLib, err error) {
  153. err = readDb.Model(&models.BaseDrugLib{}).Where("id = ? AND org_id = ? AND status = 1", id, org_id).First(&lib).Error
  154. return
  155. }
  156. func FindNewBaseDrugLibRecord(org_id int64, id int64, tx *gorm.DB) (lib models.BaseDrugLib, err error) {
  157. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? AND org_id = ? AND status = 1", id, org_id).First(&lib).Error
  158. if err != gorm.ErrRecordNotFound {
  159. if err != nil {
  160. tx.Rollback()
  161. return
  162. }
  163. }
  164. return lib, err
  165. }
  166. func FindBaseDrugLibRecordSeven(org_id int64, id int64) (lib models.BaseDrugLibSeven, err error) {
  167. err = readDb.Select("id,drug_name,max_unit,min_number,min_unit,sum_count,is_user,is_pharmacy").Where("org_id = ? AND id = ? AND status = 1", org_id, id).First(&lib).Error
  168. return
  169. }
  170. func GetDrugAutoWarehouseOut(patient_id int64, advicedate int64, drugid int64) (*models.DrugWarehouseOutInfo, error) {
  171. detail := models.DrugWarehouseOutInfo{}
  172. err := XTReadDB().Model(&detail).Where("patient_id = ? and sys_record_time =? and drug_id = ? and status = 1", patient_id, advicedate, drugid).Find(&detail).Error
  173. if err == gorm.ErrRecordNotFound {
  174. return nil, err
  175. }
  176. if err != nil {
  177. return nil, err
  178. }
  179. return &detail, nil
  180. }
  181. func GetDrugAutoWarehouseOutSeven(patient_id int64, advicedate int64, drugid int64, adviceid int64) (*models.DrugWarehouseOutInfo, error) {
  182. detail := models.DrugWarehouseOutInfo{}
  183. err := XTReadDB().Model(&detail).Where("patient_id = ? and sys_record_time =? and drug_id = ? and status = 1 and advice_id = ?", patient_id, advicedate, drugid, adviceid).Find(&detail).Error
  184. if err == gorm.ErrRecordNotFound {
  185. return nil, err
  186. }
  187. if err != nil {
  188. return nil, err
  189. }
  190. return &detail, nil
  191. }
  192. func GetDrugAutoWarehouseOutEight(patient_id int64, advicedate int64, drugid int64, adviceid int64) (models.DrugWarehouseOutInfo, error) {
  193. detail := models.DrugWarehouseOutInfo{}
  194. err = XTReadDB().Model(&detail).Where("patient_id = ? and sys_record_time =? and drug_id = ? and status = 1 and advice_id = ?", patient_id, advicedate, drugid, adviceid).Find(&detail).Error
  195. return detail, err
  196. }
  197. func GetSelfDrugAutoWarehouseOutEight(patient_id int64, advicedate int64, drugid int64, adviceid int64) (models.XtSelfDrugWarehouseOutInfo, error) {
  198. detail := models.XtSelfDrugWarehouseOutInfo{}
  199. err = XTReadDB().Model(&detail).Where("patient_id = ? and sys_record_time =? and drug_id = ? and status = 1 and advice_id = ?", patient_id, advicedate, drugid, adviceid).Find(&detail).Error
  200. return detail, err
  201. }
  202. func GetDrugAutoWarehouseOutTen(patient_id int64, advicedate int64, drugid int64, adviceid int64, warehouse_info_id int64) (models.DrugWarehouseOutInfo, error) {
  203. detail := models.DrugWarehouseOutInfo{}
  204. err = XTReadDB().Model(&detail).Where("patient_id = ? and sys_record_time =? and drug_id = ? and status = 1 and advice_id = ? and warehouse_info_id = ?", patient_id, advicedate, drugid, adviceid, warehouse_info_id).Find(&detail).Error
  205. return detail, err
  206. }
  207. func GetSelfDrugAutoWarehouseOutTen(patient_id int64, advicedate int64, drugid int64, adviceid int64, warehouse_info_id int64) (models.XtSelfDrugWarehouseOutInfo, error) {
  208. detail := models.XtSelfDrugWarehouseOutInfo{}
  209. err = XTReadDB().Model(&detail).Where("patient_id = ? and sys_record_time =? and drug_id = ? and status = 1 and advice_id = ? and warehouse_info_id = ?", patient_id, advicedate, drugid, adviceid, warehouse_info_id).Find(&detail).Error
  210. return detail, err
  211. }
  212. func GetDrugAutoWarehouseOutNight(patient_id int64, advicedate int64, drugid int64, adviceid int64) (night []*models.DrugWarehouseOutInfo, err error) {
  213. err = XTReadDB().Where("patient_id = ? and sys_record_time =? and drug_id = ? and status = 1 and advice_id = ?", patient_id, advicedate, drugid, adviceid).Find(&night).Error
  214. return night, err
  215. }
  216. func GetSelfDrugAutoWarehouseOutNight(patient_id int64, advicedate int64, drugid int64, adviceid int64) (night []*models.XtSelfDrugWarehouseOutInfo, err error) {
  217. err = XTReadDB().Where("patient_id = ? and sys_record_time =? and drug_id = ? and status = 1 and advice_id = ?", patient_id, advicedate, drugid, adviceid).Find(&night).Error
  218. return night, err
  219. }
  220. func UpdateDrugWarehouse(id int64, info models.DrugWarehouseInfo) error {
  221. err := writeDb.Model(&models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  222. return err
  223. }
  224. func UpdateSelfDrugWarehouseOne(id int64, info models.XtSelfDrugWarehouseInfo) error {
  225. err := writeDb.Model(&models.XtSelfDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  226. return err
  227. }
  228. func UpdateDrugWarehouseOne(id int64, info models.DrugWarehouseInfo) error {
  229. err := writeDb.Model(&models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  230. return err
  231. }
  232. func UpdateSelfDrugWarehouseTwo(id int64, info models.XtSelfDrugWarehouseInfo) error {
  233. err := writeDb.Model(&models.XtSelfDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  234. return err
  235. }
  236. func GetBaseDrugLibList(org_id int64, keyword string, page int64, limit int64, is_use int64, is_charge int64, is_inject int64, manufacturer int64, isRecord int64) (list []*models.BaseDrugLib, total int64, err error) {
  237. db := readDb.Model(&models.BaseDrugLib{}).Where("org_id = ? AND status = 1", org_id)
  238. if len(keyword) != 0 {
  239. keyword = "%" + keyword + "%"
  240. db = db.Where("drug_name Like ?", keyword)
  241. }
  242. if is_use != 0 {
  243. if is_use == 2 {
  244. db = db.Where("find_in_set('停用',drug_status) = 0")
  245. } else {
  246. db = db.Where("find_in_set('停用',drug_status) > 0")
  247. }
  248. }
  249. if is_charge == 1 {
  250. //if is_charge == 1 {
  251. // db = db.Where("find_in_set('收费',drug_status) > 0")
  252. //
  253. //} else {
  254. // db = db.Where("find_in_set('收费',drug_status) = 0")
  255. //}
  256. db = db.Where("is_record = ?", is_charge)
  257. }
  258. if is_charge == 2 {
  259. db = db.Where("is_record= 0 || is_record = 2")
  260. }
  261. if is_inject != 0 {
  262. //db = db.Where("drug_name Like ?", keyword)
  263. if is_inject == 1 {
  264. db = db.Where("find_in_set('注射类',drug_status) > 0")
  265. } else {
  266. db = db.Where("find_in_set('注射类',drug_status) = 0")
  267. }
  268. }
  269. if manufacturer > 0 {
  270. db = db.Where("manufacturer = ?", manufacturer)
  271. }
  272. if isRecord > 0 {
  273. db = db.Where("is_mark = ?", isRecord)
  274. }
  275. offset := (page - 1) * limit
  276. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  277. return
  278. }
  279. func CreateMedicineInsurancePercentage(mip *models.MedicineInsurancePercentage) (err error) {
  280. err = writeDb.Create(mip).Error
  281. return
  282. }
  283. func CreateUnitSafeguard(dus *models.DrugUnitSafeguard) (err error) {
  284. err = writeDb.Create(dus).Error
  285. return
  286. }
  287. func AddSigleMZSick(dealer *models.OutpatientServiceSick) (error, *models.OutpatientServiceSick) {
  288. err := writeDb.Create(&dealer).Error
  289. return err, dealer
  290. }
  291. func ModifyMZSick(mesick *models.OutpatientServiceSick) error {
  292. err := writeDb.Save(&mesick).Error
  293. return err
  294. }
  295. func FindAllMZSickList(orgId int64, page int64, limit int64) (list []*models.OutpatientServiceSick, total int64, err error) {
  296. offset := (page - 1) * limit
  297. db := readDb.Model(&models.OutpatientServiceSick{})
  298. db = db.Where("user_org_id = ? AND status = 1", orgId)
  299. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  300. return
  301. }
  302. func DeleteMZSickById(id int64) error {
  303. err := writeDb.Model(&models.OutpatientServiceSick{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  304. return err
  305. }
  306. func FindMZSickById(id int64) (*models.OutpatientServiceSick, error) {
  307. dealer := &models.OutpatientServiceSick{}
  308. err := readDb.Model(&models.OutpatientServiceSick{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  309. return dealer, err
  310. }
  311. func GetDrugWareoutInfo(id int64) (*models.XtDrugWarehouseInfo, error) {
  312. info := models.XtDrugWarehouseInfo{}
  313. err = XTReadDB().Model(&info).Where("drug_id = ? and status = 1", id).Find(&info).Error
  314. if err == gorm.ErrRecordNotFound {
  315. return nil, err
  316. }
  317. if err != nil {
  318. return nil, err
  319. }
  320. return &info, nil
  321. }
  322. func GetDrugWareOutById(id int64) (*models.XtDrugWarehouseOutInfo, error) {
  323. info := models.XtDrugWarehouseOutInfo{}
  324. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", id).Find(&info).Error
  325. if err == gorm.ErrRecordNotFound {
  326. return nil, err
  327. }
  328. if err != nil {
  329. return nil, err
  330. }
  331. return &info, nil
  332. }
  333. func DeleteBaseDrugById(id int64) error {
  334. drug := models.XtBaseDrug{}
  335. err := XTWriteDB().Model(&drug).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  336. return err
  337. }
  338. func AddSigleAddition(dealer *models.XtHisAddtionConfig) (error, *models.XtHisAddtionConfig) {
  339. err := writeDb.Create(&dealer).Error
  340. return err, dealer
  341. }
  342. func ModifyAddition(mesick *models.XtHisAddtionConfig) error {
  343. err := writeDb.Save(&mesick).Error
  344. return err
  345. }
  346. func FindAllAdditionList(orgId int64, page int64, limit int64) (list []*models.XtHisAddtionConfig, total int64, err error) {
  347. offset := (page - 1) * limit
  348. db := readDb.Model(&models.XtHisAddtionConfig{})
  349. db = db.Where("user_org_id = ? AND status = 1", orgId)
  350. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  351. return
  352. }
  353. func DeleteAdditionById(id int64) error {
  354. err := writeDb.Model(&models.XtHisAddtionConfig{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  355. return err
  356. }
  357. func FindAdditionById(id int64) (*models.XtHisAddtionConfig, error) {
  358. dealer := &models.XtHisAddtionConfig{}
  359. err := readDb.Model(&models.XtHisAddtionConfig{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  360. return dealer, err
  361. }
  362. func AddSigleDiagnose(dealer *models.HisXtDiagnoseConfig) (error, *models.HisXtDiagnoseConfig) {
  363. err := writeDb.Create(&dealer).Error
  364. return err, dealer
  365. }
  366. func ModifyDiagnose(mesick *models.HisXtDiagnoseConfig) error {
  367. err := writeDb.Save(&mesick).Error
  368. return err
  369. }
  370. func FindAllDiagnoseList(orgId int64, page int64, limit int64) (list []*models.HisXtDiagnoseConfig, total int64, err error) {
  371. offset := (page - 1) * limit
  372. db := readDb.Model(&models.HisXtDiagnoseConfig{})
  373. db = db.Where("user_org_id = ? AND status = 1", orgId)
  374. err = db.Count(&total).Offset(offset).Limit(limit).Order("sort asc").Find(&list).Error
  375. return
  376. }
  377. func DeleteDiagnoseById(id int64) error {
  378. err := writeDb.Model(&models.HisXtDiagnoseConfig{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  379. return err
  380. }
  381. func FindDiagnoseById(id int64) (*models.HisXtDiagnoseConfig, error) {
  382. dealer := &models.HisXtDiagnoseConfig{}
  383. err := readDb.Model(&models.HisXtDiagnoseConfig{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  384. return dealer, err
  385. }
  386. func GetDoseCode(dose_id int64, user_org_id int64) (error, string) {
  387. var data models.DictDataconfig
  388. err := readDb.Model(&models.DictDataconfig{}).Where(" (user_org_id = ? Or user_org_id = 0) AND parent_id = 4 AND module = 'system' AND status = 1 AND value = ?", user_org_id, dose_id).First(&data).Error
  389. return err, data.Code
  390. }
  391. func GetAllDrugList(orgid int64) (drug []*models.BaseDrugLib, err error) {
  392. err = XTReadDB().Model(&drug).Where("org_id = ? and status = 1", orgid).Find(&drug).Error
  393. return drug, err
  394. }
  395. //自动退库分装方法
  396. func DrugAutoAddCancelInfo(advice *models.HisDoctorAdviceInfo, creater int64) error {
  397. //查询该批次病人该药品出库数据
  398. info, _ := GetDrugAutoWarehouseOutEight(advice.PatientId, advice.AdviceDate, advice.DrugId, advice.ID)
  399. list, _ := GetDrugAutoWarehouseOutNight(advice.PatientId, advice.AdviceDate, advice.DrugId, advice.ID)
  400. storeHouseConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  401. //退库
  402. drug, _ := FindBaseDrugLibRecord(advice.UserOrgId, advice.DrugId)
  403. //如果剩余数量 + 退库数量 < 总入库数量
  404. if len(list) > 0 {
  405. for _, its := range list {
  406. //查询该批次病人该药品出库数据
  407. infos, _ := GetDrugAutoWarehouseOutTen(advice.PatientId, advice.AdviceDate, advice.DrugId, advice.ID, its.WarehouseInfoId)
  408. //查询该药品出库批次总入库数量
  409. druginfo, _ := GetDrugInfoSix(its.WarehouseInfoId, infos.OrgId)
  410. var out_count int64 //该批次出库库存
  411. var sum_count int64 //该批次总库存
  412. var over_count int64 //剩余数量
  413. if druginfo.MaxUnit != druginfo.MinUnit {
  414. over_count = druginfo.StockMaxNumber*drug.MinNumber + druginfo.StockMinNumber
  415. }
  416. if druginfo.MaxUnit == druginfo.MinUnit {
  417. over_count = druginfo.StockMaxNumber + druginfo.StockMinNumber
  418. }
  419. if infos.CountUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  420. out_count = infos.Count * drug.MinNumber
  421. }
  422. if infos.CountUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  423. out_count = infos.Count
  424. }
  425. if infos.CountUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  426. out_count = infos.Count
  427. }
  428. if druginfo.MaxUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  429. sum_count = druginfo.WarehousingCount * drug.MinNumber
  430. }
  431. if druginfo.MaxUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  432. sum_count = druginfo.WarehousingCount
  433. }
  434. if druginfo.MaxUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  435. sum_count = druginfo.WarehousingCount
  436. }
  437. if (over_count + out_count) <= sum_count {
  438. if its.CountUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  439. //退库
  440. warehouseInfo := models.DrugWarehouseInfo{
  441. StockMinNumber: its.Count,
  442. }
  443. UpdateDrugWarehouse(its.WarehouseInfoId, warehouseInfo)
  444. //增加退库数量
  445. AddCancelSumCountOne(storeHouseConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, its.Count)
  446. //出库数量减少
  447. UpdateSumOutDrug(advice.UserOrgId, storeHouseConfig.DrugStorehouseOut, advice.DrugId, its.Count)
  448. }
  449. if its.CountUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  450. warehouseInfo := models.DrugWarehouseInfo{
  451. StockMaxNumber: its.Count,
  452. }
  453. UpdateDrugWarehouseOne(its.WarehouseInfoId, warehouseInfo)
  454. //增加退库数量
  455. AddCancelSumCountOne(storeHouseConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, its.Count*drug.MinNumber)
  456. //出库数量减少
  457. UpdateSumOutDrug(advice.UserOrgId, storeHouseConfig.DrugStorehouseOut, advice.DrugId, its.Count*drug.MinNumber)
  458. }
  459. if its.CountUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  460. warehouseInfo := models.DrugWarehouseInfo{
  461. StockMaxNumber: its.Count,
  462. }
  463. UpdateDrugWarehouseOne(its.WarehouseInfoId, warehouseInfo)
  464. //增加退库数量
  465. AddCancelSumCountOne(storeHouseConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, its.Count*drug.MinNumber)
  466. //出库数量减少
  467. UpdateSumOutDrug(advice.UserOrgId, storeHouseConfig.DrugStorehouseOut, advice.DrugId, its.Count*drug.MinNumber)
  468. }
  469. }
  470. }
  471. floatPrescrip := strconv.FormatFloat(advice.PrescribingNumber, 'f', -1, 32)
  472. prescribingNumber, _ := strconv.ParseInt(floatPrescrip, 10, 64)
  473. if advice.PrescribingNumberUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  474. operation_time := time.Now().Unix()
  475. ctime := time.Now().Unix()
  476. timeStr := time.Now().Format("2006-01-02")
  477. timeArr := strings.Split(timeStr, "-")
  478. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  479. total = total + 1
  480. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  481. cancelStock := models.DrugCancelStock{
  482. OrderNumber: orderNumber,
  483. OperaTime: operation_time,
  484. OrgId: advice.UserOrgId,
  485. Creater: creater,
  486. Ctime: ctime,
  487. Status: 1,
  488. ReturnTime: advice.AdviceDate,
  489. Dealer: info.Dealer,
  490. Manufacturer: info.Manufacturer,
  491. Type: 1,
  492. StorehouseId: storeHouseConfig.DrugStorehouseOut,
  493. IsCheck: 1,
  494. }
  495. AddSigleDrugCancelStock(&cancelStock)
  496. manufactureName, _ := GetManufactureById(info.Manufacturer)
  497. dealer, _ := GetDealerById(info.Dealer)
  498. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  499. cancelStockInfo := models.DrugCancelStockInfo{
  500. OrderNumber: cancelInfo.OrderNumber,
  501. CancelStockId: cancelInfo.ID,
  502. DrugId: advice.DrugId,
  503. Count: prescribingNumber,
  504. Status: 1,
  505. Ctime: ctime,
  506. OrgId: advice.UserOrgId,
  507. Type: 1,
  508. Manufacturer: manufactureName.ManufacturerName,
  509. Dealer: dealer.DealerName,
  510. Total: 0,
  511. RetailPrice: info.RetailPrice,
  512. RetailTotalPrice: info.RetailTotalPrice,
  513. Price: info.Price,
  514. RegisterAccount: "",
  515. Remark: info.Remark,
  516. BatchNumber: info.BatchNumber,
  517. MaxUnit: advice.PrescribingNumberUnit,
  518. ProductDate: info.ProductDate,
  519. ExpiryDate: info.ExpiryDate,
  520. BatchNumberId: info.WarehouseInfoId,
  521. StorehouseId: storeHouseConfig.DrugStorehouseOut,
  522. IsCheck: 1,
  523. PatientId: advice.PatientId,
  524. }
  525. //查询剩余库存
  526. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, advice.UserOrgId, advice.DrugId)
  527. var sum_count int64
  528. for _, its := range stockInfo {
  529. if its.MaxUnit == drug.MaxUnit {
  530. its.StockMaxNumber = its.StockMaxNumber * drug.MinNumber
  531. }
  532. sum_count += its.StockMaxNumber + its.StockMinNumber
  533. }
  534. var count_one int64
  535. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  536. count_one = prescribingNumber * drug.MinNumber
  537. }
  538. if advice.PrescribingNumberUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  539. count_one = prescribingNumber
  540. }
  541. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  542. count_one = prescribingNumber * drug.MinNumber
  543. }
  544. flow := models.DrugFlow{
  545. WarehousingId: info.WarehouseInfoId,
  546. DrugId: info.DrugId,
  547. Number: "",
  548. BatchNumber: info.BatchNumber,
  549. Count: count_one,
  550. UserOrgId: advice.UserOrgId,
  551. PatientId: advice.PatientId,
  552. SystemTime: info.SysRecordTime,
  553. ConsumableType: 7,
  554. IsSys: 0,
  555. WarehousingOrder: "",
  556. WarehouseOutId: 0,
  557. WarehouseOutOrderNumber: info.WarehouseOutOrderNumber,
  558. IsEdit: 0,
  559. CancelStockId: cancelStock.ID,
  560. CancelOrderNumber: cancelStock.OrderNumber,
  561. Manufacturer: info.Manufacturer,
  562. Dealer: info.Dealer,
  563. Creator: creater,
  564. UpdateCreator: 0,
  565. Status: 1,
  566. Ctime: info.SysRecordTime,
  567. Mtime: 0,
  568. Price: info.Price,
  569. WarehousingDetailId: info.WarehouseInfoId,
  570. WarehouseOutDetailId: 0,
  571. CancelOutDetailId: 0,
  572. ExpireDate: info.ExpiryDate,
  573. ProductDate: info.ProductDate,
  574. MaxUnit: drug.MinUnit,
  575. MinUnit: "",
  576. AdviceId: info.AdviceId,
  577. StorehouseId: storeHouseConfig.DrugStorehouseOut,
  578. OverCount: sum_count,
  579. OperateTime: time.Now().Unix(),
  580. }
  581. CreatedCancelStock(cancelStockInfo)
  582. CreateDrugFlowOne(flow)
  583. //删除出库记录
  584. UpdateDrugFlowSeven(info.PatientId, info.SysRecordTime, info.DrugId, advice.ID, info.ID)
  585. //删除流水
  586. UpdateDrugFlowTens(info.PatientId, info.WarehouseOutOrderNumber, info.DrugId, advice.ID)
  587. }
  588. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  589. houseConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  590. operation_time := time.Now().Unix()
  591. ctime := time.Now().Unix()
  592. timeStr := time.Now().Format("2006-01-02")
  593. timeArr := strings.Split(timeStr, "-")
  594. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  595. total = total + 1
  596. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  597. cancelStock := models.DrugCancelStock{
  598. OrderNumber: orderNumber,
  599. OperaTime: operation_time,
  600. OrgId: advice.UserOrgId,
  601. Creater: creater,
  602. Ctime: ctime,
  603. Status: 1,
  604. ReturnTime: advice.AdviceDate,
  605. Dealer: info.Dealer,
  606. Manufacturer: info.Manufacturer,
  607. Type: 1,
  608. StorehouseId: houseConfig.DrugStorehouseOut,
  609. IsCheck: 1,
  610. }
  611. AddSigleDrugCancelStock(&cancelStock)
  612. manufactureName, _ := GetManufactureById(info.Manufacturer)
  613. dealer, _ := GetDealerById(info.Dealer)
  614. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  615. cancelStockInfo := models.DrugCancelStockInfo{
  616. OrderNumber: cancelInfo.OrderNumber,
  617. CancelStockId: cancelInfo.ID,
  618. DrugId: info.DrugId,
  619. Count: prescribingNumber,
  620. Status: 1,
  621. Ctime: ctime,
  622. OrgId: advice.UserOrgId,
  623. Type: 1,
  624. Manufacturer: manufactureName.ManufacturerName,
  625. Dealer: dealer.DealerName,
  626. Total: 0,
  627. RetailPrice: info.RetailPrice,
  628. RetailTotalPrice: info.RetailTotalPrice,
  629. Price: info.Price,
  630. RegisterAccount: "",
  631. Remark: info.Remark,
  632. BatchNumber: info.BatchNumber,
  633. MaxUnit: advice.PrescribingNumberUnit,
  634. ProductDate: info.ProductDate,
  635. ExpiryDate: info.ExpiryDate,
  636. BatchNumberId: info.WarehouseInfoId,
  637. StorehouseId: houseConfig.DrugStorehouseOut,
  638. IsCheck: 1,
  639. }
  640. //查询剩余库存
  641. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, advice.UserOrgId, advice.DrugId)
  642. var sum_count int64
  643. for _, its := range stockInfo {
  644. if its.MaxUnit == drug.MaxUnit {
  645. its.StockMaxNumber = its.StockMaxNumber * drug.MinNumber
  646. }
  647. sum_count += its.StockMaxNumber + its.StockMinNumber
  648. }
  649. var count_one int64
  650. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  651. count_one = prescribingNumber * drug.MinNumber
  652. }
  653. if advice.PrescribingNumberUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  654. count_one = prescribingNumber
  655. }
  656. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  657. count_one = prescribingNumber * drug.MinNumber
  658. }
  659. flow := models.DrugFlow{
  660. WarehousingId: info.WarehouseInfoId,
  661. DrugId: info.DrugId,
  662. Number: "",
  663. BatchNumber: info.BatchNumber,
  664. Count: count_one,
  665. UserOrgId: advice.UserOrgId,
  666. PatientId: advice.PatientId,
  667. SystemTime: info.SysRecordTime,
  668. ConsumableType: 7,
  669. IsSys: 0,
  670. WarehousingOrder: "",
  671. WarehouseOutId: 0,
  672. WarehouseOutOrderNumber: info.WarehouseOutOrderNumber,
  673. IsEdit: 0,
  674. CancelStockId: cancelStock.ID,
  675. CancelOrderNumber: cancelStock.OrderNumber,
  676. Manufacturer: info.Manufacturer,
  677. Dealer: info.Dealer,
  678. Creator: creater,
  679. UpdateCreator: 0,
  680. Status: 1,
  681. Ctime: info.SysRecordTime,
  682. Mtime: 0,
  683. Price: info.Price,
  684. WarehousingDetailId: info.WarehouseInfoId,
  685. WarehouseOutDetailId: 0,
  686. CancelOutDetailId: 0,
  687. ExpireDate: info.ExpiryDate,
  688. ProductDate: info.ProductDate,
  689. MaxUnit: drug.MinUnit,
  690. MinUnit: "",
  691. AdviceId: info.AdviceId,
  692. StorehouseId: houseConfig.DrugStorehouseOut,
  693. OverCount: sum_count,
  694. OperateTime: time.Now().Unix(),
  695. }
  696. CreatedCancelStock(cancelStockInfo)
  697. CreateDrugFlowOne(flow)
  698. //删除出库记录
  699. UpdateDrugFlowSeven(info.PatientId, info.SysRecordTime, info.DrugId, advice.ID, info.ID)
  700. //删除流水
  701. UpdateDrugFlowTens(info.PatientId, info.WarehouseOutOrderNumber, info.DrugId, advice.ID)
  702. }
  703. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  704. houseConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  705. operation_time := time.Now().Unix()
  706. ctime := time.Now().Unix()
  707. timeStr := time.Now().Format("2006-01-02")
  708. timeArr := strings.Split(timeStr, "-")
  709. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  710. total = total + 1
  711. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  712. cancelStock := models.DrugCancelStock{
  713. OrderNumber: orderNumber,
  714. OperaTime: operation_time,
  715. OrgId: advice.UserOrgId,
  716. Creater: creater,
  717. Ctime: ctime,
  718. Status: 1,
  719. ReturnTime: advice.AdviceDate,
  720. Dealer: info.Dealer,
  721. Manufacturer: info.Manufacturer,
  722. Type: 1,
  723. StorehouseId: houseConfig.DrugStorehouseOut,
  724. IsCheck: 1,
  725. }
  726. AddSigleDrugCancelStock(&cancelStock)
  727. manufactureName, _ := GetManufactureById(info.Manufacturer)
  728. dealer, _ := GetDealerById(info.Dealer)
  729. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  730. cancelStockInfo := models.DrugCancelStockInfo{
  731. OrderNumber: cancelInfo.OrderNumber,
  732. CancelStockId: cancelInfo.ID,
  733. DrugId: info.DrugId,
  734. Count: prescribingNumber,
  735. Status: 1,
  736. Ctime: ctime,
  737. OrgId: advice.UserOrgId,
  738. Type: 1,
  739. Manufacturer: manufactureName.ManufacturerName,
  740. Dealer: dealer.DealerName,
  741. Total: 0,
  742. RetailPrice: info.RetailPrice,
  743. RetailTotalPrice: info.RetailTotalPrice,
  744. Price: info.Price,
  745. RegisterAccount: "",
  746. Remark: info.Remark,
  747. BatchNumber: info.BatchNumber,
  748. MaxUnit: advice.PrescribingNumberUnit,
  749. ProductDate: info.ProductDate,
  750. ExpiryDate: info.ExpiryDate,
  751. BatchNumberId: info.WarehouseInfoId,
  752. StorehouseId: houseConfig.DrugStorehouseOut,
  753. IsCheck: 1,
  754. }
  755. //查询剩余库存
  756. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, advice.UserOrgId, advice.DrugId)
  757. var sum_count int64
  758. for _, its := range stockInfo {
  759. if its.MaxUnit == drug.MaxUnit {
  760. its.StockMaxNumber = its.StockMaxNumber * drug.MinNumber
  761. }
  762. sum_count += its.StockMaxNumber + its.StockMinNumber
  763. }
  764. flow := models.DrugFlow{
  765. WarehousingId: info.WarehouseInfoId,
  766. DrugId: info.DrugId,
  767. Number: "",
  768. BatchNumber: info.BatchNumber,
  769. Count: prescribingNumber,
  770. UserOrgId: advice.UserOrgId,
  771. PatientId: advice.PatientId,
  772. SystemTime: info.SysRecordTime,
  773. ConsumableType: 7,
  774. IsSys: 0,
  775. WarehousingOrder: "",
  776. WarehouseOutId: 0,
  777. WarehouseOutOrderNumber: info.WarehouseOutOrderNumber,
  778. IsEdit: 0,
  779. CancelStockId: cancelStock.ID,
  780. CancelOrderNumber: cancelStock.OrderNumber,
  781. Manufacturer: info.Manufacturer,
  782. Dealer: info.Dealer,
  783. Creator: creater,
  784. UpdateCreator: 0,
  785. Status: 1,
  786. Ctime: info.SysRecordTime,
  787. Mtime: 0,
  788. Price: info.Price,
  789. WarehousingDetailId: info.WarehouseInfoId,
  790. WarehouseOutDetailId: 0,
  791. CancelOutDetailId: 0,
  792. ExpireDate: info.ExpiryDate,
  793. ProductDate: info.ProductDate,
  794. MaxUnit: advice.PrescribingNumberUnit,
  795. MinUnit: "",
  796. AdviceId: info.AdviceId,
  797. StorehouseId: houseConfig.DrugStorehouseOut,
  798. OverCount: sum_count,
  799. OperateTime: time.Now().Unix(),
  800. }
  801. CreatedCancelStock(cancelStockInfo)
  802. CreateDrugFlowOne(flow)
  803. //删除出库记录
  804. UpdateDrugFlowSeven(info.PatientId, info.SysRecordTime, info.DrugId, advice.ID, info.ID)
  805. //删除流水
  806. UpdateDrugFlowTens(info.PatientId, info.WarehouseOutOrderNumber, info.DrugId, advice.ID)
  807. }
  808. //更新字典里面的库存
  809. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, advice.UserOrgId, advice.DrugId)
  810. //查询改药品信息
  811. medical, _ := GetBaseDrugMedical(advice.DrugId)
  812. var sum_count_one int64
  813. for _, its := range stockInfo {
  814. if its.MaxUnit == medical.MaxUnit {
  815. its.StockMaxNumber = its.StockMaxNumber * medical.MinNumber
  816. }
  817. sum_count_one += its.StockMaxNumber + its.StockMinNumber
  818. }
  819. UpdateBaseDrugSumTwo(advice.DrugId, sum_count_one, advice.UserOrgId)
  820. //更新库存
  821. UpdateSumDrug(advice.UserOrgId, storeHouseConfig.DrugStorehouseOut, advice.DrugId, sum_count_one)
  822. }
  823. return err
  824. }
  825. func GoodAutoAddCancelInfo(good *models.NewDialysisBeforePrepareGoods, creater int64) error {
  826. //查询已经出库数量
  827. info, _ := GetWareOutInfo(good.GoodId, good.RecordDate, good.UserOrgId, good.PatientId, good.OrderId)
  828. if len(info) > 0 {
  829. houseConfig, _ := GetAllStoreHouseConfig(good.UserOrgId)
  830. operation_time := time.Now().Unix()
  831. //创建退库单
  832. timeStr := time.Now().Format("2006-01-02")
  833. timeArr := strings.Split(timeStr, "-")
  834. total, _ := FindAllCancelStockTotal(good.UserOrgId)
  835. total = total + 1
  836. orderNumber := "CKTKD" + strconv.FormatInt(good.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  837. cancelStock := models.CancelStock{
  838. OrderNumber: orderNumber,
  839. OperaTime: operation_time,
  840. OrgId: good.UserOrgId,
  841. Creater: creater,
  842. Ctime: time.Now().Unix(),
  843. Status: 1,
  844. ReturnTime: good.RecordDate,
  845. Type: 1,
  846. StorehouseId: houseConfig.StorehouseOutInfo,
  847. IsCheck: 1,
  848. }
  849. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(good.RecordDate, good.UserOrgId)
  850. if msgerrkonde == gorm.ErrRecordNotFound {
  851. AddSigleCancelStock(&cancelStock)
  852. }
  853. cancel, _ := GetLastCancelStockById(good.UserOrgId)
  854. for _, item := range info {
  855. manufacturer, _ := GetManufactureById(item.Manufacturer)
  856. deaerler, _ := GetDealerById(item.Dealer)
  857. cancelStockInfo := models.CancelStockInfo{
  858. GoodId: good.GoodId,
  859. CancelStockId: cancel.ID,
  860. GoodTypeId: good.GoodTypeId,
  861. Count: item.Count,
  862. Price: item.Price,
  863. Total: 0,
  864. ProductDate: item.ProductDate,
  865. ExpiryDate: item.ExpiryDate,
  866. Ctime: time.Now().Unix(),
  867. Status: 1,
  868. OrgId: good.UserOrgId,
  869. OrderNumber: cancel.OrderNumber,
  870. Type: 0,
  871. Dealer: deaerler.DealerName,
  872. Manufacturer: manufacturer.ManufacturerName,
  873. Number: item.Number,
  874. RegisterAccount: "",
  875. Remark: "",
  876. WarehouseInfoId: item.WarehouseInfotId,
  877. PatientId: item.PatientId,
  878. RecordDate: item.SysRecordTime,
  879. StorehouseId: houseConfig.StorehouseOutInfo,
  880. IsCheck: 1,
  881. }
  882. CreateCancelStockInfoOne(&cancelStockInfo)
  883. cancelInfo, _ := GetLastCancelStockInfoByGoodId(good.GoodId)
  884. goodList, _ := GetSumGoodList(item.OrgId, houseConfig.StorehouseOutInfo, item.GoodId)
  885. var over_count int64
  886. for _, it := range goodList {
  887. over_count += it.StockCount
  888. }
  889. flow := models.VmStockFlow{
  890. WarehousingId: item.WarehouseInfotId,
  891. GoodId: item.GoodId,
  892. Number: item.Number,
  893. LicenseNumber: item.LicenseNumber,
  894. Count: item.Count,
  895. UserOrgId: item.OrgId,
  896. PatientId: item.PatientId,
  897. SystemTime: item.SysRecordTime,
  898. ConsumableType: 7,
  899. IsSys: 0,
  900. WarehousingOrder: "",
  901. WarehouseOutId: item.WarehouseOutId,
  902. WarehouseOutOrderNumber: item.WarehouseOutOrderNumber,
  903. IsEdit: 0,
  904. CancelStockId: cancel.ID,
  905. CancelOrderNumber: cancel.OrderNumber,
  906. Manufacturer: manufacturer.ID,
  907. Dealer: 0,
  908. Creator: creater,
  909. UpdateCreator: 0,
  910. Status: 1,
  911. Ctime: time.Now().Unix(),
  912. Mtime: 0,
  913. Price: item.Price,
  914. WarehousingDetailId: item.WarehouseInfotId,
  915. WarehouseOutDetailId: item.ID,
  916. CancelOutDetailId: cancelInfo.ID,
  917. ProductDate: item.ProductDate,
  918. ExpireDate: item.ExpiryDate,
  919. StorehouseId: houseConfig.StorehouseOutInfo,
  920. OverCount: over_count,
  921. OperateTime: time.Now().Unix(),
  922. }
  923. CreateStockFlowOne(flow)
  924. //回退库存
  925. UpDateWarehouseInfoByStockDelete(item.WarehouseInfotId, item.Count, item.PatientId, item.SysRecordTime, item.GoodId)
  926. //退库数量增加
  927. UpdateSumAddCancelCount(good.UserOrgId, good.GoodId, houseConfig.StorehouseOutInfo, good.Count)
  928. //出库数量减少
  929. UpdateSumCount(good.UserOrgId, houseConfig.StorehouseOutInfo, good.GoodId, good.Count)
  930. //删除出库表
  931. DeleteAutoRedeceDetailTenOne(good.UserOrgId, item.PatientId, item.SysRecordTime, item.GoodId, item.OrderId)
  932. goodListOne, _ := GetSumGoodList(item.OrgId, houseConfig.StorehouseOutInfo, item.GoodId)
  933. var flush_count int64
  934. for _, it := range goodListOne {
  935. flush_count += it.StockCount
  936. }
  937. //更新剩余库存
  938. UpdateSumGood(good.UserOrgId, houseConfig.StorehouseOutInfo, item.GoodId, flush_count)
  939. }
  940. }
  941. return err
  942. }
  943. func CreatePatientDiagnose(diagnose models.XtPatientDiagnose) error {
  944. err := XTWriteDB().Create(&diagnose).Error
  945. return err
  946. }
  947. func GetPatientDiagnose(limit int64, page int64, orgid int64) (list []*models.XtPatientDiagnose, total int64, err error) {
  948. db := XTReadDB().Model(&list).Where("status =1")
  949. if orgid > 0 {
  950. db = db.Where("user_org_id = ?", orgid)
  951. }
  952. offset := (page - 1) * limit
  953. err = db.Count(&total).Offset(offset).Limit(limit).Order("sort asc").Find(&list).Error
  954. return
  955. }
  956. func GetPatientDiagnoseDetail(id int64) (models.XtPatientDiagnose, error) {
  957. diagnose := models.XtPatientDiagnose{}
  958. err := XTReadDB().Where("id = ? and status =1", id).Find(&diagnose).Error
  959. return diagnose, err
  960. }
  961. func UpdatePatientCase(class_name string, sort int64, id int64) error {
  962. diagnose := models.XtPatientDiagnose{}
  963. err := XTReadDB().Model(&diagnose).Where("id = ? and status=1", id).Updates(map[string]interface{}{"class_name": class_name, "sort": sort}).Error
  964. return err
  965. }
  966. func DeletePatientDiagnose(id int64) error {
  967. err := XTWriteDB().Model(&models.XtPatientDiagnose{}).Where("id=? and status= 1", id).Updates(map[string]interface{}{"status": 0}).Error
  968. return err
  969. }
  970. func GetPatientGoodStockCount(patient_id int64, record_time int64, user_org_id int64) (auto []*models.AutomaticReduceDetail, err error) {
  971. err = XTReadDB().Where("patient_id = ? and record_time =? and org_id = ? and status=1", patient_id, record_time, user_org_id).Find(&auto).Error
  972. return auto, err
  973. }
  974. func GetHisAdvicelistTemplate(drug_id int64, user_org_id int64) (advice []*models.HisPrescriptionAdviceTemplate, err error) {
  975. err = XTReadDB().Where("drug_id = ? and org_id =? and status=1", drug_id, user_org_id).Find(&advice).Error
  976. return advice, err
  977. }
  978. func UpdateHisAdviceListTemplate(id int64, price float64) error {
  979. err := XTWriteDB().Model(&models.HisPrescriptionAdviceTemplate{}).Where("id =? and status=1", id).Updates(map[string]interface{}{"price": price}).Error
  980. return err
  981. }