variety.go 7.3 KB

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