base.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. package service
  2. import (
  3. "context"
  4. "dashoo.cn/opms_libary/myerrors"
  5. "dashoo.cn/opms_libary/request"
  6. "fmt"
  7. "github.com/gogf/gf/container/gmap"
  8. "github.com/gogf/gf/frame/g"
  9. "github.com/gogf/gf/util/gconv"
  10. "github.com/smallnest/rpcx/share"
  11. "log"
  12. "reflect"
  13. "dashoo.cn/common_definition/comm_def"
  14. "dashoo.cn/opms_libary/micro_srv"
  15. "github.com/gogf/gf/database/gdb"
  16. "github.com/gogf/gf/os/gtime"
  17. )
  18. var (
  19. // DingTalkSpaceId 钉钉 空间Id。
  20. DingTalkSpaceId = "21077726250"
  21. // CommonUpdateFieldEx UpdateFieldEx 更新过滤字段
  22. CommonUpdateFieldEx = []interface{}{"created_by", "created_name", "created_time"}
  23. UpdateFieldEx = []interface{}{"id", "created_by", "created_name", "created_time"}
  24. )
  25. func Sequence(db gdb.DB, name string) (string, error) {
  26. v, err := db.GetValue("select `nextval`( ? );", name)
  27. if err != nil {
  28. return "", err
  29. }
  30. return v.String(), nil
  31. }
  32. // SetCreatedInfo 插入数据库时设置创建信息
  33. func SetCreatedInfo(entry interface{}, id int, name string) {
  34. v := reflect.ValueOf(entry)
  35. t := reflect.TypeOf(entry)
  36. if t.Kind() == reflect.Map {
  37. data := entry.(map[string]interface{})
  38. data["created_by"] = id
  39. data["created_name"] = name
  40. data["created_time"] = gtime.Now()
  41. return
  42. }
  43. if t.Kind() == reflect.Ptr {
  44. t = t.Elem()
  45. v = v.Elem()
  46. }
  47. if t.Kind() == reflect.Slice {
  48. }
  49. if t.Kind() != reflect.Struct {
  50. log.Println("Check type error not Struct")
  51. return
  52. }
  53. for i := 0; i < t.NumField(); i++ {
  54. fieldName := t.Field(i).Name
  55. if tag, ok := t.Field(i).Tag.Lookup("orm"); ok {
  56. switch tag {
  57. case "created_by":
  58. v.FieldByName(fieldName).Set(reflect.ValueOf(id))
  59. case "created_name":
  60. v.FieldByName(fieldName).Set(reflect.ValueOf(name))
  61. case "created_time":
  62. v.FieldByName(fieldName).Set(reflect.ValueOf(gtime.Now()))
  63. }
  64. }
  65. }
  66. }
  67. // SetUpdatedInfo 插入数据库时设置修改信息
  68. func SetUpdatedInfo(entry interface{}, id int, name string) {
  69. v := reflect.ValueOf(entry)
  70. t := reflect.TypeOf(entry)
  71. if t.Kind() == reflect.Map {
  72. data := entry.(map[string]interface{})
  73. data["updated_by"] = id
  74. data["updated_name"] = name
  75. data["updated_time"] = gtime.Now()
  76. return
  77. }
  78. if t.Kind() == reflect.Ptr {
  79. t = t.Elem()
  80. v = v.Elem()
  81. }
  82. if t.Kind() != reflect.Struct {
  83. log.Println("Check type error not Struct")
  84. return
  85. }
  86. for i := 0; i < t.NumField(); i++ {
  87. fieldName := t.Field(i).Name
  88. if tag, ok := t.Field(i).Tag.Lookup("orm"); ok {
  89. switch tag {
  90. case "updated_by":
  91. v.FieldByName(fieldName).Set(reflect.ValueOf(id))
  92. case "updated_name":
  93. v.FieldByName(fieldName).Set(reflect.ValueOf(name))
  94. case "updated_time":
  95. v.FieldByName(fieldName).Set(reflect.ValueOf(gtime.Now()))
  96. }
  97. }
  98. }
  99. }
  100. // Div 数字转字母
  101. func Div(Num int) string {
  102. var (
  103. Str string = ""
  104. k int
  105. temp []int //保存转化后每一位数据的值,然后通过索引的方式匹配A-Z
  106. )
  107. //用来匹配的字符A-Z
  108. Slice := []string{"", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
  109. "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
  110. if Num > 26 { //数据大于26需要进行拆分
  111. for {
  112. k = Num % 26 //从个位开始拆分,如果求余为0,说明末尾为26,也就是Z,如果是转化为26进制数,则末尾是可以为0的,这里必须为A-Z中的一个
  113. if k == 0 {
  114. temp = append(temp, 26)
  115. k = 26
  116. } else {
  117. temp = append(temp, k)
  118. }
  119. Num = (Num - k) / 26 //减去Num最后一位数的值,因为已经记录在temp中
  120. if Num <= 26 { //小于等于26直接进行匹配,不需要进行数据拆分
  121. temp = append(temp, Num)
  122. break
  123. }
  124. }
  125. } else {
  126. return Slice[Num]
  127. }
  128. for _, value := range temp {
  129. Str = Slice[value] + Str //因为数据切分后存储顺序是反的,所以Str要放在后面
  130. }
  131. return Str
  132. }
  133. type GetDictReq struct {
  134. DictType string `p:"dictType" v:"required#字典类型不能为空"`
  135. DefaultValue string `p:"defaultValue"`
  136. }
  137. type GetDictLabelByTypeAndValueReq struct {
  138. DictType string `p:"dictType"` //字典类型
  139. DictValue string `p:"dictValue"` //字典标签
  140. }
  141. func baseGetDictDataByType(ctx context.Context, typ string) ([]interface{}, error) {
  142. srv := micro_srv.InitMicroSrvClient("Dict", "micro_srv.auth")
  143. defer srv.Close()
  144. resp := &comm_def.CommonMsg{}
  145. err := srv.Call(ctx, "GetDictDataByType", GetDictReq{
  146. DictType: typ,
  147. }, resp)
  148. if err != nil {
  149. return nil, fmt.Errorf("获取字典 %s %s", typ, err.Error())
  150. }
  151. fmt.Println(resp.Data)
  152. data := resp.Data.(map[string]interface{})["Values"].([]interface{})
  153. fmt.Println(data)
  154. return data, nil
  155. }
  156. func GetDictDataByType(ctx context.Context, typ string) (map[string]string, error) {
  157. data, err := baseGetDictDataByType(ctx, typ)
  158. if err != nil {
  159. return nil, err
  160. }
  161. res := map[string]string{}
  162. for _, i := range data {
  163. info := i.(map[string]interface{})
  164. res[info["DictValue"].(string)] = info["DictLabel"].(string)
  165. }
  166. return res, nil
  167. }
  168. // 有序
  169. func GetDictDataTreeByType(ctx context.Context, typ string) (*gmap.ListMap, error) {
  170. data, err := baseGetDictDataByType(ctx, typ)
  171. if err != nil {
  172. return nil, err
  173. }
  174. res := gmap.NewListMap(true)
  175. for _, i := range data {
  176. info := i.(map[string]interface{})
  177. res.Set(info["DictValue"], info["DictLabel"])
  178. }
  179. return res, nil
  180. }
  181. // 根据字典类型和字典值获取字典明细名称
  182. func GetDictLabelByTypeAndValue(ctx context.Context, dictType, value string) (string, error) {
  183. srv := micro_srv.InitMicroSrvClient("Dict", "micro_srv.auth")
  184. defer srv.Close()
  185. resp := &comm_def.CommonMsg{}
  186. err := srv.Call(ctx, "GetDictLabelByTypeAndValue", GetDictLabelByTypeAndValueReq{
  187. DictType: dictType,
  188. DictValue: value,
  189. }, resp)
  190. if err != nil {
  191. g.Log().Error(err)
  192. return value, nil
  193. }
  194. return gconv.String(resp.Data), nil
  195. }
  196. func StringSlicecontains(s []string, ele string) bool {
  197. for _, i := range s {
  198. if i == ele {
  199. return true
  200. }
  201. }
  202. return false
  203. }
  204. // CreateSystemMessage 创建系统消息
  205. func CreateSystemMessage(msg g.MapStrStr) error {
  206. srv := micro_srv.InitMicroSrvClient("SystemMessage", "micro_srv.auth")
  207. defer srv.Close()
  208. resp := &comm_def.CommonMsg{}
  209. tenant := g.Config().GetString("micro_srv.tenant")
  210. ctx := context.WithValue(context.TODO(), share.ReqMetaDataKey, map[string]string{"tenant": tenant})
  211. err := srv.Call(ctx, "Create", msg, resp)
  212. if err != nil {
  213. g.Log().Error(err)
  214. return myerrors.MicroCallError("系统创建消息失败")
  215. }
  216. fmt.Println(resp.Data)
  217. return nil
  218. }
  219. type DeptIdReq struct {
  220. DeptId int `json:"dept_id,omitempty"`
  221. Include bool `json:"include,omitempty"`
  222. }
  223. // GetUsersByDept 根据部门获取用户
  224. func GetUsersByDept(ctx context.Context, req *DeptIdReq) (map[string]int, error) {
  225. srv := micro_srv.InitMicroSrvClient("User", "micro_srv.auth")
  226. defer srv.Close()
  227. resp := &comm_def.CommonMsg{}
  228. err := srv.Call(ctx, "GetUserByDept", req, resp)
  229. if err != nil {
  230. return nil, myerrors.MicroCallError("获取部门下用户失败")
  231. }
  232. if resp.Data == nil {
  233. return nil, myerrors.TipsError("部门不存在")
  234. }
  235. fmt.Println(resp.Data)
  236. data := resp.Data.([]interface{})
  237. fmt.Println(data)
  238. res := map[string]int{}
  239. for _, i := range data {
  240. info := i.(map[string]interface{})
  241. res[info["NickName"].(string)] = gconv.Int(info["Id"])
  242. }
  243. return res, nil
  244. }
  245. type SysUserSearchReq struct {
  246. KeyWords string `json:"keyWords"`
  247. DeptId int `json:"deptId"` //部门id
  248. DeptIds []int //所属部门id数据
  249. Phone string `json:"phone"`
  250. Status string `json:"status"`
  251. Roles []string `json:"roles"`
  252. Posts []string `json:"posts"`
  253. Groups []string `json:"groups"`
  254. request.PageReq
  255. }
  256. // GetUsersByRoleCode 根据角色编码获取用户
  257. func GetUsersByRoleCode(ctx context.Context, roleCode []string, pageSize ...int) (map[string]int, error) {
  258. srv := micro_srv.InitMicroSrvClient("User", "micro_srv.auth")
  259. defer srv.Close()
  260. resp := &comm_def.CommonMsg{}
  261. req := &SysUserSearchReq{Roles: roleCode, Status: "10"}
  262. if len(pageSize) > 0 {
  263. req.PageSize = pageSize[0]
  264. }
  265. err := srv.Call(ctx, "GetList", req, resp)
  266. if err != nil {
  267. return nil, myerrors.MicroCallError("根据角色编码获取用户失败")
  268. }
  269. if resp.Data == nil {
  270. return nil, myerrors.TipsError("用户不存在")
  271. }
  272. data := resp.Data.(map[string]interface{})
  273. list := data["list"].([]interface{})
  274. res := map[string]int{}
  275. for _, i := range list {
  276. info := i.(map[string]interface{})
  277. res[info["NickName"].(string)] = gconv.Int(info["Id"])
  278. }
  279. return res, nil
  280. }
  281. // 获取用户权限
  282. func GetUserDataScope(userId int) (g.Map, error) {
  283. srv := micro_srv.InitMicroSrvClient("User", "micro_srv.auth")
  284. defer srv.Close()
  285. req := &comm_def.IdReq{Id: int64(userId)}
  286. resp := &comm_def.CommonMsg{}
  287. tenant := g.Config().GetString("micro_srv.tenant")
  288. ctx := context.WithValue(context.TODO(), share.ReqMetaDataKey, map[string]string{"tenant": tenant})
  289. err := srv.Call(ctx, "GetDataScope", req, resp)
  290. if err != nil || resp.Data == nil {
  291. g.Log().Error(err)
  292. return nil, myerrors.MicroCallError("获取用户权限失败")
  293. }
  294. return resp.Data.(g.Map), nil
  295. }
  296. // 跟进发送邮件消息
  297. func GSendMail(msg g.MapStrStr) error {
  298. srv := micro_srv.InitMicroSrvClient("Message", "micro_srv.auth")
  299. defer srv.Close()
  300. resp := &comm_def.CommonMsg{}
  301. tenant := g.Config().GetString("micro_srv.tenant")
  302. ctx := context.WithValue(context.TODO(), share.ReqMetaDataKey, map[string]string{"tenant": tenant})
  303. err := srv.Call(ctx, "SendMail", msg, resp)
  304. if err != nil {
  305. g.Log().Error(err)
  306. return myerrors.MicroCallError("项目未跟进发送邮件提醒失败")
  307. }
  308. fmt.Println(resp.Data)
  309. return nil
  310. }