variety.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package cross_variety
  2. import (
  3. "encoding/json"
  4. "eta/eta_mobile/controllers"
  5. "eta/eta_mobile/models"
  6. "eta/eta_mobile/models/data_manage/cross_variety"
  7. "eta/eta_mobile/models/data_manage/cross_variety/request"
  8. "eta/eta_mobile/models/data_manage/cross_variety/response"
  9. "eta/eta_mobile/utils"
  10. "github.com/rdlucklib/rdluck_tools/paging"
  11. "time"
  12. )
  13. // VarietyController
  14. // @Description: 品种列表
  15. type VarietyController struct {
  16. controllers.BaseAuthController
  17. }
  18. // Add
  19. // @Title 新增品种
  20. // @Description 新增品种接口
  21. // @Param request body request.AddVarietyReq true "type json string"
  22. // @Success 200 Ret=200 添加成功
  23. // @router /variety/add [post]
  24. func (c *VarietyController) Add() {
  25. br := new(models.BaseResponse).Init()
  26. defer func() {
  27. c.Data["json"] = br
  28. c.ServeJSON()
  29. }()
  30. var req request.AddVarietyReq
  31. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  32. if err != nil {
  33. br.Msg = "参数解析异常!"
  34. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  35. return
  36. }
  37. if req.VarietyName == "" {
  38. br.Msg = "请输入品种名称"
  39. br.IsSendEmail = false
  40. return
  41. }
  42. varietyName := utils.TrimStr(req.VarietyName)
  43. item, err := cross_variety.GetVarietyByName(varietyName)
  44. if err != nil && err.Error() != utils.ErrNoRow() {
  45. br.Msg = "添加失败"
  46. br.ErrMsg = "添加失败,Err:" + err.Error()
  47. return
  48. }
  49. if item != nil {
  50. br.Msg = "添加失败,品种名称不能重复"
  51. br.IsSendEmail = false
  52. return
  53. }
  54. variety := &cross_variety.ChartVariety{
  55. ChartVarietyId: 0,
  56. ChartVarietyName: varietyName,
  57. SysUserId: c.SysUser.AdminId,
  58. SysUserRealName: c.SysUser.RealName,
  59. ModifyTime: time.Now(),
  60. CreateTime: time.Now(),
  61. }
  62. err = cross_variety.AddVariety(variety)
  63. if err != nil {
  64. br.Msg = "添加品种失败"
  65. br.ErrMsg = "添加品种失败,Err:" + err.Error()
  66. return
  67. }
  68. br.Ret = 200
  69. br.Msg = "添加成功"
  70. br.IsAddLog = true
  71. br.Success = true
  72. }
  73. // Edit
  74. // @Title 编辑品种接口
  75. // @Description 编辑品种接口
  76. // @Param request body request.EditVarietyReq true "type json string"
  77. // @Success 200 Ret=200 修改成功
  78. // @router /variety/edit [post]
  79. func (c *VarietyController) Edit() {
  80. br := new(models.BaseResponse).Init()
  81. defer func() {
  82. c.Data["json"] = br
  83. c.ServeJSON()
  84. }()
  85. var req request.EditVarietyReq
  86. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  87. if err != nil {
  88. br.Msg = "参数解析异常!"
  89. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  90. return
  91. }
  92. if req.ChartVarietyId <= 0 {
  93. br.Msg = "请选择品种"
  94. br.IsSendEmail = false
  95. return
  96. }
  97. if req.VarietyName == "" {
  98. br.Msg = "请输入品种名称"
  99. br.IsSendEmail = false
  100. return
  101. }
  102. varietyName := utils.TrimStr(req.VarietyName)
  103. item, err := cross_variety.GetVarietyByName(varietyName)
  104. if err != nil && err.Error() != utils.ErrNoRow() {
  105. br.Msg = "添加失败"
  106. br.ErrMsg = "添加失败,Err:" + err.Error()
  107. return
  108. }
  109. if item != nil && item.ChartVarietyId != req.ChartVarietyId {
  110. br.Msg = "添加失败,品种名称不能重复"
  111. br.IsSendEmail = false
  112. return
  113. }
  114. // 获取品种详情
  115. varietyInfo, err := cross_variety.GetVarietyById(req.ChartVarietyId)
  116. if err != nil {
  117. br.Msg = "查询品种失败"
  118. br.ErrMsg = "查询品种失败;ERR:" + err.Error()
  119. return
  120. }
  121. // 编辑
  122. varietyInfo.ChartVarietyName = varietyName
  123. varietyInfo.ModifyTime = time.Now()
  124. err = varietyInfo.Update([]string{"ChartVarietyName", "ModifyTime"})
  125. if err != nil {
  126. br.Msg = "修改品种失败"
  127. br.ErrMsg = "修改品种失败,Err:" + err.Error()
  128. return
  129. }
  130. br.Ret = 200
  131. br.Msg = "修改成功"
  132. br.IsAddLog = true
  133. br.Success = true
  134. }
  135. // DeleteCheck
  136. // @Title 删除品种检测接口
  137. // @Description 删除品种检测接口
  138. // @Param request body request.DelVarietyReq true "type json string"
  139. // @Success 200 Ret=200 检测成功
  140. // @router /variety/delete/check [post]
  141. func (c *VarietyController) DeleteCheck() {
  142. br := new(models.BaseResponse).Init()
  143. defer func() {
  144. c.Data["json"] = br
  145. c.ServeJSON()
  146. }()
  147. var req request.DelVarietyReq
  148. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  149. if err != nil {
  150. br.Msg = "参数解析异常!"
  151. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  152. return
  153. }
  154. if req.ChartVarietyId <= 0 {
  155. br.Msg = "请选择品种"
  156. br.IsSendEmail = false
  157. return
  158. }
  159. var deleteStatus int
  160. var tipsMsg string
  161. // 获取该品种关联的图表数
  162. count, err := cross_variety.GetCountChartByVarietyId(req.ChartVarietyId)
  163. if err != nil {
  164. br.Msg = "检测异常"
  165. br.ErrMsg = "检测异常,err:" + err.Error()
  166. br.IsSendEmail = false
  167. return
  168. }
  169. if count > 0 {
  170. deleteStatus = 1
  171. tipsMsg = "已关联图表的品种,不允许删除!"
  172. }
  173. if deleteStatus == 0 {
  174. tipsMsg = "可删除,进行删除操作"
  175. }
  176. resp := response.VarietyDeleteCheckResp{
  177. DeleteStatus: deleteStatus,
  178. TipsMsg: tipsMsg,
  179. }
  180. br.Ret = 200
  181. br.Msg = "检测成功"
  182. br.Success = true
  183. br.Data = resp
  184. }
  185. // Delete
  186. // @Title 删除品种
  187. // @Description 删除品种接口
  188. // @Param request body request.DeleteChartClassifyReq true "type json string"
  189. // @Success 200 Ret=200 删除成功
  190. // @router /variety/delete [post]
  191. func (c *VarietyController) Delete() {
  192. br := new(models.BaseResponse).Init()
  193. defer func() {
  194. c.Data["json"] = br
  195. c.ServeJSON()
  196. }()
  197. var req request.DelVarietyReq
  198. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  199. if err != nil {
  200. br.Msg = "参数解析异常!"
  201. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  202. return
  203. }
  204. if req.ChartVarietyId <= 0 {
  205. br.Msg = "请选择品种"
  206. br.IsSendEmail = false
  207. return
  208. }
  209. // 获取该品种关联的图表数
  210. count, err := cross_variety.GetCountChartByVarietyId(req.ChartVarietyId)
  211. if err != nil {
  212. br.Msg = "检测异常"
  213. br.ErrMsg = "检测异常,err:" + err.Error()
  214. br.IsSendEmail = false
  215. return
  216. }
  217. if count > 0 {
  218. br.Msg = "已关联图表的品种,不允许删除!"
  219. br.ErrMsg = "已关联图表的品种,不允许删除!"
  220. br.IsSendEmail = false
  221. return
  222. }
  223. varietyInfo, err := cross_variety.GetVarietyById(req.ChartVarietyId)
  224. if err != nil {
  225. if err.Error() == utils.ErrNoRow() {
  226. br.Msg = "该品种不存在或已删除"
  227. br.IsSendEmail = false
  228. } else {
  229. br.Msg = "删除失败"
  230. br.ErrMsg = "查找品种失败,ERR:" + err.Error()
  231. }
  232. return
  233. }
  234. err = varietyInfo.Delete()
  235. if err != nil {
  236. br.Msg = "删除失败"
  237. br.ErrMsg = "删除品种失败,ERR:" + err.Error()
  238. return
  239. }
  240. br.Ret = 200
  241. br.Msg = "删除成功"
  242. br.Success = true
  243. br.IsAddLog = true
  244. }
  245. // List
  246. // @Title 获取标签中的指标与品种的映射关系
  247. // @Description 获取标签中的指标与品种的映射关系
  248. // @Success 200 Ret=200 保存成功
  249. // @router /variety/list [get]
  250. func (c *VarietyController) List() {
  251. br := new(models.BaseResponse).Init()
  252. defer func() {
  253. c.Data["json"] = br
  254. c.ServeJSON()
  255. }()
  256. // 获取数据
  257. list, err := cross_variety.GetVarietyList()
  258. if err != nil {
  259. br.Msg = "保存失败"
  260. br.ErrMsg = "保存失败,ERR:" + err.Error()
  261. return
  262. }
  263. dataCount := len(list)
  264. page := paging.GetPaging(1, dataCount, dataCount)
  265. resp := response.VarietyListResp{
  266. List: list,
  267. Paging: page,
  268. }
  269. br.Ret = 200
  270. br.Msg = "获取成功"
  271. br.Success = true
  272. br.Data = resp
  273. }