business_conf.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. go func() {
  193. b, e := json.Marshal(req)
  194. if e != nil {
  195. return
  196. }
  197. recordOb := new(models.BusinessConfOperationRecord)
  198. recordOb.SysUserId = sysUser.AdminId
  199. recordOb.SysRealName = sysUser.RealName
  200. recordOb.Content = string(b)
  201. recordOb.CreateTime = time.Now().Local()
  202. _ = recordOb.Create()
  203. }()
  204. br.Ret = 200
  205. br.Success = true
  206. br.Msg = "操作成功"
  207. }
  208. // Fetch
  209. // @Title 获取配置
  210. // @Description 获取配置
  211. // @Success 200 Ret=200 获取成功
  212. // @router /fetch [get]
  213. func (this *BusinessConfController) Fetch() {
  214. br := new(models.BaseResponse).Init()
  215. defer func() {
  216. if br.ErrMsg == "" {
  217. br.IsSendEmail = false
  218. }
  219. this.Data["json"] = br
  220. this.ServeJSON()
  221. }()
  222. sysUser := this.SysUser
  223. if sysUser == nil {
  224. br.Msg = "请登录"
  225. br.ErrMsg = "请登录,SysUser Is Empty"
  226. br.Ret = 408
  227. return
  228. }
  229. list, e := models.GetBusinessConf()
  230. if e != nil {
  231. br.Msg = "获取失败"
  232. br.ErrMsg = "获取商家配置失败, Err: " + e.Error()
  233. return
  234. }
  235. br.Data = list
  236. br.Ret = 200
  237. br.Success = true
  238. br.Msg = "获取成功"
  239. }
  240. // CodeEncrypt
  241. // @Title 商家编码加密
  242. // @Description 商家编码加密
  243. // @Success 200 Ret=200 获取成功
  244. // @router /code_encrypt [get]
  245. func (this *BusinessConfOpenController) CodeEncrypt() {
  246. br := new(models.BaseResponse).Init()
  247. defer func() {
  248. if br.ErrMsg == "" {
  249. br.IsSendEmail = false
  250. }
  251. this.Data["json"] = br
  252. this.ServeJSON()
  253. }()
  254. res := ""
  255. if utils.BusinessCode != "" {
  256. res = utils.MD5(fmt.Sprintf("%s%s", utils.BusinessCode, utils.BusinessCodeSalt))
  257. }
  258. br.Data = res
  259. br.Ret = 200
  260. br.Success = true
  261. br.Msg = "获取成功"
  262. }
  263. // SingleSave
  264. // @Title 保存单项配置
  265. // @Description 保存配置
  266. // @Param request body map[string]interface{} true "type json string"
  267. // @Success 200 Ret=200 操作成功
  268. // @router /single/save [post]
  269. func (this *BusinessConfController) SingleSave() {
  270. br := new(models.BaseResponse).Init()
  271. defer func() {
  272. if br.ErrMsg == "" {
  273. br.IsSendEmail = false
  274. }
  275. this.Data["json"] = br
  276. this.ServeJSON()
  277. }()
  278. sysUser := this.SysUser
  279. if sysUser == nil {
  280. br.Msg = "请登录"
  281. br.ErrMsg = "请登录,SysUser Is Empty"
  282. br.Ret = 408
  283. return
  284. }
  285. var req models.BusinessConfSingleSaveReq
  286. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  287. br.Msg = "参数解析异常!"
  288. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  289. return
  290. }
  291. if req.ConfKey == "" {
  292. br.Msg = "参数异常!"
  293. return
  294. }
  295. // 设置白名单,只有白名单里的配置才允许保存
  296. writeList := []string{models.BusinessConfEdbStopRefreshRule}
  297. if !utils.InArrayByStr(writeList, req.ConfKey) {
  298. br.Msg = "不支持该项配置"
  299. return
  300. }
  301. // 获取配置信息
  302. confOb, e := models.GetBusinessConfByKey(req.ConfKey)
  303. if e != nil {
  304. if e.Error() == utils.ErrNoRow() {
  305. br.Msg = "配置不存在"
  306. return
  307. }
  308. br.Msg = "保存失败"
  309. br.ErrMsg = "获取配置失败, Err: " + e.Error()
  310. return
  311. }
  312. switch confOb.ValType {
  313. case 1: // 字符串
  314. req.ConfVal = strings.TrimSpace(req.ConfVal)
  315. if confOb.Necessary == 1 && req.ConfVal == "" {
  316. br.Msg = confOb.Remark + "不可为空"
  317. return
  318. }
  319. }
  320. if req.ConfKey == models.BusinessConfEdbStopRefreshRule {
  321. //将json转为结构体
  322. rule := new(models.EdbStopRefreshRule)
  323. err := json.Unmarshal([]byte(req.ConfVal), rule)
  324. if err != nil {
  325. br.Msg = confOb.Remark + "格式错误"
  326. return
  327. }
  328. if rule.IsOpen == 1 && (rule.EdbStopDays == 0 || rule.BaseIndexStopDays == 0) {
  329. br.Msg = confOb.Remark + "天数不可设置为0"
  330. return
  331. }
  332. }
  333. if confOb.ConfVal != req.ConfVal {
  334. confOb.ConfVal = req.ConfVal
  335. if e = confOb.Update([]string{"ConfVal"}); e != nil {
  336. br.Msg = "保存失败"
  337. br.ErrMsg = "保存商家配置失败, Err: " + e.Error()
  338. return
  339. }
  340. // 操作日志
  341. go func() {
  342. b, e := json.Marshal(req)
  343. if e != nil {
  344. return
  345. }
  346. recordOb := new(models.BusinessConfOperationRecord)
  347. recordOb.SysUserId = sysUser.AdminId
  348. recordOb.SysRealName = sysUser.RealName
  349. recordOb.Content = string(b)
  350. recordOb.CreateTime = time.Now().Local()
  351. _ = recordOb.Create()
  352. }()
  353. }
  354. br.Ret = 200
  355. br.Success = true
  356. br.Msg = "操作成功"
  357. }
  358. // GetSingle
  359. // @Title 获取单项配置
  360. // @Description 保存配置
  361. // @Param request body map[string]interface{} true "type json string"
  362. // @Success 200 Ret=200 操作成功
  363. // @router /single [get]
  364. func (this *BusinessConfController) GetSingle() {
  365. br := new(models.BaseResponse).Init()
  366. defer func() {
  367. if br.ErrMsg == "" {
  368. br.IsSendEmail = false
  369. }
  370. this.Data["json"] = br
  371. this.ServeJSON()
  372. }()
  373. sysUser := this.SysUser
  374. if sysUser == nil {
  375. br.Msg = "请登录"
  376. br.ErrMsg = "请登录,SysUser Is Empty"
  377. br.Ret = 408
  378. return
  379. }
  380. confKey := this.GetString("ConfKey")
  381. if confKey == "" {
  382. br.Msg = "参数异常!"
  383. return
  384. }
  385. // 设置白名单,只有白名单里的配置才允许保存
  386. writeList := []string{models.BusinessConfEdbStopRefreshRule}
  387. if !utils.InArrayByStr(writeList, confKey) {
  388. br.Msg = "不支持该项配置"
  389. return
  390. }
  391. // 获取配置信息
  392. confOb, e := models.GetBusinessConfByKey(confKey)
  393. if e != nil {
  394. if e.Error() == utils.ErrNoRow() {
  395. br.Msg = "配置不存在"
  396. return
  397. }
  398. br.Msg = "保存失败"
  399. br.ErrMsg = "获取配置失败, Err: " + e.Error()
  400. return
  401. }
  402. resp := models.BusinessConfSingleResp{ConfVal: confOb.ConfVal}
  403. br.Ret = 200
  404. br.Success = true
  405. br.Msg = "操作成功"
  406. br.Data = resp
  407. }