package publicmethod
import (
"bytes"
"crypto/md5"
"crypto/rand"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"key_performance_indicators/middleware/grocerystore"
"key_performance_indicators/middleware/snowflake"
"key_performance_indicators/models/modelshr"
"key_performance_indicators/models/modelsschool"
"key_performance_indicators/models/modelsstorage"
"key_performance_indicators/models/modelssystempermission"
"key_performance_indicators/overall"
"math"
"math/big"
"reflect"
"strconv"
"strings"
"time"
"github.com/mozillazg/go-pinyin"
"gorm.io/gorm"
)
// 全局函数处理
// 编号,纯数字
func TableNumber ( class ... string ) ( number int64 ) {
result , _ := rand . Int ( rand . Reader , big . NewInt ( 8999 ) )
numberTeam := result . Int64 ( ) + 1000
numberStr := fmt . Sprintf ( "%v%v" , time . Now ( ) . Unix ( ) , numberTeam )
if len ( class ) > 0 {
resultLong , _ := rand . Int ( rand . Reader , big . NewInt ( 8999999 ) )
numberTeamLong := resultLong . Int64 ( ) + 1000000
numberStr = fmt . Sprintf ( "%v%v" , time . Now ( ) . Unix ( ) , numberTeamLong )
}
number , _ = strconv . ParseInt ( numberStr , 10 , 64 )
return
}
// 获取UUID
func GetUUid ( workId int64 ) ( uuId int64 ) {
snowflakeId , snowflakeErr := snowflake . NewWorker ( workId )
if snowflakeErr != nil {
uuId = TableNumber ( )
} else {
uuId = snowflakeId . GetId ( )
}
return
}
// 初始化map(泛型)
func MapOut [ T GenericityVariable ] ( ) ( data map [ T ] interface { } ) {
data = make ( map [ T ] interface { } ) //必可不少,分配内存
return data
}
//时间搓转换成日期
/ *
@ timestamp 待转换的时间戳
@ timeType 转换类型
* /
func UnixTimeToDay ( timeStamp int64 , timeType int ) ( dateStr string ) {
timeTemplate := "2006-01-02 15:04:05" //常规类型
switch timeType {
case 1 :
timeTemplate = "2006/01/02 15:04:05"
case 2 :
timeTemplate = "2006/01/02 15:04"
case 3 :
timeTemplate = "2006/01/02 15"
case 4 :
timeTemplate = "2006/01/02"
case 5 :
timeTemplate = "15:04:05"
case 6 :
timeTemplate = "15:04"
case 7 :
timeTemplate = "15"
case 8 :
timeTemplate = "04:05"
case 9 :
timeTemplate = "04"
case 10 :
timeTemplate = "05"
case 11 :
timeTemplate = "2006-01-02 15:04:05"
case 12 :
timeTemplate = "2006-01-02 15:04"
case 13 :
timeTemplate = "2006-01-02 15"
case 14 :
timeTemplate = "2006-01-02"
case 15 :
timeTemplate = "2006-01" //年月
case 16 :
timeTemplate = "2006" //年
case 17 :
timeTemplate = "01" //月
case 18 :
timeTemplate = "02" //日
case 19 : //季度
dayMonth := time . Unix ( timeStamp , 0 ) . Format ( "01" )
datMonthFloat , datMonthFloatErr := strconv . ParseFloat ( dayMonth , 10 )
if datMonthFloatErr == nil {
dateStr = strconv . FormatFloat ( math . Ceil ( datMonthFloat / 3 ) , 'f' , - 1 , 64 )
}
dateStr = "1"
case 20 :
timeTemplate = "20060102"
case 21 :
timeTemplate = "200601"
default :
timeTemplate = "2006-01-02 15:04:05" //常规类型
}
dateStr = time . Unix ( timeStamp , 0 ) . Format ( timeTemplate )
return
}
/ *
日期转时间戳
* /
func DateToTimeStamp ( dataStr string ) ( timeStamp int64 , isTrue bool ) {
isTrue = false
tmp := "2006-01-02 15:04:05"
res , err := time . ParseInLocation ( tmp , dataStr , time . Local )
timeStamp = res . Unix ( )
if err == nil {
isTrue = true
}
return
}
func DateToTimeStampOld ( dataStr string ) ( timeStamp int64 ) {
tmp := "2006-01-02 15:04:05"
res , _ := time . ParseInLocation ( tmp , dataStr , time . Local )
timeStamp = res . Unix ( )
return
}
// 数据库查询翻页
func LimitPage ( page , pageSize int ) ( offSet int ) {
if page < 1 {
page = 1
}
offSet = pageSize * ( page - 1 )
if offSet < 0 {
offSet = 0
}
return
}
// 中文首字母大写
func ChineseFirstWordCapitalize ( wordStr string ) ( firstWord string ) {
pinYinSub := pinyin . NewArgs ( )
rows := pinyin . Pinyin ( wordStr , pinYinSub )
for i := 0 ; i < len ( rows ) ; i ++ {
if len ( rows [ i ] ) != 0 {
str := rows [ i ] [ 0 ]
pi := str [ 0 : 1 ]
firstWord += string ( bytes . ToUpper ( [ ] byte ( pi ) ) )
}
}
return
}
// ZeroFillByStr
// @Description: 字符串补零
// @param str :需要操作的字符串
// @param resultLen 结果字符串的长度
// @param reverse true 为前置补零,false 为后置补零
// @return string
func ZeroFillByStr ( str string , resultLen int , reverse bool ) string {
if len ( str ) > resultLen || resultLen <= 0 {
return str
}
if reverse {
return fmt . Sprintf ( "%0*s" , resultLen , str ) //不足前置补零
}
result := str
for i := 0 ; i < resultLen - len ( str ) ; i ++ {
result += "0"
}
return result
}
/ *
* 加密算法
* /
type Md5Encryption struct {
Code string ` json:"code" `
AppKey string ` json:"appKey" `
}
func ( m * Md5Encryption ) Md5EncryptionAlgorithm ( ) ( md5Val string ) {
if m . AppKey == "" {
m . Md5EncryptionInit ( m . Code )
}
// fmt.Printf("Code ====> %v\n", m.Code)
// fmt.Printf("AppKey ====> %v\n", m.AppKey)
mdNew := md5 . New ( )
mdNew . Write ( [ ] byte ( m . AppKey ) )
keyMd5 := fmt . Sprintf ( "%x" , mdNew . Sum ( nil ) )
// fmt.Printf("Step1:--AppKey-加密->%v\n", keyMd5)
codeNewMd1 := md5 . New ( )
codeNewMd1 . Write ( [ ] byte ( m . Code ) )
codeMd1 := fmt . Sprintf ( "%x" , codeNewMd1 . Sum ( nil ) )
// fmt.Printf("Step2:--CodeString-加密->%v\n", codeMd1)
yiCeng := codeMd1 + keyMd5
// fmt.Printf("Step3:--CodeString+AppKey-->%v\n", yiCeng)
yiCengNew := md5 . New ( )
yiCengNew . Write ( [ ] byte ( yiCeng ) )
yiCengMd5 := fmt . Sprintf ( "%x" , yiCengNew . Sum ( nil ) )
// fmt.Printf("Step4:--one-加密->%v\n", yiCengMd5)
erCeng := yiCengMd5 + m . AppKey
// fmt.Printf("Step5:--one + AppKey->%v\n", erCeng)
// fmt.Printf("AppKey ===2=> %v\n", m.AppKey)
erCengNew := md5 . New ( )
erCengNew . Write ( [ ] byte ( erCeng ) )
md5Val = fmt . Sprintf ( "%x" , erCengNew . Sum ( nil ) )
// fmt.Printf("Step6:--therr--加密-->%v\n", md5Val)
// md5Val = codeMd1
return
}
func ( m * Md5Encryption ) Md5EncryptionAlgorithmFj ( ) ( md5Val string , outlog [ ] string ) {
if m . AppKey == "" {
m . Md5EncryptionInit ( m . Code )
}
// fmt.Printf("Code ====> %v\n", m.Code)
// fmt.Printf("AppKey ====> %v\n", m.AppKey)
outlog = append ( outlog , fmt . Sprintf ( "Code ====> %v" , m . Code ) )
outlog = append ( outlog , fmt . Sprintf ( "AppKey ====> %v" , m . AppKey ) )
mdNew := md5 . New ( )
mdNew . Write ( [ ] byte ( m . AppKey ) )
keyMd5 := fmt . Sprintf ( "%x" , mdNew . Sum ( nil ) )
// fmt.Printf("Step1:--AppKey-加密->%v\n", keyMd5)
outlog = append ( outlog , fmt . Sprintf ( "Step1:--AppKey-加密->%v" , keyMd5 ) )
codeNewMd1 := md5 . New ( )
codeNewMd1 . Write ( [ ] byte ( m . Code ) )
codeMd1 := fmt . Sprintf ( "%x" , codeNewMd1 . Sum ( nil ) )
// fmt.Printf("Step2:--CodeString-加密->%v\n", codeMd1)
outlog = append ( outlog , fmt . Sprintf ( "Step2:--CodeString-加密->%v" , codeMd1 ) )
yiCeng := codeMd1 + keyMd5
// fmt.Printf("Step3:--CodeString+AppKey-->%v\n", yiCeng)
outlog = append ( outlog , fmt . Sprintf ( "Step3:--CodeString+AppKey-->%v" , yiCeng ) )
yiCengNew := md5 . New ( )
yiCengNew . Write ( [ ] byte ( yiCeng ) )
yiCengMd5 := fmt . Sprintf ( "%x" , yiCengNew . Sum ( nil ) )
// fmt.Printf("Step4:--one-加密->%v\n", yiCengMd5)
outlog = append ( outlog , fmt . Sprintf ( "Step4:--one-加密->%v" , yiCengMd5 ) )
erCeng := yiCengMd5 + m . AppKey
// fmt.Printf("Step5:--one + AppKey->%v\n", erCeng)
outlog = append ( outlog , fmt . Sprintf ( "Step5:--one + AppKey->%v" , erCeng ) )
// fmt.Printf("AppKey ===2=> %v\n", m.AppKey)
erCengNew := md5 . New ( )
erCengNew . Write ( [ ] byte ( erCeng ) )
md5Val = fmt . Sprintf ( "%x" , erCengNew . Sum ( nil ) )
// fmt.Printf("Step6:--therr--加密-->%v\n", md5Val)
outlog = append ( outlog , fmt . Sprintf ( "Step6:--therr--加密-->%v" , md5Val ) )
// md5Val = codeMd1
return
}
// 初始化程序
func ( m * Md5Encryption ) Md5EncryptionInit ( code string ) {
m . AppKey = overall . CONSTANT_CONFIG . Appsetup . AppKey
m . Code = code
}
// sha1算法
func Sha1Encryption ( str string ) string {
sha1 := sha1 . New ( )
sha1 . Write ( [ ] byte ( str ) )
return hex . EncodeToString ( sha1 . Sum ( nil ) )
}
//计算当前时间N个月后的时间
/ *
@ timeStamp 当前时间戳
@ futureTime 正数多少时间后 , 负数多少时间前
* /
func GetFutureMonthTime ( timeStamp int64 , futureTime , class int ) ( dateStr int64 ) {
timeUnix := time . Unix ( timeStamp , 0 )
addDate := timeUnix . AddDate ( 0 , 0 , futureTime )
switch class {
case 2 :
addDate = timeUnix . AddDate ( 0 , futureTime , 0 )
case 3 :
addDate = timeUnix . AddDate ( futureTime , 0 , 0 )
default :
addDate = timeUnix . AddDate ( 0 , 0 , futureTime )
}
dateStr = addDate . Unix ( )
return
}
// 判断字符串是否在切片中 泛型
func IsInTrue [ T GenericityVariable ] ( key T , keyAry [ ] T ) ( isTrue bool ) {
isTrue = false
for _ , v := range keyAry {
if v == key {
return true
}
}
return
}
// 获取两个切片得差集
func DifferenceSet [ T GenericityVariable ] ( one , two [ ] T ) [ ] T {
var three [ ] T
temp := map [ T ] struct { } { }
for _ , val := range two {
if _ , ok := temp [ val ] ; ! ok {
temp [ val ] = struct { } { }
}
}
for _ , val := range one {
if _ , ok := temp [ val ] ; ! ok {
three = append ( three , val )
}
}
return three
}
//判断类型转换成
/ *
字符转int
* /
func StringToInt ( val interface { } ) ( int , error ) {
switch val . ( type ) {
case string :
return strconv . Atoi ( val . ( string ) )
case int :
return val . ( int ) , nil
default :
return 0 , nil
}
return 0 , nil
}
/ *
字符转int64
* /
func StringToInt64 ( val interface { } ) ( int64 , error ) {
switch val . ( type ) {
case string :
return strconv . ParseInt ( val . ( string ) , 10 , 64 )
case int :
return int64 ( val . ( int ) ) , nil
case int64 :
return val . ( int64 ) , nil
default :
return 0 , nil
}
return 0 , nil
}
// 获取人员TokenRedis 信息
func GetUserRedisToken ( number string ) ( manCont modelshr . ManCont , err error ) {
redisMyContKey := fmt . Sprintf ( "ScanCode:Authentication:UserCont_%v_%v" , overall . CONSTANT_CONFIG . RedisPrefixStr . Alias , number )
redisClient := grocerystore . RunRedis ( overall . CONSTANT_REDIS5 )
useMap , isTrue := redisClient . HashGetAll ( redisMyContKey )
if isTrue == false {
err . Error ( )
return
}
// fmt.Printf("%v\n", useMap)
err = MapToStruct ( useMap , & manCont , "json" )
return
}
// 获取人员Redis信息
func GetUserRedisCont ( number string ) ( manCont modelshr . ManCont , err error ) {
redisMyContKey := fmt . Sprintf ( "ScanCode:Authentication:UserCont_%v_%v" , overall . CONSTANT_CONFIG . RedisPrefixStr . Alias , number )
redisClient := grocerystore . RunRedis ( overall . CONSTANT_REDIS5 )
useMap , isTrue := redisClient . HashGetAll ( redisMyContKey )
if isTrue == false {
err . Error ( )
return
}
// fmt.Printf("%v\n", useMap)
// var manInfo models.ManCont
err = MapToStruct ( useMap , & manCont , "json" )
// manCont = manInfo
return
}
// 获取人员Redis信息返回指针
func GetUserRedisContPointer ( number string ) ( manCont modelshr . ManCont , err error ) {
redisMyContKey := fmt . Sprintf ( "ScanCode:Authentication:UserCont_%v_%v" , overall . CONSTANT_CONFIG . RedisPrefixStr . Alias , number )
// fmt.Printf("redisMyContKey------------>%v\n", redisMyContKey)
redisClient := grocerystore . RunRedis ( overall . CONSTANT_REDIS5 )
useMap , isTrue := redisClient . HashGetAll ( redisMyContKey )
if isTrue == false {
err . Error ( )
return
}
// fmt.Printf("%v\n", useMap)
// var manInfo models.ManCont
err = MapToStruct ( useMap , & manCont , "json" )
// manCont = manInfo
return
}
/ *
map [ string ] string 转 struct
* /
func MapToStruct ( mapString map [ string ] string , structInterface interface { } , tag string ) ( err error ) {
strType := reflect . TypeOf ( structInterface )
strValue := reflect . ValueOf ( structInterface )
switch strType . Kind ( ) {
case reflect . Ptr :
if strType . Elem ( ) . Kind ( ) != reflect . Struct {
// fmt.Printf("1-->%v\n", strType.Elem().Kind().String())
return errors . New ( "需要*struct类型,却传入*" + strType . Elem ( ) . Kind ( ) . String ( ) + "类型" )
}
default :
// fmt.Printf("2-->%v\n", strType.Kind().String())
return errors . New ( "需要*struct类型,却传入" + strType . Kind ( ) . String ( ) + "类型" )
}
// fmt.Printf("strType--->%v---strValue---->%v\n", strType, strValue)
strTypeElem := strType . Elem ( )
strValueElem := strValue . Elem ( )
strTypeNum := strTypeElem . NumField ( )
// fmt.Printf("strTypeElem--->%v---strValueElem---->%v---------strTypeNum--------->%v\n", strTypeElem, strValueElem, strTypeNum)
for i := 0 ; i < strTypeNum ; i ++ {
fieldName := strTypeElem . Field ( i ) . Name
if tag != "" {
fieldName = strTypeElem . Field ( i ) . Tag . Get ( tag )
}
value , ok := mapString [ fieldName ]
if ! ok {
continue
}
// fmt.Printf("name--->%v---tag---->%v----value---->%v----ok---->%v\n", fieldName, tag, value, ok)
if strValueElem . Field ( i ) . CanSet ( ) {
// fmt.Printf("Type--->%v---value---->%v\n", strValueElem.Field(i).Type(), reflect.TypeOf(value))
valType := fmt . Sprintf ( "%v" , strValueElem . Field ( i ) . Type ( ) )
switch valType {
case "int" :
valueInt , _ := strconv . Atoi ( value )
strValueElem . Field ( i ) . Set ( reflect . ValueOf ( valueInt ) )
case "int32" :
valueInt32 , _ := strconv . ParseInt ( value , 10 , 32 )
strValueElem . Field ( i ) . Set ( reflect . ValueOf ( int32 ( valueInt32 ) ) )
case "int64" :
valueInt64 , _ := strconv . ParseInt ( value , 10 , 64 )
strValueElem . Field ( i ) . Set ( reflect . ValueOf ( valueInt64 ) )
case "float32" :
valuefloat32 , _ := strconv . ParseFloat ( value , 32 )
strValueElem . Field ( i ) . Set ( reflect . ValueOf ( float32 ( valuefloat32 ) ) )
case "float64" :
valuefloat64 , _ := strconv . ParseFloat ( value , 64 )
strValueElem . Field ( i ) . Set ( reflect . ValueOf ( valuefloat64 ) )
case "string" :
strValueElem . Field ( i ) . Set ( reflect . ValueOf ( value ) )
default :
}
} else {
continue
}
}
return
}
// 翻页设置
func PageTurningSettings ( gormDb * gorm . DB , page , pageSize int ) * gorm . DB {
if page < 0 {
page = 1
}
if pageSize < 0 {
pageSize = 10
}
gormDb = gormDb . Limit ( pageSize ) . Offset ( LimitPage ( page , pageSize ) )
return gormDb
}
//浮点数保留小数
/ *
@ value 浮点数值
@ weishu 要保留的小数位数
* /
func DecimalEs ( value float64 , weishu int ) float64 {
switch weishu {
case 3 :
value , _ = strconv . ParseFloat ( fmt . Sprintf ( "%.3f" , value ) , 64 )
case 4 :
value , _ = strconv . ParseFloat ( fmt . Sprintf ( "%.4f" , value ) , 64 )
case 5 :
value , _ = strconv . ParseFloat ( fmt . Sprintf ( "%.5f" , value ) , 64 )
case 6 :
value , _ = strconv . ParseFloat ( fmt . Sprintf ( "%.6f" , value ) , 64 )
default :
value , _ = strconv . ParseFloat ( fmt . Sprintf ( "%.2f" , value ) , 64 )
}
return value
}
// 递归查找
func RecursionOrgLeve ( superior int64 , leve int64 ) ( groupId int64 ) {
if leve == 0 {
leve = 1
}
var orgMap modelshr . OrgContType
err := orgMap . GetCont ( map [ string ] interface { } { "`id`" : superior , "`state`" : 1 } , "`id`" , "`superior`" , "`level`" )
if err != nil {
return
}
if orgMap . Level <= leve {
groupId = orgMap . Id
return
} else {
groupId = RecursionOrgLeve ( orgMap . Superior , leve )
}
return
}
// 获取集团、公司、部门、二级部门、工段
func GetOrgStructure ( orgId int64 ) ( groupId , companyId , departmentId , sunDepartId , workShopId int64 ) {
var orgContTypeInfo modelshr . OrgContType
err := orgContTypeInfo . GetCont ( map [ string ] interface { } { "`id`" : orgId } )
if err != nil {
return
}
switch orgContTypeInfo . Level {
case 1 :
groupId = orgContTypeInfo . Id
case 2 :
groupId = RecursionOrgLeve ( orgContTypeInfo . Superior , 1 )
workShopId = orgContTypeInfo . Id
case 3 :
groupId = RecursionOrgLeve ( orgContTypeInfo . Superior , 2 )
companyId = orgContTypeInfo . Id
case 4 :
groupId = RecursionOrgLeve ( orgContTypeInfo . Superior , 2 )
companyId = RecursionOrgLeve ( orgContTypeInfo . Superior , 3 )
departmentId = orgContTypeInfo . Id
case 5 :
groupId = RecursionOrgLeve ( orgContTypeInfo . Superior , 2 )
companyId = RecursionOrgLeve ( orgContTypeInfo . Superior , 3 )
departmentId = RecursionOrgLeve ( orgContTypeInfo . Superior , 4 )
sunDepartId = orgContTypeInfo . Id
case 6 :
groupId = RecursionOrgLeve ( orgContTypeInfo . Superior , 2 )
companyId = RecursionOrgLeve ( orgContTypeInfo . Superior , 3 )
departmentId = RecursionOrgLeve ( orgContTypeInfo . Superior , 4 )
sunDepartId = RecursionOrgLeve ( orgContTypeInfo . Superior , 5 )
workShopId = orgContTypeInfo . Id
default :
groupId = orgContTypeInfo . Id
companyId = orgContTypeInfo . Id
workShopId = orgContTypeInfo . Id
}
return
}
// 获取指定行政组织id,所有子类
func GetDepartmentSun ( superior int64 , idary [ ] int64 ) ( orgIdAry [ ] int64 ) {
var orgAry [ ] modelshr . AdministrativeOrganization
err := overall . CONSTANT_DB_HR . Model ( & modelshr . AdministrativeOrganization { } ) . Select ( "`id`" ) . Where ( "`state` = 1 AND `superior` = ?" , superior ) . Find ( & orgAry ) . Error
if err != nil {
return
}
for _ , v := range orgAry {
if IsInTrue [ int64 ] ( v . Id , orgIdAry ) == false {
orgIdAry = append ( orgIdAry , v . Id )
GetDepartmentSun ( v . Id , orgIdAry )
} else {
GetDepartmentSun ( v . Id , orgIdAry )
}
}
return
}
// 获取集团或第一实权部门
func GetGroupOrDepartPower ( parentId int64 , departId ... int64 ) int64 {
var orgCont modelshr . AdministrativeOrganization
err := orgCont . GetCont ( map [ string ] interface { } { "id" : parentId } , "id" , "organization_type" , "superior" , "ispower" )
if err != nil {
return parentId
}
if len ( departId ) > 0 {
if orgCont . IsPower == 1 {
return orgCont . Id
}
return GetGroupOrDepartPower ( orgCont . Superior , 1 )
} else {
if orgCont . OrganizationType <= 2 {
return orgCont . Id
}
return GetGroupOrDepartPower ( orgCont . Superior )
}
return orgCont . Id
}
// 时间计算(年、季、月、周、日、时、分、秒)
func ComputingTime ( timeStamp int64 , timeType int ) ( timeDataInt int64 ) {
var timeData string = "1"
switch timeType {
case 1 :
timeData = time . Unix ( timeStamp , 0 ) . Format ( "2006" ) //年
case 2 :
dayMonth := time . Unix ( timeStamp , 0 ) . Format ( "01" ) //季度
datMonthFloat , datMonthFloatErr := strconv . ParseFloat ( dayMonth , 10 )
if datMonthFloatErr == nil {
timeData = strconv . FormatFloat ( math . Ceil ( datMonthFloat / 3 ) , 'f' , - 1 , 64 )
} else {
timeData = "1"
}
case 3 :
timeData = time . Unix ( timeStamp , 0 ) . Format ( "01" ) //月份
case 4 : //当前日期为本月第几周
monthFirstDayStr := DateToTimeStampOld ( UnixTimeToDay ( timeStamp , 15 ) + "-01 00:00:00" ) //获取指定月第一天时间戳
dayTime := time . Unix ( monthFirstDayStr , 0 )
dayOfWeek := int ( dayTime . Weekday ( ) ) //获取本月第一天是周几
if dayOfWeek == 0 {
dayOfWeek = 7
}
dayNumber , dayNumberErr := strconv . ParseInt ( UnixTimeToDay ( timeStamp , 18 ) , 10 , 64 ) //获取今天是几号
if dayNumberErr != nil {
dayNumber = 1
}
daysAndWeeksDiff := dayNumber - ( 8 - int64 ( dayOfWeek ) )
if daysAndWeeksDiff <= 0 {
timeData = "1"
} else {
daysAndWeeksDiffFlot , daysAndWeeksDiffFloatErr := strconv . ParseFloat ( strconv . FormatInt ( daysAndWeeksDiff , 10 ) , 10 )
if daysAndWeeksDiffFloatErr == nil {
daysWeeksDiffVal := math . Ceil ( daysAndWeeksDiffFlot / 7 ) + 1
timeData = strconv . FormatFloat ( daysWeeksDiffVal , 'f' , - 1 , 64 )
} else {
timeData = "1"
}
}
case 5 :
timeData = time . Unix ( timeStamp , 0 ) . Format ( "02" ) //天
case 7 :
timeData = time . Unix ( timeStamp , 0 ) . Format ( "15" ) //时
case 8 :
timeData = time . Unix ( timeStamp , 0 ) . Format ( "04" ) //分
case 9 :
timeData = time . Unix ( timeStamp , 0 ) . Format ( "05" ) //秒
default :
timeData = "0"
}
timeDataInt , timeDataIntErr := strconv . ParseInt ( timeData , 10 , 64 )
if timeDataIntErr != nil {
timeDataInt = 0
return
}
return
}
/ *
获取岗位权限
@ ordid 行政组织
@ postid 岗位
@ system 系统标识
* /
func GetPostPower ( ordid , postid int64 , system string ) ( menuIdAry , menuUrl [ ] string , err error ) {
redisFileEmpowerKey := fmt . Sprintf ( "ScanCode:SystemEmpower:Authorize_%v_%v_%v" , overall . CONSTANT_CONFIG . RedisPrefixStr . Alias , system , postid )
redisClient := grocerystore . RunRedis ( overall . CONSTANT_REDIS5 )
empowerRedisToken , isTrue := redisClient . HashGetAll ( redisFileEmpowerKey )
// fmt.Printf("empowerRedisToken------------->%v---------->isTrue------------->%v---------->redisFileEmpowerKey------------->%v\n", empowerRedisToken, isTrue, redisFileEmpowerKey)
if isTrue == false {
fmt . Printf ( "GetPostPower-1-->%v\n" , empowerRedisToken [ "menuid" ] )
menuIdAry , menuUrl , err = readMenuList ( postid , system )
writeRedisData := MapOut [ string ] ( )
writeRedisData [ "menuid" ] = strings . Join ( menuIdAry , "," )
writeRedisData [ "menuurl" ] = strings . Join ( menuUrl , "|_^_|" )
redisClient . SetRedisTime ( 10800 )
redisClient . HashMsetAdd ( redisFileEmpowerKey , writeRedisData )
} else {
// fmt.Printf("GetPostPower-2-->%v\n", empowerRedisToken["menuid"])
menuIdAry = strings . Split ( empowerRedisToken [ "menuid" ] , "," )
menuUrl = strings . Split ( empowerRedisToken [ "menuurl" ] , "|_^_|" )
// fmt.Printf("GetPostPower-3-->%v-->%v\n", len(menuIdAry), menuIdAry)
if len ( menuIdAry ) <= 1 {
menuIdAry , menuUrl , err = readMenuList ( postid , system )
writeRedisData := MapOut [ string ] ( )
writeRedisData [ "menuid" ] = strings . Join ( menuIdAry , "," )
writeRedisData [ "menuurl" ] = strings . Join ( menuUrl , "|_^_|" )
redisClient . SetRedisTime ( 10800 )
redisClient . HashMsetAdd ( redisFileEmpowerKey , writeRedisData )
}
}
return
}
/ *
读取菜单
* /
func readMenuList ( postid int64 , system string ) ( menuIdAry , menuUrl [ ] string , err error ) {
var ponitId string
err = overall . CONSTANT_DB_System_Permission . Model ( & modelssystempermission . Empower { } ) . Select ( "`point_id`" ) . Where ( "`state` = 1 AND `post_id` = ? AND `system` = ?" , postid , system ) . Find ( & ponitId ) . Error
if err != nil {
return
}
menuIdAry = strings . Split ( ponitId , "," )
if len ( menuIdAry ) < 1 {
return
}
switch system {
case "cangchu" :
menuUrl , err = getStorageSystemEmpower ( menuIdAry )
default :
//绩效考核系统
menuUrl , err = getKpiSystemEmpower ( menuIdAry )
}
return
}
/ *
获取绩效考核系统授权菜单
@ menuIdAry 菜单ID
* /
func getKpiSystemEmpower ( menuIdAry [ ] string ) ( menuUrl [ ] string , err error ) {
if len ( menuIdAry ) < 1 {
return
}
var empowerMenuUrl [ ] string
err = overall . CONSTANT_DB_Master . Model ( & modelsschool . SystemMenuSchool { } ) . Distinct ( "`m_url`" ) . Where ( "`m_steat` = 1 AND `m_id` IN ?" , menuIdAry ) . Find ( & empowerMenuUrl ) . Error
if err != nil || len ( empowerMenuUrl ) < 1 {
return
}
for i := 0 ; i < len ( empowerMenuUrl ) ; i ++ {
if empowerMenuUrl [ i ] != "#" && IsInTrue [ string ] ( empowerMenuUrl [ i ] , menuUrl ) == false {
menuUrl = append ( menuUrl , empowerMenuUrl [ i ] )
}
}
return
}
/ *
获取仓储考核系统授权菜单
@ menuIdAry 菜单ID
* /
func getStorageSystemEmpower ( menuIdAry [ ] string ) ( menuUrl [ ] string , err error ) {
if len ( menuIdAry ) < 1 {
return
}
var empowerMenuUrl [ ] string
err = overall . CONSTANT_DB_Storage . Model ( & modelsstorage . AccesstoAddress { } ) . Distinct ( "`url`" ) . Where ( "`state` = 1 AND `id` IN ?" , menuIdAry ) . Find ( & empowerMenuUrl ) . Error
if err != nil || len ( empowerMenuUrl ) < 1 {
return
}
// fmt.Printf("empowerMenuUrl---------->%v\n", empowerMenuUrl)
for i := 0 ; i < len ( empowerMenuUrl ) ; i ++ {
if empowerMenuUrl [ i ] != "#" && empowerMenuUrl [ i ] != "" && IsInTrue [ string ] ( empowerMenuUrl [ i ] , menuUrl ) == false {
menuUrl = append ( menuUrl , empowerMenuUrl [ i ] )
}
}
return
}
// 获取公司最小主责部门
func GetMinMainDutyDeparment ( company string ) ( organization [ ] modelshr . AdministrativeOrganization ) {
var allOrg [ ] modelshr . AdministrativeOrganization
err := overall . CONSTANT_DB_HR . Model ( & modelshr . AdministrativeOrganization { } ) . Where ( "ispower = 1 AND state = 1 AND organization_type > 2 AND superior = ?" , company ) . Find ( & allOrg ) . Error
if err != nil || len ( allOrg ) < 1 {
return
}
for _ , v := range allOrg {
sunOrgList , orgSunErr := getSunOrgList ( v . Id )
if orgSunErr == nil && len ( sunOrgList ) > 0 {
for _ , sv := range sunOrgList {
organization = append ( organization , sv )
}
} else {
organization = append ( organization , v )
}
}
return
}
// 获取下级行政组织是否有主行政部门
func getSunOrgList ( orgId int64 ) ( orgList [ ] modelshr . AdministrativeOrganization , ovErr error ) {
ovErr = overall . CONSTANT_DB_HR . Where ( "state = 1" ) . Where ( "ispower = 1 AND superior = ?" , orgId ) . Find ( & orgList ) . Error
return
}
// 递归无限极菜单树
func GetMenuThree ( jurisd int , parentId string , threeData [ ] MenuContList ) [ ] PowerThree {
treeList := [ ] PowerThree { }
for _ , v := range threeData {
if v . ParentId == parentId {
child := GetMenuThree ( jurisd , v . Id , threeData )
var node PowerThree
node . Id = v . Id
node . Name = v . Name
node . ParentId = v . ParentId
isTrue , menuOper := MenuOperation ( jurisd , v . Id )
if isTrue == true {
node . MenuOperation = menuOper
// fmt.Printf("GetMenuThree-1111--%v--->%v\n", v.Id, menuOper)
}
node . Child = child
treeList = append ( treeList , node )
}
}
return treeList
}
// 获取菜单操作项目
func MenuOperation ( jurisd int , menuId string ) ( isTrue bool , operation [ ] MenuContList ) {
var operList [ ] modelsschool . MenuOperation
isTrue = false
operErr := overall . CONSTANT_DB_Master . Where ( "`menu_id` = ?" , menuId ) . Order ( "oper_id desc" ) . Find ( & operList ) . Error
if operErr != nil || len ( operList ) < 1 {
return
}
isTrue = true
for i := 0 ; i < len ( operList ) ; i ++ {
var menSmaiCont MenuContList
menSmaiCont . Id = strconv . FormatInt ( operList [ i ] . OperId , 10 )
menSmaiCont . Name = operList [ i ] . OperTitle
menSmaiCont . ParentId = strconv . FormatInt ( operList [ i ] . MenuId , 10 )
operation = append ( operation , menSmaiCont )
}
// fmt.Printf("ApiUrl---%v--->%v--->%v--->%v\n", menuId, isTrue, operErr, operList)
return
}
/ *
递归无限极菜单树
* /
func GetMenuThrees ( jurisd int , parentId string , threeData [ ] MenuContList ) [ ] PowerThree {
treeList := [ ] PowerThree { }
for _ , v := range threeData {
if v . ParentId == parentId {
child := GetMenuThrees ( jurisd , v . Id , threeData )
var node PowerThree
node . Id = v . Id
node . Name = v . Name
node . ParentId = v . ParentId
node . Child = child
// fmt.Printf("GetMenuThree-22222--%v--->%v\n", v.Id, node)
treeList = append ( treeList , node )
}
}
return treeList
}