company.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. package service
  2. import (
  3. "github.com/gogf/gf/v2/util/gconv"
  4. "log"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "app.yhyue.com/moapp/jybase/common"
  9. "app.yhyue.com/moapp/jybase/date"
  10. . "bp.jydev.jianyu360.cn/BaseService/biService/entity"
  11. "bp.jydev.jianyu360.cn/BaseService/biService/rpc/biservice"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. // 判断是不是集团公司 判断在不在工商库 return 1集团公司 2工商库 3都是 -1都不是
  15. func GetCompanyType(this *biservice.CompanyReq) int {
  16. status := -1
  17. if this.CompanyName == "" {
  18. return status
  19. }
  20. isGroup, isCommerce := CompanyType(this.CompanyName)
  21. //判断
  22. if isGroup && isCommerce {
  23. status = 3
  24. } else if isGroup {
  25. status = 1
  26. } else if isCommerce {
  27. status = 2
  28. }
  29. return status
  30. }
  31. func CompanyType(companyName string) (bool, bool) {
  32. isGroup, isCommerce := false, false
  33. //是否是集团
  34. if c := JyBiTidb.CountBySql(`select count(1) from group_company_name where company_name=?`, companyName); c > 0 {
  35. isGroup = true
  36. }
  37. //是否在工商库
  38. if c := MgoQyxy.Count("qyxy_std", map[string]interface{}{"company_name": companyName, "company_type": map[string]interface{}{"$ne": "个体工商户"}}); c > 0 {
  39. isCommerce = true
  40. }
  41. return isGroup, isCommerce
  42. }
  43. // 查询是否达上限
  44. func FindUpperLimit(positionId int64) bool {
  45. if positionId == 0 {
  46. return false
  47. }
  48. return JyBiTidb.CountBySql(`select count(1) from dwd_f_crm_clue_info where position_id=? and is_transfer != 1 `, positionId) >= 400
  49. }
  50. // CompanyCount 分配的数量
  51. // CompanyCounts 目前数量
  52. // ClueCount 线索的数量
  53. func DistributeClueShow(this *biservice.DistributeClueShowReq, allocationCap int64) *biservice.DistributeClueShowResp {
  54. rdata := []*biservice.DistributeClueShowss{}
  55. pArr := []string{}
  56. for _, c := range this.Datas { //初始化返回数据
  57. m := &biservice.DistributeClueShowss{}
  58. m.PositionId = c.PositionId
  59. m.ClueCount = 0
  60. m.CompanyCounts = 0
  61. //现有数据查询
  62. existingCount := JyBiTidb.CountBySql(`select count(1) from dwd_f_crm_clue_info where position_id=? and is_assign=1 and is_transfer != 1 `, m.PositionId)
  63. m.ExistingCount = existingCount
  64. if existingCount >= allocationCap {
  65. if this.DataType != 1 {
  66. return &biservice.DistributeClueShowResp{
  67. ErrorCode: 1,
  68. ErrorMsg: "超出私海线索上限",
  69. }
  70. } else {
  71. continue
  72. }
  73. }
  74. if this.DataType == 1 {
  75. m.CompanyCount = 0
  76. m.MinCount = 0
  77. } else {
  78. if c.DistributedCount <= 0 {
  79. continue
  80. }
  81. m.CompanyCount = c.DistributedCount
  82. }
  83. rdata = append(rdata, m)
  84. pArr = append(pArr, strconv.FormatInt(c.PositionId, 10))
  85. }
  86. if len(rdata) == 0 {
  87. return &biservice.DistributeClueShowResp{
  88. ErrorCode: 0,
  89. Data: rdata,
  90. }
  91. }
  92. clueArrs1 := []string{}
  93. clueArrs1 = strings.Split(this.ClueIdList, ",")
  94. logx.Info("clueArrs1 ", clueArrs1)
  95. for _, v := range strings.Split(this.ClueIdList, ",") {
  96. count := 1
  97. id := v
  98. minCount, minIndex := int64(0), -1
  99. isindexok := false
  100. k1 := 0
  101. for _, r := range rdata {
  102. if !r.FullStatus {
  103. if this.DataType == 1 {
  104. if k1 == 0 {
  105. minCount = r.CompanyCounts
  106. break
  107. }
  108. } else {
  109. if r.CompanyCounts < r.CompanyCount {
  110. minCount = r.CompanyCounts
  111. }
  112. }
  113. }
  114. }
  115. for _, r := range rdata {
  116. if !r.FullStatus {
  117. if this.DataType == 1 {
  118. if r.CompanyCounts < minCount {
  119. minCount = r.CompanyCounts
  120. }
  121. } else {
  122. if r.CompanyCounts < minCount && r.CompanyCounts < r.CompanyCount {
  123. minCount = r.CompanyCounts
  124. }
  125. }
  126. }
  127. }
  128. for k, r := range rdata {
  129. countNumber := r.ExistingCount + r.ClueCount + int64(count)
  130. if this.DataType == 1 {
  131. if countNumber <= allocationCap {
  132. if r.CompanyCounts <= minCount {
  133. minCount = r.CompanyCounts
  134. minIndex = k
  135. }
  136. }
  137. } else {
  138. if countNumber <= allocationCap {
  139. if r.CompanyCounts <= minCount && r.CompanyCounts < r.CompanyCount {
  140. minCount = r.CompanyCounts
  141. minIndex = k
  142. }
  143. }
  144. }
  145. }
  146. if minIndex == -1 {
  147. continue
  148. }
  149. if this.DataType == 1 {
  150. countNumber := rdata[minIndex].ExistingCount + rdata[minIndex].ClueCount + int64(count)
  151. if countNumber == allocationCap {
  152. rdata[minIndex].FullStatus = true
  153. }
  154. rdata[minIndex].CompanyCount++
  155. rdata[minIndex].CompanyCounts++
  156. rdata[minIndex].ClueCount += int64(count)
  157. } else {
  158. countNumber := rdata[minIndex].ExistingCount + rdata[minIndex].ClueCount + int64(count)
  159. if rdata[minIndex].CompanyCounts < rdata[minIndex].CompanyCount {
  160. isindexok = true
  161. rdata[minIndex].CompanyCounts++
  162. rdata[minIndex].ClueCount += int64(count)
  163. if countNumber == allocationCap {
  164. rdata[minIndex].FullStatus = true
  165. }
  166. }
  167. }
  168. if this.DataType == 3 && isindexok {
  169. dclue(gconv.Int64(id), rdata[minIndex].PositionId, this.IsTask, this.PositionId)
  170. log.Println("3333", id, rdata[minIndex].PositionId)
  171. }
  172. }
  173. /*if this.DataType == 2 {
  174. for _, v := range rdata {
  175. if this.UpdatePositionID == v.PositionId {
  176. if v.CompanyCounts < v.CompanyCount {
  177. return &biservice.DistributeClueShowResp{
  178. ErrorCode: 1,
  179. ErrorMsg: "超出私海线索上限",
  180. }
  181. }
  182. }
  183. }
  184. }*/
  185. return &biservice.DistributeClueShowResp{
  186. ErrorCode: 0,
  187. Data: rdata,
  188. }
  189. }
  190. func dclue(v, positionId, isTask, thispositionId int64) {
  191. clueData := JyBiTidb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"id": v}, "", "")
  192. nowTime := time.Now().Format(date.Date_Full_Layout)
  193. if clueData != nil && len(*clueData) > 0 {
  194. isAssign := common.IntAll((*clueData)["is_assign"])
  195. clueSeatNumber := common.ObjToString((*clueData)["seatNumber"])
  196. oldName, seatNumber, name := "", "", ""
  197. trailstatus := common.ObjToString((*clueData)["trailstatus"])
  198. saleData := JyBiTidb.SelectBySql(`select * from dwd_f_crm_personnel_management where seat_number != "" and seat_number is not null`)
  199. if saleData != nil && len(*saleData) > 0 {
  200. for _, v := range *saleData {
  201. if common.ObjToString(v["seat_number"]) == clueSeatNumber {
  202. oldName = common.ObjToString(v["name"])
  203. }
  204. if common.Int64All(v["position_id"]) == positionId {
  205. seatNumber = common.ObjToString(v["seat_number"])
  206. name = common.ObjToString(v["name"])
  207. }
  208. }
  209. }
  210. if isAssign == 1 {
  211. oldpositionId := common.Int64All((*clueData)["position_id"])
  212. updateClue := map[string]interface{}{
  213. "position_id": positionId,
  214. "seatNumber": seatNumber,
  215. "is_assign": 1,
  216. "updatetime": nowTime,
  217. "comeintime": nowTime,
  218. "comeinsource_private": 4,
  219. "level_open": nil,
  220. "clue_level": nil,
  221. "out_task_time": nil,
  222. "out_task_status": nil,
  223. }
  224. if oldName != name {
  225. updateClue["start_trail_time"] = nil
  226. updateClue["next_trail_time"] = nil
  227. }
  228. if isTask == int64(1) {
  229. updateClue["is_task"] = 1
  230. updateClue["task_time"] = nowTime
  231. updateClue["tasktime"] = nowTime
  232. updateClue["taskstatus"] = 0
  233. updateClue["tasksource"] = "主动分配客户"
  234. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  235. "clue_id": v,
  236. "position_id": positionId,
  237. "change_type": "加入任务车",
  238. "new_value": "主动分配客户",
  239. "createtime": nowTime,
  240. "BCPCID": common.GetRandom(32),
  241. "operator_id": thispositionId,
  242. })
  243. }
  244. ok := JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": v}, updateClue)
  245. if ok {
  246. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  247. "clue_id": v,
  248. "position_id": positionId,
  249. "change_field": "position_id",
  250. "change_type": "所属人变更",
  251. "old_value": oldName,
  252. "new_value": name,
  253. "createtime": nowTime,
  254. "BCPCID": common.GetRandom(32),
  255. "operator_id": thispositionId,
  256. })
  257. //标签变更
  258. label := gconv.Int64((*clueData)["label"])
  259. taskstatus := gconv.Int64(updateClue["taskstatus"])
  260. if isTask == int64(1) {
  261. taskstatus = 0
  262. }
  263. LabelHandel(nil, label, 3, v, "", trailstatus, trailstatus, name, oldName, taskstatus)
  264. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  265. "clue_id": v,
  266. "position_id": oldpositionId,
  267. "change_field": "trailstatus",
  268. "change_type": "基本信息变更",
  269. "old_value": CodeTrail[trailstatus],
  270. "new_value": "流失",
  271. "createtime": nowTime,
  272. "BCPCID": common.GetRandom(32),
  273. "operator_id": thispositionId,
  274. })
  275. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  276. "clue_id": v,
  277. "position_id": positionId,
  278. "change_field": "trailstatus",
  279. "change_type": "基本信息变更",
  280. "old_value": CodeTrail[trailstatus],
  281. "new_value": "新增",
  282. "createtime": nowTime,
  283. "BCPCID": common.GetRandom(32),
  284. "operator_id": thispositionId,
  285. })
  286. } else {
  287. logx.Info("私海修改失败 ", v, positionId, seatNumber)
  288. }
  289. } else {
  290. updateClue := map[string]interface{}{
  291. "position_id": positionId,
  292. "seatNumber": seatNumber,
  293. "is_assign": 1,
  294. "updatetime": nowTime,
  295. "comeintime": nowTime,
  296. "comeinsource_private": 4,
  297. "is_task": 0,
  298. "taskstatus": 0,
  299. "level_open": nil,
  300. "clue_level": nil,
  301. "out_task_time": nil,
  302. "out_task_status": nil,
  303. "next_trail_time": nil,
  304. "start_trail_time": nil,
  305. // "comeinsource_open": nil,
  306. }
  307. updateClue["trailstatus"] = "01"
  308. if isTask == int64(1) {
  309. updateClue["is_task"] = 1
  310. updateClue["task_time"] = nowTime
  311. updateClue["tasktime"] = nowTime
  312. updateClue["taskstatus"] = 0
  313. updateClue["tasksource"] = "主动分配客户"
  314. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  315. "clue_id": v,
  316. "position_id": positionId,
  317. "change_type": "加入任务车",
  318. "new_value": "主动分配客户",
  319. "createtime": nowTime,
  320. "BCPCID": common.GetRandom(32),
  321. "operator_id": thispositionId,
  322. })
  323. }
  324. ok := JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": v}, updateClue)
  325. if ok {
  326. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  327. "clue_id": v,
  328. "position_id": positionId,
  329. "change_field": "position_id",
  330. "change_type": "所属人变更",
  331. "old_value": "/",
  332. "new_value": name,
  333. "createtime": nowTime,
  334. "BCPCID": common.GetRandom(32),
  335. "operator_id": thispositionId,
  336. })
  337. label := gconv.Int64((*clueData)["label"])
  338. taskstatus := gconv.Int64(updateClue["taskstatus"])
  339. if isTask == int64(1) {
  340. taskstatus = 0
  341. }
  342. LabelHandel(nil, label, 3, v, "", "01", trailstatus, name, "/", taskstatus)
  343. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  344. "clue_id": v,
  345. "position_id": positionId,
  346. "change_field": "trailstatus",
  347. "change_type": "基本信息变更",
  348. "old_value": "商机线索",
  349. "new_value": "新增",
  350. "createtime": nowTime,
  351. "BCPCID": common.GetRandom(32),
  352. "operator_id": thispositionId,
  353. })
  354. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  355. "clue_id": v,
  356. "position_id": positionId,
  357. "change_field": "trailstatus",
  358. "change_type": "基本信息变更",
  359. "old_value": CodeTrail[trailstatus],
  360. "new_value": "商机线索",
  361. "createtime": nowTime,
  362. "BCPCID": common.GetRandom(32),
  363. "operator_id": thispositionId,
  364. })
  365. } else {
  366. logx.Info("私海插入失败 ", v, positionId, seatNumber)
  367. }
  368. }
  369. }
  370. }