manage_center_service.go 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. package service
  2. import (
  3. "XT_New/models"
  4. "github.com/jinzhu/gorm"
  5. "strconv"
  6. "strings"
  7. "time"
  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 FindBaseDrugLibRecordSeven(org_id int64, id int64) (lib models.BaseDrugLibSeven, err error) {
  157. 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
  158. return
  159. }
  160. func GetDrugAutoWarehouseOut(patient_id int64, advicedate int64, drugid int64) (*models.DrugWarehouseOutInfo, error) {
  161. detail := models.DrugWarehouseOutInfo{}
  162. err := XTReadDB().Model(&detail).Where("patient_id = ? and sys_record_time =? and drug_id = ? and status = 1", patient_id, advicedate, drugid).Find(&detail).Error
  163. if err == gorm.ErrRecordNotFound {
  164. return nil, err
  165. }
  166. if err != nil {
  167. return nil, err
  168. }
  169. return &detail, nil
  170. }
  171. func GetDrugAutoWarehouseOutSeven(patient_id int64, advicedate int64, drugid int64, adviceid int64) (*models.DrugWarehouseOutInfo, error) {
  172. detail := models.DrugWarehouseOutInfo{}
  173. 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
  174. if err == gorm.ErrRecordNotFound {
  175. return nil, err
  176. }
  177. if err != nil {
  178. return nil, err
  179. }
  180. return &detail, nil
  181. }
  182. func GetDrugAutoWarehouseOutEight(patient_id int64, advicedate int64, drugid int64, adviceid int64) (models.DrugWarehouseOutInfo, error) {
  183. detail := models.DrugWarehouseOutInfo{}
  184. 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
  185. return detail, err
  186. }
  187. func GetDrugAutoWarehouseOutNight(patient_id int64, advicedate int64, drugid int64, adviceid int64) (night []*models.DrugWarehouseOutInfo, err error) {
  188. 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
  189. return night, err
  190. }
  191. func UpdateDrugWarehouse(id int64, info models.DrugWarehouseInfo) error {
  192. err := writeDb.Model(&models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  193. return err
  194. }
  195. func UpdateDrugWarehouseOne(id int64, info models.DrugWarehouseInfo) error {
  196. err := writeDb.Model(&models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  197. return err
  198. }
  199. 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) {
  200. db := readDb.Model(&models.BaseDrugLib{}).Where("org_id = ? AND status = 1", org_id)
  201. if len(keyword) != 0 {
  202. keyword = "%" + keyword + "%"
  203. db = db.Where("drug_name Like ?", keyword)
  204. }
  205. if is_use != 0 {
  206. if is_use == 2 {
  207. db = db.Where("find_in_set('停用',drug_status) = 0")
  208. } else {
  209. db = db.Where("find_in_set('停用',drug_status) > 0")
  210. }
  211. }
  212. if is_charge == 1 {
  213. //if is_charge == 1 {
  214. // db = db.Where("find_in_set('收费',drug_status) > 0")
  215. //
  216. //} else {
  217. // db = db.Where("find_in_set('收费',drug_status) = 0")
  218. //}
  219. db = db.Where("is_record = ?", is_charge)
  220. }
  221. if is_charge == 2 {
  222. db = db.Where("is_record= 0 || is_record = 2")
  223. }
  224. if is_inject != 0 {
  225. //db = db.Where("drug_name Like ?", keyword)
  226. if is_inject == 1 {
  227. db = db.Where("find_in_set('注射类',drug_status) > 0")
  228. } else {
  229. db = db.Where("find_in_set('注射类',drug_status) = 0")
  230. }
  231. }
  232. if manufacturer > 0 {
  233. db = db.Where("manufacturer = ?", manufacturer)
  234. }
  235. if isRecord > 0 {
  236. db = db.Where("is_mark = ?", isRecord)
  237. }
  238. offset := (page - 1) * limit
  239. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  240. return
  241. }
  242. func CreateMedicineInsurancePercentage(mip *models.MedicineInsurancePercentage) (err error) {
  243. err = writeDb.Create(mip).Error
  244. return
  245. }
  246. func CreateUnitSafeguard(dus *models.DrugUnitSafeguard) (err error) {
  247. err = writeDb.Create(dus).Error
  248. return
  249. }
  250. func AddSigleMZSick(dealer *models.OutpatientServiceSick) (error, *models.OutpatientServiceSick) {
  251. err := writeDb.Create(&dealer).Error
  252. return err, dealer
  253. }
  254. func ModifyMZSick(mesick *models.OutpatientServiceSick) error {
  255. err := writeDb.Save(&mesick).Error
  256. return err
  257. }
  258. func FindAllMZSickList(orgId int64, page int64, limit int64) (list []*models.OutpatientServiceSick, total int64, err error) {
  259. offset := (page - 1) * limit
  260. db := readDb.Model(&models.OutpatientServiceSick{})
  261. db = db.Where("user_org_id = ? AND status = 1", orgId)
  262. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  263. return
  264. }
  265. func DeleteMZSickById(id int64) error {
  266. err := writeDb.Model(&models.OutpatientServiceSick{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  267. return err
  268. }
  269. func FindMZSickById(id int64) (*models.OutpatientServiceSick, error) {
  270. dealer := &models.OutpatientServiceSick{}
  271. err := readDb.Model(&models.OutpatientServiceSick{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  272. return dealer, err
  273. }
  274. func GetDrugWareoutInfo(id int64) (*models.XtDrugWarehouseInfo, error) {
  275. info := models.XtDrugWarehouseInfo{}
  276. err = XTReadDB().Model(&info).Where("drug_id = ? and status = 1", id).Find(&info).Error
  277. if err == gorm.ErrRecordNotFound {
  278. return nil, err
  279. }
  280. if err != nil {
  281. return nil, err
  282. }
  283. return &info, nil
  284. }
  285. func GetDrugWareOutById(id int64) (*models.XtDrugWarehouseOutInfo, error) {
  286. info := models.XtDrugWarehouseOutInfo{}
  287. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", id).Find(&info).Error
  288. if err == gorm.ErrRecordNotFound {
  289. return nil, err
  290. }
  291. if err != nil {
  292. return nil, err
  293. }
  294. return &info, nil
  295. }
  296. func DeleteBaseDrugById(id int64) error {
  297. drug := models.XtBaseDrug{}
  298. err := XTWriteDB().Model(&drug).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  299. return err
  300. }
  301. func AddSigleAddition(dealer *models.XtHisAddtionConfig) (error, *models.XtHisAddtionConfig) {
  302. err := writeDb.Create(&dealer).Error
  303. return err, dealer
  304. }
  305. func ModifyAddition(mesick *models.XtHisAddtionConfig) error {
  306. err := writeDb.Save(&mesick).Error
  307. return err
  308. }
  309. func FindAllAdditionList(orgId int64, page int64, limit int64) (list []*models.XtHisAddtionConfig, total int64, err error) {
  310. offset := (page - 1) * limit
  311. db := readDb.Model(&models.XtHisAddtionConfig{})
  312. db = db.Where("user_org_id = ? AND status = 1", orgId)
  313. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  314. return
  315. }
  316. func DeleteAdditionById(id int64) error {
  317. err := writeDb.Model(&models.XtHisAddtionConfig{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  318. return err
  319. }
  320. func FindAdditionById(id int64) (*models.XtHisAddtionConfig, error) {
  321. dealer := &models.XtHisAddtionConfig{}
  322. err := readDb.Model(&models.XtHisAddtionConfig{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  323. return dealer, err
  324. }
  325. func AddSigleDiagnose(dealer *models.HisXtDiagnoseConfig) (error, *models.HisXtDiagnoseConfig) {
  326. err := writeDb.Create(&dealer).Error
  327. return err, dealer
  328. }
  329. func ModifyDiagnose(mesick *models.HisXtDiagnoseConfig) error {
  330. err := writeDb.Save(&mesick).Error
  331. return err
  332. }
  333. func FindAllDiagnoseList(orgId int64, page int64, limit int64) (list []*models.HisXtDiagnoseConfig, total int64, err error) {
  334. offset := (page - 1) * limit
  335. db := readDb.Model(&models.HisXtDiagnoseConfig{})
  336. db = db.Where("user_org_id = ? AND status = 1", orgId)
  337. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  338. return
  339. }
  340. func DeleteDiagnoseById(id int64) error {
  341. err := writeDb.Model(&models.HisXtDiagnoseConfig{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  342. return err
  343. }
  344. func FindDiagnoseById(id int64) (*models.HisXtDiagnoseConfig, error) {
  345. dealer := &models.HisXtDiagnoseConfig{}
  346. err := readDb.Model(&models.HisXtDiagnoseConfig{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  347. return dealer, err
  348. }
  349. func GetDoseCode(dose_id int64, user_org_id int64) (error, string) {
  350. var data models.DictDataconfig
  351. 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
  352. return err, data.Code
  353. }
  354. func GetAllDrugList(orgid int64) (drug []*models.BaseDrugLib, err error) {
  355. err = XTReadDB().Model(&drug).Where("org_id = ? and status = 1", orgid).Find(&drug).Error
  356. return drug, err
  357. }
  358. //自动退库分装方法
  359. func DrugAutoAddCancelInfo(advice *models.HisDoctorAdviceInfo, creater int64) error {
  360. //查询该病人该药品是否有库存记录
  361. info, _ := GetDrugAutoWarehouseOutEight(advice.PatientId, advice.AdviceDate, advice.DrugId, advice.ID)
  362. list, _ := GetDrugAutoWarehouseOutNight(advice.PatientId, advice.AdviceDate, advice.DrugId, advice.ID)
  363. storeHouseConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  364. //退库
  365. drug, _ := FindBaseDrugLibRecord(advice.UserOrgId, advice.DrugId)
  366. //如果剩余数量 + 退库数量 < 总入库数量
  367. if len(list) > 0 {
  368. for _, its := range list {
  369. //查询该药品出库批次总入库数量
  370. druginfo, _ := GetDrugInfoSix(its.WarehouseInfoId, info.OrgId)
  371. var out_count int64 //该批次出库库存
  372. var sum_count int64 //该批次总库存
  373. var over_count int64 //剩余数量
  374. over_count = druginfo.StockMaxNumber*drug.MinNumber + druginfo.StockMinNumber
  375. if info.CountUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  376. out_count = info.Count * drug.MinNumber
  377. }
  378. if info.CountUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  379. out_count = info.Count
  380. }
  381. if info.CountUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  382. out_count = info.Count
  383. }
  384. if druginfo.MaxUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  385. sum_count = druginfo.WarehousingCount * drug.MinNumber
  386. }
  387. if druginfo.MaxUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  388. sum_count = druginfo.WarehousingCount
  389. }
  390. if druginfo.MaxUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  391. sum_count = druginfo.WarehousingCount
  392. }
  393. if (over_count + out_count) <= sum_count {
  394. if its.CountUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  395. //退库
  396. warehouseInfo := models.DrugWarehouseInfo{
  397. StockMinNumber: its.Count,
  398. }
  399. UpdateDrugWarehouse(its.WarehouseInfoId, warehouseInfo)
  400. //增加退库数量
  401. AddCancelSumCountOne(storeHouseConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, its.Count)
  402. //出库数量减少
  403. UpdateSumOutDrug(advice.UserOrgId, storeHouseConfig.DrugStorehouseOut, advice.DrugId, its.Count)
  404. }
  405. if its.CountUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  406. warehouseInfo := models.DrugWarehouseInfo{
  407. StockMaxNumber: its.Count,
  408. }
  409. UpdateDrugWarehouseOne(its.WarehouseInfoId, warehouseInfo)
  410. //增加退库数量
  411. AddCancelSumCountOne(storeHouseConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, its.Count*drug.MinNumber)
  412. //出库数量减少
  413. UpdateSumOutDrug(advice.UserOrgId, storeHouseConfig.DrugStorehouseOut, advice.DrugId, its.Count*drug.MinNumber)
  414. }
  415. if its.CountUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  416. warehouseInfo := models.DrugWarehouseInfo{
  417. StockMaxNumber: its.Count,
  418. }
  419. UpdateDrugWarehouseOne(its.WarehouseInfoId, warehouseInfo)
  420. //增加退库数量
  421. AddCancelSumCountOne(storeHouseConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, its.Count*drug.MinNumber)
  422. //出库数量减少
  423. UpdateSumOutDrug(advice.UserOrgId, storeHouseConfig.DrugStorehouseOut, advice.DrugId, its.Count*drug.MinNumber)
  424. }
  425. }
  426. }
  427. floatPrescrip := strconv.FormatFloat(advice.PrescribingNumber, 'f', -1, 32)
  428. prescribingNumber, _ := strconv.ParseInt(floatPrescrip, 10, 64)
  429. if advice.PrescribingNumberUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  430. operation_time := time.Now().Unix()
  431. ctime := time.Now().Unix()
  432. timeStr := time.Now().Format("2006-01-02")
  433. timeArr := strings.Split(timeStr, "-")
  434. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  435. total = total + 1
  436. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  437. cancelStock := models.DrugCancelStock{
  438. OrderNumber: orderNumber,
  439. OperaTime: operation_time,
  440. OrgId: advice.UserOrgId,
  441. Creater: creater,
  442. Ctime: ctime,
  443. Status: 1,
  444. ReturnTime: advice.AdviceDate,
  445. Dealer: info.Dealer,
  446. Manufacturer: info.Manufacturer,
  447. Type: 1,
  448. StorehouseId: storeHouseConfig.DrugStorehouseOut,
  449. IsCheck: 1,
  450. }
  451. AddSigleDrugCancelStock(&cancelStock)
  452. manufactureName, _ := GetManufactureById(info.Manufacturer)
  453. dealer, _ := GetDealerById(info.Dealer)
  454. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  455. cancelStockInfo := models.DrugCancelStockInfo{
  456. OrderNumber: cancelInfo.OrderNumber,
  457. CancelStockId: cancelInfo.ID,
  458. DrugId: advice.DrugId,
  459. Count: prescribingNumber,
  460. Status: 1,
  461. Ctime: ctime,
  462. OrgId: advice.UserOrgId,
  463. Type: 1,
  464. Manufacturer: manufactureName.ManufacturerName,
  465. Dealer: dealer.DealerName,
  466. Total: 0,
  467. RetailPrice: info.RetailPrice,
  468. RetailTotalPrice: info.RetailTotalPrice,
  469. Price: info.Price,
  470. RegisterAccount: "",
  471. Remark: info.Remark,
  472. BatchNumber: info.BatchNumber,
  473. MaxUnit: advice.PrescribingNumberUnit,
  474. ProductDate: info.ProductDate,
  475. ExpiryDate: info.ExpiryDate,
  476. BatchNumberId: info.WarehouseInfoId,
  477. StorehouseId: storeHouseConfig.DrugStorehouseOut,
  478. IsCheck: 1,
  479. }
  480. //查询剩余库存
  481. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, advice.UserOrgId, advice.DrugId)
  482. var sum_count int64
  483. for _, its := range stockInfo {
  484. if its.MaxUnit == drug.MaxUnit {
  485. its.StockMaxNumber = its.StockMaxNumber * drug.MinNumber
  486. }
  487. sum_count += its.StockMaxNumber + its.StockMinNumber
  488. }
  489. var count_one int64
  490. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  491. count_one = prescribingNumber * drug.MinNumber
  492. }
  493. if advice.PrescribingNumberUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  494. count_one = prescribingNumber
  495. }
  496. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  497. count_one = prescribingNumber * drug.MinNumber
  498. }
  499. flow := models.DrugFlow{
  500. WarehousingId: info.WarehouseInfoId,
  501. DrugId: info.DrugId,
  502. Number: "",
  503. BatchNumber: info.BatchNumber,
  504. Count: count_one,
  505. UserOrgId: advice.UserOrgId,
  506. PatientId: advice.PatientId,
  507. SystemTime: info.SysRecordTime,
  508. ConsumableType: 7,
  509. IsSys: 0,
  510. WarehousingOrder: "",
  511. WarehouseOutId: 0,
  512. WarehouseOutOrderNumber: info.WarehouseOutOrderNumber,
  513. IsEdit: 0,
  514. CancelStockId: cancelStock.ID,
  515. CancelOrderNumber: cancelStock.OrderNumber,
  516. Manufacturer: info.Manufacturer,
  517. Dealer: info.Dealer,
  518. Creator: creater,
  519. UpdateCreator: 0,
  520. Status: 1,
  521. Ctime: time.Now().Unix(),
  522. Mtime: 0,
  523. Price: info.Price,
  524. WarehousingDetailId: info.WarehouseInfoId,
  525. WarehouseOutDetailId: 0,
  526. CancelOutDetailId: 0,
  527. ExpireDate: info.ExpiryDate,
  528. ProductDate: info.ProductDate,
  529. MaxUnit: drug.MinUnit,
  530. MinUnit: "",
  531. AdviceId: info.AdviceId,
  532. StorehouseId: storeHouseConfig.DrugStorehouseOut,
  533. OverCount: sum_count,
  534. }
  535. CreatedCancelStock(cancelStockInfo)
  536. CreateDrugFlowOne(flow)
  537. //删除出库记录
  538. UpdateDrugFlowSeven(info.PatientId, info.SysRecordTime, info.DrugId, advice.ID, info.ID)
  539. //删除流水
  540. UpdateDrugFlowTens(info.PatientId, info.WarehouseOutOrderNumber, info.DrugId, advice.ID)
  541. }
  542. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  543. houseConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  544. operation_time := time.Now().Unix()
  545. ctime := time.Now().Unix()
  546. timeStr := time.Now().Format("2006-01-02")
  547. timeArr := strings.Split(timeStr, "-")
  548. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  549. total = total + 1
  550. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  551. cancelStock := models.DrugCancelStock{
  552. OrderNumber: orderNumber,
  553. OperaTime: operation_time,
  554. OrgId: advice.UserOrgId,
  555. Creater: creater,
  556. Ctime: ctime,
  557. Status: 1,
  558. ReturnTime: advice.AdviceDate,
  559. Dealer: info.Dealer,
  560. Manufacturer: info.Manufacturer,
  561. Type: 1,
  562. StorehouseId: houseConfig.DrugStorehouseOut,
  563. IsCheck: 1,
  564. }
  565. AddSigleDrugCancelStock(&cancelStock)
  566. manufactureName, _ := GetManufactureById(info.Manufacturer)
  567. dealer, _ := GetDealerById(info.Dealer)
  568. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  569. cancelStockInfo := models.DrugCancelStockInfo{
  570. OrderNumber: cancelInfo.OrderNumber,
  571. CancelStockId: cancelInfo.ID,
  572. DrugId: info.DrugId,
  573. Count: prescribingNumber,
  574. Status: 1,
  575. Ctime: ctime,
  576. OrgId: advice.UserOrgId,
  577. Type: 1,
  578. Manufacturer: manufactureName.ManufacturerName,
  579. Dealer: dealer.DealerName,
  580. Total: 0,
  581. RetailPrice: info.RetailPrice,
  582. RetailTotalPrice: info.RetailTotalPrice,
  583. Price: info.Price,
  584. RegisterAccount: "",
  585. Remark: info.Remark,
  586. BatchNumber: info.BatchNumber,
  587. MaxUnit: advice.PrescribingNumberUnit,
  588. ProductDate: info.ProductDate,
  589. ExpiryDate: info.ExpiryDate,
  590. BatchNumberId: info.WarehouseInfoId,
  591. StorehouseId: houseConfig.DrugStorehouseOut,
  592. IsCheck: 1,
  593. }
  594. //查询剩余库存
  595. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, advice.UserOrgId, advice.DrugId)
  596. var sum_count int64
  597. for _, its := range stockInfo {
  598. if its.MaxUnit == drug.MaxUnit {
  599. its.StockMaxNumber = its.StockMaxNumber * drug.MinNumber
  600. }
  601. sum_count += its.StockMaxNumber + its.StockMinNumber
  602. }
  603. var count_one int64
  604. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  605. count_one = prescribingNumber * drug.MinNumber
  606. }
  607. if advice.PrescribingNumberUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  608. count_one = prescribingNumber
  609. }
  610. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  611. count_one = prescribingNumber * drug.MinNumber
  612. }
  613. flow := models.DrugFlow{
  614. WarehousingId: info.WarehouseInfoId,
  615. DrugId: info.DrugId,
  616. Number: "",
  617. BatchNumber: info.BatchNumber,
  618. Count: count_one,
  619. UserOrgId: advice.UserOrgId,
  620. PatientId: advice.PatientId,
  621. SystemTime: info.SysRecordTime,
  622. ConsumableType: 7,
  623. IsSys: 0,
  624. WarehousingOrder: "",
  625. WarehouseOutId: 0,
  626. WarehouseOutOrderNumber: info.WarehouseOutOrderNumber,
  627. IsEdit: 0,
  628. CancelStockId: cancelStock.ID,
  629. CancelOrderNumber: cancelStock.OrderNumber,
  630. Manufacturer: info.Manufacturer,
  631. Dealer: info.Dealer,
  632. Creator: creater,
  633. UpdateCreator: 0,
  634. Status: 1,
  635. Ctime: time.Now().Unix(),
  636. Mtime: 0,
  637. Price: info.Price,
  638. WarehousingDetailId: info.WarehouseInfoId,
  639. WarehouseOutDetailId: 0,
  640. CancelOutDetailId: 0,
  641. ExpireDate: info.ExpiryDate,
  642. ProductDate: info.ProductDate,
  643. MaxUnit: drug.MinUnit,
  644. MinUnit: "",
  645. AdviceId: info.AdviceId,
  646. StorehouseId: houseConfig.DrugStorehouseOut,
  647. OverCount: sum_count,
  648. }
  649. CreatedCancelStock(cancelStockInfo)
  650. CreateDrugFlowOne(flow)
  651. //删除出库记录
  652. UpdateDrugFlowSeven(info.PatientId, info.SysRecordTime, info.DrugId, advice.ID, info.ID)
  653. //删除流水
  654. UpdateDrugFlowTens(info.PatientId, info.WarehouseOutOrderNumber, info.DrugId, advice.ID)
  655. }
  656. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  657. houseConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  658. operation_time := time.Now().Unix()
  659. ctime := time.Now().Unix()
  660. timeStr := time.Now().Format("2006-01-02")
  661. timeArr := strings.Split(timeStr, "-")
  662. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  663. total = total + 1
  664. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  665. cancelStock := models.DrugCancelStock{
  666. OrderNumber: orderNumber,
  667. OperaTime: operation_time,
  668. OrgId: advice.UserOrgId,
  669. Creater: creater,
  670. Ctime: ctime,
  671. Status: 1,
  672. ReturnTime: advice.AdviceDate,
  673. Dealer: info.Dealer,
  674. Manufacturer: info.Manufacturer,
  675. Type: 1,
  676. StorehouseId: houseConfig.DrugStorehouseOut,
  677. IsCheck: 1,
  678. }
  679. AddSigleDrugCancelStock(&cancelStock)
  680. manufactureName, _ := GetManufactureById(info.Manufacturer)
  681. dealer, _ := GetDealerById(info.Dealer)
  682. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  683. cancelStockInfo := models.DrugCancelStockInfo{
  684. OrderNumber: cancelInfo.OrderNumber,
  685. CancelStockId: cancelInfo.ID,
  686. DrugId: info.DrugId,
  687. Count: prescribingNumber,
  688. Status: 1,
  689. Ctime: ctime,
  690. OrgId: advice.UserOrgId,
  691. Type: 1,
  692. Manufacturer: manufactureName.ManufacturerName,
  693. Dealer: dealer.DealerName,
  694. Total: 0,
  695. RetailPrice: info.RetailPrice,
  696. RetailTotalPrice: info.RetailTotalPrice,
  697. Price: info.Price,
  698. RegisterAccount: "",
  699. Remark: info.Remark,
  700. BatchNumber: info.BatchNumber,
  701. MaxUnit: advice.PrescribingNumberUnit,
  702. ProductDate: info.ProductDate,
  703. ExpiryDate: info.ExpiryDate,
  704. BatchNumberId: info.WarehouseInfoId,
  705. StorehouseId: houseConfig.DrugStorehouseOut,
  706. IsCheck: 1,
  707. }
  708. //查询剩余库存
  709. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, advice.UserOrgId, advice.DrugId)
  710. var sum_count int64
  711. for _, its := range stockInfo {
  712. if its.MaxUnit == drug.MaxUnit {
  713. its.StockMaxNumber = its.StockMaxNumber * drug.MinNumber
  714. }
  715. sum_count += its.StockMaxNumber + its.StockMinNumber
  716. }
  717. flow := models.DrugFlow{
  718. WarehousingId: info.WarehouseInfoId,
  719. DrugId: info.DrugId,
  720. Number: "",
  721. BatchNumber: info.BatchNumber,
  722. Count: prescribingNumber,
  723. UserOrgId: advice.UserOrgId,
  724. PatientId: 0,
  725. SystemTime: info.SysRecordTime,
  726. ConsumableType: 7,
  727. IsSys: 0,
  728. WarehousingOrder: "",
  729. WarehouseOutId: 0,
  730. WarehouseOutOrderNumber: info.WarehouseOutOrderNumber,
  731. IsEdit: 0,
  732. CancelStockId: cancelStock.ID,
  733. CancelOrderNumber: cancelStock.OrderNumber,
  734. Manufacturer: info.Manufacturer,
  735. Dealer: info.Dealer,
  736. Creator: creater,
  737. UpdateCreator: 0,
  738. Status: 1,
  739. Ctime: time.Now().Unix(),
  740. Mtime: 0,
  741. Price: info.Price,
  742. WarehousingDetailId: info.WarehouseInfoId,
  743. WarehouseOutDetailId: 0,
  744. CancelOutDetailId: 0,
  745. ExpireDate: info.ExpiryDate,
  746. ProductDate: info.ProductDate,
  747. MaxUnit: advice.PrescribingNumberUnit,
  748. MinUnit: "",
  749. AdviceId: info.AdviceId,
  750. StorehouseId: houseConfig.DrugStorehouseOut,
  751. OverCount: sum_count,
  752. }
  753. CreatedCancelStock(cancelStockInfo)
  754. CreateDrugFlowOne(flow)
  755. //删除出库记录
  756. UpdateDrugFlowSeven(info.PatientId, info.SysRecordTime, info.DrugId, advice.ID, info.ID)
  757. //删除流水
  758. UpdateDrugFlowTens(info.PatientId, info.WarehouseOutOrderNumber, info.DrugId, advice.ID)
  759. }
  760. //更新字典里面的库存
  761. stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, advice.UserOrgId, advice.DrugId)
  762. //查询改药品信息
  763. medical, _ := GetBaseDrugMedical(advice.DrugId)
  764. var sum_count_one int64
  765. for _, its := range stockInfo {
  766. if its.MaxUnit == medical.MaxUnit {
  767. its.StockMaxNumber = its.StockMaxNumber * medical.MinNumber
  768. }
  769. sum_count_one += its.StockMaxNumber + its.StockMinNumber
  770. }
  771. UpdateBaseDrugSumTwo(advice.DrugId, sum_count_one, advice.UserOrgId)
  772. //更新库存
  773. UpdateSumDrug(advice.UserOrgId, storeHouseConfig.DrugStorehouseOut, advice.DrugId, sum_count_one)
  774. }
  775. return err
  776. }
  777. func GoodAutoAddCancelInfo(good *models.NewDialysisBeforePrepareGoods, creater int64) error {
  778. //查询已经出库数量
  779. info, _ := GetWareOutInfo(good.GoodId, good.RecordDate, good.UserOrgId, good.PatientId)
  780. if len(info) > 0 {
  781. houseConfig, _ := GetAllStoreHouseConfig(good.UserOrgId)
  782. operation_time := time.Now().Unix()
  783. //创建退库单
  784. timeStr := time.Now().Format("2006-01-02")
  785. timeArr := strings.Split(timeStr, "-")
  786. total, _ := FindAllCancelStockTotal(good.UserOrgId)
  787. total = total + 1
  788. orderNumber := "CKTKD" + strconv.FormatInt(good.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  789. cancelStock := models.CancelStock{
  790. OrderNumber: orderNumber,
  791. OperaTime: operation_time,
  792. OrgId: good.UserOrgId,
  793. Creater: creater,
  794. Ctime: time.Now().Unix(),
  795. Status: 1,
  796. ReturnTime: good.RecordDate,
  797. Type: 1,
  798. StorehouseId: houseConfig.StorehouseOutInfo,
  799. IsCheck: 1,
  800. }
  801. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(good.RecordDate, good.UserOrgId)
  802. if msgerrkonde == gorm.ErrRecordNotFound {
  803. AddSigleCancelStock(&cancelStock)
  804. }
  805. cancel, _ := GetLastCancelStockById(good.UserOrgId)
  806. for _, item := range info {
  807. manufacturer, _ := GetManufactureById(item.Manufacturer)
  808. deaerler, _ := GetDealerById(item.Dealer)
  809. cancelStockInfo := models.CancelStockInfo{
  810. GoodId: good.GoodId,
  811. CancelStockId: cancel.ID,
  812. GoodTypeId: good.GoodTypeId,
  813. Count: item.Count,
  814. Price: item.Price,
  815. Total: 0,
  816. ProductDate: item.ProductDate,
  817. ExpiryDate: item.ExpiryDate,
  818. Ctime: time.Now().Unix(),
  819. Status: 1,
  820. OrgId: good.UserOrgId,
  821. OrderNumber: cancel.OrderNumber,
  822. Type: 0,
  823. Dealer: deaerler.DealerName,
  824. Manufacturer: manufacturer.ManufacturerName,
  825. Number: item.Number,
  826. RegisterAccount: "",
  827. Remark: "",
  828. WarehouseInfoId: item.WarehouseInfotId,
  829. PatientId: item.PatientId,
  830. RecordDate: item.SysRecordTime,
  831. StorehouseId: houseConfig.StorehouseOutInfo,
  832. IsCheck: 1,
  833. }
  834. CreateCancelStockInfoOne(&cancelStockInfo)
  835. cancelInfo, _ := GetLastCancelStockInfoByGoodId(good.GoodId)
  836. goodList, _ := GetSumGoodList(item.OrgId, houseConfig.StorehouseOutInfo, item.GoodId)
  837. var over_count int64
  838. for _, it := range goodList {
  839. over_count += it.StockCount
  840. }
  841. flow := models.VmStockFlow{
  842. WarehousingId: item.WarehouseInfotId,
  843. GoodId: item.GoodId,
  844. Number: item.Number,
  845. LicenseNumber: item.LicenseNumber,
  846. Count: item.Count,
  847. UserOrgId: item.OrgId,
  848. PatientId: item.PatientId,
  849. SystemTime: item.SysRecordTime,
  850. ConsumableType: 7,
  851. IsSys: 0,
  852. WarehousingOrder: "",
  853. WarehouseOutId: item.WarehouseOutId,
  854. WarehouseOutOrderNumber: item.WarehouseOutOrderNumber,
  855. IsEdit: 0,
  856. CancelStockId: cancel.ID,
  857. CancelOrderNumber: cancel.OrderNumber,
  858. Manufacturer: manufacturer.ID,
  859. Dealer: 0,
  860. Creator: creater,
  861. UpdateCreator: 0,
  862. Status: 1,
  863. Ctime: time.Now().Unix(),
  864. Mtime: 0,
  865. Price: item.Price,
  866. WarehousingDetailId: item.WarehouseInfotId,
  867. WarehouseOutDetailId: item.ID,
  868. CancelOutDetailId: cancelInfo.ID,
  869. ProductDate: item.ProductDate,
  870. ExpireDate: item.ExpiryDate,
  871. StorehouseId: houseConfig.StorehouseOutInfo,
  872. OverCount: over_count,
  873. }
  874. CreateStockFlowOne(flow)
  875. //回退库存
  876. UpDateWarehouseInfoByStockDelete(item.WarehouseInfotId, item.Count, item.PatientId, item.SysRecordTime, item.GoodId)
  877. //退库数量增加
  878. UpdateSumAddCancelCount(good.UserOrgId, good.GoodId, houseConfig.StorehouseOutInfo, good.Count)
  879. //出库数量减少
  880. UpdateSumCount(good.UserOrgId, houseConfig.StorehouseOutInfo, good.GoodId, good.Count)
  881. //删除出库表
  882. DeleteAutoRedeceDetailTen(good.UserOrgId, item.PatientId, item.SysRecordTime, item.GoodId)
  883. goodListOne, _ := GetSumGoodList(item.OrgId, houseConfig.StorehouseOutInfo, item.GoodId)
  884. var flush_count int64
  885. for _, it := range goodListOne {
  886. flush_count += it.StockCount
  887. }
  888. //更新剩余库存
  889. UpdateSumGood(good.UserOrgId, houseConfig.StorehouseOutInfo, item.GoodId, flush_count)
  890. }
  891. }
  892. return err
  893. }