common_api_controller.go 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package new_mobile_api_controllers
  2. import (
  3. "XT_New/controllers"
  4. "XT_New/enums"
  5. "XT_New/models"
  6. "XT_New/service"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/jinzhu/gorm"
  10. "strconv"
  11. "time"
  12. )
  13. type CommonApiController struct {
  14. controllers.BaseAuthAPIController
  15. }
  16. func (this *CommonApiController) GetInspectionMajor() {
  17. major, err := service.GetInspectionMajor(0)
  18. if err != nil {
  19. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  20. return
  21. }
  22. this.ServeSuccessJSON(map[string]interface{}{
  23. "inspection": major,
  24. })
  25. }
  26. func (this *CommonApiController) GetInspectionMinor() {
  27. id, _ := this.GetInt64("id")
  28. minor, err := service.GetInspectionMinor(id)
  29. if err != nil {
  30. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  31. return
  32. }
  33. this.ServeSuccessJSON(map[string]interface{}{
  34. "inspection": minor,
  35. })
  36. }
  37. func (this *CommonApiController) GetInspectionRange() {
  38. id, _ := this.GetInt64("id")
  39. fmt.Println("id", id)
  40. inspectionRange, err := service.GetInspectionRange(id)
  41. if err != nil {
  42. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  43. return
  44. }
  45. this.ServeSuccessJSON(map[string]interface{}{
  46. "inspectionRange": inspectionRange,
  47. })
  48. }
  49. func (this *CommonApiController) SaveConfiguration() {
  50. adminInfo := this.GetAdminUserInfo()
  51. orgid := adminInfo.CurrentOrgId
  52. dataBody := make(map[string]interface{}, 0)
  53. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  54. fmt.Println("err", err)
  55. inspectionmajor := int64(dataBody["inspectionMajor"].(float64))
  56. fmt.Println("大项", inspectionmajor)
  57. inspectionMinor := int64(dataBody["inspectionMinor"].(float64))
  58. fmt.Println("小项", inspectionMinor)
  59. minrange := dataBody["min_range"].(string)
  60. fmt.Println("minragne", minrange)
  61. largerange := dataBody["large_range"].(string)
  62. fmt.Println("largerange", largerange)
  63. sort := dataBody["sort"].(string)
  64. sorts, err := strconv.ParseInt(sort, 10, 64)
  65. fmt.Println("sort", sort)
  66. standard := models.XtQualityControlStandard{
  67. InspectionMajor: inspectionmajor,
  68. InspectionMinor: inspectionMinor,
  69. MinRange: minrange,
  70. LargeRange: largerange,
  71. Sort: sorts,
  72. UserOrgId: orgid,
  73. Status: 1,
  74. CreatedTime: time.Now().Unix(),
  75. }
  76. fmt.Println(standard)
  77. err = service.SaveInspection(&standard)
  78. if err != nil {
  79. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  80. return
  81. }
  82. this.ServeSuccessJSON(map[string]interface{}{
  83. "standard": standard,
  84. })
  85. }
  86. func (this *CommonApiController) GetConfigurationlist() {
  87. limit, _ := this.GetInt64("limit")
  88. page, _ := this.GetInt64("page")
  89. adminUser := this.GetAdminUserInfo()
  90. orgid := adminUser.CurrentOrgId
  91. configurationlist, total, err := service.GetConfigurationlist(orgid, limit, page)
  92. if err != nil {
  93. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  94. return
  95. }
  96. this.ServeSuccessJSON(map[string]interface{}{
  97. "configurationlist": configurationlist,
  98. "total": total,
  99. })
  100. }
  101. func (this *CommonApiController) GetConfigurationDetail() {
  102. id, _ := this.GetInt64("id")
  103. fmt.Println("id是", id)
  104. detail, err := service.GetConfigurationDetail(id)
  105. if err != nil {
  106. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  107. return
  108. }
  109. this.ServeSuccessJSON(map[string]interface{}{
  110. "configurationdetail": detail,
  111. })
  112. }
  113. func (this *CommonApiController) GetAllInspectionminor() {
  114. minor, err := service.GetAllInspectionMinor(0)
  115. if err != nil {
  116. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  117. return
  118. }
  119. this.ServeSuccessJSON(map[string]interface{}{
  120. "minor": minor,
  121. })
  122. }
  123. func (this *CommonApiController) UpdateConfiguration() {
  124. id, _ := this.GetInt64("id")
  125. fmt.Println("id", id)
  126. dataBody := make(map[string]interface{}, 0)
  127. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  128. fmt.Println("err", err)
  129. inspectionmajor := int64(dataBody["inspectionMajor"].(float64))
  130. fmt.Println("大项", inspectionmajor)
  131. inspectionMinor := int64(dataBody["inspectionMinor"].(float64))
  132. fmt.Println("小项", inspectionMinor)
  133. minrange := dataBody["min_range"].(string)
  134. fmt.Println("minragne", minrange)
  135. largerange := dataBody["large_range"].(string)
  136. fmt.Println("largerange", largerange)
  137. sort := int64(dataBody["sort"].(float64))
  138. standard := models.XtQualityControlStandard{
  139. InspectionMajor: inspectionmajor,
  140. InspectionMinor: inspectionMinor,
  141. MinRange: minrange,
  142. LargeRange: largerange,
  143. Sort: sort,
  144. }
  145. err = service.UpdarteConfiguration(&standard, id)
  146. if err != nil {
  147. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  148. return
  149. }
  150. this.ServeSuccessJSON(map[string]interface{}{
  151. "standard": standard,
  152. })
  153. }
  154. func (this *CommonApiController) DeleteConfiguration() {
  155. id, _ := this.GetInt64("id")
  156. err := service.DeleteConfiguration(id)
  157. if err != nil {
  158. this.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除失败")
  159. return
  160. }
  161. returnData := make(map[string]interface{}, 0)
  162. returnData["msg"] = "ok"
  163. this.ServeSuccessJSON(returnData)
  164. return
  165. }
  166. func (this *CommonApiController) GetAllInspectiondata() {
  167. adminUser := this.GetAdminUserInfo()
  168. orgid := adminUser.CurrentOrgId
  169. //查询该机构是否有数据
  170. _, errcode := service.GetAllInspectionData(orgid)
  171. if errcode == gorm.ErrRecordNotFound {
  172. inspection, err := service.GetAllInspectiondatatwo(0)
  173. if err != nil {
  174. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  175. return
  176. }
  177. this.ServeSuccessJSON(map[string]interface{}{
  178. "inspection": inspection,
  179. })
  180. } else if errcode == nil {
  181. inspection, err := service.GetAllInspectiondatatwo(orgid)
  182. if err != nil {
  183. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  184. return
  185. }
  186. this.ServeSuccessJSON(map[string]interface{}{
  187. "inspection": inspection,
  188. })
  189. }
  190. }
  191. func (this *CommonApiController) SaveCheckConfiguration() {
  192. dataBody := make(map[string]interface{}, 0)
  193. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  194. fmt.Println("err", err)
  195. adminUser := this.GetAdminUserInfo()
  196. orgid := adminUser.CurrentOrgId
  197. inspectionmajor := int64(dataBody["inspectionMajor"].(float64))
  198. fmt.Println("大项", inspectionmajor)
  199. frequency := dataBody["frequency"].(string)
  200. fmt.Println("凭次", frequency)
  201. sort := dataBody["sort"].(string)
  202. sorts, err := strconv.ParseInt(sort, 10, 64)
  203. fmt.Println("sort", sort)
  204. _, errcode := service.GetInspectionMajorById(inspectionmajor)
  205. if errcode == gorm.ErrRecordNotFound {
  206. configuration := models.XtCheckConfiguration{
  207. InspectionMajor: inspectionmajor,
  208. InspectionFrequency: frequency,
  209. Sort: sorts,
  210. UserOrgId: orgid,
  211. Status: 1,
  212. CreatedTime: time.Now().Unix(),
  213. }
  214. err := service.CreateCheckConfiguration(&configuration)
  215. if err != nil {
  216. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  217. return
  218. }
  219. this.ServeSuccessJSON(map[string]interface{}{
  220. "configuration": configuration,
  221. })
  222. } else if errcode == nil {
  223. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  224. return
  225. }
  226. }
  227. func (this *CommonApiController) GetAllCheckList() {
  228. adminUser := this.GetAdminUserInfo()
  229. orgid := adminUser.CurrentOrgId
  230. fmt.Println("org", orgid)
  231. page, _ := this.GetInt64("page")
  232. limit, _ := this.GetInt64("limit")
  233. checkList, total, err := service.GetAllCheckList(orgid, page, limit)
  234. if err != nil {
  235. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  236. return
  237. }
  238. this.ServeSuccessJSON(map[string]interface{}{
  239. "checklist": checkList,
  240. "total": total,
  241. })
  242. }
  243. func (this *CommonApiController) GetCheckdetail() {
  244. id, _ := this.GetInt64("id")
  245. detail, err := service.GetCheckDetail(id)
  246. if err != nil {
  247. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  248. return
  249. }
  250. this.ServeSuccessJSON(map[string]interface{}{
  251. "checkdetail": detail,
  252. })
  253. }
  254. func (this *CommonApiController) UpdateCheck() {
  255. id, _ := this.GetInt64("id")
  256. fmt.Println(id)
  257. dataBody := make(map[string]interface{}, 0)
  258. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  259. fmt.Println(err)
  260. inspectionmajor := int64(dataBody["inspectionMajor"].(float64))
  261. fmt.Println("大项", inspectionmajor)
  262. frequency := dataBody["frequency"].(string)
  263. fmt.Println("凭次", frequency)
  264. sort := int64(dataBody["sort"].(float64))
  265. fmt.Println("sort", sort)
  266. Inspection, err := service.GetInspectionMajorById(inspectionmajor)
  267. if Inspection.ID > 0 && Inspection.ID != id {
  268. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  269. return
  270. }
  271. configuration := models.XtCheckConfiguration{
  272. InspectionMajor: inspectionmajor,
  273. InspectionFrequency: frequency,
  274. Sort: sort,
  275. }
  276. err = service.UpdateCheck(&configuration, id)
  277. if err != nil {
  278. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  279. return
  280. }
  281. this.ServeSuccessJSON(map[string]interface{}{
  282. "configuration": configuration,
  283. })
  284. }
  285. func (this *CommonApiController) DeleteCheck() {
  286. id, _ := this.GetInt64("id")
  287. err := service.DeleteCheck(id)
  288. if err != nil {
  289. this.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除失败")
  290. return
  291. }
  292. returnData := make(map[string]interface{}, 0)
  293. returnData["msg"] = "ok"
  294. this.ServeSuccessJSON(returnData)
  295. return
  296. }