self_drug_service.go 46KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. package service
  2. import (
  3. "Xcx_New/models"
  4. "fmt"
  5. "github.com/jinzhu/gorm"
  6. "time"
  7. )
  8. func GetCurrentPatient(orgid int64) (patients []*models.DrugPatients, err error) {
  9. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&patients).Error
  10. return patients, err
  11. }
  12. func GetAllDrugName(orgid int64) (baseDrug []*models.XtBaseDrug, err error) {
  13. err = XTReadDB().Group("drug_name").Where("org_id = ? and status = 1 and find_in_set('停用',drug_status) = 0", orgid).Find(&baseDrug).Error
  14. return baseDrug, err
  15. }
  16. func GetDrugName(drugname string, orgid int64) (*models.XtDrugName, error) {
  17. drugName := models.XtDrugName{}
  18. err := XTReadDB().Model(&drugName).Where("drug_name = ? and user_org_id = ? and status =1", drugname, orgid).Find(&drugName).Error
  19. if err == gorm.ErrRecordNotFound {
  20. return nil, err
  21. }
  22. if err != nil {
  23. return nil, err
  24. }
  25. return &drugName, nil
  26. }
  27. func SaveDrugName(drugname *models.XtDrugName) error {
  28. err := XTWriteDB().Create(&drugname).Error
  29. return err
  30. }
  31. func GetDrugNameList(orgid int64) (druname []*models.XtDrugName, err error) {
  32. err = XTReadDB().Model(&druname).Where("user_org_id = ? and status = 1", orgid).Find(&druname).Error
  33. return druname, err
  34. }
  35. func GetRulleName(orgid int64, name string) (baseDrug []*models.XtBaseDrug, err error) {
  36. err = XTReadDB().Model(&baseDrug).Where("org_id = ? and drug_name = ? and status = 1", orgid, name).Find(&baseDrug).Error
  37. return baseDrug, err
  38. }
  39. func GetDrugId(id int64) (models.XtSelfMedical, error) {
  40. medical := models.XtSelfMedical{}
  41. err := XTReadDB().Model(&medical).Where("id = ? and status = 1", id).Find(&medical).Error
  42. return medical, err
  43. }
  44. func GetRulleList(orgid int64, id int64) (rulleName []*models.XtStandName, err error) {
  45. err = XTReadDB().Model(&rulleName).Where("user_org_id = ? and drug_id = ? and status = 1", orgid, id).Find(&rulleName).Error
  46. return rulleName, err
  47. }
  48. func GetUnitByBaseId(id int64) (models.XtBaseDrug, error) {
  49. drug := models.XtBaseDrug{}
  50. err := XTReadDB().Model(&drug).Where("id = ? and status = 1", id).Find(&drug).Error
  51. return drug, err
  52. }
  53. func IsExistStandName(drugname string, drugspec string, orgid int64) (*models.XtStandName, error) {
  54. standName := models.XtStandName{}
  55. err := XTReadDB().Model(&standName).Where("drug_name = ? and drug_spec = ? and user_org_id = ? and status = 1", drugname, drugspec, orgid).Find(&standName).Error
  56. if err == gorm.ErrRecordNotFound {
  57. return nil, err
  58. }
  59. if err != nil {
  60. return nil, err
  61. }
  62. return &standName, nil
  63. }
  64. func SaveRulleName(rullName *models.XtStandName) error {
  65. err := XTWriteDB().Model(&rullName).Create(&rullName).Error
  66. return err
  67. }
  68. func GetRullerNameDetail(id int64) (models.XtStandName, error) {
  69. rullerName := models.XtStandName{}
  70. err := XTReadDB().Model(&rullerName).Where("id= ? and status = 1", id).Find(&rullerName).Error
  71. return rullerName, err
  72. }
  73. func GetIsExit(drugname string, drugspec string, id int64) (*models.XtStandName, error) {
  74. standName := models.XtStandName{}
  75. err := XTReadDB().Model(&standName).Where("drug_name = ? and drug_spec = ? and id <> ? and status = 1", drugname, drugspec, id).Find(&standName).Error
  76. if err == gorm.ErrRecordNotFound {
  77. return nil, err
  78. }
  79. if err != nil {
  80. return nil, err
  81. }
  82. return &standName, nil
  83. }
  84. func UpdatedRullerName(id int64, re *models.XtStandName) error {
  85. err := XTWriteDB().Model(&re).Where("id=?", id).Update(map[string]interface{}{"drug_spec": re.DrugSpec, "min_unit": re.MinUnit, "price": re.Price, "drug_stock_limit": re.DrugStockLimit, "drug_name": re.DrugName, "drug_name_id": re.DrugNameId, "drug_id": re.DrugId}).Error
  86. return err
  87. }
  88. //func GetAllDrugNameList(orgid int64) (rullername []*models.XtStandName, err error) {
  89. //
  90. // err = XTReadDB().Group("drug_name").Model(&rullername).Where("user_org_id = ? and status = 1", orgid).Find(&rullername).Error
  91. // return rullername, err
  92. //}
  93. func GetAllDrugNameList(orgid int64) (drugName []*models.XtDrugName, err error) {
  94. err = XTReadDB().Model(&drugName).Where("user_org_id = ? and status = 1", orgid).Find(&drugName).Error
  95. return drugName, err
  96. }
  97. func GetRullerListByDrugName(drugname string, orgid int64) (rullername []*models.XtStandName, err error) {
  98. err = XTReadDB().Model(&rullername).Where("drug_id = ? and user_org_id = ? and status = 1", drugname, orgid).Find(&rullername).Error
  99. return rullername, err
  100. }
  101. func GetBaseDrugMedical(id int64) (models.XtBaseDrug, error) {
  102. drug := models.XtBaseDrug{}
  103. err := XTReadDB().Model(&drug).Where("id = ? and status = 1", id).Find(&drug).Error
  104. return drug, err
  105. }
  106. func GetSelfMedicalByDrugName(drugname string, drug_spec string, patientid int64) (*models.XtSelfMedical, error) {
  107. medical := models.XtSelfMedical{}
  108. err := XTReadDB().Model(&medical).Where("drug_name = ? and drug_spec = ? and patient_id = ? and status = 1", drugname, drug_spec, patientid).Find(&medical).Error
  109. if err == gorm.ErrRecordNotFound {
  110. return nil, err
  111. }
  112. if err != nil {
  113. return nil, err
  114. }
  115. return &medical, err
  116. }
  117. func CreateSelfMedical(medical *models.XtSelfMedical) error {
  118. err = XTWriteDB().Model(&medical).Create(&medical).Error
  119. return err
  120. }
  121. func GetCurrentOrgAllStaff(orgid int64, app int64) (role []*models.SgjUserAdminRole, err error) {
  122. err = UserReadDB().Model(&role).Where("org_id = ? and app_id = ? and status = 1", orgid, app).Find(&role).Error
  123. return role, err
  124. }
  125. func CreateStock(stock *models.XtSelfStock) error {
  126. err := XTWriteDB().Model(&stock).Create(&stock).Error
  127. return err
  128. }
  129. func CreateOutStock(stock *models.XtSelfOutStock) error {
  130. err := XTWriteDB().Model(&stock).Create(&stock).Error
  131. return err
  132. }
  133. func GetStandDrugByDrugName(drugname string, orgid int64) (*models.XtStandName, error) {
  134. standname := models.XtStandName{}
  135. err := XTReadDB().Model(&standname).Where("drug_name = ? and user_org_id = ? and status = 1", drugname, orgid).Find(&standname).Error
  136. if err == gorm.ErrRecordNotFound {
  137. return nil, err
  138. }
  139. if err != nil {
  140. return nil, err
  141. }
  142. return &standname, nil
  143. }
  144. func DeleteDrugName(id int64) (models.XtDrugName, error) {
  145. standName := models.XtDrugName{}
  146. err := XTWriteDB().Model(&standName).Where("id = ?", id).Update(map[string]interface{}{"status": 0}).Error
  147. return standName, err
  148. }
  149. func GetDrugSetByUserOrgId(orgid int64) (*models.XtDrugSet, error) {
  150. set := models.XtDrugSet{}
  151. err := XTReadDB().Model(&set).Where("user_org_id = ? and status = 1", orgid).Find(&set).Error
  152. if err == gorm.ErrRecordNotFound {
  153. return nil, err
  154. }
  155. if err != nil {
  156. return nil, err
  157. }
  158. return &set, nil
  159. }
  160. func SaveRadio(drugSet *models.XtDrugSet) error {
  161. err := XTWriteDB().Model(&drugSet).Create(&drugSet).Error
  162. return err
  163. }
  164. func UpdateDrugSet(set *models.XtDrugSet, orgid int64) error {
  165. err := XTWriteDB().Model(&set).Where("user_org_id = ?", orgid).Update(map[string]interface{}{"drug_start": set.DrugStart}).Error
  166. return err
  167. }
  168. func GetMedicalsByName(orgid int64, drugname string) (*models.XtSelfMedical, error) {
  169. medical := models.XtSelfMedical{}
  170. err := XTReadDB().Model(&medical).Where("user_org_id = ? and drug_name = ? and status = 1", orgid, drugname).Find(&medical).Error
  171. if err == gorm.ErrRecordNotFound {
  172. return nil, err
  173. }
  174. if err != nil {
  175. return nil, err
  176. }
  177. return &medical, nil
  178. }
  179. func DeleteDrugStand(id int64) error {
  180. standName := models.XtStandName{}
  181. err := XTWriteDB().Model(&standName).Where("id =?", id).Update(map[string]interface{}{"status": 0}).Error
  182. return err
  183. }
  184. //func GetStockList(patientid int64, startime int64, keyword string) (stock []*models.SelfStocks, err error) {
  185. // db := XTReadDB().Table("xt_self_stock as x").Where("x.status = 1")
  186. // likeKey := "%" + keyword + "%"
  187. // table := XTReadDB().Table("xt_patients as s")
  188. // fmt.Println(table)
  189. // if patientid > 0 {
  190. // db = db.Where("x.patient_id = ?", patientid)
  191. // }
  192. // if startime > 0 {
  193. // db = db.Where("x.storck_time <= ?", startime)
  194. // }
  195. // err = db.Group("x.drug_name,x.drug_spec").Select("x.id,x.drug_name,x.drug_name_id,x.drug_spec,x.remarks,x.store_number,x.admin_user_id,x.storck_time,x.user_org_id,x.stock_in_number,x.patient_id,x.min_unit,sum(x.store_number) as total").Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Scan(&stock).Error
  196. // return stock, err
  197. //}
  198. func GetStockList(patientid int64, startime int64, keyword string, orgid int64) (stock []*models.XtSelfMedicals, err error) {
  199. likeKey := "%" + keyword + "%"
  200. db := XTReadDB().Table("xt_self_medical as x").Where("x.status = 1")
  201. if patientid > 0 {
  202. db = db.Where("x.patient_id = ?", patientid)
  203. }
  204. if orgid > 0 {
  205. db = db.Where("x.user_org_id = ?", orgid)
  206. }
  207. err = db.Group("x.drug_name,x.drug_spec").Select("x.id,x.drug_name,x.drug_name_id,x.drug_spec,x.min_unit,sum(t.store_number) as total,t.remarks,x.patient_id,t.stock_in_number,t.storck_time,t.admin_user_id,t.medic_id").Joins("left join xt_self_stock as t on t.medic_id = x.id").Where("t.storck_time<=?", startime).Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Scan(&stock).Error
  208. return stock, err
  209. }
  210. func GetOutStockList(patientid int64, startime int64, keyword string, orgid int64) (outStock []*models.SelfOutStock, err error) {
  211. likeKey := "%" + keyword + "%"
  212. db := XTReadDB().Table("xt_self_medical as x").Where("x.status = 1")
  213. if patientid > 0 {
  214. db = db.Where("x.patient_id = ?", patientid)
  215. }
  216. if orgid > 0 {
  217. db = db.Where("x.user_org_id = ?", orgid)
  218. }
  219. err = db.Group("x.drug_name,x.drug_spec").Select("x.id,x.drug_name,x.drug_name_id,x.drug_spec,x.min_unit,sum(t.outstore_number) as count,t.remarks,x.patient_id,t.stock_out_number,t.storck_time,t.admin_user_id,t.medic_id").Joins("left join xt_self_out_stock as t on t.medic_id = x.id").Where("t.storck_time<=?", startime).Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Scan(&outStock).Error
  220. return outStock, err
  221. }
  222. func GetTotalOutStockList(patientid int64, startime int64, keyword string, orgid int64) (outStock []*models.SelfOutStock, err error) {
  223. likeKey := "%" + keyword + "%"
  224. db := XTReadDB().Table("xt_self_out_stock as x").Where("x.status = 1")
  225. if patientid > 0 {
  226. db = db.Where("x.patient_id = ?", patientid)
  227. }
  228. if orgid > 0 {
  229. db = db.Where("x.user_org_id = ?", orgid)
  230. }
  231. if startime > 0 {
  232. db = db.Where("x.storck_time<=?", startime)
  233. }
  234. err = db.Select("x.id,x.drug_name,x.drug_name_id,x.drug_spec,x.remarks,x.outstore_number,x.admin_user_id,x.storck_time,x.user_org_id,x.patient_id,x.exit_mode,x.medic_id").Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Find(&outStock).Error
  235. return outStock, err
  236. }
  237. //func GetOutStockList(patientid int64, startime int64, keyword string) (outStock []*models.XtSelfOutStocks, err error) {
  238. // db := XTReadDB().Table("xt_self_out_stock as x").Where("x.status = 1")
  239. // likeKey := "%" + keyword + "%"
  240. // table := XTReadDB().Table("xt_patients as s")
  241. // fmt.Println(table)
  242. // if patientid > 0 {
  243. // db = db.Where("x.patient_id = ?", patientid)
  244. // }
  245. // if startime > 0 {
  246. // db = db.Where("x.storck_time <= ?", startime)
  247. // }
  248. // err = db.Group("x.drug_name,x.drug_spec").Select("x.drug_name,x.drug_spec,x.patient_id,x.stock_out_number,sum(x.outstore_number) as count").Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Scan(&outStock).Error
  249. // return outStock, err
  250. //}
  251. func GetMedicalList(patientid int64, orgid int64, keyword string) (medical []*models.XtSelfMedicals, err error) {
  252. likeKey := "%" + keyword + "%"
  253. db := XTReadDB().Table("xt_self_medical as x").Where("x.status = 1")
  254. if patientid > 0 {
  255. db = db.Where("x.patient_id = ?", patientid)
  256. }
  257. if orgid > 0 {
  258. db = db.Where("x.user_org_id = ?", orgid)
  259. }
  260. err = db.Select("x.drug_name,x.id,x.drug_spec,x.patient_id,x.min_unit").Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Group("x.drug_name,x.drug_spec").Scan(&medical).Error
  261. //err = XTReadDB().Model(&medical).Where("patient_id = ? and user_org_id = ? and status = 1", patientid, orgid).Group("drug_name,drug_spec").Find(&medical).Error
  262. return medical, err
  263. }
  264. func GetStockOutDetail(durgname string, patientid int64) (stock []*models.XtSelfOutStock, err error) {
  265. err = XTReadDB().Model(&stock).Where("drug_name = ? and patient_id = ? and status = 1", durgname, patientid).Find(&stock).Error
  266. return stock, err
  267. }
  268. func DeleteDrugStockNumber(drugname string, patientid int64) (models.XtSelfStock, error) {
  269. stock := models.XtSelfStock{}
  270. err := XTWriteDB().Model(&stock).Where("drug_name = ? and patient_id = ?", drugname, patientid).Update(map[string]interface{}{"status": 0}).Error
  271. return stock, err
  272. }
  273. func GetStockDetail(drugname string, drugspec string, startime int64, endtime int64, patientid int64, orgid int64) (stock []*models.XtSelfStock, err error) {
  274. err = XTReadDB().Model(&stock).Where("drug_name = ? and drug_spec = ? and patient_id =? and user_org_id = ? and status = 1 and storck_time>=? and storck_time<=?", drugname, drugspec, patientid, orgid, startime, endtime).Order("storck_time asc").Find(&stock).Error
  275. return stock, err
  276. }
  277. func GetOutStockDetail(drugname string, drugspec string, startime int64, endtime int64, patientid int64, orgid int64) (outStock []*models.XtSelfOutStock, err error) {
  278. err = XTReadDB().Model(&outStock).Where("drug_name = ? and drug_spec=? and patient_id = ? and user_org_id = ? and status = 1 and storck_time>=? and storck_time<=?", drugname, drugspec, patientid, orgid, startime, endtime).Order("storck_time asc").Find(&outStock).Error
  279. return outStock, err
  280. }
  281. func GetAllPatientStockList(drug_name string, drug_spec string, startime int64, endtime int64, keyword string, orgid int64) (stocks []*models.SelfStocks, err error) {
  282. db := XTReadDB().Table("xt_self_stock as x").Where("x.status = 1")
  283. likeKey := "%" + keyword + "%"
  284. table := XTReadDB().Table("xt_patients as s")
  285. fmt.Println(table)
  286. if len(drug_name) > 0 {
  287. db = db.Where("x.drug_name = ?", drug_name)
  288. }
  289. if len(drug_spec) > 0 {
  290. db = db.Where("x.drug_spec = ?", drug_spec)
  291. }
  292. //if startime > 0 {
  293. // db = db.Where("x.storck_time >= ?", startime)
  294. //}
  295. //if endtime > 0 {
  296. // db = db.Where("x.storck_time <= ?", endtime)
  297. //}
  298. if orgid > 0 {
  299. db = db.Where("x.user_org_id = ?", orgid)
  300. }
  301. err = db.Group("x.patient_id,x.drug_name,x.drug_spec").Select("t.drug_name,t.drug_spec,x.patient_id,x.store_number,sum(x.store_number) as total,s.name,s.dialysis_no,x.min_unit,x.drug_name_id").Joins("left join xt_patients as s on s.id = x.patient_id").Joins("left join xt_self_medical as t on t.id = x.medic_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Scan(&stocks).Error
  302. return stocks, err
  303. }
  304. func GetAllPatientOutStockList(drug_name string, drug_spec string, startime int64, endtime int64, keyword string, orgid int64) (outStock []*models.XtSelfOutStocks, err error) {
  305. db := XTReadDB().Table("xt_self_out_stock as x").Where("x.status = 1")
  306. likeKey := "%" + keyword + "%"
  307. table := XTReadDB().Table("xt_patients as s")
  308. fmt.Println(table)
  309. if len(drug_name) > 0 {
  310. db = db.Where("x.drug_name = ?", drug_name)
  311. }
  312. if len(drug_spec) > 0 {
  313. db = db.Where("x.drug_spec=?", drug_spec)
  314. }
  315. if startime > 0 {
  316. db = db.Where("x.storck_time >= ?", startime)
  317. }
  318. if endtime > 0 {
  319. db = db.Where("x.storck_time<=?", endtime)
  320. }
  321. if orgid > 0 {
  322. db = db.Where("x.user_org_id = ?", orgid)
  323. }
  324. err = db.Group("x.patient_id,x.drug_name,x.drug_spec").Select("x.drug_name,x.drug_spec,x.patient_id,x.stock_out_number,sum(x.outstore_number) as count,s.name,s.dialysis_no,x.drug_name_id").Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Scan(&outStock).Error
  325. return outStock, err
  326. }
  327. func GetDrugDataByPatientId(patientid int64) (selfMedical []*models.XtSelfMedical, err error) {
  328. err = XTReadDB().Group("drug_name,drug_spec").Model(&selfMedical).Where("patient_id = ? and status = 1", patientid).Find(&selfMedical).Error
  329. return selfMedical, err
  330. }
  331. func GetDruckStockConfig(orgid int64) (models.XtDrugStockConfig, error) {
  332. drugConfig := models.XtDrugStockConfig{}
  333. err := XTReadDB().Model(&drugConfig).Where("org_id = ? and status = 1", orgid).Find(&drugConfig).Error
  334. return drugConfig, err
  335. }
  336. func GetSetSelfMedical(orgid int64) (models.XtDrugSet, error) {
  337. medical := models.XtDrugSet{}
  338. err := XTReadDB().Model(&medical).Where("user_org_id = ? and status = 1", orgid).Find(&medical).Error
  339. return medical, err
  340. }
  341. func GetSelfMedicalList(patientid int64) (selfmedical []*models.XtSelfMedical, err error) {
  342. err = XTReadDB().Group("drug_name").Model(&selfmedical).Where("patient_id = ? and status = 1", patientid).Find(&selfmedical).Error
  343. return selfmedical, err
  344. }
  345. func GetDrugDescByDrugName(drugname string, patient_id int64, orgid int64) (selfMedical []*models.XtSelfMedical, err error) {
  346. err = XTReadDB().Model(&selfMedical).Where("drug_name = ? and patient_id = ? and user_org_id = ? and status = 1", drugname, patient_id, orgid).Find(&selfMedical).Error
  347. return selfMedical, err
  348. }
  349. func GetBaseMedcal(drugname string, orgid int64) (baseDrug []*models.XtBaseDrug, err error) {
  350. err = XTReadDB().Model(&baseDrug).Where("drug_name = ? and org_id = ? and status = 1", drugname, orgid).Find(&baseDrug).Error
  351. return baseDrug, err
  352. }
  353. func GetTotalBaseMedicalCount(drug_id int64, orgid int64) (drugInfo []*models.DrugWarehousingInfo, err error) {
  354. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  355. err = db.Select("x.drug_id,sum(x.warehousing_count) as count").Where("drug_id = ? and org_id = ?", drug_id, orgid).Scan(&drugInfo).Error
  356. return drugInfo, err
  357. }
  358. func GetTotalBaseMedicalCountOut(drug_id int64, orgid int64) (drugOut []*models.DrugWarehousingInfo, err error) {
  359. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  360. err = db.Select("x.drug_id,sum(x.count) as count").Where("drug_id = ? and org_id = ?", drug_id, orgid).Scan(&drugOut).Error
  361. return drugOut, err
  362. }
  363. func GetDrugWareseOut(startime int64, orgid int64) (*models.XtDrugWarehouse, error) {
  364. warehouse := models.XtDrugWarehouse{}
  365. err := XTReadDB().Model(&warehouse).Where("warehousing_time = ? and org_id = ? and status = 1", startime, orgid).Find(&warehouse).Error
  366. if err == gorm.ErrRecordNotFound {
  367. return nil, err
  368. }
  369. if err != nil {
  370. return nil, err
  371. }
  372. return &warehouse, nil
  373. }
  374. func GetManufacturer(id int64) (models.XtBaseDrug, error) {
  375. drug := models.XtBaseDrug{}
  376. err := XTReadDB().Model(&drug).Where("id = ? and status = 1", id).Find(&drug).Error
  377. return drug, err
  378. }
  379. func CreateDrugWarehoue(warehouse *models.XtDrugWarehouse) error {
  380. err := XTWriteDB().Model(&warehouse).Create(&warehouse).Error
  381. return err
  382. }
  383. func GetTodayWareHousringOrder(startime int64, orgid int64) (models.XtDrugWarehouseOut, error) {
  384. warehouse := models.XtDrugWarehouseOut{}
  385. err := XTReadDB().Model(&warehouse).Where("warehouse_out_time = ? and org_id = ? and status = 1", startime, orgid).Find(&warehouse).Error
  386. return warehouse, err
  387. }
  388. func CreateDrugWareHouseOut(out *models.XtDrugWarehouseOut) error {
  389. err := XTWriteDB().Model(&out).Create(&out).Error
  390. return err
  391. }
  392. func CreateWareHouseOutInfo(info *models.XtDrugWarehouseOutInfo) error {
  393. err := XTWriteDB().Model(&info).Create(&info).Error
  394. return err
  395. }
  396. func CreateAotoMaticReduceDetail(detail *models.XtDrugAutomaticReduceDetail) error {
  397. err := XTWriteDB().Model(&detail).Create(&detail).Error
  398. return err
  399. }
  400. func GetDrugStockConfig(orgid int64) (models.XtDrugStockConfig, error) {
  401. config := models.XtDrugStockConfig{}
  402. err := XTReadDB().Model(&config).Where("org_id =? and status = 1", orgid).Find(&config).Error
  403. return config, err
  404. }
  405. func GetDrugSet(orgid int64) (models.XtDrugSet, error) {
  406. drugSet := models.XtDrugSet{}
  407. err := XTReadDB().Model(&drugSet).Where("user_org_id =? and status = 1", orgid).Find(&drugSet).Error
  408. return drugSet, err
  409. }
  410. type Drugs struct {
  411. ID int64 `gorm:"column:id" json:"id" form:"id"`
  412. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  413. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  414. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  415. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  416. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  417. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  418. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  419. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  420. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  421. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  422. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  423. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  424. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  425. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  426. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  427. Status int64 `gorm:"column:status" json:"status" form:"status"`
  428. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  429. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  430. Drugs []*Drugs `gorm:"ForeignKey:DrugName;AssociationForeignKey:DrugName" json:"drug_specs"`
  431. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  432. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  433. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  434. }
  435. func (Drugs) TableName() string {
  436. return "xt_base_drug"
  437. }
  438. func GetAllBaseDrugLibList(org_id int64) (drugs []*Drugs, err error) {
  439. db := readDb.Model(&models.BaseDrugLib{}).Preload("Drugs", "status = 1 AND org_id = ? AND find_in_set('停用',drug_status) = 0", org_id).Where("org_id = ? AND status = 1 AND find_in_set('停用',drug_status) = 0", org_id).Group("drug_name")
  440. err = db.Find(&drugs).Error
  441. return
  442. }
  443. type PrivateDrug struct {
  444. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  445. ID int64 `gorm:"column:id" json:"id" form:"id"`
  446. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  447. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  448. DrugNameId int64 `gorm:"column:drug_name_id" json:"drug_name_id" form:"drug_name_id"`
  449. Status int64 `gorm:"column:status" json:"status" form:"status"`
  450. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  451. CreatedTime int64 `gorm:"column:created_time" json:"created_time" form:"created_time"`
  452. UpdatedTime int64 `gorm:"column:updated_time" json:"updated_time" form:"updated_time"`
  453. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  454. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  455. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  456. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  457. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  458. PrivateDrug []*PrivateDrug `gorm:"ForeignKey:DrugName;AssociationForeignKey:DrugName" json:"drug_specs"`
  459. }
  460. func (PrivateDrug) TableName() string {
  461. return "xt_self_medical"
  462. }
  463. func GetPrivateDrugList(patient_id int64, org_id int64) (pds []*PrivateDrug, err error) {
  464. err = XTReadDB().Model(&PrivateDrug{}).Preload("PrivateDrug", "status = 1 AND user_org_id = ? AND patient_id = ? ", org_id, patient_id).Where("patient_id = ? and status = 1 AND user_org_id = ?", patient_id, org_id).Group("drug_name_id").Find(&pds).Error
  465. return
  466. }
  467. func GetAllMedicalList(orgid int64) (list []*models.XtSelfMedical, err error) {
  468. err = XTReadDB().Model(&list).Where("user_org_id = ? and status = 1", orgid).Group("drug_name").Find(&list).Error
  469. return list, err
  470. }
  471. func DeleteDrugNameById(id int64) (models.XtSelfMedical, error) {
  472. medical := models.XtSelfMedical{}
  473. err := XTWriteDB().Model(&medical).Where("id = ?", id).Updates(map[string]interface{}{"status": 0}).Error
  474. return medical, err
  475. }
  476. func GetDrugNameIsExist(drugid int64, orgid int64) (*models.XtDrugName, error) {
  477. drugName := models.XtDrugName{}
  478. err := XTReadDB().Model(&drugName).Where("drug_id = ? and user_org_id = ? and status = 1", drugid, orgid).Find(&drugName).Error
  479. if err == gorm.ErrRecordNotFound {
  480. return nil, err
  481. }
  482. if err != nil {
  483. return nil, err
  484. }
  485. return &drugName, nil
  486. }
  487. func UpdatedDrugName(id int64, orgid int64, drugName *models.XtDrugName) error {
  488. err := XTWriteDB().Model(&drugName).Where("drug_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"drug_name": drugName.DrugName}).Error
  489. return err
  490. }
  491. func GetStandNameIsExist(id int64, orgid int64) (*models.XtStandName, error) {
  492. standName := models.XtStandName{}
  493. err := XTReadDB().Model(&standName).Where("drug_name_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&standName).Error
  494. if err == gorm.ErrRecordNotFound {
  495. return nil, err
  496. }
  497. if err != nil {
  498. return nil, err
  499. }
  500. return &standName, nil
  501. }
  502. func UpdateStandName(id int64, orgid int64, standName *models.XtStandName) error {
  503. err := XTWriteDB().Model(&standName).Where("drug_name_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"drug_name": standName.DrugName, "drug_spec": standName.DrugSpec, "drug_stock_limit": standName.DrugStockLimit, "min_unit": standName.MinUnit}).Error
  504. return err
  505. }
  506. func GetSelfMedicalIsExist(id int64, orgid int64) (*models.XtSelfMedical, error) {
  507. medical := models.XtSelfMedical{}
  508. err := XTReadDB().Model(&medical).Where("drug_name_id = ? and user_org_id = ? and status =1", id, orgid).Find(&medical).Error
  509. if err == gorm.ErrRecordNotFound {
  510. return nil, err
  511. }
  512. if err != nil {
  513. return nil, err
  514. }
  515. return &medical, nil
  516. }
  517. func UpdateSelfMedical(id int64, orgid int64, medical *models.XtSelfMedical) error {
  518. err := XTWriteDB().Model(&medical).Where("drug_name_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"drug_name": medical.DrugName, "drug_spec": medical.DrugSpec, "execution_frequency": medical.ExecutionFrequency, "prescribing_number": medical.PrescribingNumber, "delivery_way": medical.DeliveryWay, "single_dose": medical.SingleDose, "min_unit": medical.MinUnit}).Error
  519. return err
  520. }
  521. func GetDrugNameId(id int64, orgid int64) (models.XtSelfMedical, error) {
  522. medical := models.XtSelfMedical{}
  523. err := XTReadDB().Model(&medical).Where("drug_name_id = ? and status = 1 and user_org_id = ?", id, orgid).Find(&medical).Error
  524. return medical, err
  525. }
  526. func GetSelfStosckIsExist(id int64, orgid int64) (*models.XtSelfStock, error) {
  527. medical := models.XtSelfStock{}
  528. err := XTReadDB().Model(&medical).Where("drug_name_id = ? and user_org_id = ? and status =1", id, orgid).Find(&medical).Error
  529. if err == gorm.ErrRecordNotFound {
  530. return nil, err
  531. }
  532. if err != nil {
  533. return nil, err
  534. }
  535. return &medical, nil
  536. }
  537. func UpdatedSelfStock(stock *models.XtSelfStock, id int64, orgid int64) error {
  538. err := XTWriteDB().Model(&stock).Where("drug_name_id=? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"drug_name": stock.DrugName, "min_unit": stock.MinUnit, "drug_spec": stock.DrugSpec}).Error
  539. return err
  540. }
  541. func GetOutSelfOutStockIsExist(id int64, orgid int64) (*models.XtSelfOutStock, error) {
  542. outStock := models.XtSelfOutStock{}
  543. err := XTReadDB().Model(&outStock).Where("drug_name_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&outStock).Error
  544. if err == gorm.ErrRecordNotFound {
  545. return nil, err
  546. }
  547. if err != nil {
  548. return nil, err
  549. }
  550. return &outStock, nil
  551. }
  552. func UpdatedOutSelfStock(stock *models.XtSelfOutStock, id int64, orgid int64) error {
  553. err := XTWriteDB().Model(&stock).Where("drug_name_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"drug_name": stock.DrugName, "drug_spec": stock.DrugSpec}).Error
  554. return err
  555. }
  556. func GetDoctorAdviceIsExist(id int64, orgid int64) (*models.DoctorAdvice, error) {
  557. advice := models.DoctorAdvice{}
  558. err := XTReadDB().Model(&advice).Where("drug_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&advice).Error
  559. if err == gorm.ErrRecordNotFound {
  560. return nil, err
  561. }
  562. if err != nil {
  563. return nil, err
  564. }
  565. return &advice, nil
  566. }
  567. func UpdateNoExcuteDoctorAdvice(id int64, orgid int64, advice *models.DoctorAdvice) error {
  568. err := XTWriteDB().Model(&advice).Where("drug_id = ? and user_org_id = ? and status = 1 and execution_state = 2", id, orgid).Updates(map[string]interface{}{"advice_name": advice.AdviceName, "advice_desc": advice.AdviceDesc, "single_dose": advice.SingleDose, "single_dose_unit": advice.SingleDoseUnit, "delivery_way": advice.DeliveryWay, "execution_frequency": advice.ExecutionFrequency}).Error
  569. return err
  570. }
  571. func GetDoctorAdviceTemplateIsExist(id int64, orgid int64) (*models.DoctorAdviceTemplate, error) {
  572. template := models.DoctorAdviceTemplate{}
  573. err := XTReadDB().Model(&template).Where("drug_id = ? and org_id = ? and status = 1", id, orgid).Find(&template).Error
  574. if err == gorm.ErrRecordNotFound {
  575. return nil, err
  576. }
  577. if err != nil {
  578. return nil, err
  579. }
  580. return &template, nil
  581. }
  582. func UpdatedDoctorAdviceTemplate(id int64, orgid int64, template *models.DoctorAdviceTemplate) error {
  583. err := XTWriteDB().Model(&template).Where("drug_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"advice_name": template.AdviceName, "advice_desc": template.AdviceDesc, "single_dose": template.SingleDose, "single_dose_unit": template.SingleDoseUnit, "delivery_way": template.DeliveryWay, "execution_frequency": template.ExecutionFrequency}).Error
  584. return err
  585. }
  586. func ToSeachPatient(keyword string, orgid int64) (patients []*models.Patients, err error) {
  587. likeKey := "%" + keyword + "%"
  588. err = XTReadDB().Model(&patients).Where("name like ? and user_org_id = ? and status = 1", likeKey, orgid).Find(&patients).Error
  589. return patients, err
  590. }
  591. func GetMedicalDrugNameList(orgid int64) (drugName []*models.DrugName, err error) {
  592. db := XTReadDB().Model(&drugName).Where("user_org_id = ? and status = 1", orgid)
  593. err = db.Preload("XtStandName", "status =1 and user_org_id = ?", orgid).Find(&drugName).Error
  594. return drugName, err
  595. }
  596. func GetSelfOutStock(orgid int64, advicedate int64, patientid int64, adivicename string, advicedesc string) (*models.XtSelfOutStock, error) {
  597. stock := models.XtSelfOutStock{}
  598. err = XTReadDB().Model(&stock).Where("user_org_id = ? and storck_time = ? and patient_id = ? and drug_name = ? and drug_spec = ? and status = 1", orgid, advicedate, patientid, adivicename, advicedesc).Find(&stock).Error
  599. if err == gorm.ErrRecordNotFound {
  600. return nil, err
  601. }
  602. if err != nil {
  603. return nil, err
  604. }
  605. return &stock, err
  606. }
  607. func UpdatedSelfOutSatock(id int64, stock *models.XtSelfOutStock) error {
  608. err := XTWriteDB().Model(&stock).Where("id = ?", id).Updates(map[string]interface{}{"drug_name": stock.DrugName, "drug_name_id": stock.DrugNameId, "drug_spec": stock.DrugSpec, "outstore_number": stock.OutstoreNumber, "admin_user_id": stock.AdminUserId, "storck_time": stock.StorckTime, "patient_id": stock.PatientId, "stock_out_number": stock.StockOutNumber, "exit_mode": stock.ExitMode, "medic_id": stock.MedicId, "updated_time": time.Now().Unix()}).Error
  609. return err
  610. }
  611. func GetSelfStockQuery(typename int64, keywords string, startime int64, endtime int64, orgid int64, limit int64, page int64) (selfoutstock []*models.XtSelfOutStockOne, total int64, err error) {
  612. offset := (page - 1) * limit
  613. db := XTReadDB().Table("xt_self_stock as x").Where("x.status = 1")
  614. likeKey := "%" + keywords + "%"
  615. if len(keywords) > 0 {
  616. db = db.Where("x.drug_name like ? ", likeKey)
  617. }
  618. if startime > 0 {
  619. db = db.Where("x.storck_time >=?", startime)
  620. }
  621. if endtime > 0 {
  622. db = db.Where("x.storck_time<=?", endtime)
  623. }
  624. if orgid > 0 {
  625. db = db.Where("x.user_org_id = ?", orgid)
  626. }
  627. if typename > 0 {
  628. err = db.Select("x.drug_name,x.drug_name_id,x.drug_spec,Sum(store_number) as count,b.drug_type").Joins("left join xt_base_drug as b on b.id = x.drug_name_id and b.org_id = ? and b.status =1 ", orgid).Where("b.drug_type = ?", typename).Group("x.drug_name_id").Count(&total).Offset(offset).Limit(limit).Scan(&selfoutstock).Error
  629. } else {
  630. err = db.Select("x.drug_name,x.drug_name_id,x.drug_spec,Sum(store_number) as count,b.drug_type").Joins("left join xt_base_drug as b on b.id = x.drug_name_id and b.org_id = ? and b.status =1", orgid).Group("x.drug_name_id").Count(&total).Offset(offset).Limit(limit).Scan(&selfoutstock).Error
  631. }
  632. return selfoutstock, total, err
  633. }
  634. func GetSelfOutStockQuery(startTime int64, endTime int64, orgid int64) (selfoutstock []*models.XtSelfOutStockOne, err error) {
  635. db := XTReadDB().Table("xt_self_out_stock as x").Where("x.status = 1")
  636. if startTime > 0 {
  637. db = db.Where("x.storck_time >?", startTime)
  638. }
  639. if endTime > 0 {
  640. db = db.Where("x.storck_time<=?", endTime)
  641. }
  642. if orgid > 0 {
  643. db = db.Where("x.user_org_id = ?", orgid)
  644. }
  645. err = db.Select("x.drug_name_id,Sum(x.outstore_number) as count").Group("x.drug_name").Scan(&selfoutstock).Error
  646. return selfoutstock, err
  647. }
  648. func GetLastDrugWarehouseOutInfo(drugid int64, warehouseOutId int64, orgid int64) (models.XtDrugWarehouseOutInfo, error) {
  649. info := models.XtDrugWarehouseOutInfo{}
  650. err = XTReadDB().Model(&info).Where("drug_id = ? and warehouse_out_id = ? and org_id = ? and status = 1", drugid, warehouseOutId, orgid).Last(&info).Error
  651. return info, err
  652. }
  653. func PostSearchDrugWarehouseList(keyword string, orgid int64) (info []*models.BloodDrugWarehouseInfo, err error) {
  654. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  655. table := XTReadDB().Table("xt_base_drug as t").Where("t.status")
  656. fmt.Println(table)
  657. likeKey := "%" + keyword + "%"
  658. if orgid > 0 {
  659. db = db.Where("x.org_id = ?", orgid)
  660. }
  661. err = db.Group("x.drug_id").Select("x.id,x.warehousing_id,x.drug_id,t.number,x.product_date,x.expiry_date,x.warehousing_count,x.price,x.total_price,x.dealer,x.remark,x.warehousing_order,x.warehouseing_unit,x.stock_max_number,x.stock_min_number,x.batch_number,t.drug_origin_place,t.drug_name,t.min_unit,x.manufacturer,t.retail_price,t.max_unit,t.min_number,t.dose,t.dose_unit,t.last_price,t.drug_origin_place").Joins("left join xt_base_drug as t on t.id = x.drug_id").Where("t.drug_name like ? and t.org_id = ?", likeKey, orgid).Order("x.ctime desc").Scan(&info).Error
  662. return info, err
  663. }
  664. func CreateDrugPrice(price models.XtDrugAdjustPrice) error {
  665. err := XTWriteDB().Create(&price).Error
  666. return err
  667. }
  668. func GetDrugPriceList(startime int64, endtime int64, orgId int64, keyword string, limit int64, page int64) (adjust []*models.VmDrugAdjustPrice, total int64, err error) {
  669. //likeKey := "%" + keyword + "%"
  670. //offset := (page - 1) * limit
  671. //db := XTReadDB().Model(&adjust).Where("status = 1")
  672. //if startime > 0 {
  673. // db = db.Where("ctime >=?", startime)
  674. //}
  675. //if endtime > 0 {
  676. // db = db.Where("ctime<=?", endtime)
  677. //}
  678. //if orgId > 0 {
  679. // db = db.Where("user_org_id = ?", orgId)
  680. //}
  681. //if len(keyword) > 0 {
  682. // db = db.Where("warehousing_order like ?", likeKey)
  683. //}
  684. //err = db.Count(&total).Offset(offset).Limit(limit).Find(&adjust).Error
  685. //return adjust, total, err
  686. likeKey := "%" + keyword + "%"
  687. offset := (page - 1) * limit
  688. db := XTReadDB().Table("xt_drug_adjust_price as x").Where("x.status = 1")
  689. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  690. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  691. fmt.Println(table, tab)
  692. if len(keyword) > 0 {
  693. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  694. }
  695. if orgId > 0 {
  696. db = db.Where("x.user_org_id = ?", orgId)
  697. }
  698. err = db.Group("x.id").Select("x.id,x.drug_name,x.specification_name,x.warehousing_unit,x.count,x.last_price,x.retail_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.drug_id,x.warehousing_order,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.number").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_base_drug as t on t.id =x.drug_id").Count(&total).Offset(offset).Limit(limit).Scan(&adjust).Error
  699. return adjust, total, err
  700. }
  701. func UpdateDrugAdjuestPrice(ids []string, info models.XtDrugAdjustPrice) error {
  702. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  703. return err
  704. }
  705. func GetDrugAdjuestPrice(ids []string) (info []*models.XtDrugAdjustPrice, err error) {
  706. err = XTReadDB().Model(&info).Where("id in(?) and status = 1", ids).Find(&info).Error
  707. return info, err
  708. }
  709. func GetDrugDamage(ids []string) (info []*models.XtDrugDamage, err error) {
  710. err = XTReadDB().Model(&info).Where("id in(?) and status = 1", ids).Find(&info).Error
  711. return info, err
  712. }
  713. func CreateDrugDamage(damage models.XtDrugDamage) error {
  714. err := XTWriteDB().Create(&damage).Error
  715. return err
  716. }
  717. func GetDrugDamageList(startime int64, endtime int64, orgId int64, keyword string, limit int64, page int64) (adjust []*models.XtDrugDamage, total int64, err error) {
  718. likeKey := "%" + keyword + "%"
  719. offset := (page - 1) * limit
  720. db := XTReadDB().Table("xt_drug_damage as x").Where("x.status = 1")
  721. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  722. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  723. fmt.Println(table, tab)
  724. if len(keyword) > 0 {
  725. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  726. }
  727. if orgId > 0 {
  728. db = db.Where("x.user_org_id = ?", orgId)
  729. }
  730. if startime > 0 {
  731. db = db.Where("x.ctime >=?", startime)
  732. }
  733. if endtime > 0 {
  734. db = db.Where("x.ctime <=?", endtime)
  735. }
  736. err = db.Group("x.id").Select("x.id,x.drug_name,x.specification_name,x.warehousing_unit,x.count,x.last_price,x.retail_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.drug_id,x.warehousing_order,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.number,x.drug_origin_place,x.warehousing_info_id,x.batch_number,x.stock_max_number,x.stock_min_number").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_base_drug as t on t.id =x.drug_id").Count(&total).Offset(offset).Limit(limit).Scan(&adjust).Error
  737. return adjust, total, err
  738. }
  739. func UpdateDrugDamage(ids []string, damage models.XtDrugDamage) error {
  740. err := XTWriteDB().Model(&damage).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": damage.Checker, "checker_status": damage.CheckerStatus, "checker_time": damage.CheckerTime}).Error
  741. return err
  742. }
  743. func GetDrugModifyPrice(id int64) (models.XtDrugAdjustPrice, error) {
  744. adjuest := models.XtDrugAdjustPrice{}
  745. err := XTReadDB().Model(&adjuest).Where("id=? and status = 1", id).Find(&adjuest).Error
  746. return adjuest, err
  747. }
  748. func ModifyDrugPrice(id int64, adjust models.XtDrugAdjustPrice) error {
  749. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"drug_name": adjust.DrugName, "retail_price": adjust.RetailPrice, "new_price": adjust.NewPrice, "count": adjust.Count, "remark": adjust.Remark, "specification_name": adjust.SpecificationName, "warehousing_unit": adjust.WarehousingUnit, "manufacturer": adjust.Manufacturer, "number": adjust.Number, "drug_id": adjust.DrugId}).Error
  750. return err
  751. }
  752. func GetModifyPricePrint(ids []string) (info []*models.XtDrugAdjustPrice, err error) {
  753. err = XTReadDB().Model(&info).Where("id in(?) and status = 1", ids).Find(&info).Error
  754. return info, err
  755. }
  756. func GetDrugDamageDetail(id int64) (models.XtDrugDamage, error) {
  757. damage := models.XtDrugDamage{}
  758. err := XTReadDB().Model(&damage).Where("id=? and status = 1", id).Find(&damage).Error
  759. return damage, err
  760. }
  761. func ModifyDrugDamage(id int64, damage models.XtDrugDamage) error {
  762. err := XTWriteDB().Model(&damage).Where("id=? and status = 1", id).Updates(map[string]interface{}{"drug_name": damage.DrugName, "warehousing_unit": damage.WarehousingUnit, "count": damage.Count, "last_price": damage.LastPrice, "retail_price": damage.RetailPrice, "new_price": damage.NewPrice, "manufacturer": damage.Manufacturer, "remark": damage.Remark, "number": damage.Number, "drug_id": damage.DrugId, "stock_max_number": damage.StockMaxNumber, "stock_min_number": damage.StockMinNumber, "warehousing_info_id": damage.WarehousingInfoId, "drug_origin_place": damage.DrugOriginPlace}).Error
  763. return err
  764. }
  765. func DeleteDrugDamage(id int64) error {
  766. damage := models.XtDrugDamage{}
  767. err := XTWriteDB().Model(&damage).Where("id=? and status = 1").Updates(map[string]interface{}{"status": 0}).Error
  768. return err
  769. }
  770. func GetDrugDamagePrint(ids []string) (damage []*models.XtDrugDamage, err error) {
  771. err = XTReadDB().Model(&damage).Where("id in(?) and status = 1", ids).Find(&damage).Error
  772. return damage, err
  773. }
  774. func GetWarehoseInfoById(id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  775. err = XTReadDB().Model(&info).Where("drug_id=? and status = 1", id).Find(&info).Error
  776. return info, err
  777. }
  778. func CreateDrugInventory(inventory models.XtDrugInventory) error {
  779. err := XTWriteDB().Create(&inventory).Error
  780. return err
  781. }
  782. func GetDrugInventoryList(keyword string, page int64, limit int64, orgid int64, startime int64, endtime int64) (list []*models.VmDrugInventory, total int64, err error) {
  783. fmt.Println("startdate", limit, page)
  784. likeKey := "%" + keyword + "%"
  785. offset := (page - 1) * limit
  786. db := XTReadDB().Table("xt_drug_inventory as x").Where("x.status = 1")
  787. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  788. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  789. fmt.Println(table, tab)
  790. if len(keyword) > 0 {
  791. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  792. }
  793. if orgid > 0 {
  794. db = db.Where("x.user_org_id = ?", orgid)
  795. }
  796. if startime > 0 {
  797. db = db.Where("x.start_time >=?", startime)
  798. }
  799. if endtime > 0 {
  800. db = db.Where("x.start_time<=?", endtime)
  801. }
  802. err = db.Select("x.id,x.drug_name,x.specification_name,x.warehousing_unit,x.count,x.last_price,x.retail_price,x.manufacturer,x.dealer,x.remark,x.drug_id,x.warehousing_order,x.number,x.batch_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,t.dose,t.dose_unit,t.max_unit,t.min_unit").Joins("left join sgj_users.sgj_user_admin_role as r on r.id = x.creater").Joins("left join xt_base_drug as t on t.id =x.drug_id").Count(&total).Offset(offset).Limit(limit).Scan(&list).Error
  803. return list, total, err
  804. }
  805. func UpdateDrugInventory(ids []string, inventory models.XtDrugInventory) error {
  806. err := XTWriteDB().Model(&inventory).Where("id in(?) and status = 1", ids).Updates(map[string]interface{}{"checker": inventory.Checker, "checker_status": inventory.CheckerStatus, "checker_time": inventory.CheckerTime}).Error
  807. return err
  808. }
  809. func GetDrugInventoryDetail(id int64) (models.XtDrugInventory, error) {
  810. inventory := models.XtDrugInventory{}
  811. err := XTReadDB().Model(&inventory).Where("id = ? and status = 1", id).Find(&inventory).Error
  812. return inventory, err
  813. }
  814. func ModifyDrugInventory(id int64, info models.XtDrugInventory) error {
  815. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"drug_name": info.DrugName, "specification_name": info.SpecificationName, "warehousing_unit": info.WarehousingUnit, "count": info.Count, "last_price": info.LastPrice, "manufacturer": info.Manufacturer, "remark": info.Remark, "drug_id": info.DrugId, "warehousing_order": info.WarehousingOrder, "number": info.Number, "total": info.Number, "drug_origin_place": info.DrugOriginPlace}).Error
  816. return err
  817. }
  818. func DeleteDrugInventory(id int64) error {
  819. inventory := models.XtDrugInventory{}
  820. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  821. return err
  822. }
  823. func GetDrugInventoryPrintList(ids []string) (info []*models.XtDrugInventory, err error) {
  824. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&info).Error
  825. return info, err
  826. }
  827. func GetDrugInventoryDetailList(keyword string, page int64, limit int64, orgid int64) (list []*models.VmDrugInventory, total int64, err error) {
  828. fmt.Println("startdate", limit, page)
  829. likeKey := "%" + keyword + "%"
  830. offset := (page - 1) * limit
  831. db := XTReadDB().Table("xt_drug_inventory as x").Where("x.status = 1")
  832. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  833. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  834. fmt.Println(table, tab)
  835. if len(keyword) > 0 {
  836. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  837. }
  838. if orgid > 0 {
  839. db = db.Where("x.user_org_id = ?", orgid)
  840. }
  841. err = db.Select("x.id,x.drug_name,x.specification_name,x.warehousing_unit,x.count,x.last_price,x.retail_price,x.manufacturer,x.dealer,x.remark,x.drug_id,x.warehousing_order,x.number,x.batch_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.drug_origin_place,t.dose,t.dose_unit,t.max_unit,t.min_unit").Joins("left join sgj_users.sgj_user_admin_role as r on r.id = x.creater").Joins("left join xt_base_drug as t on t.id =x.drug_id").Count(&total).Offset(offset).Limit(limit).Scan(&list).Error
  842. return list, total, err
  843. }
  844. func UpdateDrugProofInventory(id int64, inventory *models.XtDrugInventory) error {
  845. err := XTWriteDB().Model(&inventory).Where("id=? and status = 1", id).Updates(map[string]interface{}{"warehouse_info_id": inventory.WarehouseInfoId, "proof_count": inventory.ProofCount, "warehousing_unit": inventory.WarehousingUnit, "remark": inventory.Remark}).Error
  846. return err
  847. }
  848. func UpdateDrugWarehouseInfoById(id int64, inventory *models.XtDrugWarehouseInfo) error {
  849. err := XTWriteDB().Model(&inventory).Where("id = ?", id).Updates(map[string]interface{}{"stock_max_number": inventory.StockMaxNumber}).Error
  850. return err
  851. }
  852. func GetDrugWarehouseInfoTotal(id int64) (models.XtDrugWarehouseInfo, error) {
  853. info := models.XtDrugWarehouseInfo{}
  854. err := XTReadDB().Where("id = ? and status = 1", id).Preload("XtBaseDrug", "status = 1").Find(&info).Error
  855. return info, err
  856. }