participateBid.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/date"
  5. "app.yhyue.com/moapp/jybase/encrypt"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/gogf/gf/v2/container/gset"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. "jyBXCore/rpc/model/mysql"
  11. "jyBXCore/rpc/type/bxcore"
  12. "jyBXCore/rpc/util"
  13. "strings"
  14. )
  15. const (
  16. PositionTypeEnt = 1 // 职位类型企业
  17. PositionTypePersonal = 0 // 职位类型个人
  18. ButtonValueParticipate = 0 // 参标按钮 显示值 0-参标
  19. ButtonValueParticipated = 1 // 按钮显示值 列表页面 1-已参标
  20. RoleEntManager = 1 // 企业管理员角色
  21. RoleDepartManager = 2 // 部门管理员角色
  22. BidTypeDirect = 1 // 直接投标
  23. BidTypeChanel = 2 // 渠道投标
  24. RecordTypeBidStatus = 1 // 存储类型 1:投标状态更新
  25. )
  26. type ParticipateBid struct {
  27. EntId int64
  28. EntUserId int64
  29. PositionType int64
  30. PositionId int64
  31. EntRoleId int64 // 角色
  32. }
  33. func NewParticipateBid(entId, entUserId, positionType, positionId int64) ParticipateBid {
  34. return ParticipateBid{
  35. EntId: entId,
  36. EntUserId: entUserId,
  37. PositionType: positionType,
  38. PositionId: positionId,
  39. }
  40. }
  41. // PersonalExistProject 个人版要展示的参标按钮 查询出已经参标的项目信息 用于后边格式化数据
  42. func (p *ParticipateBid) PersonalExistProject(projectId []string) map[string]struct{} {
  43. rs := mysql.ParticipateProjectPersonal(p.PositionId, projectId)
  44. existProjectSet := map[string]struct{}{}
  45. if rs != nil && len(*rs) > 0 { // 如果查到了 说明已经参标 这部分应该显示已参标
  46. // 处理成map
  47. for i := 0; i < len(*rs); i++ {
  48. existId := common.ObjToString((*rs)[i]["project_id"])
  49. existProjectSet[existId] = struct{}{}
  50. }
  51. }
  52. return existProjectSet
  53. }
  54. // ListPersonalFormat 列表页个人版 参标按钮 格式化数据
  55. func (p *ParticipateBid) ListPersonalFormat(existProjectSet map[string]struct{}, infoM map[string]string) []*bxcore.ShowInfo {
  56. // 处理成 要返回的返回数据
  57. var formatList []*bxcore.ShowInfo
  58. for k, v := range infoM {
  59. buttonValue := ButtonValueParticipate // 不存在应该显示参标
  60. if _, ok := existProjectSet[v]; ok { // 存在说明应该显示已参标
  61. buttonValue = ButtonValueParticipated
  62. }
  63. formatList = append(formatList, &bxcore.ShowInfo{
  64. Id: encrypt.EncodeArticleId2ByCheck(k),
  65. Value: int64(buttonValue),
  66. })
  67. }
  68. return formatList
  69. }
  70. // EntExistProject 企业版 查出来企业下已经参标的这个项目的以及参标人信息 用于后边格式化数据判断有没有自己
  71. func (p *ParticipateBid) EntExistProject(projectId []string) map[string]string {
  72. rs := mysql.ParticipateProjectEnt(p.EntId, projectId)
  73. existProjectMap := map[string]string{}
  74. if rs != nil && len(*rs) > 0 { // 如果查到了 说明这个项目公司里面已经参标 处理一下信息用于后边判断是否是自己参标
  75. // 处理成map
  76. for i := 0; i < len(*rs); i++ {
  77. existId := common.ObjToString((*rs)[i]["project_id"])
  78. personIds := common.ObjToString((*rs)[i]["personIds"])
  79. existProjectMap[existId] = personIds
  80. }
  81. }
  82. return existProjectMap
  83. }
  84. // ListEntFormat 企业版 列表页数据格式化
  85. func (p *ParticipateBid) ListEntFormat(existProjectMap, infoM map[string]string, isAllow bool) []*bxcore.ShowInfo {
  86. // 处理成 要返回的返回数据
  87. var formatList []*bxcore.ShowInfo
  88. for k, v := range infoM {
  89. buttonValue := ButtonValueParticipate // 不存在时-显示参标
  90. if userIds, ok := existProjectMap[v]; ok { // 存在时 说明项目在企业下已经参标 需要进一步判断
  91. // 判断已经存在的参标人中是否包含自己 包含时 显示成已参标
  92. if ContainId(userIds, common.InterfaceToStr(p.EntUserId)) {
  93. buttonValue = ButtonValueParticipated
  94. } else if isAllow { // 不包含自己时需要 进一步判断公司设置是否允许多人参标
  95. // 允许时显示成 参标
  96. buttonValue = ButtonValueParticipate
  97. } else { // 不允许时 跳过该条信息
  98. continue
  99. }
  100. }
  101. formatList = append(formatList, &bxcore.ShowInfo{
  102. Id: encrypt.EncodeArticleId2ByCheck(k),
  103. Value: int64(buttonValue),
  104. })
  105. }
  106. return formatList
  107. }
  108. // DetailEntFormat 企业版 详情页数据格式化 返回数据 参标按钮 终止参标按钮 转给同事按钮 参标人姓名
  109. func (p *ParticipateBid) DetailEntFormat(existProjectMap map[string]string, isValid, isAllow bool, bidEndTime int64) (formatData *bxcore.ParticipateDetailInfo) {
  110. // 处理成 要返回的返回数据
  111. formatData = &bxcore.ParticipateDetailInfo{}
  112. if len(existProjectMap) == 0 && isValid {
  113. // 无参标人 展示参标按钮 其余按钮不显示
  114. formatData.ShowParticipate = true
  115. return
  116. }
  117. formatData.BidEndTime = bidEndTime
  118. persons := ""
  119. projectId := ""
  120. for k, v := range existProjectMap { // 这是为了取参标人id信息 列表页是多条数据 详情页这里 map里面只会有一条数据
  121. projectId = k
  122. persons = v
  123. break
  124. }
  125. // 参标人信息 处理成姓名
  126. nameRs := mysql.GetNameByUserIds(persons)
  127. if nameRs != nil && len(*nameRs) > 0 {
  128. formatData.UserName = common.ObjToString((*nameRs)[0]["name"])
  129. }
  130. if !isValid {
  131. return
  132. }
  133. // 如果是管理员 显示 终止参标按钮、转给同事按钮
  134. if p.EntRoleId == RoleEntManager || p.EntRoleId == RoleDepartManager {
  135. formatData.ProjectId = encrypt.EncodeArticleId2ByCheck(projectId)
  136. formatData.ShowStopParticipate = true
  137. formatData.ShowTransfer = true
  138. }
  139. // 如果包含自己 显示终止参标按钮
  140. if ContainId(persons, common.InterfaceToStr(p.EntUserId)) {
  141. formatData.ShowStopParticipate = true
  142. } else if isAllow { // 如果允许多人 显示参标按钮
  143. formatData.ShowParticipate = true
  144. }
  145. return
  146. }
  147. // DetailPersonalFormat 详情页个人版 按钮格式化数据
  148. func (p *ParticipateBid) DetailPersonalFormat(existProjectSet map[string]struct{}, isValid bool, bidEndTime int64) (formatData *bxcore.ParticipateDetailInfo) {
  149. // 处理成 要返回的返回数据
  150. formatData = &bxcore.ParticipateDetailInfo{}
  151. if !isValid {
  152. return
  153. }
  154. // 存在在说明已经参标 显示终止参标
  155. if len(existProjectSet) > 0 {
  156. formatData.BidEndTime = bidEndTime
  157. formatData.ShowStopParticipate = true
  158. } else {
  159. // 不存在则说明 未参标 参标按钮展示
  160. formatData.ShowParticipate = true
  161. }
  162. return
  163. }
  164. // HandlerProjectId 返回信息的映射集合,项目id列表
  165. func HandlerProjectId(projectInfos []map[string]interface{}, infoIds map[string]struct{}) (map[string]string, []string) {
  166. result := map[string]string{}
  167. projectIdList := []string{}
  168. // 记录infoid 和项目id 对应关系 用于最后处理返回的数据
  169. for i := 0; i < len(projectInfos); i++ {
  170. _id := common.ObjToString(projectInfos[i]["_id"])
  171. projectIdList = append(projectIdList, _id)
  172. list, b := projectInfos[i]["list"].([]interface{})
  173. if !b {
  174. continue
  175. }
  176. for j := 0; j < len(list); j++ {
  177. infoidMap := common.ObjToMap(list[j])
  178. if infoidMap == nil {
  179. continue
  180. }
  181. infoid := common.ObjToString((*infoidMap)["infoid"])
  182. if _, ok := infoIds[infoid]; ok && infoid != "" {
  183. result[infoid] = _id
  184. break
  185. }
  186. }
  187. }
  188. return result, projectIdList
  189. }
  190. // DecodeId 解密标讯id 返回一个信息id的列表 和 集合
  191. func DecodeId(ids string) (result []string, resultSet map[string]struct{}) {
  192. idList := strings.Split(ids, ",")
  193. resultSet = map[string]struct{}{}
  194. for i := 0; i < len(idList); i++ {
  195. decodeArray := encrypt.DecodeArticleId2ByCheck(idList[i])
  196. if len(decodeArray) == 1 && decodeArray[0] != "" {
  197. result = append(result, decodeArray[0])
  198. resultSet[decodeArray[0]] = struct{}{}
  199. }
  200. }
  201. return
  202. }
  203. // ContainId 用于判断给定的逗号分割的字符串中是否包含 目标的字符串
  204. func ContainId(ids string, objId string) bool {
  205. list := strings.Split(ids, ",")
  206. for i := 0; i < len(list); i++ {
  207. if list[i] == objId {
  208. return true
  209. }
  210. }
  211. return false
  212. }
  213. // CheckBidPower 投标状态更新/查看记录 验证权限(参标人或者是该企业下的管理员)
  214. func (p *ParticipateBid) CheckBidPower(projectId string, valid bool) (b bool) {
  215. switch p.PositionType {
  216. case PositionTypePersonal:
  217. // 查个人id
  218. b = mysql.CheckParticipatePersonal(projectId, p.PositionId, valid)
  219. case PositionTypeEnt:
  220. // 查企业
  221. if p.EntRoleId == RoleEntManager || p.EntRoleId == RoleDepartManager {
  222. b = mysql.CheckParticipateManager(projectId, p.EntId, valid)
  223. } else {
  224. // 查参标人
  225. b = mysql.CheckParticipateEntUser(projectId, p.PositionId, valid)
  226. }
  227. }
  228. return
  229. }
  230. // UpdateBidStatus 更新投标状态
  231. func (p *ParticipateBid) UpdateBidStatus(in *bxcore.UpdateBidStatusReq, projectId string) (flag bool) {
  232. if p.PositionType == PositionTypeEnt {
  233. pLock := util.GetParticipateLock(fmt.Sprintf("%d_%s", p.EntId, projectId))
  234. pLock.Lock()
  235. defer pLock.Unlock()
  236. }
  237. // 如果查出来旧的 那么就需要做新旧对比
  238. oldMap := p.GetLastBidStatus(projectId) // 查询出最新的招标状态信息
  239. // 新的
  240. if in.BidStage == nil {
  241. in.BidStage = []string{}
  242. }
  243. newMap := map[string]interface{}{
  244. "bidType": in.BidType,
  245. "bidStage": in.BidStage,
  246. "isWin": in.IsWin,
  247. "channelName": in.ChannelName,
  248. "channelPerson": in.ChannelPerson,
  249. "channelPhone": in.ChannelPhone,
  250. "winner": in.Winner,
  251. }
  252. // 新旧对比 处理成要保存的字段
  253. recordContent, err := processRecordStr(oldMap, newMap)
  254. if err != nil {
  255. return false
  256. }
  257. // 保存
  258. recordData := map[string]interface{}{
  259. "ent_id": p.EntId,
  260. "ent_user_id": p.EntUserId,
  261. "position_id": p.PositionId,
  262. "project_id": projectId,
  263. "record_content": recordContent,
  264. "record_type": RecordTypeBidStatus,
  265. "create_date": date.NowFormat(date.Date_Full_Layout),
  266. }
  267. flag = mysql.InsertBidContent(recordData)
  268. return
  269. }
  270. // GetLastBidStatus 获取投标状态信息
  271. func (p *ParticipateBid) GetLastBidStatus(projectId string) map[string]interface{} {
  272. rs := map[string]interface{}{}
  273. // 查询项目投标信息 区分企业和个人
  274. var result *[]map[string]interface{}
  275. switch p.PositionType {
  276. case PositionTypeEnt:
  277. result = mysql.GetBidContentEnt(projectId, p.EntId)
  278. case PositionTypePersonal:
  279. result = mysql.GetBidContentPersonal(projectId, p.PositionId)
  280. }
  281. if rs != nil && len(*result) > 0 {
  282. content := common.ObjToMap((*result)[0]["record_content"])
  283. if content != nil {
  284. bidStatus := common.ObjToMap((*content)["after"])
  285. if bidStatus != nil {
  286. rs = *bidStatus
  287. }
  288. }
  289. }
  290. return rs
  291. }
  292. func (p ParticipateBid) ParticipateContentFormat(data map[string]interface{}) (rs bxcore.ParticipateContentData) {
  293. if data == nil {
  294. return
  295. }
  296. rs.BidType = common.Int64All(data["bidType"])
  297. rs.ChannelPhone = common.ObjToString(data["channelPhone"])
  298. rs.ChannelPerson = common.ObjToString(data["channelPerson"])
  299. rs.ChannelName = common.ObjToString(data["channelName"])
  300. rs.IsWin = common.Int64All(data["isWin"])
  301. rs.Winner = common.ObjToString(data["winner"])
  302. tmp := data["bidStage"].([]interface{})
  303. rs.BidStage = common.ObjArrToStringArr(tmp)
  304. return rs
  305. }
  306. // GetBidRecords 获取操作记录
  307. func (p *ParticipateBid) GetBidRecords(projectId string, page, pageSize int64) *bxcore.ParticipateRecordsData {
  308. data := bxcore.ParticipateRecordsData{}
  309. var rs *[]map[string]interface{}
  310. var total int64
  311. switch p.PositionType {
  312. case PositionTypeEnt:
  313. // 1. 查询出操作记录
  314. rs, total = mysql.GetBidRecordsEnt(projectId, p.EntId, page, pageSize)
  315. case PositionTypePersonal:
  316. // 个人版不展示姓名
  317. rs, total = mysql.GetBidRecordsPersonal(projectId, p.PositionId, page, pageSize)
  318. }
  319. if rs == nil || len(*rs) == 0 {
  320. return &data
  321. }
  322. data.Total = total
  323. data.List = p.BidRecordsFormat(*rs)
  324. return &data
  325. }
  326. // BidRecordsFormat 获取操作记录格式化
  327. func (p ParticipateBid) BidRecordsFormat(data []map[string]interface{}) []*bxcore.ParticipateRecords {
  328. records := []*bxcore.ParticipateRecords{}
  329. switch p.PositionType {
  330. case PositionTypeEnt:
  331. // 用户id
  332. // 拿到所有的用户id
  333. userIdArr := []string{}
  334. userIdMap := map[int64]string{}
  335. for i := 0; i < len(data); i++ {
  336. userId := common.Int64All(data[i]["ent_user_id"])
  337. if _, ok := userIdMap[userId]; !ok {
  338. userIdMap[userId] = ""
  339. userIdArr = append(userIdArr, fmt.Sprint(userId))
  340. }
  341. }
  342. // 根据id查询出姓名{id:name}
  343. userIdMap = getUserIdName(userIdArr)
  344. // 遍历数据 换成姓名
  345. for i := 0; i < len(data); i++ {
  346. id := common.Int64All(data[i]["ent_user_id"])
  347. person := ""
  348. if name, ok := userIdMap[id]; ok {
  349. person = name
  350. }
  351. tmp := bxcore.ParticipateRecords{
  352. RecordsData: common.ObjToString(data[i]["record_content"]),
  353. UpdateDate: common.ObjToString(data[i]["create_date"]),
  354. UpdatePerson: person,
  355. RecordType: common.Int64All(data[i]["record_type"]),
  356. }
  357. records = append(records, &tmp)
  358. }
  359. case PositionTypePersonal:
  360. // 遍历数据
  361. for i := 0; i < len(data); i++ {
  362. tmp := bxcore.ParticipateRecords{
  363. RecordsData: common.ObjToString(data[i]["record_content"]),
  364. UpdateDate: common.ObjToString(data[i]["create_date"]),
  365. RecordType: common.Int64All(data[i]["record_type"]),
  366. }
  367. records = append(records, &tmp)
  368. }
  369. }
  370. return records
  371. }
  372. // 获取id和姓名的对应关系
  373. func getUserIdName(userIdArr []string) map[int64]string {
  374. rs := map[int64]string{}
  375. if len(userIdArr) > 0 {
  376. userIdStr := strings.Join(userIdArr, ",")
  377. userRs := mysql.GetUserMap(userIdStr)
  378. if userRs == nil || len(*userRs) == 0 {
  379. return rs
  380. }
  381. for i := 0; i < len(*userRs); i++ {
  382. user := (*userRs)[i]
  383. id := common.Int64All(user["id"])
  384. name := common.ObjToString(user["name"])
  385. rs[id] = name
  386. }
  387. }
  388. return rs
  389. }
  390. // 处理操作动作
  391. var (
  392. ParticipateBidContentKey = map[string]string{
  393. "bidType": "投标类型",
  394. "bidStage": "投标项目阶段",
  395. "isWin": "是否中标",
  396. "channelName": "渠道名称",
  397. "channelPerson": "联系人",
  398. "channelPhone": "联系电话",
  399. "winner": "中标单位",
  400. }
  401. BidTypeMap = map[int]interface{}{
  402. BidTypeDirect: "直接投标",
  403. BidTypeChanel: "渠道投标",
  404. }
  405. WinMap = map[int]interface{}{
  406. 1: "是",
  407. 2: "否",
  408. 0: "未选择",
  409. }
  410. )
  411. // 处理操作记录
  412. func processRecordStr(oldMap, newMap map[string]interface{}) (recordContent string, err error) {
  413. actonStr := "%s%s%s"
  414. changeField := []string{}
  415. result := []string{}
  416. for k, fieldName := range ParticipateBidContentKey {
  417. changeStr := ""
  418. switch k {
  419. case "bidType":
  420. oldv := common.IntAll(oldMap[k])
  421. newv := common.IntAll(newMap[k])
  422. if oldv == newv { // 没有改变
  423. continue
  424. }
  425. newBidType := BidTypeMap[newv]
  426. if newBidType == nil {
  427. newBidType = "未选择"
  428. }
  429. if oldv == 0 && newv != 0 { // 说明是新增
  430. changeStr = fmt.Sprintf(actonStr, fieldName, ": (新增)", newBidType)
  431. } else { // 调整
  432. changeStr = fmt.Sprintf(actonStr, fieldName, "(调整):", newBidType)
  433. }
  434. case "isWin":
  435. oldV := common.IntAll(oldMap[k])
  436. newV := common.IntAll(newMap[k])
  437. if oldV == newV { // 没有改变
  438. continue
  439. }
  440. fieldName := fieldName
  441. if common.IntAll(newMap["bidType"]) == BidTypeChanel {
  442. fieldName = fmt.Sprintf("%s%s", "渠道", fieldName)
  443. }
  444. changeStr = fmt.Sprintf(actonStr, fieldName, "(调整) 为", WinMap[newV])
  445. case "bidStage":
  446. bidAction := "%s%s"
  447. bidChangeArr := []string{}
  448. oldSet := gset.NewFrom(oldMap[k])
  449. newSet := gset.NewFrom(newMap[k])
  450. // 判断相等
  451. if oldSet.Equal(newSet) {
  452. continue
  453. }
  454. // 差集计算
  455. // 取消勾选的
  456. cancleSet := oldSet.Diff(newSet)
  457. cancleSet.Iterator(func(v interface{}) bool {
  458. bidChangeArr = append(bidChangeArr, fmt.Sprintf(bidAction, "(取消勾选)", v))
  459. return true
  460. })
  461. // 新增的
  462. addSet := newSet.Diff(oldSet)
  463. addSet.Iterator(func(v interface{}) bool {
  464. bidChangeArr = append(bidChangeArr, fmt.Sprintf(bidAction, "(新增)", v))
  465. fmt.Println(v)
  466. return true
  467. })
  468. tmpStr := strings.Join(bidChangeArr, " ")
  469. changeStr = fmt.Sprintf(actonStr, fieldName, " :", tmpStr)
  470. default:
  471. oldV := common.ObjToString(oldMap[k])
  472. newV := common.ObjToString(newMap[k])
  473. if oldV == newV { // 没有变化
  474. continue
  475. }
  476. changeStr = fmt.Sprintf(actonStr, fieldName, "(调整)为", fmt.Sprintf("\"%s\"", newV))
  477. }
  478. result = append(result, changeStr)
  479. changeField = append(changeField, k)
  480. }
  481. recordMap := map[string]interface{}{
  482. "content": strings.Join(result, ";"), // 变更内容 文字描述
  483. "before": oldMap, // 变更前
  484. "after": newMap, // 变更后
  485. "changeField": changeField, // 涉及变更的字段
  486. }
  487. tmp, err := json.Marshal(recordMap)
  488. if err != nil {
  489. logx.Error("序列化操作记录失败:", err)
  490. return "", err
  491. }
  492. if len(result) == 0 {
  493. logx.Error("没有更新的内容:", recordContent)
  494. err = fmt.Errorf("没有变更的内容,不用更新")
  495. }
  496. return string(tmp), err
  497. }