business_conf.go 10 KB

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