123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- package controllers
- import (
- "encoding/json"
- "eta/eta_api/models"
- "eta/eta_api/services"
- "eta/eta_api/utils"
- "fmt"
- "github.com/shopspring/decimal"
- "html"
- "strconv"
- "strings"
- "time"
- )
- // BusinessConfController 商家配置
- type BusinessConfController struct {
- BaseAuthController
- }
- type BusinessConfOpenController struct {
- BaseCommonController
- }
- // Save
- // @Title 保存配置
- // @Description 保存配置
- // @Param request body map[string]interface{} true "type json string"
- // @Success 200 Ret=200 操作成功
- // @router /save [post]
- func (this *BusinessConfController) Save() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req map[string]interface{}
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- // 获取配置信息
- confOb := new(models.BusinessConf)
- list, e := confOb.GetItemsByCondition("", make([]interface{}, 0), []string{}, "")
- if e != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "获取配置列表失败, Err: " + e.Error()
- return
- }
- confMap := make(map[string]*models.BusinessConf)
- for _, c := range list {
- confMap[c.ConfKey] = c
- }
- openApprove := ""
- approveType := ""
- // 根据配置类型取值
- updates := make([]models.BusinessConfUpdate, 0)
- for k, v := range req {
- // 过滤掉表中没有的key
- conf := confMap[k]
- if conf == nil {
- continue
- }
- switch conf.ValType {
- case 1: // 字符串
- str, ok := v.(string)
- if !ok {
- continue
- }
- str = strings.TrimSpace(str)
- if conf.Necessary == 1 && str == "" {
- br.Msg = conf.Remark + "不可为空"
- return
- }
- updates = append(updates, models.BusinessConfUpdate{
- ConfKey: k,
- ConfVal: str,
- })
- // 取出审批参数
- if k == models.BusinessConfIsReportApprove {
- openApprove = str
- }
- if k == models.BusinessConfReportApproveType {
- // 打开审批默认为内部审批方式
- if openApprove == "true" && str == "" {
- str = models.BusinessConfReportApproveTypeEta
- }
- approveType = str
- }
- case 2: // 数值
- vDeci, err := decimal.NewFromString(fmt.Sprint(v))
- if err != nil {
- continue
- }
- num, _ := vDeci.Float64()
- if conf.Necessary == 1 && num <= 0 {
- br.Msg = conf.Remark + "不可为空"
- return
- }
- val := strconv.FormatFloat(num, 'f', 0, 64)
- updates = append(updates, models.BusinessConfUpdate{
- ConfKey: k,
- ConfVal: val,
- })
- case 3: // 字符串数组
- arr, ok := v.([]interface{})
- if !ok {
- continue
- }
- if conf.Necessary == 1 && len(arr) == 0 {
- br.Msg = conf.Remark + "不可为空"
- return
- }
- strArr := make([]string, 0)
- for _, a := range arr {
- if s, ok2 := a.(string); ok2 {
- strArr = append(strArr, s)
- }
- }
- val := strings.Join(strArr, ",")
- updates = append(updates, models.BusinessConfUpdate{
- ConfKey: k,
- ConfVal: val,
- })
- case 4: // 富文本
- content, ok := v.(string)
- if !ok {
- continue
- }
- content = strings.TrimSpace(content)
- if conf.Necessary == 1 && content == "" {
- br.Msg = conf.Remark + "不可为空"
- return
- }
- content = html.EscapeString(content)
- updates = append(updates, models.BusinessConfUpdate{
- ConfKey: k,
- ConfVal: content,
- })
- }
- }
- // 校验报告审批是否可以切换
- confOpenApprove := confMap[models.BusinessConfIsReportApprove]
- confApproveType := confMap[models.BusinessConfReportApproveType]
- if confOpenApprove != nil && confApproveType != nil {
- // 仅校验有审批->无审批, 或是有审批->切换审批方式的情况
- if openApprove == "false" && confOpenApprove.ConfVal == "true" || (openApprove == "true" && openApprove == confOpenApprove.ConfVal && confApproveType.ConfVal != approveType) {
- ok, e := services.CheckCloseReportApproveConf()
- if e != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "校验是否可以关闭报告审批失败, Err: " + e.Error()
- return
- }
- if !ok {
- br.Msg = "当前有未走完流程的报告,请走完流程后再做变更"
- return
- }
- }
- // 审批设置切换对未发布/待提交报告状态的重置
- needReset := false
- changeType := ""
- if openApprove == "false" && confOpenApprove.ConfVal == "true" {
- needReset = true
- }
- if openApprove == "true" && confOpenApprove.ConfVal == "false" {
- needReset = true
- changeType = approveType
- }
- if openApprove == "true" && openApprove == confOpenApprove.ConfVal && confApproveType.ConfVal != approveType {
- needReset = true
- changeType = approveType
- }
- if needReset {
- go services.ConfigChangeResetReportState(changeType)
- }
- }
- if len(updates) > 0 {
- if e = models.UpdateBusinessConfMulti(updates); e != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "保存商家配置失败, Err: " + e.Error()
- return
- }
- }
- // 操作日志
- go func() {
- b, e := json.Marshal(req)
- if e != nil {
- return
- }
- recordOb := new(models.BusinessConfOperationRecord)
- recordOb.SysUserId = sysUser.AdminId
- recordOb.SysRealName = sysUser.RealName
- recordOb.Content = string(b)
- recordOb.CreateTime = time.Now().Local()
- _ = recordOb.Create()
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // Fetch
- // @Title 获取配置
- // @Description 获取配置
- // @Success 200 Ret=200 获取成功
- // @router /fetch [get]
- func (this *BusinessConfController) Fetch() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- list, e := models.GetBusinessConf()
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取商家配置失败, Err: " + e.Error()
- return
- }
- br.Data = list
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
- // CodeEncrypt
- // @Title 商家编码加密
- // @Description 商家编码加密
- // @Success 200 Ret=200 获取成功
- // @router /code_encrypt [get]
- func (this *BusinessConfOpenController) CodeEncrypt() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- res := ""
- if utils.BusinessCode != "" {
- res = utils.MD5(fmt.Sprintf("%s%s", utils.BusinessCode, utils.BusinessCodeSalt))
- }
- br.Data = res
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
- // SingleSave
- // @Title 保存单项配置
- // @Description 保存配置
- // @Param request body map[string]interface{} true "type json string"
- // @Success 200 Ret=200 操作成功
- // @router /single/save [post]
- func (this *BusinessConfController) SingleSave() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.BusinessConfSingleSaveReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- if req.ConfKey == "" {
- br.Msg = "参数异常!"
- return
- }
- // 设置白名单,只有白名单里的配置才允许保存
- writeList := []string{models.BusinessConfEdbStopRefreshRule}
- if !utils.InArrayByStr(writeList, req.ConfKey) {
- br.Msg = "不支持该项配置"
- return
- }
- // 获取配置信息
- confOb, e := models.GetBusinessConfByKey(req.ConfKey)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "配置不存在"
- return
- }
- br.Msg = "保存失败"
- br.ErrMsg = "获取配置失败, Err: " + e.Error()
- return
- }
- switch confOb.ValType {
- case 1: // 字符串
- req.ConfVal = strings.TrimSpace(req.ConfVal)
- if confOb.Necessary == 1 && req.ConfVal == "" {
- br.Msg = confOb.Remark + "不可为空"
- return
- }
- }
- if req.ConfKey == models.BusinessConfEdbStopRefreshRule {
- //将json转为结构体
- rule := new(models.EdbStopRefreshRule)
- err := json.Unmarshal([]byte(req.ConfVal), rule)
- if err != nil {
- br.Msg = confOb.Remark + "格式错误"
- return
- }
- if rule.IsOpen == 1 && (rule.EdbStopDays == 0 || rule.BaseIndexStopDays == 0) {
- br.Msg = confOb.Remark + "天数不可设置为0"
- return
- }
- }
- if confOb.ConfVal != req.ConfVal {
- confOb.ConfVal = req.ConfVal
- if e = confOb.Update([]string{"ConfVal"}); e != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "保存商家配置失败, Err: " + e.Error()
- return
- }
- // 操作日志
- go func() {
- b, e := json.Marshal(req)
- if e != nil {
- return
- }
- recordOb := new(models.BusinessConfOperationRecord)
- recordOb.SysUserId = sysUser.AdminId
- recordOb.SysRealName = sysUser.RealName
- recordOb.Content = string(b)
- recordOb.CreateTime = time.Now().Local()
- _ = recordOb.Create()
- }()
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // GetSingle
- // @Title 获取单项配置
- // @Description 保存配置
- // @Param request body map[string]interface{} true "type json string"
- // @Success 200 Ret=200 操作成功
- // @router /single [get]
- func (this *BusinessConfController) GetSingle() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- confKey := this.GetString("ConfKey")
- if confKey == "" {
- br.Msg = "参数异常!"
- return
- }
- // 设置白名单,只有白名单里的配置才允许保存
- writeList := []string{models.BusinessConfEdbStopRefreshRule}
- if !utils.InArrayByStr(writeList, confKey) {
- br.Msg = "不支持该项配置"
- return
- }
- // 获取配置信息
- confOb, e := models.GetBusinessConfByKey(confKey)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "配置不存在"
- return
- }
- br.Msg = "保存失败"
- br.ErrMsg = "获取配置失败, Err: " + e.Error()
- return
- }
- resp := models.BusinessConfSingleResp{ConfVal: confOb.ConfVal}
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- br.Data = resp
- }
|