123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- package service
- import (
- "app.yhyue.com/moapp/dataDeduplication/entity"
- "app.yhyue.com/moapp/dataDeduplication/rpc/deduplication"
- "crypto/md5"
- "fmt"
- "github.com/go-xorm/xorm"
- "io"
- "log"
- "strconv"
- "strings"
- )
- //定义orm引擎
- var Engine *xorm.Engine
- type DeduplicationService struct{}
- var PREFIX = "qc"
- //数据判重
- func (service *DeduplicationService) DataDeduplicateInsert(data *deduplication.Request) (*deduplication.Info, string) {
- log.Println("开始=====")
- orm := Engine.NewSession()
- defer orm.Close()
- // 模运算取企业id
- number, _ := strconv.Atoi(data.EntId)
- tableName := PREFIX + fmt.Sprintf("%03d", number%100)
- // 查询
- var rs []*entity.Deduplication
- var tmpList []string
- var valueList []interface{}
- var selectSql string
- if data.IsEnt {
- valueList = append(valueList, data.EntId)
- } else {
- valueList = append(valueList, data.PersonId, data.EntId)
- }
- for _, v := range strings.Split(data.InfoId, ",") {
- tmpList = append(tmpList, "?")
- valueList = append(valueList, v)
- }
- if data.IsEnt {
- selectSql = fmt.Sprintf("ent_id=? and info_id in (%s)", strings.Join(tmpList, ","))
- } else {
- selectSql = fmt.Sprintf("person_id = ? and ent_id=? and info_id in (%s)", strings.Join(tmpList, ","))
- }
- log.Println(selectSql)
- infoIdList := strings.Split(data.InfoId, ",")
- totalInfoCount := len(infoIdList)
- err := orm.Table(tableName).Cols("info_id").Where(selectSql, valueList...).Find(&rs)
- totalExist := len(rs)
- log.Println(totalExist, "已存在")
- if err != nil {
- log.Println(err, "判重查询失败")
- return &deduplication.Info{
- TotalCount: 0,
- ExistCount: 0,
- NewCount: 0,
- IsInsert: false,
- }, "判重查询失败"
- }
- if data.IsInsert {
- existIdMap := map[string]bool{}
- for _, v := range rs {
- existIdMap[v.InfoId] = true
- }
- // 开启事务
- orm.Begin()
- // 新增
- var insertList []entity.Deduplication
- for _, id := range infoIdList {
- if existIdMap[id] {
- log.Println("id已存在", id)
- continue
- }
- log.Println("新增", id)
- temData := entity.Deduplication{
- InfoId: id,
- EntId: data.EntId,
- PersonId: data.PersonId,
- }
- insertList = append(insertList, temData)
- if len(insertList) > 100 {
- _, err3 := orm.Table(tableName).Insert(insertList)
- insertList = []entity.Deduplication{}
- if err3 != nil {
- orm.Rollback()
- log.Println(err3, "新增数据失败")
- return &deduplication.Info{
- TotalCount: int64(totalInfoCount),
- ExistCount: int64(totalExist),
- NewCount: int64(totalInfoCount - totalExist),
- IsInsert: false,
- }, "新增数据失败"
- }
- }
- }
- if len(insertList) > 0 {
- _, err3 := orm.Table(tableName).Insert(insertList)
- if err3 != nil {
- orm.Rollback()
- log.Println(err3, "新增数据失败")
- return &deduplication.Info{
- TotalCount: int64(totalInfoCount),
- ExistCount: int64(totalExist),
- NewCount: int64(totalInfoCount - totalExist),
- IsInsert: false,
- }, "新增数据失败"
- }
- }
- err := orm.Commit()
- if err != nil {
- log.Println("提交失败")
- return &deduplication.Info{
- TotalCount: int64(totalInfoCount),
- ExistCount: int64(totalExist),
- NewCount: int64(totalInfoCount - totalExist),
- IsInsert: false,
- }, "提交失败"
- } else {
- log.Println("提交成功")
- return &deduplication.Info{
- TotalCount: int64(totalInfoCount),
- ExistCount: int64(totalExist),
- NewCount: int64(totalInfoCount - totalExist),
- IsInsert: true,
- }, ""
- }
- }
- return &deduplication.Info{
- TotalCount: int64(totalInfoCount),
- ExistCount: int64(totalExist),
- NewCount: int64(totalInfoCount - totalExist),
- IsInsert: false,
- }, ""
- }
- // 根据账户id进行判重
- func (service *DeduplicationService) DataDeduplicateByAccountId(data *deduplication.ByAccountRequest) (*deduplication.Info, string) {
- log.Println("开始=====")
- orm := Engine.NewSession()
- defer orm.Close()
- // 模运算取企业id todo 需要区分是int类型还是mongodb objectid类型 看一下咋转
- //var num01 int
- number, errConv := strconv.Atoi(data.AccountId)
- log.Println(55555,number,errConv)
- if errConv!=nil{
- log.Println("不是int类型的,hash后再取模寻表")
- b := a(data.AccountId)
- ss:=b[len(b)-2:]
- bt,_:=strconv.ParseInt(ss,16,64)
- number =int(bt)
- log.Println("哈希取模后的表序号",number)
- }
- tableName := PREFIX + fmt.Sprintf("%03d", number%100)
- // 查询
- //var rs entity.Deduplication
- var tmpList []string
- var valueList []interface{}
- var selectSql string
- valueList = append(valueList, data.AccountId)
- valueList = append(valueList, data.DataDesc)
- for _, v := range strings.Split(data.InfoId, ",") {
- if strings.TrimSpace(v)!=""{
- tmpList = append(tmpList, "?")
- valueList = append(valueList, v)
- }
- }
- var rs []*entity.Deduplication
- selectSql = fmt.Sprintf("account_id=? and data_desc=? and info_id in (%s)", strings.Join(tmpList, ","))
- infoIdList := strings.Split(data.InfoId, ",")
- totalInfoCount := len(infoIdList)
- err := orm.Table(tableName).Cols("info_id").Where(selectSql, valueList...).Find(&rs)
- if err != nil {
- log.Println(err, "判重查询失败")
- return &deduplication.Info{
- TotalCount: 0,
- ExistCount: 0,
- NewCount: 0,
- IsInsert: false,
- }, "判重查询失败"
- }
- existInfoIdMap := map[string]bool{}
- existIdList := []string{}
- for _, v := range rs {
- if existInfoIdMap[v.InfoId] {
- continue
- }else {
- existIdList = append(existIdList,v.InfoId)
- }
- }
- count := int64(len(existIdList))
- return &deduplication.Info{
- TotalCount: int64(totalInfoCount),
- ExistCount: count,
- NewCount: int64(totalInfoCount) - count,
- IsInsert: false,
- }, ""
- }
- // 根据账户id进行判重并存入数据
- func (service *DeduplicationService) DataDeduplicateAndSave(data *deduplication.ByAccountRequest) (*deduplication.Info, string) {
- log.Println("开始=====")
- orm := Engine.NewSession()
- defer orm.Close()
- // 模运算取企业id 企业id是int 类型的直接对100取模
- // objectid类型的哈希后取模 这里使用md5后取后两位数字转10进制 然后对100取模
- number, errConv := strconv.Atoi(data.AccountId)
- log.Println(55555,number,errConv)
- if errConv!=nil{
- log.Println("不是int类型的,hash后再取模寻表")
- b := a(data.AccountId)
- ss:=b[len(b)-2:]
- bt,_:=strconv.ParseInt(ss,16,64)
- number =int(bt)
- log.Println(bt)
- }
- tableName := PREFIX + fmt.Sprintf("%03d", number%100)
- // 查询
- var rs []*entity.Deduplication
- var tmpList []string
- var valueList []interface{}
- var selectSql string
- valueList = append(valueList, data.AccountId)
- valueList = append(valueList, data.DataDesc)
- for _, v := range strings.Split(data.InfoId, ",") {
- if strings.TrimSpace(v)!=""{
- tmpList = append(tmpList, "?")
- valueList = append(valueList, v)
- }
- }
- selectSql = fmt.Sprintf("account_id=? and data_desc=? and info_id in (%s)", strings.Join(tmpList, ","))
- infoIdList := strings.Split(data.InfoId, ",")
- totalInfoCount := len(infoIdList)
- err := orm.Table(tableName).Cols("info_id").Where(selectSql, valueList...).Find(&rs)
- existInfoIdMap := map[string]bool{}
- existIdList := []string{}
- for _, v := range rs {
- if existInfoIdMap[v.InfoId] {
- continue
- }else {
- existIdList = append(existIdList,v.InfoId)
- }
- }
- count := len(existIdList)
- log.Println(count, "已存在数据量")
- if err != nil {
- log.Println(err, "判重查询失败")
- return &deduplication.Info{
- TotalCount: 0,
- ExistCount: 0,
- NewCount: 0,
- }, "判重查询失败"
- }
- existIdMap := map[string]bool{}
- for _, v := range rs {
- existIdMap[v.InfoId] = true
- }
- // 新增
- var insertList []entity.Deduplication
- for _, id := range infoIdList {
- if existIdMap[id] {
- log.Println("id已存在", id)
- continue
- }
- log.Println("新增", id)
- temData := entity.Deduplication{
- InfoId: id,
- EntId: "0",
- PersonId: data.PersonId,
- AccountId: data.AccountId,
- DataDesc: data.DataDesc,
- }
- insertList = append(insertList, temData)
- }
- go SaveMysql(tableName,insertList)
- return &deduplication.Info{
- TotalCount: int64(totalInfoCount),
- ExistCount: int64(count),
- NewCount: int64(totalInfoCount - count),
- }, ""
- }
- func (service *DeduplicationService) EntCount(data *deduplication.GetEntCountRequest) (int64, string) {
- log.Println("开始=====")
- orm := Engine.NewSession()
- defer orm.Close()
- // 模运算取企业id
- number, _ := strconv.Atoi(data.EntId)
- tableName := PREFIX + fmt.Sprintf("%03d", number%100)
- // 查询
- var rs entity.Deduplication
- count, err := orm.Table(tableName).Where("ent_id=?", data.EntId).Count(rs)
- if err != nil {
- return 0, "查询失败"
- }
- log.Println("count=====", count)
- return count, ""
- }
- func a(data string) string {
- t := md5.New()
- io.WriteString(t, data)
- return fmt.Sprintf("%x", t.Sum(nil))
- }
- func SaveMysql(tableName string,saveList []entity.Deduplication) {
- //
- log.Println("保存数据开始")
- orm := Engine.NewSession()
- defer orm.Close()
- orm.Begin()
- insertList := []entity.Deduplication{}
- for _, saveData := range saveList {
- insertList = append(insertList, saveData)
- if len(insertList) > 500 {
- _, err3 := orm.Table(tableName).Insert(insertList)
- insertList = []entity.Deduplication{}
- if err3 != nil {
- orm.Rollback()
- log.Println(err3, "新增数据失败")
- }
- }
- }
- if len(insertList) > 0 {
- _, err3 := orm.Table(tableName).Insert(insertList)
- if err3 != nil {
- orm.Rollback()
- log.Println(err3, "新增数据失败")
- }
- }
- err2 := orm.Commit()
- log.Println("err2", err2)
- log.Println("保存数据结束")
- }
|