participateBid.go 18 KB

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