Browse Source

Merge branch '20201014_xt_api_new_branch' of http://git.shengws.com/csx/XT_New into 20201014_xt_api_new_branch

csx 4 years ago
parent
commit
9130c5da8d

+ 360 - 149
controllers/patient_api_controller.go View File

@@ -1573,171 +1573,382 @@ func (c *PatientApiController) ExecDoctorAdvice() {
1573 1573
 	advice.ExecutionTime = theTime.Unix()
1574 1574
 	// advice.Checker = checker
1575 1575
 
1576
-	adviceName, _ := service.FindDoctorAdvice(adminUserInfo.CurrentOrgId, id)
1577
-	fmt.Println("adviceName", adviceName.Way)
1578
-	timeStr := time.Now().Format("2006-01-02")
1579
-	fmt.Println("timestr", timeStr)
1580
-
1581
-	//如果医嘱已执行,生成自备药出库单
1582
-	//来自药品库
1583
-	if adviceName.ExecutionState == 1 && adviceName.Way == 1 {
1584
-
1585
-		//查询是否开启药品库
1586
-		stockConfig, _ := service.GetDrugStockConfig(adminUserInfo.CurrentOrgId)
1587
-		//开启药品库
1588
-		if stockConfig.IsOpen == 1 {
1589
-			timeLayout := "2006-01-02"
1590
-			loc, _ := time.LoadLocation("Local")
1591
-			//产询今日是否存在出库单
1592
-			timeStr := time.Now().Format("2006-01-02")
1593
-			theTime, _ := time.ParseInLocation(timeLayout+" 15:04:05", timeStr+" 00:00:00", loc)
1594
-			_, errcode := service.GetDrugWareseOut(theTime.Unix(), adminUserInfo.CurrentOrgId)
1595
-			if errcode == gorm.ErrRecordNotFound {
1576
+	//adviceName, _ := service.FindDoctorAdvice(adminUserInfo.CurrentOrgId, id)
1577
+	//fmt.Println("adviceName", adviceName.Way)
1578
+
1579
+	//处理出库相关逻辑
1580
+
1581
+	//1.判断是否启用药品管理和自备药出库功能
1582
+
1583
+	//药品管理信息
1584
+	_, drugStockConfig := service.FindDrugStockAutomaticReduceRecordByOrgId(adminUserInfo.CurrentOrgId)
1585
+
1586
+	//自备药信息
1587
+	privateDrugConfig, _ := service.GetDrugSetByUserOrgId(adminUserInfo.CurrentOrgId)
1588
+	fmt.Println("drugStockConfig9999999999999", drugStockConfig.IsOpen)
1589
+	if drugStockConfig.IsOpen == 1 {
1590
+		adviceName, _ := service.FindDoctorAdvice(adminUserInfo.CurrentOrgId, id)
1591
+		//isHasWay := false //用来判断是否包含来自药品库的医嘱
1592
+
1593
+		if adviceName.Way == 1 {
1594
+			fmt.Println("药品库进来没有--------------------")
1595
+			out, err := service.FindDrugStockOutByIsSys(adminUserInfo.CurrentOrgId, 1, adviceName.RecordDate)
1596
+			fmt.Println("err================", err)
1597
+			if err == gorm.ErrRecordNotFound {
1598
+				//没有记录,则创建出库单
1599
+				timeStr := time.Now().Format("2006-01-02")
1596 1600
 				timeArr := strings.Split(timeStr, "-")
1597 1601
 				total, _ := service.FindAllDrugWarehouseOut(adminUserInfo.CurrentOrgId)
1598
-
1599 1602
 				total = total + 1
1600 1603
 				warehousing_out_order := strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
1601 1604
 				number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
1602 1605
 				number = number + total
1603
-				warehousing_out_order = "YPRKD" + strconv.FormatInt(number, 10)
1604
-				//查询厂家
1605
-				drug, _ := service.GetManufacturer(adviceName.DrugId)
1606
-				//创建
1607
-				warehouse := models.XtDrugWarehouse{
1608
-					WarehousingOrder: warehousing_out_order,
1609
-					OperationTime:    theTime.Unix(),
1610
-					Creater:          adminUserInfo.AdminUser.Id,
1611
-					OrgId:            adminUserInfo.CurrentOrgId,
1612
-					Status:           1,
1613
-					WarehousingTime:  theTime.Unix(),
1614
-					Manufacturer:     drug.Manufacturer,
1615
-					Type:             1,
1606
+				warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
1607
+				creater := adminUserInfo.AdminUser.Id
1608
+				warehouseOut := models.DrugWarehouseOut{
1609
+					WarehouseOutOrderNumber: warehousing_out_order,
1610
+					OperationTime:           time.Now().Unix(),
1611
+					OrgId:                   adminUserInfo.CurrentOrgId,
1612
+					Creater:                 creater,
1613
+					Ctime:                   time.Now().Unix(),
1614
+					Status:                  1,
1615
+					WarehouseOutTime:        adviceName.RecordDate,
1616
+					Dealer:                  0,
1617
+					Manufacturer:            0,
1618
+					Type:                    1,
1619
+					IsSys:                   1,
1620
+				}
1621
+				fmt.Println("触发了梅花园")
1622
+				err := service.AddSigleDrugWarehouseOut(&warehouseOut)
1623
+				if err != nil {
1624
+					utils.TraceLog("创建出库单失败 err = %v", err)
1625
+				} else {
1626
+					if adviceName.Way == 1 {
1627
+						if adviceName.PrescribingNumber > 0 {
1628
+							warehouseOutInfo := &models.DrugWarehouseOutInfo{
1629
+								WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
1630
+								WarehouseOutId:          warehouseOut.ID,
1631
+								Status:                  1,
1632
+								Ctime:                   time.Now().Unix(),
1633
+								Remark:                  "",
1634
+								OrgId:                   adminUserInfo.CurrentOrgId,
1635
+								Type:                    1,
1636
+								Manufacturer:            0,
1637
+								Dealer:                  0,
1638
+								IsSys:                   1,
1639
+								SysRecordTime:           adviceName.RecordDate,
1640
+								DrugId:                  adviceName.DrugId,
1641
+							}
1642
+							prescribingNumber := strconv.FormatFloat(math.Abs(adviceName.PrescribingNumber), 'f', 0, 64)
1643
+							count, _ := strconv.ParseInt(prescribingNumber, 10, 64)
1644
+							warehouseOutInfo.Count = count
1645
+							stockInInfo, _ := service.FindLastDrugStockInInfoRecord(adviceName.DrugId, adminUserInfo.CurrentOrgId)
1646
+							warehouseOutInfo.Price = stockInInfo.Price
1647
+							err := service.AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
1648
+							if err == nil {
1649
+								details := &models.DrugAutomaticReduceDetail{
1650
+									WarehouseOutId:          warehouseOutInfo.ID,
1651
+									WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
1652
+									PatientId:               adviceName.PatientId,
1653
+									Ctime:                   time.Now().Unix(),
1654
+									Mtime:                   time.Now().Unix(),
1655
+									Status:                  1,
1656
+									RecordTime:              adviceName.RecordDate,
1657
+									OrgId:                   adminUserInfo.CurrentOrgId,
1658
+									DrugId:                  adviceName.DrugId,
1659
+									Count:                   count,
1660
+								}
1661
+								service.AddSigleDrugAutoReduceRecordInfo(details)
1662
+							}
1663
+						}
1664
+					}
1665
+				}
1666
+			} else if err == nil {
1667
+
1668
+				if adviceName.Way == 1 {
1669
+					outInfo, err := service.FindDrugStockOutInfoByTypeId(adminUserInfo.CurrentOrgId, adviceName.DrugId, out.ID, out.WarehouseOutOrderNumber)
1670
+					if err == gorm.ErrRecordNotFound {
1671
+						warehouseOutInfo := &models.DrugWarehouseOutInfo{
1672
+							WarehouseOutOrderNumber: out.WarehouseOutOrderNumber,
1673
+							WarehouseOutId:          out.ID,
1674
+							Status:                  1,
1675
+							Ctime:                   time.Now().Unix(),
1676
+							Remark:                  "",
1677
+							OrgId:                   adminUserInfo.CurrentOrgId,
1678
+							Type:                    1,
1679
+							Manufacturer:            0,
1680
+							Dealer:                  0,
1681
+							IsSys:                   1,
1682
+							SysRecordTime:           adviceName.RecordDate,
1683
+						}
1684
+
1685
+						stockInInfo, _ := service.FindLastDrugStockInInfoRecord(adviceName.DrugId, adminUserInfo.CurrentOrgId)
1686
+						warehouseOutInfo.Price = stockInInfo.Price
1687
+						warehouseOutInfo.DrugId = adviceName.DrugId
1688
+						prescribingNumber := strconv.FormatFloat(math.Abs(adviceName.PrescribingNumber), 'f', 0, 64)
1689
+						count, _ := strconv.ParseInt(prescribingNumber, 10, 64)
1690
+						warehouseOutInfo.Count = count
1691
+						err := service.AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
1692
+						if err == nil {
1693
+							details := &models.DrugAutomaticReduceDetail{
1694
+								WarehouseOutId:          warehouseOutInfo.ID,
1695
+								WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
1696
+								PatientId:               adviceName.PatientId,
1697
+								Ctime:                   time.Now().Unix(),
1698
+								Mtime:                   time.Now().Unix(),
1699
+								Status:                  1,
1700
+								RecordTime:              adviceName.RecordDate,
1701
+								OrgId:                   adminUserInfo.CurrentOrgId,
1702
+								DrugId:                  adviceName.DrugId,
1703
+								Count:                   count,
1704
+							}
1705
+							service.AddSigleDrugAutoReduceRecordInfo(details)
1706
+						}
1707
+
1708
+					} else if err == nil { //记录存在,则将增加数量
1709
+						if outInfo.ID > 0 {
1710
+							prescribingNumber := strconv.FormatFloat(math.Abs(adviceName.PrescribingNumber), 'f', 0, 64)
1711
+							count, _ := strconv.ParseInt(prescribingNumber, 10, 64)
1712
+							service.UpdateDrugStockOutInfoCount2(adminUserInfo.CurrentOrgId, outInfo.ID, count)
1713
+						}
1714
+
1715
+						count, _ := service.FindPatientDrugAutomaticReduceRecord(adminUserInfo.CurrentOrgId, adviceName.RecordDate, adviceName.DrugId, adviceName.PatientId)
1716
+						if count == 0 {
1717
+							prescribingNumber := strconv.FormatFloat(math.Abs(adviceName.PrescribingNumber), 'f', 0, 64)
1718
+							count, _ := strconv.ParseInt(prescribingNumber, 10, 64)
1719
+							details := &models.DrugAutomaticReduceDetail{
1720
+								WarehouseOutId:          outInfo.ID,
1721
+								WarehouseOutOrderNumber: outInfo.WarehouseOutOrderNumber,
1722
+								PatientId:               adviceName.PatientId,
1723
+								Ctime:                   time.Now().Unix(),
1724
+								Mtime:                   time.Now().Unix(),
1725
+								Status:                  1,
1726
+								RecordTime:              adviceName.RecordDate,
1727
+								OrgId:                   adminUserInfo.CurrentOrgId,
1728
+								DrugId:                  adviceName.DrugId,
1729
+								Count:                   count,
1730
+							}
1731
+							service.AddSigleDrugAutoReduceRecordInfo(details)
1732
+						} else if count == 1 {
1733
+							prescribingNumber := strconv.FormatFloat(math.Abs(adviceName.PrescribingNumber), 'f', 0, 64)
1734
+							count, _ := strconv.ParseInt(prescribingNumber, 10, 64)
1735
+							service.UpdateDrugUserInfoDetails(adviceName.DrugId, adviceName.RecordDate, adminUserInfo.CurrentOrgId, adviceName.PatientId, count, &outInfo)
1736
+						}
1737
+
1738
+					}
1616 1739
 				}
1617
-				service.CreateDrugWarehoue(&warehouse)
1618
-			}
1619 1740
 
1620
-			timeArr := strings.Split(timeStr, "-")
1621
-			total, _ := service.FindAllDrugWarehouseOut(adminUserInfo.CurrentOrgId)
1622
-
1623
-			total = total + 1
1624
-			warehousing_out_order := strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
1625
-			number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
1626
-			number = number + total
1627
-			warehousing_out_order = "YPRKD" + strconv.FormatInt(number, 10)
1628
-			drug, _ := service.GetManufacturer(adviceName.DrugId)
1629
-			out := models.XtDrugWarehouseOut{
1630
-				WarehouseOutOrderNumber: warehousing_out_order,
1631
-				OperationTime:           theTime.Unix(),
1632
-				Creater:                 adminUserInfo.AdminUser.Id,
1633
-				OrgId:                   adminUserInfo.CurrentOrgId,
1634
-				Ctime:                   time.Now().Unix(),
1635
-				Status:                  1,
1636
-				WarehouseOutTime:        theTime.Unix(),
1637
-				Manufacturer:            drug.Manufacturer,
1638
-				Type:                    1,
1639
-				IsSys:                   1,
1640
-			}
1641
-			service.CreateDrugWareHouseOut(&out)
1642
-
1643
-			//查询今日的出库单号
1644
-			orderOut, _ := service.GetTodayWareHousringOrder(theTime.Unix(), adminUserInfo.CurrentOrgId)
1645
-			prescribingNumber := strconv.FormatFloat(math.Abs(adviceName.PrescribingNumber), 'f', 0, 64)
1646
-			outStoreNumber, _ := strconv.ParseInt(prescribingNumber, 10, 64)
1647
-			timeArrs := strings.Split(timeStr, "-")
1648
-			totals, _ := service.FindAllDrugWarehouseOut(adminUserInfo.CurrentOrgId)
1649
-
1650
-			totals = totals + 1
1651
-			warehousing_out_orders := strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArrs[0] + timeArrs[1] + timeArrs[2] + "000"
1652
-			numbers, _ := strconv.ParseInt(warehousing_out_orders, 10, 64)
1653
-			number = numbers + totals
1654
-			warehousing_out_order = "YPCKD" + strconv.FormatInt(numbers, 10)
1655
-			info := models.XtDrugWarehouseOutInfo{
1656
-				WarehouseOutId:          orderOut.ID,
1657
-				DrugId:                  adviceName.DrugId,
1658
-				Count:                   outStoreNumber,
1659
-				Price:                   drug.RetailPrice,
1660
-				TotalPrice:              drug.RetailPrice,
1661
-				ProductDate:             theTime.Unix(),
1662
-				Ctime:                   time.Now().Unix(),
1663
-				Status:                  1,
1664
-				OrgId:                   adminUserInfo.CurrentOrgId,
1665
-				IsCancel:                2,
1666
-				WarehouseOutOrderNumber: warehousing_out_order,
1667
-				Type:                    1,
1668
-				Manufacturer:            orderOut.Manufacturer,
1669
-				IsSys:                   1,
1670
-				SysRecordTime:           theTime.Unix(),
1671
-			}
1672
-			service.CreateWareHouseOutInfo(&info)
1673
-
1674
-			timeArrss := strings.Split(timeStr, "-")
1675
-			totalss, _ := service.FindAllDrugWarehouseOut(adminUserInfo.CurrentOrgId)
1676
-
1677
-			totalss = totalss + 1
1678
-			warehousing_out_orderss := strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArrss[0] + timeArrss[1] + timeArrss[2] + "000"
1679
-			numberss, _ := strconv.ParseInt(warehousing_out_orderss, 10, 64)
1680
-			number = numberss + totalss
1681
-			warehousing_out_order = "YPCKD" + strconv.FormatInt(numberss, 10)
1682
-			detail := models.XtDrugAutomaticReduceDetail{
1683
-				WarehouseOutOrderNumber: warehousing_out_order,
1684
-				WarehouseOutId:          orderOut.ID,
1685
-				PatientId:               adviceName.PatientId,
1686
-				Ctime:                   time.Now().Unix(),
1687
-				RecordTime:              theTime.Unix(),
1688
-				OrgId:                   adminUserInfo.CurrentOrgId,
1689
-				GoodId:                  adviceName.DrugId,
1690
-				Count:                   outStoreNumber,
1691
-				Status:                  1,
1692 1741
 			}
1693
-			service.CreateAotoMaticReduceDetail(&detail)
1694 1742
 		}
1695 1743
 	}
1696 1744
 
1697
-	if adviceName.ExecutionState == 1 && adviceName.Way == 2 {
1698
-
1699
-		//是否开启自备药
1700
-		medicalSet, _ := service.GetSetSelfMedical(adminUserInfo.CurrentOrgId)
1701
-		//开启自备药,生成出库单
1702
-		if medicalSet.DrugStart == 1 {
1703
-			prescribingNumber := strconv.FormatFloat(math.Abs(adviceName.PrescribingNumber), 'f', 0, 64)
1704
-			outStoreNumber, _ := strconv.ParseInt(prescribingNumber, 10, 64)
1705
-
1706
-			timeStr := time.Now().Format("2006-01-02")
1707
-			timeArr := strings.Split(timeStr, "-")
1708
-			total, _ := service.FindAllDrugWarehouseOut(adminUserInfo.CurrentOrgId)
1709
-
1710
-			total = total + 1
1711
-			warehousing_out_order := strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
1712
-			number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
1713
-			number = number + total
1714
-			warehousing_out_order = "CKD" + strconv.FormatInt(number, 10)
1715
-
1716
-			outStock := models.XtSelfOutStock{
1717
-				DrugName:       adviceName.AdviceName,
1718
-				DrugSpec:       adviceName.AdviceDesc,
1719
-				OutstoreNumber: outStoreNumber,
1720
-				AdminUserId:    adminUserInfo.AdminUser.Id,
1721
-				CreatedTime:    time.Now().Unix(),
1722
-				Status:         1,
1723
-				PatientId:      adviceName.PatientId,
1724
-				StockOutNumber: warehousing_out_order,
1725
-				ExitMode:       2,
1726
-				StorckTime:     adviceName.AdviceDate,
1727
-				MedicId:        adviceName.DrugId,
1728
-				UserOrgId:      adminUserInfo.CurrentOrgId,
1745
+	fmt.Println("222222222222", privateDrugConfig.DrugStart)
1746
+	adviceName, _ := service.FindDoctorAdvice(adminUserInfo.CurrentOrgId, id)
1747
+	if adviceName.Way == 2 {
1748
+		if privateDrugConfig != nil && privateDrugConfig.DrugStart == 1 {
1749
+			fmt.Println("进来了没有-------------------------------")
1750
+			adviceName, _ := service.FindDoctorAdvice(adminUserInfo.CurrentOrgId, id)
1751
+			fmt.Println("3333333333", adviceName.ExecutionState)
1752
+			if adviceName.ExecutionState == 1 {
1753
+				prescribingNumber := strconv.FormatFloat(math.Abs(adviceName.PrescribingNumber), 'f', 0, 64)
1754
+				count, _ := strconv.ParseInt(prescribingNumber, 10, 64)
1755
+
1756
+				timeStr := time.Now().Format("2006-01-02")
1757
+				timeArr := strings.Split(timeStr, "-")
1758
+				total, _ := service.FindAllDrugWarehouseOut(adminUserInfo.CurrentOrgId)
1759
+
1760
+				total = total + 1
1761
+				warehousing_out_order := strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
1762
+				number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
1763
+				number = number + total
1764
+				warehousing_out_order = "CKD" + strconv.FormatInt(number, 10)
1765
+				//插入自备药出库单
1766
+				outStock := models.XtSelfOutStock{
1767
+					DrugName:       adviceName.AdviceName,
1768
+					DrugNameId:     adviceName.DrugId,
1769
+					DrugSpec:       adviceName.AdviceDesc,
1770
+					OutstoreNumber: count,
1771
+					AdminUserId:    adminUserInfo.AdminUser.Id,
1772
+					StorckTime:     adviceName.RecordDate,
1773
+					UserOrgId:      adminUserInfo.CurrentOrgId,
1774
+					CreatedTime:    time.Now().Unix(),
1775
+					Status:         1,
1776
+					PatientId:      adviceName.PatientId,
1777
+					ExitMode:       2,
1778
+					MedicId:        adviceName.DrugId,
1779
+					StockOutNumber: warehousing_out_order,
1780
+				}
1781
+				service.CreateOutStock(&outStock)
1729 1782
 			}
1730
-			//创建出库单,自动出库
1731
-			service.CreateOutStock(&outStock)
1732
-			c.ServeSuccessJSON(map[string]interface{}{
1733
-				"msg":    "ok",
1734
-				"advice": advice,
1735
-			})
1736
-			return
1737 1783
 		}
1738
-
1739 1784
 	}
1740 1785
 
1786
+	c.ServeSuccessJSON(map[string]interface{}{
1787
+		"msg":    "ok",
1788
+		"advice": advice,
1789
+	})
1790
+	return
1791
+
1792
+	////如果医嘱已执行,生成自备药出库单
1793
+	//  //来自药品库
1794
+	//  if adviceName.ExecutionState == 1 && adviceName.Way == 1 {
1795
+	//
1796
+	//    //查询是否开启药品库
1797
+	//    stockConfig, _ := service.GetDrugStockConfig(adminUserInfo.CurrentOrgId)
1798
+	//    //开启药品库
1799
+	//    if stockConfig.IsOpen == 1 {
1800
+	//      timeLayout := "2006-01-02"
1801
+	//      loc, _ := time.LoadLocation("Local")
1802
+	//      //产询今日是否存在出库单
1803
+	//      timeStr := time.Now().Format("2006-01-02")
1804
+	//      theTime, _ := time.ParseInLocation(timeLayout+" 15:04:05", timeStr+" 00:00:00", loc)
1805
+	//      _, errcode := service.GetDrugWareseOut(theTime.Unix(), adminUserInfo.CurrentOrgId)
1806
+	//      if errcode == gorm.ErrRecordNotFound {
1807
+	//        timeArr := strings.Split(timeStr, "-")
1808
+	//        total, _ := service.FindAllDrugWarehouseOut(adminUserInfo.CurrentOrgId)
1809
+	//
1810
+	//        total = total + 1
1811
+	//        warehousing_out_order := strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
1812
+	//        number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
1813
+	//        number = number + total
1814
+	//        warehousing_out_order = "YPRKD" + strconv.FormatInt(number, 10)
1815
+	//        //查询厂家
1816
+	//        drug, _ := service.GetManufacturer(adviceName.DrugId)
1817
+	//        //创建
1818
+	//        warehouse := models.XtDrugWarehouse{
1819
+	//          WarehousingOrder: warehousing_out_order,
1820
+	//          OperationTime:    theTime.Unix(),
1821
+	//          Creater:          adminUserInfo.AdminUser.Id,
1822
+	//          OrgId:            adminUserInfo.CurrentOrgId,
1823
+	//          Status:           1,
1824
+	//          WarehousingTime:  theTime.Unix(),
1825
+	//          Manufacturer:     drug.Manufacturer,
1826
+	//          Type:             1,
1827
+	//        }
1828
+	//        service.CreateDrugWarehoue(&warehouse)
1829
+	//      }
1830
+	//
1831
+	//      timeArr := strings.Split(timeStr, "-")
1832
+	//      total, _ := service.FindAllDrugWarehouseOut(adminUserInfo.CurrentOrgId)
1833
+	//
1834
+	//      total = total + 1
1835
+	//      warehousing_out_order := strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
1836
+	//      number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
1837
+	//      number = number + total
1838
+	//      warehousing_out_order = "YPRKD" + strconv.FormatInt(number, 10)
1839
+	//      drug, _ := service.GetManufacturer(adviceName.DrugId)
1840
+	//      out := models.XtDrugWarehouseOut{
1841
+	//        WarehouseOutOrderNumber: warehousing_out_order,
1842
+	//        OperationTime:           theTime.Unix(),
1843
+	//        Creater:                 adminUserInfo.AdminUser.Id,
1844
+	//        OrgId:                   adminUserInfo.CurrentOrgId,
1845
+	//        Ctime:                   time.Now().Unix(),
1846
+	//        Status:                  1,
1847
+	//        WarehouseOutTime:        theTime.Unix(),
1848
+	//        Manufacturer:            drug.Manufacturer,
1849
+	//        Type:                    1,
1850
+	//        IsSys:                   1,
1851
+	//      }
1852
+	//      service.CreateDrugWareHouseOut(&out)
1853
+	//
1854
+	//      //查询今日的出库单号
1855
+	//      orderOut, _ := service.GetTodayWareHousringOrder(theTime.Unix(), adminUserInfo.CurrentOrgId)
1856
+	//      prescribingNumber := strconv.FormatFloat(math.Abs(adviceName.PrescribingNumber), 'f', 0, 64)
1857
+	//      outStoreNumber, _ := strconv.ParseInt(prescribingNumber, 10, 64)
1858
+	//      timeArrs := strings.Split(timeStr, "-")
1859
+	//      totals, _ := service.FindAllDrugWarehouseOut(adminUserInfo.CurrentOrgId)
1860
+	//
1861
+	//      totals = totals + 1
1862
+	//      warehousing_out_orders := strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArrs[0] + timeArrs[1] + timeArrs[2] + "000"
1863
+	//      numbers, _ := strconv.ParseInt(warehousing_out_orders, 10, 64)
1864
+	//      number = numbers + totals
1865
+	//      warehousing_out_order = "YPCKD" + strconv.FormatInt(numbers, 10)
1866
+	//      info := models.XtDrugWarehouseOutInfo{
1867
+	//        WarehouseOutId:          orderOut.ID,
1868
+	//        DrugId:                  adviceName.DrugId,
1869
+	//        Count:                   outStoreNumber,
1870
+	//        Price:                   drug.RetailPrice,
1871
+	//        TotalPrice:              drug.RetailPrice,
1872
+	//        ProductDate:             theTime.Unix(),
1873
+	//        Ctime:                   time.Now().Unix(),
1874
+	//        Status:                  1,
1875
+	//        OrgId:                   adminUserInfo.CurrentOrgId,
1876
+	//        IsCancel:                2,
1877
+	//        WarehouseOutOrderNumber: warehousing_out_order,
1878
+	//        Type:                    1,
1879
+	//        Manufacturer:            orderOut.Manufacturer,
1880
+	//        IsSys:                   1,
1881
+	//        SysRecordTime:           theTime.Unix(),
1882
+	//      }
1883
+	//      service.CreateWareHouseOutInfo(&info)
1884
+	//
1885
+	//      timeArrss := strings.Split(timeStr, "-")
1886
+	//      totalss, _ := service.FindAllDrugWarehouseOut(adminUserInfo.CurrentOrgId)
1887
+	//
1888
+	//      totalss = totalss + 1
1889
+	//      warehousing_out_orderss := strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArrss[0] + timeArrss[1] + timeArrss[2] + "000"
1890
+	//      numberss, _ := strconv.ParseInt(warehousing_out_orderss, 10, 64)
1891
+	//      number = numberss + totalss
1892
+	//      warehousing_out_order = "YPCKD" + strconv.FormatInt(numberss, 10)
1893
+	//      detail := models.XtDrugAutomaticReduceDetail{
1894
+	//        WarehouseOutOrderNumber: warehousing_out_order,
1895
+	//        WarehouseOutId:          orderOut.ID,
1896
+	//        PatientId:               adviceName.PatientId,
1897
+	//        Ctime:                   time.Now().Unix(),
1898
+	//        RecordTime:              theTime.Unix(),
1899
+	//        OrgId:                   adminUserInfo.CurrentOrgId,
1900
+	//        GoodId:                  adviceName.DrugId,
1901
+	//        Count:                   outStoreNumber,
1902
+	//        Status:                  1,
1903
+	//      }
1904
+	//      service.CreateAotoMaticReduceDetail(&detail)
1905
+	//    }
1906
+	//}
1907
+
1908
+	//if adviceName.ExecutionState == 1 && adviceName.Way == 2 {
1909
+	//
1910
+	//	//是否开启自备药
1911
+	//	medicalSet, _ := service.GetSetSelfMedical(adminUserInfo.CurrentOrgId)
1912
+	//	//开启自备药,生成出库单
1913
+	//	if medicalSet.DrugStart == 1 {
1914
+	//		prescribingNumber := strconv.FormatFloat(math.Abs(adviceName.PrescribingNumber), 'f', 0, 64)
1915
+	//		outStoreNumber, _ := strconv.ParseInt(prescribingNumber, 10, 64)
1916
+	//
1917
+	//		timeStr := time.Now().Format("2006-01-02")
1918
+	//		timeArr := strings.Split(timeStr, "-")
1919
+	//		total, _ := service.FindAllDrugWarehouseOut(adminUserInfo.CurrentOrgId)
1920
+	//
1921
+	//		total = total + 1
1922
+	//		warehousing_out_order := strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
1923
+	//		number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
1924
+	//		number = number + total
1925
+	//		warehousing_out_order = "CKD" + strconv.FormatInt(number, 10)
1926
+	//
1927
+	//		outStock := models.XtSelfOutStock{
1928
+	//			DrugName:       adviceName.AdviceName,
1929
+	//			DrugSpec:       adviceName.AdviceDesc,
1930
+	//			OutstoreNumber: outStoreNumber,
1931
+	//			AdminUserId:    adminUserInfo.AdminUser.Id,
1932
+	//			CreatedTime:    time.Now().Unix(),
1933
+	//			Status:         1,
1934
+	//			PatientId:      adviceName.PatientId,
1935
+	//			StockOutNumber: warehousing_out_order,
1936
+	//			ExitMode:       2,
1937
+	//			StorckTime:     adviceName.AdviceDate,
1938
+	//			MedicId:        adviceName.DrugId,
1939
+	//			UserOrgId:      adminUserInfo.CurrentOrgId,
1940
+	//		}
1941
+	//		//创建出库单,自动出库
1942
+	//		service.CreateOutStock(&outStock)
1943
+	//		c.ServeSuccessJSON(map[string]interface{}{
1944
+	//			"msg":    "ok",
1945
+	//			"advice": advice,
1946
+	//		})
1947
+	//		return
1948
+	//	}
1949
+	//
1950
+	//}
1951
+
1741 1952
 }
1742 1953
 
1743 1954
 func (c *PatientApiController) CheckGroupAdvice() {

+ 129 - 43
controllers/self_drug_api_congtroller.go View File

@@ -45,6 +45,9 @@ func SelfDrugRouters() {
45 45
 	beego.Router("/api/drug/getselfmedicallist", &SelfDrugApiController{}, "Get:GetSelfMedicalList")
46 46
 	beego.Router("/api/drug/getdrugdescbydrugname", &SelfDrugApiController{}, "Get:GetDrugDescByDrugName")
47 47
 	beego.Router("/api/drug/getdrugset", &SelfDrugApiController{}, "Get:GetDrugSet")
48
+	beego.Router("/api/drug/getallmedicallist", &SelfDrugApiController{}, "Get:GetAllMedicalList")
49
+	beego.Router("/api/drug/getstandname", &SelfDrugApiController{}, "Get:GetStandName")
50
+	beego.Router("/api/drug/getrullerlist", &SelfDrugApiController{}, "Get:GetRullerList")
48 51
 }
49 52
 
50 53
 func (this *SelfDrugApiController) GetCurrentPatient() {
@@ -131,15 +134,14 @@ func (this *SelfDrugApiController) GetRulleName() {
131 134
 
132 135
 	adminUserInfo := this.GetAdminUserInfo()
133 136
 	orgId := adminUserInfo.CurrentOrgId
134
-	name := this.GetString("name")
135
-	rulleName, err := service.GetRulleName(orgId, name)
136
-	rullerList, err := service.GetRulleList(orgId, name)
137
+	id, _ := this.GetInt64("id")
138
+	drugId, _ := service.GetDrugId(id)
139
+	rullerList, err := service.GetRulleList(orgId, drugId.DrugId)
137 140
 	if err != nil {
138 141
 		this.ServeFailJsonSend(enums.ErrorCodeDataException, "更新设备失败")
139 142
 		return
140 143
 	}
141 144
 	this.ServeSuccessJSON(map[string]interface{}{
142
-		"rulleName":  rulleName,
143 145
 		"rullerList": rullerList,
144 146
 	})
145 147
 }
@@ -162,6 +164,7 @@ func (this *SelfDrugApiController) SaveRulleName() {
162 164
 	drug_spec := this.GetString("drug_spec")
163 165
 	drug_stock_limit := this.GetString("drug_stock_limit")
164 166
 	drug_name_id, _ := this.GetInt64("drug_name_id")
167
+	drug_id, _ := this.GetInt64("drug_id")
165 168
 	price, _ := this.GetInt64("price")
166 169
 	prices := strconv.FormatInt(price, 10)
167 170
 	durg_price, _ := strconv.ParseFloat(prices, 64)
@@ -179,6 +182,7 @@ func (this *SelfDrugApiController) SaveRulleName() {
179 182
 		UserOrgId:      orgId,
180 183
 		Status:         1,
181 184
 		CreatedTime:    time.Now().Unix(),
185
+		DrugId:         drug_id,
182 186
 	}
183 187
 
184 188
 	err := service.SaveRulleName(&drugSpecName)
@@ -216,6 +220,8 @@ func (this *SelfDrugApiController) UpdatedRullerName() {
216 220
 	drug_spec := this.GetString("drug_spec")
217 221
 	unit := this.GetString("unit")
218 222
 	drug_name_id, _ := this.GetInt64("drug_name_id")
223
+	drug_id, _ := this.GetInt64("drug_id")
224
+
219 225
 	RullerName := models.XtStandName{
220 226
 		DrugName:       drug_name,
221 227
 		DrugStockLimit: drug_stock_limit,
@@ -223,16 +229,23 @@ func (this *SelfDrugApiController) UpdatedRullerName() {
223 229
 		DrugSpec:       drug_spec,
224 230
 		MinUnit:        unit,
225 231
 		DrugNameId:     drug_name_id,
232
+		DrugId:         drug_id,
226 233
 	}
227
-
228
-	err := service.UpdatedRullerName(id, &RullerName)
229
-	if err != nil {
230
-		this.ServeFailJsonSend(enums.ErrorCodeDataException, "更新设备失败")
234
+	_, errcode := service.GetIsExit(drug_name, drug_spec, id)
235
+	if errcode == gorm.ErrRecordNotFound {
236
+		err := service.UpdatedRullerName(id, &RullerName)
237
+		if err != nil {
238
+			this.ServeFailJsonSend(enums.ErrorCodeDataException, "更新设备失败")
239
+			return
240
+		}
241
+		this.ServeSuccessJSON(map[string]interface{}{
242
+			"RullerName": RullerName,
243
+		})
244
+	} else if errcode == nil {
245
+		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
231 246
 		return
232 247
 	}
233
-	this.ServeSuccessJSON(map[string]interface{}{
234
-		"RullerName": RullerName,
235
-	})
248
+
236 249
 }
237 250
 
238 251
 func (this *SelfDrugApiController) GetAllDrugNameList() {
@@ -252,12 +265,11 @@ func (this *SelfDrugApiController) GetAllDrugNameList() {
252 265
 
253 266
 func (this *SelfDrugApiController) GetRullerListByDrugName() {
254 267
 
255
-	drug_name := this.GetString("drug_name")
256
-	fmt.Println(drug_name)
268
+	id := this.GetString("id")
257 269
 	adminUserInfo := this.GetAdminUserInfo()
258 270
 	orgId := adminUserInfo.CurrentOrgId
259 271
 	fmt.Println("orgid", orgId)
260
-	drugName, err := service.GetRullerListByDrugName(drug_name, orgId)
272
+	drugName, err := service.GetRullerListByDrugName(id, orgId)
261 273
 	if err != nil {
262 274
 		this.ServeFailJsonSend(enums.ErrorCodeDataException, "更新设备失败")
263 275
 		return
@@ -282,7 +294,7 @@ func (this *SelfDrugApiController) SaveSelfMedicines() {
282 294
 		drug_name_id := int64(items["drug_name_id"].(float64))
283 295
 		drug_spec := items["drug_spec"].(string)
284 296
 		min_unit := items["min_unit"].(string)
285
-
297
+		drug_id := int64(items["drug_id"].(float64))
286 298
 		//根据drug_id
287 299
 		drugMedical, _ := service.GetBaseDrugMedical(drug_name_id)
288 300
 		medical := models.XtSelfMedical{
@@ -298,6 +310,7 @@ func (this *SelfDrugApiController) SaveSelfMedicines() {
298 310
 			PrescribingNumber:  drugMedical.PrescribingNumber,
299 311
 			DeliveryWay:        drugMedical.DeliveryWay,
300 312
 			SingleDose:         drugMedical.SingleDose,
313
+			DrugId:             drug_id,
301 314
 		}
302 315
 		//查询同个病人同个药品同个规格是否已存在
303 316
 		_, errcode := service.GetSelfMedicalByDrugName(drug_name, drug_spec, patient_id)
@@ -461,12 +474,12 @@ func (this *SelfDrugApiController) SaveOutStock() {
461 474
 func (this *SelfDrugApiController) DeleteDrugById() {
462 475
 	id, _ := this.GetInt64("id")
463 476
 	drug_name := this.GetString("drug_name")
464
-	fmt.Println("drug_name", drug_name)
477
+
465 478
 	adminUserInfo := this.GetAdminUserInfo()
466 479
 	orgId := adminUserInfo.CurrentOrgId
467 480
 
468 481
 	_, errcode := service.GetStandDrugByDrugName(drug_name, orgId)
469
-	fmt.Println("errcode2222222222222", errcode)
482
+
470 483
 	if errcode == gorm.ErrRecordNotFound {
471 484
 		service.DeleteDrugName(id)
472 485
 		returnData := make(map[string]interface{}, 0)
@@ -515,11 +528,22 @@ func (this *SelfDrugApiController) SaveRadio() {
515 528
 func (this *SelfDrugApiController) DeleteDrugStand() {
516 529
 
517 530
 	id, _ := this.GetInt64("id")
518
-	err := service.DeleteDrugStand(id)
519
-	fmt.Println(err)
520
-	returnData := make(map[string]interface{}, 0)
521
-	returnData["msg"] = "ok"
522
-	this.ServeSuccessJSON(returnData)
531
+	name := this.GetString("name")
532
+	adminUserInfo := this.GetAdminUserInfo()
533
+	orgId := adminUserInfo.CurrentOrgId
534
+
535
+	_, errcode := service.GetMedicalsByName(orgId, name)
536
+	if errcode == gorm.ErrRecordNotFound {
537
+		err := service.DeleteDrugStand(id)
538
+		fmt.Println(err)
539
+		returnData := make(map[string]interface{}, 0)
540
+		returnData["msg"] = "ok"
541
+		this.ServeSuccessJSON(returnData)
542
+	} else if errcode == nil {
543
+		this.ServeFailJsonSend(enums.ErrorCodeDataException, "更新设备失败")
544
+		return
545
+	}
546
+
523 547
 }
524 548
 
525 549
 func (this *SelfDrugApiController) GetStockList() {
@@ -649,27 +673,46 @@ func (this *SelfDrugApiController) GetSelfMedicalList() {
649 673
 	patient_id, _ := this.GetInt64("patient_id")
650 674
 	adminUserInfo := this.GetAdminUserInfo()
651 675
 	orgId := adminUserInfo.CurrentOrgId
652
-	//查询该机构是否开启自备药
653
-	medical, _ := service.GetSetSelfMedical(orgId)
654
-
655
-	//查询该机构下的药品库
656
-	drugName, _ := service.GetAllDrugName(orgId)
657
-	//开启
658
-	if medical.DrugStart == 1 {
659
-		list, err := service.GetSelfMedicalList(patient_id)
660
-		if err != nil {
661
-			this.ServeFailJsonSend(enums.ErrorCodeDataException, "更新设备失败")
662
-			return
663
-		}
664
-		this.ServeSuccessJSON(map[string]interface{}{
665
-			"medicalList": list,
666
-			"drugName":    drugName,
667
-		})
668
-	} else {
669
-		this.ServeSuccessJSON(map[string]interface{}{
670
-			"drugName": drugName,
671
-		})
672
-	}
676
+	_, drugStockConfig := service.FindDrugStockAutomaticReduceRecordByOrgId(orgId)
677
+	privateDrugConfig, _ := service.GetDrugSetByUserOrgId(orgId)
678
+	drugList, _ := service.GetAllBaseDrugLibList(orgId)
679
+	privateDrugList, _ := service.GetPrivateDrugList(patient_id, orgId)
680
+
681
+	this.ServeSuccessJSON(map[string]interface{}{
682
+		"base_drug_config":    drugStockConfig,
683
+		"private_drug_config": privateDrugConfig,
684
+		"base_drug_list":      drugList,
685
+		"private_drug_list":   privateDrugList,
686
+	})
687
+	////查询是否开启药品库
688
+	//config, _ := service.GetDruckStockConfig(orgId)
689
+	//
690
+	//fmt.Println("--------------------------",config.IsOpen)
691
+	////开启
692
+	//if config.IsOpen == 1 {
693
+	//  //查询该机构下的药品库
694
+	//  drugName, _ := service.GetAllDrugName(orgId)
695
+	//
696
+	//  //查询该机构是否开启自备药
697
+	//  medical, _ := service.GetSetSelfMedical(orgId)
698
+	//  fmt.Println("medical+++++++++++++++++",medical.DrugStart)
699
+	//  //开启
700
+	//  if medical.DrugStart == 1 {
701
+	//    list, err := service.GetSelfMedicalList(patient_id)
702
+	//    if err != nil {
703
+	//      this.ServeFailJsonSend(enums.ErrorCodeDataException, "更新设备失败")
704
+	//      return
705
+	//    }
706
+	//    this.ServeSuccessJSON(map[string]interface{}{
707
+	//      "medicalList": list,
708
+	//      "drugName":    drugName,
709
+	//    })
710
+	//  } else {
711
+	//    this.ServeSuccessJSON(map[string]interface{}{
712
+	//      "drugName": drugName,
713
+	//    })
714
+	//  }
715
+	//}
673 716
 }
674 717
 
675 718
 func (this *SelfDrugApiController) GetDrugDescByDrugName() {
@@ -727,3 +770,46 @@ func (this *SelfDrugApiController) GetDrugSet() {
727 770
 	})
728 771
 
729 772
 }
773
+
774
+func (this *SelfDrugApiController) GetAllMedicalList() {
775
+
776
+	adminUserInfo := this.GetAdminUserInfo()
777
+	orgId := adminUserInfo.CurrentOrgId
778
+	list, err := service.GetAllMedicalList(orgId)
779
+	if err != nil {
780
+		this.ServeFailJsonSend(enums.ErrorCodeDataException, "更新设备失败")
781
+		return
782
+	}
783
+	this.ServeSuccessJSON(map[string]interface{}{
784
+		"drugName": list,
785
+	})
786
+}
787
+
788
+func (this *SelfDrugApiController) GetStandName() {
789
+
790
+	name := this.GetString("name")
791
+	adminUserInfo := this.GetAdminUserInfo()
792
+	orgId := adminUserInfo.CurrentOrgId
793
+	rullerlist, err := service.GetRulleName(orgId, name)
794
+	if err != nil {
795
+		this.ServeFailJsonSend(enums.ErrorCodeDataException, "更新设备失败")
796
+		return
797
+	}
798
+	this.ServeSuccessJSON(map[string]interface{}{
799
+		"rullerlist": rullerlist,
800
+	})
801
+}
802
+
803
+func (this *SelfDrugApiController) GetRullerList() {
804
+	id, _ := this.GetInt64("id")
805
+	adminUserInfo := this.GetAdminUserInfo()
806
+	orgId := adminUserInfo.CurrentOrgId
807
+	rullerList, err := service.GetRulleList(orgId, id)
808
+	if err != nil {
809
+		this.ServeFailJsonSend(enums.ErrorCodeDataException, "更新设备失败")
810
+		return
811
+	}
812
+	this.ServeSuccessJSON(map[string]interface{}{
813
+		"rullerlist": rullerList,
814
+	})
815
+}

+ 2 - 0
models/self_drug_models.go View File

@@ -95,6 +95,7 @@ type XtStandName struct {
95 95
 	CreatedTime    int64   `gorm:"column:created_time" json:"created_time" form:"created_time"`
96 96
 	UpdatedTime    int64   `gorm:"column:updated_time" json:"updated_time" form:"updated_time"`
97 97
 	DrugName       string  `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
98
+	DrugId         int64   `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
98 99
 }
99 100
 
100 101
 func (XtStandName) TableName() string {
@@ -133,6 +134,7 @@ type XtSelfMedical struct {
133 134
 	PrescribingNumber  float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
134 135
 	DeliveryWay        string  `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
135 136
 	SingleDose         float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
137
+	DrugId             int64   `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
136 138
 }
137 139
 
138 140
 func (XtSelfMedical) TableName() string {

+ 60 - 9
service/self_drug_service.go View File

@@ -50,9 +50,15 @@ func GetRulleName(orgid int64, name string) (baseDrug []*models.XtBaseDrug, err
50 50
 	return baseDrug, err
51 51
 }
52 52
 
53
-func GetRulleList(orgid int64, name string) (rulleName []*models.XtStandName, err error) {
53
+func GetDrugId(id int64) (models.XtSelfMedical, error) {
54
+	medical := models.XtSelfMedical{}
55
+	err := XTReadDB().Model(&medical).Where("id = ? and status = 1", id).Find(&medical).Error
56
+	return medical, err
57
+}
54 58
 
55
-	err = XTReadDB().Model(&rulleName).Where("user_org_id = ? and drug_name = ? and status = 1", orgid, name).Find(&rulleName).Error
59
+func GetRulleList(orgid int64, id int64) (rulleName []*models.XtStandName, err error) {
60
+
61
+	err = XTReadDB().Model(&rulleName).Where("user_org_id = ? and drug_id = ? and status = 1", orgid, id).Find(&rulleName).Error
56 62
 	return rulleName, err
57 63
 }
58 64
 
@@ -74,21 +80,40 @@ func GetRullerNameDetail(id int64) (models.XtStandName, error) {
74 80
 	return rullerName, err
75 81
 }
76 82
 
83
+func GetIsExit(drugname string, drugspec string, id int64) (*models.XtStandName, error) {
84
+
85
+	standName := models.XtStandName{}
86
+	err := XTReadDB().Model(&standName).Where("drug_name = ? and drug_spec = ? and id <> ? and status = 1", drugname, drugspec, id).Find(&standName).Error
87
+	if err == gorm.ErrRecordNotFound {
88
+		return nil, err
89
+	}
90
+	if err != nil {
91
+		return nil, err
92
+	}
93
+	return &standName, nil
94
+}
95
+
77 96
 func UpdatedRullerName(id int64, re *models.XtStandName) error {
78 97
 
79
-	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}).Error
98
+	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
80 99
 	return err
81 100
 }
82 101
 
83
-func GetAllDrugNameList(orgid int64) (rullername []*models.XtStandName, err error) {
102
+//func GetAllDrugNameList(orgid int64) (rullername []*models.XtStandName, err error) {
103
+//
104
+//	err = XTReadDB().Group("drug_name").Model(&rullername).Where("user_org_id = ? and status = 1", orgid).Find(&rullername).Error
105
+//	return rullername, err
106
+//}
107
+
108
+func GetAllDrugNameList(orgid int64) (drugName []*models.XtDrugName, err error) {
84 109
 
85
-	err = XTReadDB().Group("drug_name").Model(&rullername).Where("user_org_id = ? and status = 1", orgid).Find(&rullername).Error
86
-	return rullername, err
110
+	err = XTReadDB().Model(&drugName).Where("user_org_id = ? and status = 1", orgid).Find(&drugName).Error
111
+	return drugName, err
87 112
 }
88 113
 
89 114
 func GetRullerListByDrugName(drugname string, orgid int64) (rullername []*models.XtStandName, err error) {
90 115
 
91
-	err = XTReadDB().Model(&rullername).Where("drug_name = ? and user_org_id = ? and status = 1", drugname, orgid).Find(&rullername).Error
116
+	err = XTReadDB().Model(&rullername).Where("drug_id = ? and user_org_id = ? and status = 1", drugname, orgid).Find(&rullername).Error
92 117
 	return rullername, err
93 118
 }
94 119
 
@@ -146,8 +171,8 @@ func GetStandDrugByDrugName(drugname string, orgid int64) (*models.XtStandName,
146 171
 	return &standname, nil
147 172
 }
148 173
 
149
-func DeleteDrugName(id int64) (models.XtSelfMedical, error) {
150
-	standName := models.XtSelfMedical{}
174
+func DeleteDrugName(id int64) (models.XtDrugName, error) {
175
+	standName := models.XtDrugName{}
151 176
 	err := XTWriteDB().Model(&standName).Where("id = ?", id).Update(map[string]interface{}{"status": 0}).Error
152 177
 	return standName, err
153 178
 }
@@ -176,6 +201,19 @@ func UpdateDrugSet(set *models.XtDrugSet, orgid int64) error {
176 201
 	return err
177 202
 }
178 203
 
204
+func GetMedicalsByName(orgid int64, drugname string) (*models.XtSelfMedical, error) {
205
+
206
+	medical := models.XtSelfMedical{}
207
+	err := XTReadDB().Model(&medical).Where("user_org_id = ? and drug_name = ? and status = 1", orgid, drugname).Find(&medical).Error
208
+	if err == gorm.ErrRecordNotFound {
209
+		return nil, err
210
+	}
211
+	if err != nil {
212
+		return nil, err
213
+	}
214
+	return &medical, nil
215
+}
216
+
179 217
 func DeleteDrugStand(id int64) error {
180 218
 
181 219
 	standName := models.XtStandName{}
@@ -335,6 +373,13 @@ func GetDrugDataByPatientId(patientid int64) (selfMedical []*models.XtSelfMedica
335 373
 	return selfMedical, err
336 374
 }
337 375
 
376
+func GetDruckStockConfig(orgid int64) (models.XtDrugStockConfig, error) {
377
+
378
+	drugConfig := models.XtDrugStockConfig{}
379
+	err := XTReadDB().Model(&drugConfig).Where("org_id = ? and status = 1", orgid).Find(&drugConfig).Error
380
+	return drugConfig, err
381
+}
382
+
338 383
 func GetSetSelfMedical(orgid int64) (models.XtDrugSet, error) {
339 384
 	medical := models.XtDrugSet{}
340 385
 	err := XTReadDB().Model(&medical).Where("user_org_id = ? and status = 1", orgid).Find(&medical).Error
@@ -493,3 +538,9 @@ func GetPrivateDrugList(patient_id int64, org_id int64) (pds []*PrivateDrug, err
493 538
 	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").Find(&pds).Error
494 539
 	return
495 540
 }
541
+
542
+func GetAllMedicalList(orgid int64) (list []*models.XtSelfMedical, err error) {
543
+
544
+	err = XTReadDB().Model(&list).Where("user_org_id = ? and status = 1", orgid).Group("drug_name").Find(&list).Error
545
+	return list, err
546
+}