dataModel.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package controllers
  2. import (
  3. "cc-officialweb/models"
  4. "cc-officialweb/service"
  5. "cc-officialweb/unity"
  6. "encoding/json"
  7. beego "github.com/beego/beego/v2/server/web"
  8. "github.com/go-playground/validator/v10"
  9. "strconv"
  10. )
  11. type DataModelController struct {
  12. beego.Controller
  13. }
  14. // GetData 获取数据
  15. func (d *DataModelController) GetData() {
  16. data, err := service.GetDataModel()
  17. if err != nil {
  18. d.Data["json"] = JSON{Code: 103, Msg: "获取失败", Data: ""}
  19. d.ServeJSON()
  20. return
  21. } else {
  22. d.Data["json"] = JSON{Code: 200, Msg: "获取成功", Data: data}
  23. d.ServeJSON()
  24. }
  25. }
  26. // GetDatas 获取数据
  27. func (d *DataModelController) GetDatas() {
  28. var page unity.PageParams
  29. err := json.Unmarshal(d.Ctx.Input.RequestBody, &page)
  30. if err != nil {
  31. d.Data["json"] = &JSON{Code: 101, Msg: "参数错误"}
  32. d.ServeJSON()
  33. return
  34. }
  35. result, total, current, err := unity.Paginate(page, models.Data{})
  36. if err != nil {
  37. d.Data["json"] = &JSON{Code: 101, Msg: "查询失败"}
  38. d.ServeJSON()
  39. return
  40. } else {
  41. d.Data["json"] = &JSON{Code: 200, Msg: "查询成功", Data: &JSONS{
  42. Current: current,
  43. Size: total,
  44. Data: result,
  45. }}
  46. d.ServeJSON()
  47. return
  48. }
  49. }
  50. // GetDataType 获得数据类型
  51. func (d *DataModelController) GetDataType() {
  52. data, err := service.GetDataType()
  53. if err == nil {
  54. d.Data["json"] = &JSON{Code: 200, Msg: "查询成功", Data: data}
  55. d.ServeJSON()
  56. } else {
  57. d.Data["json"] = &JSON{Code: 101, Msg: "查询失败"}
  58. d.ServeJSON()
  59. return
  60. }
  61. }
  62. // GetDatasByType 获取数据类型
  63. func (d *DataModelController) GetDatasByType() {
  64. types := d.GetString("types")
  65. err2 := validator.New().Var(types, "required")
  66. if err2 != nil {
  67. d.Data["json"] = JSON{Code: 103, Msg: "获取失败缺少必要参数", Data: ""}
  68. }
  69. data, err := service.GetDataByType(types)
  70. if err != nil {
  71. d.Data["json"] = JSON{Code: 103, Msg: "获取失败", Data: ""}
  72. d.ServeJSON()
  73. return
  74. } else {
  75. d.Data["json"] = JSON{Code: 200, Msg: "获取成功", Data: data}
  76. d.ServeJSON()
  77. }
  78. }
  79. // UpdataData 更新数据
  80. func (d *DataModelController) UpdataData() {
  81. var dataDto models.DataDto
  82. var data models.Data
  83. err := json.Unmarshal(d.Ctx.Input.RequestBody, &dataDto)
  84. if err != nil {
  85. d.Data["json"] = JSON{Code: 103, Msg: "json解析失败", Data: ""}
  86. d.ServeJSON()
  87. return
  88. }
  89. data.Title = dataDto.Title
  90. data.Types = dataDto.Types
  91. data.Nums = dataDto.Nums
  92. data.IsIndex = dataDto.IsIndex
  93. id, err := unity.UpdateById(dataDto.ID, &data)
  94. if err == nil {
  95. d.Data["json"] = JSON{Code: 200, Msg: "更新成功", Data: id}
  96. d.ServeJSON()
  97. } else {
  98. d.Data["json"] = JSON{Code: 103, Msg: "更新失败", Data: ""}
  99. d.ServeJSON()
  100. }
  101. }
  102. // DeleteDataById 根据id删除数据
  103. func (d *DataModelController) DeleteDataById() {
  104. getString := d.GetString("id")
  105. err := validator.New().Var("id", "required")
  106. if err != nil {
  107. d.Data["json"] = JSON{Code: 104, Msg: "id不能为空", Data: ""}
  108. d.ServeJSON()
  109. }
  110. atoi, _ := strconv.Atoi(getString)
  111. id, err := unity.DeleteById(atoi, models.Data{})
  112. if err != nil {
  113. d.Data["json"] = JSON{Code: 104, Msg: "删除失败", Data: ""}
  114. d.ServeJSON()
  115. } else {
  116. d.Data["json"] = JSON{Code: 200, Msg: "删除成功", Data: id}
  117. d.ServeJSON()
  118. }
  119. }
  120. // AddData 添加数据
  121. func (d *DataModelController) AddData() {
  122. var data models.Data
  123. var dataDto models.DataDto
  124. err := json.Unmarshal(d.Ctx.Input.RequestBody, &dataDto)
  125. if err != nil {
  126. d.Data["json"] = JSON{Code: 103, Msg: "json解析失败", Data: ""}
  127. d.ServeJSON()
  128. return
  129. }
  130. data.Nums = dataDto.Nums
  131. data.Title = dataDto.Title
  132. data.Types = dataDto.Types
  133. data.IsIndex = dataDto.IsIndex
  134. add, err := unity.Add(&data)
  135. if err != nil {
  136. d.Data["json"] = JSON{Code: 104, Msg: "添加失败", Data: ""}
  137. d.ServeJSON()
  138. } else {
  139. d.Data["json"] = JSON{Code: 200, Msg: "添加成功", Data: add}
  140. d.ServeJSON()
  141. }
  142. }
  143. // UpdataModel 更新数据
  144. func (d *DataModelController) UpdataModel() {
  145. var dataModel models.DataMOdelDto
  146. var data models.DataMOdel
  147. err := json.Unmarshal(d.Ctx.Input.RequestBody, &dataModel)
  148. if err != nil {
  149. d.Data["json"] = JSON{Code: 103, Msg: "json解析失败", Data: ""}
  150. d.ServeJSON()
  151. return
  152. }
  153. data.AfterSalesEarlyWarningService = dataModel.AfterSalesEarlyWarningService
  154. data.Airpacing = dataModel.Airpacing
  155. data.BigDataManagementPlatform = dataModel.BigDataManagementPlatform
  156. data.Cdc = dataModel.Cdc
  157. data.CdcInjectionSites = dataModel.CdcInjectionSites
  158. data.ChainPharmacies = dataModel.ChainPharmacies
  159. data.ColdChainLogisticsCompany = dataModel.ColdChainLogisticsCompany
  160. data.ColdChainTraining = dataModel.ColdChainTraining
  161. data.ColdStorage = dataModel.ColdStorage
  162. data.Counties = dataModel.Counties
  163. data.Dehumidifiers = dataModel.Dehumidifiers
  164. data.DvisualizationPlatform = dataModel.DvisualizationPlatform
  165. data.Freezer = dataModel.Freezer
  166. data.Hospital = dataModel.Hospital
  167. data.Incubator = dataModel.Incubator
  168. data.IncubatorSafetyPlatform = dataModel.IncubatorSafetyPlatform
  169. data.NumberOfProbes = dataModel.NumberOfProbes
  170. data.NumberOfStores = dataModel.NumberOfStores
  171. data.NumberOfValidationReports = dataModel.NumberOfValidationReports
  172. data.NumberOfValidationScenarios = dataModel.NumberOfValidationScenarios
  173. data.Pharmaceutical = dataModel.Pharmaceutical
  174. data.PharmaceuticalCompanies = dataModel.PharmaceuticalCompanies
  175. data.Prefecture = dataModel.Prefecture
  176. data.Provinces = dataModel.Provinces
  177. data.RefrigeratedCabinets = dataModel.RefrigeratedCabinets
  178. data.RefrigeratedTrucks = dataModel.RefrigeratedTrucks
  179. data.SafetyPlatformForRefrigeratedTrucks = dataModel.SafetyPlatformForRefrigeratedTrucks
  180. data.ShadedCabinet = dataModel.ShadedCabinet
  181. data.ShadedStorage = dataModel.ShadedStorage
  182. data.ID = dataModel.ID
  183. model := service.UpdateDataModel(data)
  184. if model {
  185. d.Data["json"] = JSON{Code: 200, Msg: "更新成功", Data: ""}
  186. d.ServeJSON()
  187. } else {
  188. d.Data["json"] = JSON{Code: 103, Msg: "更新失败", Data: ""}
  189. d.ServeJSON()
  190. }
  191. }
  192. // DeleteDataModel 删除数据
  193. func (d *DataModelController) DeleteDataModel() {
  194. getString := d.GetString("id")
  195. err := validator.New().Var("id", "required")
  196. if err != nil {
  197. d.Data["json"] = JSON{Code: 104, Msg: "id不能为空", Data: ""}
  198. d.ServeJSON()
  199. }
  200. atoi, _ := strconv.Atoi(getString)
  201. id, err := unity.DeleteById(atoi, models.Data{})
  202. if err != nil {
  203. d.Data["json"] = JSON{Code: 104, Msg: "删除失败", Data: ""}
  204. d.ServeJSON()
  205. } else {
  206. d.Data["json"] = JSON{Code: 200, Msg: "删除成功", Data: id}
  207. d.ServeJSON()
  208. }
  209. }
  210. // AddDataModel 添加数据模型
  211. func (d *DataModelController) AddDataModel() {
  212. var data models.DataMOdel
  213. var dataModel models.DataMOdelDto
  214. err := json.Unmarshal(d.Ctx.Input.RequestBody, &dataModel)
  215. if err != nil {
  216. d.Data["json"] = JSON{Code: 103, Msg: "json解析失败", Data: ""}
  217. d.ServeJSON()
  218. return
  219. }
  220. data.AfterSalesEarlyWarningService = dataModel.AfterSalesEarlyWarningService
  221. data.Airpacing = dataModel.Airpacing
  222. data.BigDataManagementPlatform = dataModel.BigDataManagementPlatform
  223. data.Cdc = dataModel.Cdc
  224. data.CdcInjectionSites = dataModel.CdcInjectionSites
  225. data.ChainPharmacies = dataModel.ChainPharmacies
  226. data.ColdChainLogisticsCompany = dataModel.ColdChainLogisticsCompany
  227. data.ColdChainTraining = dataModel.ColdChainTraining
  228. data.ColdStorage = dataModel.ColdStorage
  229. data.Counties = dataModel.Counties
  230. data.Dehumidifiers = dataModel.Dehumidifiers
  231. data.DvisualizationPlatform = dataModel.DvisualizationPlatform
  232. data.Freezer = dataModel.Freezer
  233. data.Hospital = dataModel.Hospital
  234. data.Incubator = dataModel.Incubator
  235. data.IncubatorSafetyPlatform = dataModel.IncubatorSafetyPlatform
  236. data.NumberOfProbes = dataModel.NumberOfProbes
  237. data.NumberOfStores = dataModel.NumberOfStores
  238. data.NumberOfValidationReports = dataModel.NumberOfValidationReports
  239. data.NumberOfValidationScenarios = dataModel.NumberOfValidationScenarios
  240. data.Pharmaceutical = dataModel.Pharmaceutical
  241. data.PharmaceuticalCompanies = dataModel.PharmaceuticalCompanies
  242. data.Prefecture = dataModel.Prefecture
  243. data.Provinces = dataModel.Provinces
  244. data.RefrigeratedCabinets = dataModel.RefrigeratedCabinets
  245. data.RefrigeratedTrucks = dataModel.RefrigeratedTrucks
  246. data.SafetyPlatformForRefrigeratedTrucks = dataModel.SafetyPlatformForRefrigeratedTrucks
  247. data.ShadedCabinet = dataModel.ShadedCabinet
  248. data.ShadedStorage = dataModel.ShadedStorage
  249. add, err := unity.Add(&data)
  250. if err != nil {
  251. d.Data["json"] = JSON{Code: 104, Msg: "添加失败", Data: ""}
  252. d.ServeJSON()
  253. } else {
  254. d.Data["json"] = JSON{Code: 200, Msg: "添加成功", Data: add}
  255. d.ServeJSON()
  256. }
  257. }