base.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. func baseGetDictDataByType(ctx context.Context, typ string) ([]interface{}, error) {
  138. srv := micro_srv.InitMicroSrvClient("Dict", "micro_srv.auth")
  139. defer srv.Close()
  140. resp := &comm_def.CommonMsg{}
  141. err := srv.Call(ctx, "GetDictDataByType", GetDictReq{
  142. DictType: typ,
  143. }, resp)
  144. if err != nil {
  145. return nil, fmt.Errorf("获取字典 %s %s", typ, err.Error())
  146. }
  147. fmt.Println(resp.Data)
  148. data := resp.Data.(map[string]interface{})["Values"].([]interface{})
  149. fmt.Println(data)
  150. return data, nil
  151. }
  152. func GetDictDataByType(ctx context.Context, typ string) (map[string]string, error) {
  153. data, err := baseGetDictDataByType(ctx, typ)
  154. if err != nil {
  155. return nil, err
  156. }
  157. res := map[string]string{}
  158. for _, i := range data {
  159. info := i.(map[string]interface{})
  160. res[info["DictValue"].(string)] = info["DictLabel"].(string)
  161. }
  162. return res, nil
  163. }
  164. // 有序
  165. func GetDictDataTreeByType(ctx context.Context, typ string) (*gmap.ListMap, error) {
  166. data, err := baseGetDictDataByType(ctx, typ)
  167. if err != nil {
  168. return nil, err
  169. }
  170. res := gmap.NewListMap(true)
  171. for _, i := range data {
  172. info := i.(map[string]interface{})
  173. res.Set(info["DictValue"], info["DictLabel"])
  174. }
  175. return res, nil
  176. }
  177. func StringSlicecontains(s []string, ele string) bool {
  178. for _, i := range s {
  179. if i == ele {
  180. return true
  181. }
  182. }
  183. return false
  184. }
  185. // CreateSystemMessage 创建系统消息
  186. func CreateSystemMessage(msg g.MapStrStr) error {
  187. srv := micro_srv.InitMicroSrvClient("SystemMessage", "micro_srv.auth")
  188. defer srv.Close()
  189. resp := &comm_def.CommonMsg{}
  190. tenant := g.Config().GetString("micro_srv.tenant")
  191. ctx := context.WithValue(context.TODO(), share.ReqMetaDataKey, map[string]string{"tenant": tenant})
  192. err := srv.Call(ctx, "Create", msg, resp)
  193. if err != nil {
  194. g.Log().Error(err)
  195. return myerrors.MicroCallError("系统创建消息失败")
  196. }
  197. fmt.Println(resp.Data)
  198. return nil
  199. }
  200. type DeptIdReq struct {
  201. DeptId int `json:"dept_id,omitempty"`
  202. Include bool `json:"include,omitempty"`
  203. }
  204. // GetUsersByDept 根据部门获取用户
  205. func GetUsersByDept(ctx context.Context, req *DeptIdReq) (map[string]int, error) {
  206. srv := micro_srv.InitMicroSrvClient("User", "micro_srv.auth")
  207. defer srv.Close()
  208. resp := &comm_def.CommonMsg{}
  209. err := srv.Call(ctx, "GetUserByDept", req, resp)
  210. if err != nil {
  211. return nil, myerrors.MicroCallError("获取部门下用户失败")
  212. }
  213. if resp.Data == nil {
  214. return nil, myerrors.TipsError("部门不存在")
  215. }
  216. fmt.Println(resp.Data)
  217. data := resp.Data.([]interface{})
  218. fmt.Println(data)
  219. res := map[string]int{}
  220. for _, i := range data {
  221. info := i.(map[string]interface{})
  222. res[info["NickName"].(string)] = gconv.Int(info["Id"])
  223. }
  224. return res, nil
  225. }
  226. type SysUserSearchReq struct {
  227. KeyWords string `json:"keyWords"`
  228. DeptId int `json:"deptId"` //部门id
  229. DeptIds []int //所属部门id数据
  230. Phone string `json:"phone"`
  231. Status string `json:"status"`
  232. Roles []string `json:"roles"`
  233. Posts []string `json:"posts"`
  234. Groups []string `json:"groups"`
  235. request.PageReq
  236. }
  237. // GetUsersByRoleCode 根据角色编码获取用户
  238. func GetUsersByRoleCode(ctx context.Context, roleCode []string, pageSize ...int) (map[string]int, error) {
  239. srv := micro_srv.InitMicroSrvClient("User", "micro_srv.auth")
  240. defer srv.Close()
  241. resp := &comm_def.CommonMsg{}
  242. req := &SysUserSearchReq{Roles: roleCode}
  243. if len(pageSize) > 0 {
  244. req.PageSize = pageSize[0]
  245. }
  246. err := srv.Call(ctx, "GetList", req, resp)
  247. if err != nil {
  248. return nil, myerrors.MicroCallError("根据角色编码获取用户失败")
  249. }
  250. if resp.Data == nil {
  251. return nil, myerrors.TipsError("用户不存在")
  252. }
  253. data := resp.Data.(map[string]interface{})
  254. list := data["list"].([]interface{})
  255. res := map[string]int{}
  256. for _, i := range list {
  257. info := i.(map[string]interface{})
  258. res[info["NickName"].(string)] = gconv.Int(info["Id"])
  259. }
  260. return res, nil
  261. }
  262. //跟进发送邮件消息
  263. func GSendMail(msg g.MapStrStr) error {
  264. srv := micro_srv.InitMicroSrvClient("Message", "micro_srv.auth")
  265. defer srv.Close()
  266. resp := &comm_def.CommonMsg{}
  267. tenant := g.Config().GetString("micro_srv.tenant")
  268. ctx := context.WithValue(context.TODO(), share.ReqMetaDataKey, map[string]string{"tenant": tenant})
  269. err := srv.Call(ctx, "SendMail", msg, resp)
  270. if err != nil {
  271. g.Log().Error(err)
  272. return myerrors.MicroCallError("项目未跟进发送邮件提醒失败")
  273. }
  274. fmt.Println(resp.Data)
  275. return nil
  276. }