base.go 9.6 KB

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