business_conf.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/services"
  6. biapprove "eta_gn/eta_api/services/bi_approve"
  7. "eta_gn/eta_api/utils"
  8. "fmt"
  9. "html"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/shopspring/decimal"
  14. )
  15. // BusinessConfController 商家配置
  16. type BusinessConfController struct {
  17. BaseAuthController
  18. }
  19. type BusinessConfOpenController struct {
  20. BaseCommonController
  21. }
  22. // Save
  23. // @Title 保存配置
  24. // @Description 保存配置
  25. // @Param request body map[string]interface{} true "type json string"
  26. // @Success 200 Ret=200 操作成功
  27. // @router /save [post]
  28. func (this *BusinessConfController) Save() {
  29. br := new(models.BaseResponse).Init()
  30. defer func() {
  31. if br.ErrMsg == "" {
  32. br.IsSendEmail = false
  33. }
  34. this.Data["json"] = br
  35. this.ServeJSON()
  36. }()
  37. sysUser := this.SysUser
  38. if sysUser == nil {
  39. br.Msg = "请登录"
  40. br.ErrMsg = "请登录,SysUser Is Empty"
  41. br.Ret = 408
  42. return
  43. }
  44. var req map[string]interface{}
  45. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  46. br.Msg = "参数解析异常!"
  47. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  48. return
  49. }
  50. // 获取配置信息
  51. confOb := new(models.BusinessConf)
  52. list, e := confOb.GetItemsByCondition("", make([]interface{}, 0), []string{}, "")
  53. if e != nil {
  54. br.Msg = "保存失败"
  55. br.ErrMsg = "获取配置列表失败, Err: " + e.Error()
  56. return
  57. }
  58. confMap := make(map[string]*models.BusinessConf)
  59. for _, c := range list {
  60. confMap[c.ConfKey] = c
  61. }
  62. openApprove := ""
  63. approveType := ""
  64. curBiAprrove := ""
  65. // 根据配置类型取值
  66. updates := make([]models.BusinessConfUpdate, 0)
  67. for k, v := range req {
  68. // 过滤掉表中没有的key
  69. conf := confMap[k]
  70. if conf == nil {
  71. continue
  72. }
  73. switch conf.ValType {
  74. case 1: // 字符串
  75. str, ok := v.(string)
  76. if !ok {
  77. continue
  78. }
  79. str = strings.TrimSpace(str)
  80. if conf.Necessary == 1 && str == "" {
  81. br.Msg = conf.Remark + "不可为空"
  82. return
  83. }
  84. updates = append(updates, models.BusinessConfUpdate{
  85. ConfKey: k,
  86. ConfVal: str,
  87. })
  88. // 取出审批参数
  89. if k == models.BusinessConfIsReportApprove {
  90. openApprove = str
  91. }
  92. if k == models.BusinessConfReportApproveType {
  93. // 打开审批默认为内部审批方式
  94. if openApprove == "true" && str == "" {
  95. str = models.BusinessConfReportApproveTypeEta
  96. }
  97. approveType = str
  98. }
  99. if k == models.BusinessConfIsBIApprove {
  100. curBiAprrove = str
  101. }
  102. case 2: // 数值
  103. vDeci, err := decimal.NewFromString(fmt.Sprint(v))
  104. if err != nil {
  105. continue
  106. }
  107. num, _ := vDeci.Float64()
  108. if conf.Necessary == 1 && num <= 0 {
  109. br.Msg = conf.Remark + "不可为空"
  110. return
  111. }
  112. val := strconv.FormatFloat(num, 'f', 0, 64)
  113. updates = append(updates, models.BusinessConfUpdate{
  114. ConfKey: k,
  115. ConfVal: val,
  116. })
  117. case 3: // 字符串数组
  118. arr, ok := v.([]interface{})
  119. if !ok {
  120. continue
  121. }
  122. if conf.Necessary == 1 && len(arr) == 0 {
  123. br.Msg = conf.Remark + "不可为空"
  124. return
  125. }
  126. strArr := make([]string, 0)
  127. for _, a := range arr {
  128. if s, ok2 := a.(string); ok2 {
  129. strArr = append(strArr, s)
  130. }
  131. }
  132. val := strings.Join(strArr, ",")
  133. updates = append(updates, models.BusinessConfUpdate{
  134. ConfKey: k,
  135. ConfVal: val,
  136. })
  137. case 4: // 富文本
  138. content, ok := v.(string)
  139. if !ok {
  140. continue
  141. }
  142. content = strings.TrimSpace(content)
  143. if conf.Necessary == 1 && content == "" {
  144. br.Msg = conf.Remark + "不可为空"
  145. return
  146. }
  147. content = html.EscapeString(content)
  148. updates = append(updates, models.BusinessConfUpdate{
  149. ConfKey: k,
  150. ConfVal: content,
  151. })
  152. }
  153. }
  154. // 校验报告审批是否可以切换
  155. confOpenApprove := confMap[models.BusinessConfIsReportApprove]
  156. confApproveType := confMap[models.BusinessConfReportApproveType]
  157. if confOpenApprove != nil && confApproveType != nil {
  158. // 仅校验有审批->无审批, 或是有审批->切换审批方式的情况
  159. if openApprove == "false" && confOpenApprove.ConfVal == "true" || (openApprove == "true" && openApprove == confOpenApprove.ConfVal && confApproveType.ConfVal != approveType) {
  160. ok, e := services.CheckCloseReportApproveConf()
  161. if e != nil {
  162. br.Msg = "保存失败"
  163. br.ErrMsg = "校验是否可以关闭报告审批失败, Err: " + e.Error()
  164. return
  165. }
  166. if !ok {
  167. br.Msg = "当前有未走完流程的报告,请走完流程后再做变更"
  168. return
  169. }
  170. }
  171. // 审批设置切换对未发布/待提交报告状态的重置
  172. needReset := false
  173. changeType := ""
  174. if openApprove == "false" && confOpenApprove.ConfVal == "true" {
  175. needReset = true
  176. }
  177. if openApprove == "true" && confOpenApprove.ConfVal == "false" {
  178. needReset = true
  179. changeType = approveType
  180. }
  181. if openApprove == "true" && openApprove == confOpenApprove.ConfVal && confApproveType.ConfVal != approveType {
  182. needReset = true
  183. changeType = approveType
  184. }
  185. if needReset {
  186. go services.ConfigChangeResetReportState(changeType)
  187. }
  188. }
  189. if curBiAprrove != "" {
  190. oldBiApprove := confMap[models.BusinessConfIsBIApprove]
  191. if oldBiApprove != nil && oldBiApprove.ConfVal != curBiAprrove && oldBiApprove.ConfVal == "true" && curBiAprrove == "false" {
  192. ok, err := biapprove.CheckHasApprovingBi()
  193. if err != nil {
  194. br.Msg = "保存失败"
  195. br.ErrMsg = "校验是否有正在审批的BI失败, Err: " + err.Error()
  196. return
  197. }
  198. if ok {
  199. br.Msg = "当前有未走完流程的看板,请走完流程后再做变更"
  200. return
  201. }
  202. }
  203. }
  204. if len(updates) > 0 {
  205. if e = models.UpdateBusinessConfMulti(updates); e != nil {
  206. br.Msg = "保存失败"
  207. br.ErrMsg = "保存商家配置失败, Err: " + e.Error()
  208. return
  209. }
  210. }
  211. // 删除研报声明缓存
  212. disclaimerCacheKey := "hongze_yb:business_conf:disclaimer"
  213. _ = utils.Rc.Delete(disclaimerCacheKey)
  214. // 操作日志
  215. go func() {
  216. b, e := json.Marshal(req)
  217. if e != nil {
  218. return
  219. }
  220. recordOb := new(models.BusinessConfOperationRecord)
  221. recordOb.SysUserId = sysUser.AdminId
  222. recordOb.SysRealName = sysUser.RealName
  223. recordOb.Content = string(b)
  224. recordOb.CreateTime = time.Now().Local()
  225. _ = recordOb.Create()
  226. }()
  227. br.Ret = 200
  228. br.Success = true
  229. br.Msg = "操作成功"
  230. }
  231. // Fetch
  232. // @Title 获取配置
  233. // @Description 获取配置
  234. // @Success 200 Ret=200 获取成功
  235. // @router /fetch [get]
  236. func (this *BusinessConfController) Fetch() {
  237. br := new(models.BaseResponse).Init()
  238. defer func() {
  239. if br.ErrMsg == "" {
  240. br.IsSendEmail = false
  241. }
  242. this.Data["json"] = br
  243. this.ServeJSON()
  244. }()
  245. sysUser := this.SysUser
  246. if sysUser == nil {
  247. br.Msg = "请登录"
  248. br.ErrMsg = "请登录,SysUser Is Empty"
  249. br.Ret = 408
  250. return
  251. }
  252. list, e := models.GetBusinessConf()
  253. if e != nil {
  254. br.Msg = "获取失败"
  255. br.ErrMsg = "获取商家配置失败, Err: " + e.Error()
  256. return
  257. }
  258. br.Data = list
  259. br.Ret = 200
  260. br.Success = true
  261. br.Msg = "获取成功"
  262. }
  263. // CodeEncrypt
  264. // @Title 商家编码加密
  265. // @Description 商家编码加密
  266. // @Success 200 Ret=200 获取成功
  267. // @router /code_encrypt [get]
  268. func (this *BusinessConfOpenController) CodeEncrypt() {
  269. br := new(models.BaseResponse).Init()
  270. defer func() {
  271. if br.ErrMsg == "" {
  272. br.IsSendEmail = false
  273. }
  274. this.Data["json"] = br
  275. this.ServeJSON()
  276. }()
  277. res := ""
  278. if utils.BusinessCode != "" {
  279. res = utils.MD5(fmt.Sprintf("%s%s", utils.BusinessCode, utils.BusinessCodeSalt))
  280. }
  281. br.Data = res
  282. br.Ret = 200
  283. br.Success = true
  284. br.Msg = "获取成功"
  285. }
  286. // SingleSave
  287. // @Title 保存单项配置
  288. // @Description 保存配置
  289. // @Param request body map[string]interface{} true "type json string"
  290. // @Success 200 Ret=200 操作成功
  291. // @router /single/save [post]
  292. func (this *BusinessConfController) SingleSave() {
  293. br := new(models.BaseResponse).Init()
  294. defer func() {
  295. if br.ErrMsg == "" {
  296. br.IsSendEmail = false
  297. }
  298. this.Data["json"] = br
  299. this.ServeJSON()
  300. }()
  301. sysUser := this.SysUser
  302. if sysUser == nil {
  303. br.Msg = "请登录"
  304. br.ErrMsg = "请登录,SysUser Is Empty"
  305. br.Ret = 408
  306. return
  307. }
  308. var req models.BusinessConfSingleSaveReq
  309. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  310. br.Msg = "参数解析异常!"
  311. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  312. return
  313. }
  314. if req.ConfKey == "" {
  315. br.Msg = "参数异常!"
  316. return
  317. }
  318. // 设置白名单,只有白名单里的配置才允许保存
  319. writeList := []string{models.BusinessConfEdbStopRefreshRule}
  320. if !utils.InArrayByStr(writeList, req.ConfKey) {
  321. br.Msg = "不支持该项配置"
  322. return
  323. }
  324. // 获取配置信息
  325. confOb, e := models.GetBusinessConfByKey(req.ConfKey)
  326. if e != nil {
  327. if utils.IsErrNoRow(e) {
  328. br.Msg = "配置不存在"
  329. return
  330. }
  331. br.Msg = "保存失败"
  332. br.ErrMsg = "获取配置失败, Err: " + e.Error()
  333. return
  334. }
  335. switch confOb.ValType {
  336. case 1: // 字符串
  337. req.ConfVal = strings.TrimSpace(req.ConfVal)
  338. if confOb.Necessary == 1 && req.ConfVal == "" {
  339. br.Msg = confOb.Remark + "不可为空"
  340. return
  341. }
  342. }
  343. if req.ConfKey == models.BusinessConfEdbStopRefreshRule {
  344. //将json转为结构体
  345. rule := new(models.EdbStopRefreshRule)
  346. err := json.Unmarshal([]byte(req.ConfVal), rule)
  347. if err != nil {
  348. br.Msg = confOb.Remark + "格式错误"
  349. return
  350. }
  351. if rule.IsOpen == 1 && (rule.EdbStopDays == 0 || rule.BaseIndexStopDays == 0) {
  352. br.Msg = confOb.Remark + "天数不可设置为0"
  353. return
  354. }
  355. }
  356. if confOb.ConfVal != req.ConfVal {
  357. confOb.ConfVal = req.ConfVal
  358. if e = confOb.Update([]string{"ConfVal"}); e != nil {
  359. br.Msg = "保存失败"
  360. br.ErrMsg = "保存商家配置失败, Err: " + e.Error()
  361. return
  362. }
  363. // 操作日志
  364. go func() {
  365. b, e := json.Marshal(req)
  366. if e != nil {
  367. return
  368. }
  369. recordOb := new(models.BusinessConfOperationRecord)
  370. recordOb.SysUserId = sysUser.AdminId
  371. recordOb.SysRealName = sysUser.RealName
  372. recordOb.Content = string(b)
  373. recordOb.CreateTime = time.Now().Local()
  374. _ = recordOb.Create()
  375. }()
  376. }
  377. br.Ret = 200
  378. br.Success = true
  379. br.Msg = "操作成功"
  380. }
  381. // GetSingle
  382. // @Title 获取单项配置
  383. // @Description 保存配置
  384. // @Param request body map[string]interface{} true "type json string"
  385. // @Success 200 Ret=200 操作成功
  386. // @router /single [get]
  387. func (this *BusinessConfController) GetSingle() {
  388. br := new(models.BaseResponse).Init()
  389. defer func() {
  390. if br.ErrMsg == "" {
  391. br.IsSendEmail = false
  392. }
  393. this.Data["json"] = br
  394. this.ServeJSON()
  395. }()
  396. sysUser := this.SysUser
  397. if sysUser == nil {
  398. br.Msg = "请登录"
  399. br.ErrMsg = "请登录,SysUser Is Empty"
  400. br.Ret = 408
  401. return
  402. }
  403. confKey := this.GetString("ConfKey")
  404. if confKey == "" {
  405. br.Msg = "参数异常!"
  406. return
  407. }
  408. // 设置白名单,只有白名单里的配置才允许保存
  409. writeList := []string{models.BusinessConfEdbStopRefreshRule}
  410. if !utils.InArrayByStr(writeList, confKey) {
  411. br.Msg = "不支持该项配置"
  412. return
  413. }
  414. // 获取配置信息
  415. confOb, e := models.GetBusinessConfByKey(confKey)
  416. if e != nil {
  417. if utils.IsErrNoRow(e) {
  418. br.Msg = "配置不存在"
  419. return
  420. }
  421. br.Msg = "保存失败"
  422. br.ErrMsg = "获取配置失败, Err: " + e.Error()
  423. return
  424. }
  425. resp := models.BusinessConfSingleResp{ConfVal: confOb.ConfVal}
  426. br.Ret = 200
  427. br.Success = true
  428. br.Msg = "操作成功"
  429. br.Data = resp
  430. }