business_conf.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. "html"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. // BusinessConfController 商家配置
  14. type BusinessConfController struct {
  15. BaseAuthController
  16. }
  17. type BusinessConfOpenController struct {
  18. BaseCommonController
  19. }
  20. // Save
  21. // @Title 保存配置
  22. // @Description 保存配置
  23. // @Param request body map[string]interface{} true "type json string"
  24. // @Success 200 Ret=200 操作成功
  25. // @router /save [post]
  26. func (this *BusinessConfController) Save() {
  27. br := new(models.BaseResponse).Init()
  28. defer func() {
  29. if br.ErrMsg == "" {
  30. br.IsSendEmail = false
  31. }
  32. this.Data["json"] = br
  33. this.ServeJSON()
  34. }()
  35. sysUser := this.SysUser
  36. if sysUser == nil {
  37. br.Msg = "请登录"
  38. br.ErrMsg = "请登录,SysUser Is Empty"
  39. br.Ret = 408
  40. return
  41. }
  42. var req map[string]interface{}
  43. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  44. br.Msg = "参数解析异常!"
  45. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  46. return
  47. }
  48. // 获取配置信息
  49. confOb := new(models.BusinessConf)
  50. list, e := confOb.GetItemsByCondition("", make([]interface{}, 0), []string{}, "")
  51. if e != nil {
  52. br.Msg = "保存失败"
  53. br.ErrMsg = "获取配置列表失败, Err: " + e.Error()
  54. return
  55. }
  56. confMap := make(map[string]*models.BusinessConf)
  57. for _, c := range list {
  58. confMap[c.ConfKey] = c
  59. }
  60. openApprove := ""
  61. approveType := ""
  62. // 根据配置类型取值
  63. updates := make([]models.BusinessConfUpdate, 0)
  64. for k, v := range req {
  65. // 过滤掉表中没有的key
  66. conf := confMap[k]
  67. if conf == nil {
  68. continue
  69. }
  70. switch conf.ValType {
  71. case 1: // 字符串
  72. str, ok := v.(string)
  73. if !ok {
  74. continue
  75. }
  76. str = strings.TrimSpace(str)
  77. if conf.Necessary == 1 && str == "" {
  78. br.Msg = conf.Remark + "不可为空"
  79. return
  80. }
  81. updates = append(updates, models.BusinessConfUpdate{
  82. ConfKey: k,
  83. ConfVal: str,
  84. })
  85. // 取出审批参数
  86. if k == models.BusinessConfIsReportApprove {
  87. openApprove = str
  88. }
  89. if k == models.BusinessConfReportApproveType {
  90. // 打开审批默认为内部审批方式
  91. if openApprove == "true" && str == "" {
  92. str = models.BusinessConfReportApproveTypeEta
  93. }
  94. approveType = str
  95. }
  96. case 2: // 数值
  97. num, ok := v.(float64)
  98. if !ok {
  99. continue
  100. }
  101. if conf.Necessary == 1 && num <= 0 {
  102. br.Msg = conf.Remark + "不可为空"
  103. return
  104. }
  105. val := strconv.FormatFloat(num, 'f', 0, 64)
  106. updates = append(updates, models.BusinessConfUpdate{
  107. ConfKey: k,
  108. ConfVal: val,
  109. })
  110. case 3: // 字符串数组
  111. arr, ok := v.([]interface{})
  112. if !ok {
  113. continue
  114. }
  115. if conf.Necessary == 1 && len(arr) == 0 {
  116. br.Msg = conf.Remark + "不可为空"
  117. return
  118. }
  119. strArr := make([]string, 0)
  120. for _, a := range arr {
  121. if s, ok2 := a.(string); ok2 {
  122. strArr = append(strArr, s)
  123. }
  124. }
  125. val := strings.Join(strArr, ",")
  126. updates = append(updates, models.BusinessConfUpdate{
  127. ConfKey: k,
  128. ConfVal: val,
  129. })
  130. case 4: // 富文本
  131. content, ok := v.(string)
  132. if !ok {
  133. continue
  134. }
  135. content = strings.TrimSpace(content)
  136. if conf.Necessary == 1 && content == "" {
  137. br.Msg = conf.Remark + "不可为空"
  138. return
  139. }
  140. content = html.EscapeString(content)
  141. updates = append(updates, models.BusinessConfUpdate{
  142. ConfKey: k,
  143. ConfVal: content,
  144. })
  145. }
  146. }
  147. // 校验报告审批是否可以切换
  148. confOpenApprove := confMap[models.BusinessConfIsReportApprove]
  149. confApproveType := confMap[models.BusinessConfReportApproveType]
  150. if confOpenApprove != nil && confApproveType != nil {
  151. // 仅校验有审批->无审批, 或是有审批->切换审批方式的情况
  152. if openApprove == "false" && confOpenApprove.ConfVal == "true" || (openApprove == "true" && openApprove == confOpenApprove.ConfVal && confApproveType.ConfVal != approveType) {
  153. ok, e := services.CheckCloseReportApproveConf()
  154. if e != nil {
  155. br.Msg = "保存失败"
  156. br.ErrMsg = "校验是否可以关闭报告审批失败, Err: " + e.Error()
  157. return
  158. }
  159. if !ok {
  160. br.Msg = "当前有未走完流程的报告,请走完流程后再做变更"
  161. return
  162. }
  163. }
  164. // 审批设置切换对未发布/待提交报告状态的重置
  165. needReset := false
  166. changeType := ""
  167. if openApprove == "false" && confOpenApprove.ConfVal == "true" {
  168. needReset = true
  169. }
  170. if openApprove == "true" && confOpenApprove.ConfVal == "false" {
  171. needReset = true
  172. changeType = approveType
  173. }
  174. if openApprove == "true" && openApprove == confOpenApprove.ConfVal && confApproveType.ConfVal != approveType {
  175. needReset = true
  176. changeType = approveType
  177. }
  178. if needReset {
  179. go services.ConfigChangeResetReportState(changeType)
  180. }
  181. }
  182. if len(updates) > 0 {
  183. if e = models.UpdateBusinessConfMulti(updates); e != nil {
  184. br.Msg = "保存失败"
  185. br.ErrMsg = "保存商家配置失败, Err: " + e.Error()
  186. return
  187. }
  188. }
  189. // 操作日志
  190. go func() {
  191. b, e := json.Marshal(req)
  192. if e != nil {
  193. return
  194. }
  195. recordOb := new(models.BusinessConfOperationRecord)
  196. recordOb.SysUserId = sysUser.AdminId
  197. recordOb.SysRealName = sysUser.RealName
  198. recordOb.Content = string(b)
  199. recordOb.CreateTime = time.Now().Local()
  200. _ = recordOb.Create()
  201. }()
  202. br.Ret = 200
  203. br.Success = true
  204. br.Msg = "操作成功"
  205. }
  206. // Fetch
  207. // @Title 获取配置
  208. // @Description 获取配置
  209. // @Success 200 Ret=200 获取成功
  210. // @router /fetch [get]
  211. func (this *BusinessConfController) Fetch() {
  212. br := new(models.BaseResponse).Init()
  213. defer func() {
  214. if br.ErrMsg == "" {
  215. br.IsSendEmail = false
  216. }
  217. this.Data["json"] = br
  218. this.ServeJSON()
  219. }()
  220. sysUser := this.SysUser
  221. if sysUser == nil {
  222. br.Msg = "请登录"
  223. br.ErrMsg = "请登录,SysUser Is Empty"
  224. br.Ret = 408
  225. return
  226. }
  227. list, e := models.GetBusinessConf()
  228. if e != nil {
  229. br.Msg = "获取失败"
  230. br.ErrMsg = "获取商家配置失败, Err: " + e.Error()
  231. return
  232. }
  233. br.Data = list
  234. br.Ret = 200
  235. br.Success = true
  236. br.Msg = "获取成功"
  237. }
  238. // CodeEncrypt
  239. // @Title 商家编码加密
  240. // @Description 商家编码加密
  241. // @Success 200 Ret=200 获取成功
  242. // @router /code_encrypt [get]
  243. func (this *BusinessConfOpenController) CodeEncrypt() {
  244. br := new(models.BaseResponse).Init()
  245. defer func() {
  246. if br.ErrMsg == "" {
  247. br.IsSendEmail = false
  248. }
  249. this.Data["json"] = br
  250. this.ServeJSON()
  251. }()
  252. res := ""
  253. if utils.BusinessCode != "" {
  254. res = utils.MD5(fmt.Sprintf("%s%s", utils.BusinessCode, utils.BusinessCodeSalt))
  255. }
  256. br.Data = res
  257. br.Ret = 200
  258. br.Success = true
  259. br.Msg = "获取成功"
  260. }