business_conf.go 9.7 KB

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