participateBid.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. package mysql
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/date"
  5. "app.yhyue.com/moapp/jybase/encrypt"
  6. "database/sql"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. IC "jyBXCore/rpc/init"
  11. "jyBXCore/rpc/model/es"
  12. "jyBXCore/rpc/type/bxcore"
  13. "log"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. //投标状态更新内容
  19. type PartStatusContent struct {
  20. BidStage []string `json:"bidStage"` //投标项目阶段
  21. BidType int64 `json:"bidType"` //投标类型
  22. ChannelName string `json:"channelName"` //渠道名称
  23. ChannelPerson string `json:"channelPerson"` //联系人
  24. ChannelPhone string `json:"channelPhone"` //联系电话
  25. IsWin int64 `json:"isWin"` //渠道是否中标
  26. Winner string `json:"winner"` //中标单位
  27. }
  28. //参标
  29. type RecordsContent struct {
  30. After PartStatusContent `json:"after"` //更新前
  31. Before PartStatusContent `json:"before"` //更新后
  32. ChangeField []string `json:"changeField"` //更新字段
  33. Content string `json:"content"` //更新内容
  34. }
  35. var (
  36. PartTable = "participate"
  37. ParticipateBidRecordsTable = "participate_bid_records"
  38. ParticipateUserTable = "participate_user" // 参标用户表
  39. )
  40. //划转参标信息
  41. func TransferParticipateInfo(projectId string, in *bxcore.ParticipateActionReq) error {
  42. defer MC.Catch()
  43. //保存或更新新跟踪人
  44. if !IC.BaseMysql.ExecTx("划转参标信息", func(tx *sql.Tx) bool {
  45. var (
  46. b1 = true
  47. b2, b3 bool
  48. now = time.Now()
  49. content = "%s划转给%s%s"
  50. lastNotes = ",保留原参标人"
  51. fromUserNames []string
  52. ids []int
  53. )
  54. partInfo := IC.BaseMysql.SelectBySqlByTx(tx, "SELECT id,position_id FROM "+ParticipateUserTable+" WHERE project_id = ? AND ent_id = ? AND state > -1", projectId, in.EntId)
  55. if partInfo == nil || len(*partInfo) == 0 {
  56. logx.Info("当前项目不满足划转条件")
  57. return false
  58. } else {
  59. for _, v := range *partInfo {
  60. ids = append(ids, MC.IntAll(v["id"]))
  61. positionId := MC.Int64All(v["position_id"])
  62. userInfo := IC.Middleground.UserCenter.IdentityByPositionId(positionId)
  63. if userInfo.EntUserName != "" {
  64. fromUserNames = append(fromUserNames, userInfo.EntUserName)
  65. }
  66. }
  67. }
  68. if len(fromUserNames) == 0 {
  69. logx.Info("原参标人信息查询有误")
  70. return false
  71. }
  72. //是否保留原参标人
  73. if !in.IsRetain {
  74. lastNotes = ""
  75. //不保留 原参标人,获取把原参标人信息
  76. //当前项目有参标人 更新参标人状态
  77. b1 = IC.BaseMysql.UpdateByTx(tx, ParticipateUserTable, map[string]interface{}{
  78. "ent_id": in.EntId,
  79. "project_id": projectId,
  80. }, map[string]interface{}{
  81. "state": -1,
  82. "mark": -2, //0:参标;1:被划入;-1:终止参标;-2:被划走
  83. "update_date": date.FormatDate(&now, date.Date_Full_Layout),
  84. })
  85. }
  86. //查询划转人信息
  87. entUserId, _ := strconv.ParseInt(in.ToEntUserId, 10, 64)
  88. userInfo := IC.Middleground.UserCenter.IdentityByEntUserId(entUserId)
  89. positionId := userInfo.PositionId
  90. content = fmt.Sprintf(content, strings.Join(fromUserNames, ","), userInfo.EntUserName, lastNotes)
  91. //划转记录
  92. b2 = IC.BaseMysql.InsertByTx(tx, ParticipateBidRecordsTable, map[string]interface{}{
  93. "ent_id": in.EntId,
  94. "ent_user_id": entUserId,
  95. "position_id": positionId,
  96. "project_id": projectId,
  97. "record_type": 0,
  98. "record_content": content,
  99. "create_date": date.FormatDate(&now, date.Date_Full_Layout),
  100. }) > 0
  101. //保存参标--participate_user
  102. //查看是否参标过当前项目
  103. if c := IC.BaseMysql.CountBySql("SELECT count(id) FROM "+ParticipateUserTable+" WHERE position_id = ? AND project_id = ? AND ent_id = ?", positionId, projectId, in.EntId); c > 0 {
  104. //更新
  105. b3 = IC.BaseMysql.UpdateByTx(tx, ParticipateUserTable, map[string]interface{}{
  106. "position_id": positionId,
  107. "project_id": projectId,
  108. "ent_id": in.EntId,
  109. }, map[string]interface{}{
  110. "state": 0,
  111. "mark": 1, //0:参标;1:被划入;-1:终止参标;-2:被划走
  112. "update_date": date.FormatDate(&now, date.Date_Full_Layout),
  113. })
  114. } else {
  115. //保存
  116. b3 = IC.BaseMysql.InsertByTx(tx, ParticipateUserTable, map[string]interface{}{
  117. "ent_id": in.EntId,
  118. "ent_user_id": entUserId,
  119. "position_id": positionId,
  120. "project_id": projectId,
  121. "state": 0,
  122. "mark": 1, //0:参标;1:被划入;-1:终止参标;-2:被划走
  123. "create_date": date.FormatDate(&now, date.Date_Full_Layout),
  124. "update_date": date.FormatDate(&now, date.Date_Full_Layout),
  125. }) > 0
  126. }
  127. return b1 && b2 && b3
  128. }) {
  129. logx.Info(in.PositionId, "---终止---", projectId)
  130. return fmt.Errorf("终止参标更新信息出错")
  131. }
  132. return nil
  133. }
  134. //终止参标
  135. func CancelParticipateInfo(in *bxcore.ParticipateActionReq, roleId int64) error {
  136. defer MC.Catch()
  137. if !IC.BaseMysql.ExecTx("终止参标", func(tx *sql.Tx) bool {
  138. var (
  139. b1, b2 bool
  140. now = time.Now()
  141. tip = "终止参标(被)"
  142. )
  143. //管理员终止:当前项目 其他参标人也被终止
  144. query := map[string]interface{}{
  145. "project_id": in.ProjectIds,
  146. "ent_id": in.EntId,
  147. }
  148. //个人终止:仅仅终止本人参标项目
  149. if roleId == 0 {
  150. query["position_id"] = in.PositionId
  151. tip = "终止参标"
  152. }
  153. insert := map[string]interface{}{
  154. "state": -1,
  155. "mark": -1, //0:参标;1:被划入;-1:终止参标;-2:被划走
  156. "update_date": date.FormatDate(&now, date.Date_Full_Layout),
  157. }
  158. //更新参标participate_user
  159. b1 = IC.BaseMysql.UpdateByTx(tx, ParticipateUserTable, query, insert)
  160. //保存参标记录--participate_bid_records
  161. b2 = IC.BaseMysql.InsertByTx(tx, ParticipateBidRecordsTable, map[string]interface{}{
  162. "ent_id": in.EntId,
  163. "ent_user_id": in.EntUserId,
  164. "position_id": in.PositionId,
  165. "project_id": in.ProjectIds,
  166. "record_type": 0,
  167. "record_content": tip,
  168. "create_date": date.FormatDate(&now, date.Date_Full_Layout),
  169. }) > 0
  170. return b1 && b2
  171. }) {
  172. logx.Info(in.PositionId, "---终止---", in.ProjectIds)
  173. return fmt.Errorf("终止参标更新信息出错")
  174. }
  175. return nil
  176. }
  177. //保存参标信息
  178. func SaveParticipateInfo(in *bxcore.ParticipateActionReq) error {
  179. defer MC.Catch()
  180. if !IC.BaseMysql.ExecTx("保存|更新参标信息及保存参标记录", func(tx *sql.Tx) bool {
  181. var (
  182. b1, b2, b3 bool
  183. now = time.Now()
  184. )
  185. //保存参标--participate_user
  186. //查看是否参标过当前项目
  187. if c := IC.BaseMysql.CountBySql("SELECT count(id) FROM "+ParticipateUserTable+" WHERE position_id = ? AND project_id = ? AND ent_id = ?", in.PositionId, in.ProjectIds, in.EntId); c > 0 {
  188. //更新
  189. b1 = IC.BaseMysql.UpdateByTx(tx, ParticipateUserTable, map[string]interface{}{
  190. "state": 0,
  191. "mark": 0,
  192. "update_date": date.FormatDate(&now, date.Date_Full_Layout),
  193. }, map[string]interface{}{
  194. "position_id": in.PositionId,
  195. "ent_id": in.EntId,
  196. "project_id": in.ProjectIds,
  197. })
  198. } else {
  199. //保存
  200. b1 = IC.BaseMysql.InsertByTx(tx, ParticipateUserTable, map[string]interface{}{
  201. "ent_id": in.EntId,
  202. "ent_user_id": in.EntUserId,
  203. "position_id": in.PositionId,
  204. "project_id": in.ProjectIds,
  205. "state": 0,
  206. "mark": 0,
  207. "create_date": date.FormatDate(&now, date.Date_Full_Layout),
  208. "update_date": date.FormatDate(&now, date.Date_Full_Layout),
  209. }) > 0
  210. }
  211. if !b1 {
  212. return false
  213. }
  214. //保存参标记录participate_bid_records
  215. b2 = IC.BaseMysql.InsertByTx(tx, ParticipateBidRecordsTable, map[string]interface{}{
  216. "ent_id": in.EntId,
  217. "ent_user_id": in.EntUserId,
  218. "position_id": in.PositionId,
  219. "project_id": in.ProjectIds,
  220. "record_type": 0,
  221. "record_content": "参标",
  222. "create_date": date.FormatDate(&now, date.Date_Full_Layout),
  223. }) > 0
  224. if !b2 {
  225. return false
  226. }
  227. //保存或更新项目信息
  228. //有问题 其他回滚,项目信息不用回滚
  229. b3 = UpdateProjectInfo(in.ProjectIds, es.GetProjectInfo(in.ProjectIds), es.GetBiddingInfo(in.BidIds)) == nil
  230. return b1 && b2 && b3
  231. }) {
  232. logx.Info(in.PositionId, "---保存---", in.BidIds)
  233. return fmt.Errorf("保存参标信息出错")
  234. }
  235. return nil
  236. }
  237. //查询当前招标信息是否已被参标
  238. func IsParticipatedByBidId(in *bxcore.ParticipateActionReq) bool {
  239. defer MC.Catch()
  240. //如果不允许多人参标 当前项目是否已经有企业其他人员参标
  241. query := fmt.Sprintf(`SELECT count(id) FROM `+ParticipateUserTable+` WHERE %s AND project_id = %s AND state >-1`, "%s", in.BidIds)
  242. if in.PositionType > 0 { //企业版
  243. query = fmt.Sprintf(query, fmt.Sprintf("ent_id = %d", in.EntId))
  244. } else { //个人版
  245. query = fmt.Sprintf(query, fmt.Sprintf("position_id = %d", in.PositionId))
  246. }
  247. return IC.BaseMysql.CountBySql(query) > 0
  248. }
  249. //获取参标权限
  250. func GetParticipateIsAllow(query map[string]interface{}) (b bool) {
  251. defer MC.Catch()
  252. if info, ok := IC.Mgo.FindOne(PartTable, query); ok {
  253. if info != nil {
  254. if (*info)["i_isallow"] != nil {
  255. b = MC.IntAll((*info)["i_isallow"]) > 0
  256. }
  257. }
  258. }
  259. return
  260. }
  261. //更新设置信息
  262. func UpdateParticipateSetInfo(in *bxcore.ParticipateSetUpInfoReq) error {
  263. defer MC.Catch()
  264. query := map[string]interface{}{
  265. "i_positionid": in.PositionId,
  266. }
  267. if in.PositionType > 0 {
  268. //企业版 判断是否是管理员
  269. //判断用户身份
  270. userInfo := IC.Middleground.PowerCheckCenter.Check(in.AppId, in.UserId, in.NewUserId, in.AccountId, in.EntId, in.PositionType, in.PositionId)
  271. if userInfo.Ent.EntRoleId == 0 {
  272. return fmt.Errorf("当前企业身份无权限")
  273. }
  274. query["i_entid"] = in.EntId
  275. }
  276. upsert := map[string]interface{}{
  277. "i_entid": in.EntId,
  278. "i_entuserid": in.EntUserId,
  279. "i_positionid": in.PositionId,
  280. "l_createtime": time.Now().Unix(),
  281. }
  282. if in.IsAllow != "" {
  283. isAllow, _ := strconv.Atoi(in.IsAllow)
  284. upsert["i_isallow"] = isAllow
  285. }
  286. if len(in.BidType) > 0 {
  287. upsert["o_bidtype"] = in.BidType
  288. }
  289. if len(in.RemindRule) > 0 {
  290. upsert["o_remindrule"] = in.RemindRule
  291. }
  292. if ok := IC.Mgo.Update(PartTable, query, map[string]interface{}{
  293. "$set": upsert,
  294. }, true, false); ok {
  295. return nil
  296. }
  297. return fmt.Errorf("更新失败")
  298. }
  299. //查询企业|个人参标设置信息
  300. func GetParticipateSetInfo(in *bxcore.ParticipateSetUpInfoReq) (*bxcore.ParticipateSetUpInfo, error) {
  301. defer MC.Catch()
  302. query := map[string]interface{}{
  303. "i_positionid": in.PositionId,
  304. }
  305. if in.PositionType > 0 {
  306. query["i_entid"] = in.EntId
  307. }
  308. if setInfo, ok := IC.Mgo.FindOne(PartTable, query); ok {
  309. var (
  310. isAllow = ""
  311. bidType []*bxcore.BidTypeReq
  312. remindRule []*bxcore.RemindRuleReq
  313. )
  314. bidType = append(bidType, &bxcore.BidTypeReq{
  315. Name: "直接投标",
  316. Content: []string{"未报名", "已报名", "投标决策", "编制投标文件", "递交投标文件", "中标公示", "签合同", "已结束"},
  317. }, &bxcore.BidTypeReq{
  318. Name: "渠道投标",
  319. Content: []string{"已报名", "签合同", "已结束"},
  320. })
  321. remindRule = append(remindRule, &bxcore.RemindRuleReq{
  322. BidState: "直接投标",
  323. Remainder: 72,
  324. Node: "编制投标文件",
  325. })
  326. if setInfo != nil {
  327. if (*setInfo)["i_isallow"] != nil {
  328. isAllow = strconv.Itoa(MC.IntAll((*setInfo)["i_isallow"]))
  329. }
  330. if (*setInfo)["bidType"] != nil {
  331. if sbb, err := json.Marshal((*setInfo)["o_bidtype"]); err == nil {
  332. if err := json.Unmarshal(sbb, &bidType); err != nil {
  333. logx.Info("bidType json un err:", err.Error())
  334. return nil, err
  335. }
  336. } else {
  337. logx.Info("bidType json err:", err.Error())
  338. return nil, err
  339. }
  340. }
  341. if (*setInfo)["o_remindrule"] != nil {
  342. if sbr, err := json.Marshal((*setInfo)["o_remindrule"]); err == nil {
  343. if err := json.Unmarshal(sbr, &remindRule); err != nil {
  344. logx.Info("remindRule json un err:", err.Error())
  345. return nil, err
  346. }
  347. } else {
  348. logx.Info("remindRule json err:", err.Error())
  349. return nil, err
  350. }
  351. }
  352. }
  353. return &bxcore.ParticipateSetUpInfo{
  354. IsAllow: isAllow,
  355. BidType: bidType,
  356. RemindRule: remindRule,
  357. }, nil
  358. }
  359. return nil, nil
  360. }
  361. //保存或更新tidb 项目信息
  362. func UpdateProjectInfo(id string, pInfo map[string]interface{}, bInfo map[string]interface{}) error {
  363. //id 项目id
  364. //name 项目名称
  365. //area 省份
  366. //city 城市
  367. //buyer 采购单位
  368. //budget 预算
  369. //bid_open_time 开标时间
  370. //bid_time 招标时间 bidding表
  371. //bid_end_time 开标结束时间 bidding表
  372. //pici 批次 轮询更新数据
  373. //
  374. projectInfo := map[string]interface{}{
  375. "id": id,
  376. "name": MC.ObjToString(pInfo["projectname"]),
  377. "area": MC.ObjToString(pInfo["area"]),
  378. "city": MC.ObjToString(pInfo["city"]),
  379. "buyer": MC.ObjToString(pInfo["buyer"]),
  380. "budget": MC.Int64All(pInfo["budget"]),
  381. }
  382. if pInfo["bidopentime"] != nil {
  383. openTime := pInfo["bidopentime"]
  384. projectInfo["bid_open_time"] = date.FormatDateWithObj(&openTime, date.Date_Full_Layout)
  385. }
  386. if pInfo["pici"] != nil {
  387. pici := pInfo["pici"]
  388. projectInfo["pici"] = date.FormatDateWithObj(&pici, date.Date_Full_Layout)
  389. }
  390. // 项目表:zbtime 招标时间是 biding表:publishtime发布时间
  391. if pInfo["zbtime"] != nil {
  392. bidTime := pInfo["zbtime"]
  393. projectInfo["bid_time"] = date.FormatDateWithObj(&bidTime, date.Date_Full_Layout)
  394. }
  395. if bInfo["bidendtime"] != nil {
  396. bidEndTime := bInfo["bidendtime"]
  397. projectInfo["bid_end_time"] = date.FormatDateWithObj(&bidEndTime, date.Date_Full_Layout)
  398. }
  399. if c := IC.BaseMysql.CountBySql(`SELECT COUNT(id) FROM project WHERE id = ?`, id); c > 0 {
  400. if ok := IC.BaseMysql.Update("project", map[string]interface{}{
  401. "id": id,
  402. }, projectInfo); !ok {
  403. return fmt.Errorf("项目信息更新异常", id)
  404. }
  405. } else {
  406. if i := IC.BaseMysql.Insert("project", projectInfo); i < 0 {
  407. return fmt.Errorf("项目信息插入异常", id)
  408. }
  409. }
  410. return nil
  411. }
  412. //参标列表其他条件
  413. func ParticipateListSql(in *bxcore.ParticipateListReq) string {
  414. //b project表
  415. //a participate_user表
  416. now := time.Now()
  417. nowDate := date.FormatDate(&now, date.Date_Full_Layout)
  418. //查询tidb base_service.project
  419. conditionSql := ` WHERE 1=1 `
  420. //地区
  421. if in.Area != "" {
  422. conditionSql += fmt.Sprintf(" AND pt.area IN ('%s') ", strings.ReplaceAll(in.Area, ",", "','"))
  423. }
  424. //城市
  425. if in.City != "" {
  426. conditionSql += fmt.Sprintf(" AND pt.city IN ('%s') ", strings.ReplaceAll(in.City, ",", "','"))
  427. }
  428. //关键词
  429. if in.Keywords != "" {
  430. kSql := ` AND (`
  431. for kk, kv := range strings.Split(in.Keywords, " ") {
  432. if kk > 0 {
  433. kSql += " OR "
  434. }
  435. kSql += " pt.name like '%" + kv + "%'"
  436. }
  437. kSql += `)`
  438. conditionSql += kSql
  439. }
  440. //招标日期
  441. if in.BidTime != "" && strings.Contains(in.BidTime, "-") {
  442. startTime := strings.Split(in.BidTime, "-")[0]
  443. entTime := strings.Split(in.BidTime, "-")[1]
  444. if startTime != "" {
  445. startTimeInt, _ := strconv.ParseInt(startTime, 10, 64)
  446. conditionSql += ` AND pt.bid_time > '` + date.FormatDateByInt64(&startTimeInt, date.Date_Full_Layout) + `'`
  447. }
  448. if entTime != "" {
  449. entTimeInt, _ := strconv.ParseInt(entTime, 10, 64)
  450. conditionSql += ` AND pt.bid_time < '` + date.FormatDateByInt64(&entTimeInt, date.Date_Full_Layout) + `'`
  451. }
  452. }
  453. //招标截止日期
  454. if in.BidEndTime != "" {
  455. //投标截止日期规则:
  456. //1、开始时间小于当前时间 ,结束时间大于当前时间,投标截止状态按钮未截止和已截止可用;
  457. //2、结束时间小于当前时间|开始时间大于当前时间,投标截止状态按钮未截止和已截止不可用;
  458. //3、需要前端做成连动
  459. startTime := strings.Split(in.BidEndTime, "-")[0]
  460. endTime := strings.Split(in.BidEndTime, "-")[1]
  461. startTimeInt, _ := strconv.ParseInt(startTime, 10, 64)
  462. endTimeInt, _ := strconv.ParseInt(endTime, 10, 64)
  463. if startTimeInt > 0 && endTimeInt > 0 && startTimeInt > endTimeInt {
  464. logx.Info(fmt.Sprintf("投标截止日期 %d 开始时间 大于 结束时间%d!!!", startTimeInt, endTimeInt))
  465. } else {
  466. switch in.BidEndStatus {
  467. case 0:
  468. if startTimeInt > 0 {
  469. conditionSql += ` AND pt.bid_end_time > '` + date.FormatDateByInt64(&startTimeInt, date.Date_Full_Layout) + `'`
  470. }
  471. if endTimeInt > 0 {
  472. conditionSql += ` AND pt.bid_end_time < '` + date.FormatDateByInt64(&endTimeInt, date.Date_Full_Layout) + `'`
  473. }
  474. case 1: //投标截止状态:1:未截止;2:已截止;3:终止参标
  475. //未截止:
  476. var (
  477. endBool = true
  478. )
  479. //如果结束时间存在且小于当前时间,投标截止日期 范围都是已截止 不会存在未截止的数据
  480. if endTimeInt > 0 {
  481. conditionSql += ` AND pt.bid_end_time < '` + date.FormatDateByInt64(&endTimeInt, date.Date_Full_Layout) + `'`
  482. endBool = endTimeInt > now.Unix()
  483. }
  484. //开始时间小于 当前时间
  485. if endBool && now.Unix() > startTimeInt {
  486. startTimeInt = now.Unix()
  487. }
  488. //存在开始时间为0的情况
  489. if startTimeInt > 0 {
  490. conditionSql += ` AND pt.bid_end_time > '` + date.FormatDateByInt64(&startTimeInt, date.Date_Full_Layout) + `'`
  491. }
  492. case 2: //投标截止状态:1:未截止;2:已截止;3:终止参标
  493. //如果开始时间存在且大于当前时间,投标截止日期 范围都是未截止 不会存在已截止的数据
  494. var (
  495. startBool = true
  496. )
  497. if startTimeInt > 0 {
  498. conditionSql += ` AND pt.bid_end_time > '` + date.FormatDateByInt64(&startTimeInt, date.Date_Full_Layout) + `'`
  499. startBool = startTimeInt < now.Unix()
  500. }
  501. if startBool && (endTimeInt == 0 || now.Unix() < endTimeInt) {
  502. endTimeInt = now.Unix()
  503. }
  504. //存在结束时间为0的情况
  505. if endTimeInt > 0 {
  506. conditionSql += ` AND pt.bid_end_time < '` + date.FormatDateByInt64(&endTimeInt, date.Date_Full_Layout) + `'`
  507. }
  508. case 3:
  509. conditionSql += ` AND pug.state < 0 `
  510. }
  511. }
  512. } else if in.BidEndStatus > 0 { //投标截止状态1:未截止;2:已截止;3:终止参标
  513. switch in.BidEndStatus {
  514. case 1:
  515. conditionSql += ` AND pt.bid_end_time > '` + nowDate + `'`
  516. case 2:
  517. conditionSql += ` AND pt.bid_end_time < '` + nowDate + `'`
  518. case 3:
  519. conditionSql += ` AND pug.state < 0 `
  520. }
  521. }
  522. //开标时间
  523. if in.BidOpenTime != "" {
  524. startTime := strings.Split(in.BidOpenTime, "-")[0]
  525. entTime := strings.Split(in.BidOpenTime, "-")[1]
  526. if startTime != "" {
  527. startTimeInt, _ := strconv.ParseInt(startTime, 10, 64)
  528. conditionSql += ` AND pt.bid_open_time > '` + date.FormatDateByInt64(&startTimeInt, date.Date_Full_Layout) + `'`
  529. }
  530. if entTime != "" {
  531. entTimeInt, _ := strconv.ParseInt(entTime, 10, 64)
  532. conditionSql += ` AND pt.bid_open_time < '` + date.FormatDateByInt64(&entTimeInt, date.Date_Full_Layout) + `'`
  533. }
  534. }
  535. //开标状态1:未开标;2:已开标
  536. if in.BidOpenStatus > 0 {
  537. switch in.BidOpenStatus {
  538. case 1:
  539. conditionSql += ` AND pt.bid_open_time > '` + nowDate + `'`
  540. case 2:
  541. conditionSql += ` AND pt.bid_open_time < '` + nowDate + `'`
  542. }
  543. }
  544. //参标人 管理员权限
  545. if in.EntUserIds != "" && in.PositionType > 0 {
  546. conditionSql += ` AND (`
  547. for k, v := range strings.Split(in.EntUserIds, ",") {
  548. if k > 0 {
  549. conditionSql += " OR "
  550. }
  551. conditionSql += ` FIND_IN_SET(` + v + ` , pug.ent_user_id) `
  552. }
  553. conditionSql += `)`
  554. }
  555. //默认按照投标截止日期正序排列、1:开标时间正序、2:更新状态时间倒序
  556. switch in.OrderNum {
  557. case 1:
  558. conditionSql += ` ORDER BY pt.bid_open_time ASC`
  559. case 2:
  560. conditionSql += ` ORDER BY pug.update_date DESC`
  561. default:
  562. conditionSql += ` ORDER BY pt.bid_end_time ASC`
  563. }
  564. logx.Info(conditionSql)
  565. return conditionSql
  566. }
  567. //个人或员工查询参标列表
  568. func SingleParticipateList(in *bxcore.ParticipateListReq, conditionSql string) (data *bxcore.ParticipateData, err error) {
  569. defer MC.Catch()
  570. data = &bxcore.ParticipateData{
  571. Count: 0,
  572. List: []*bxcore.ParticipateList{},
  573. }
  574. //员工|个人列表
  575. singlePersonSql := `SELECT %s FROM ` + ParticipateUserTable + ` pug LEFT JOIN project pt ON pug.project_id = pt.id WHERE pug.position_id = ? `
  576. singlePersonSql += conditionSql
  577. countSql := fmt.Sprintf(singlePersonSql, " COUNT(pt.id) ")
  578. count := IC.BaseMysql.CountBySql(countSql, in.PositionId)
  579. if count > 0 {
  580. data.Count = count
  581. listSql := fmt.Sprintf(singlePersonSql, " pug.update_date,pt.* ")
  582. //分页
  583. listSql += fmt.Sprintf(` LIMIT %d,%d`, in.PageNum, in.PageSize)
  584. list := IC.BaseMysql.SelectBySql(listSql, in.PositionId)
  585. if list != nil && len(*list) > 0 {
  586. for _, v := range *list {
  587. data.List = append(data.List, &bxcore.ParticipateList{
  588. Id: encrypt.EncodeArticleId2ByCheck(MC.ObjToString(v["id"])),
  589. ProjectName: MC.ObjToString(v["name"]),
  590. Buyer: MC.ObjToString(v["buyer"]),
  591. Budget: MC.ObjToString(v["budget"]),
  592. BidTime: MC.ObjToString(v["bid_time"]),
  593. BidEndTime: MC.ObjToString(v["bid_end_time"]),
  594. BidOpenTime: MC.ObjToString(v["bid_open_time"]),
  595. UpdateStatusTime: MC.ObjToString(v["update_date"]),
  596. UpdateStatusCon: GetParticipateContent("s", in.PositionId, MC.ObjToString(v["id"])), //查询最后一次 投标状态更新,
  597. })
  598. }
  599. return data, nil
  600. }
  601. return nil, fmt.Errorf("数据异常")
  602. }
  603. return data, nil
  604. }
  605. //管理员获取参标列表数据
  606. func AdminParticipateList(in *bxcore.ParticipateListReq, conditionSql string) (data *bxcore.ParticipateData, err error) {
  607. defer MC.Catch()
  608. data = &bxcore.ParticipateData{
  609. Count: 0,
  610. List: []*bxcore.ParticipateList{},
  611. }
  612. adminSql := `SELECT %s FROM (SELECT pu.ent_id, pu.project_id, GROUP_CONCAT(pu.ent_user_id SEPARATOR ',') ent_user_id, MAX(pu.update_date) update_date,MAX(pu.state) state FROM ` + ParticipateUserTable + ` pu WHERE pu.ent_id = ? AND NOT EXISTS ( SELECT 1 FROM ` + ParticipateUserTable + ` WHERE project_id = pu.project_id AND state > pu. state ) GROUP BY pu.project_id ) pug LEFT JOIN project pt ON pug.project_id = pt.id`
  613. adminCountSql := fmt.Sprintf(adminSql, "COUNT(pt.id)") + conditionSql
  614. log.Println(adminCountSql)
  615. count := IC.BaseMysql.CountBySql(adminCountSql, in.EntId)
  616. if count > 0 {
  617. data.Count = count
  618. adminListSql := fmt.Sprintf(adminSql, "pt.*, pug.ent_user_id,pug.update_date") + conditionSql
  619. list := IC.BaseMysql.SelectBySql(adminListSql, in.EntId)
  620. if list != nil && len(*list) > 0 {
  621. for _, v := range *list {
  622. data.List = append(data.List, &bxcore.ParticipateList{
  623. Id: encrypt.EncodeArticleId2ByCheck(MC.ObjToString(v["id"])),
  624. ProjectName: MC.ObjToString(v["name"]),
  625. Buyer: MC.ObjToString(v["buyer"]),
  626. Budget: MC.ObjToString(v["budget"]),
  627. BidTime: MC.ObjToString(v["bid_time"]),
  628. BidEndTime: MC.ObjToString(v["bid_end_time"]),
  629. BidOpenTime: MC.ObjToString(v["bid_open_time"]),
  630. UpdateStatusTime: MC.ObjToString(v["update_date"]),
  631. UpdateStatusCon: GetParticipateContent("e", in.EntId, MC.ObjToString(v["id"])), //查询最后一次 投标状态更新
  632. Participants: GetParticipateUserName(MC.ObjToString(v["ent_user_id"])), //参标人信息
  633. })
  634. }
  635. return data, nil
  636. }
  637. return nil, fmt.Errorf("数据异常")
  638. }
  639. return data, nil
  640. }
  641. //获取最新参标 更新内容
  642. func GetParticipateContent(s string, id int64, projectId string) string {
  643. identitySql := `ent_id = ?`
  644. if s == "s" {
  645. identitySql = `position_id = ?`
  646. }
  647. recordsSql := `SELECT record_content,record_type FROM ` + ParticipateBidRecordsTable + ` WHERE ` + identitySql + ` AND project_id = ? ORDER BY create_date DESC LIMIT 1;`
  648. records := IC.BaseMysql.SelectBySql(recordsSql, id, projectId)
  649. if records != nil && len(*records) > 0 {
  650. rec := (*records)[0]
  651. switch MC.IntAll(rec["record_type"]) {
  652. case 0:
  653. return MC.ObjToString(rec["record_content"])
  654. case 1:
  655. recordContent := *MC.ObjToMap(rec["record_content"])
  656. rb, err := json.Marshal(recordContent)
  657. if err != nil {
  658. log.Println(err.Error())
  659. return ""
  660. }
  661. var rc = RecordsContent{
  662. After: PartStatusContent{},
  663. Before: PartStatusContent{},
  664. }
  665. err1 := json.Unmarshal(rb, &rc)
  666. if err1 == nil {
  667. return rc.Content
  668. }
  669. }
  670. }
  671. return ""
  672. }
  673. //根据ent_user_id 获取参标人昵称,企业管理员现在都是“我”
  674. func GetParticipateUserName(entUserId string) string {
  675. if entUserId != "" {
  676. var userNames []string
  677. for _, v := range strings.Split(entUserId, ",") {
  678. entUserInfos := IC.MainMysql.SelectBySql(`SELECT * FROM entniche_user WHERE id = ?`, v)
  679. if entUserInfos != nil && len(*entUserInfos) > 0 {
  680. entUserInfo := (*entUserInfos)[0]
  681. if entUserInfo["name"] != nil {
  682. if userName := MC.ObjToString(entUserInfo["name"]); userName != "" {
  683. userNames = append(userNames, userName)
  684. }
  685. }
  686. }
  687. }
  688. return strings.Join(userNames, ",")
  689. }
  690. return ""
  691. }
  692. // GetBidContentEnt 企业版 获取投标状态更新内容
  693. func GetBidContentEnt(projectId string, entId int64) *[]map[string]interface{} {
  694. // record_type '默认0:参标、划转、取消参标;1:投标状态更新存储'
  695. query := "SELECT * FROM " + ParticipateBidRecordsTable + " where project_id=? and ent_id=? and record_type=1 order by create_date desc limit 1; "
  696. return IC.BaseMysql.SelectBySql(query, projectId, entId)
  697. }
  698. // GetBidContentPersonal 个人版 获取投标状态更新内容
  699. func GetBidContentPersonal(projectId string, positionId int64) *[]map[string]interface{} {
  700. query := "SELECT * FROM " + ParticipateBidRecordsTable + " where project_id=? and position_id=? and record_type=1 order by create_date desc limit 1;"
  701. return IC.BaseMysql.SelectBySql(query, projectId, positionId)
  702. }
  703. // UpdateBidContent 更新投标状态信息以及操作记录
  704. func UpdateBidContent(recordData map[string]interface{}) (flag bool) {
  705. r2 := IC.BaseMysql.Insert(ParticipateBidRecordsTable, recordData)
  706. return r2 > 0
  707. }
  708. // InsertBidContent 新增投标状态信息及操作记录
  709. func InsertBidContent(recordData map[string]interface{}) (flag bool) {
  710. r2 := IC.BaseMysql.Insert(ParticipateBidRecordsTable, recordData)
  711. return r2 > 0
  712. }
  713. // GetBidRecordsEnt 获取操作记录列表企业
  714. func GetBidRecordsEnt(projectId string, entId, page, pageSize int64) (rs *[]map[string]interface{}, total int64) {
  715. query := "SELECT * FROM " + ParticipateBidRecordsTable + " where project_id=? and ent_id=? order by create_date desc limit ?,?"
  716. countQuery := "SELECT count(id) FROM " + ParticipateBidRecordsTable + " where project_id=? and ent_id=? ;"
  717. rs = IC.BaseMysql.SelectBySql(query, projectId, entId, (page-1)*pageSize, pageSize)
  718. total = IC.BaseMysql.CountBySql(countQuery, projectId, entId)
  719. return rs, total
  720. }
  721. // GetBidRecordsPersonal 获取操作记录列表个人
  722. func GetBidRecordsPersonal(projectId string, positionId, page, pageSize int64) (rs *[]map[string]interface{}, total int64) {
  723. query := "SELECT * FROM " + ParticipateBidRecordsTable + " where project_id=? and position_id=? order by create_date desc limit ?,?;"
  724. countQuery := "SELECT count(id) FROM " + ParticipateBidRecordsTable + " where project_id=? and position_id=? ;"
  725. rs = IC.BaseMysql.SelectBySql(query, projectId, positionId, (page-1)*pageSize, pageSize)
  726. total = IC.BaseMysql.CountBySql(countQuery, projectId, positionId)
  727. return rs, total
  728. }
  729. // GetUserMap 查询用户id的姓名
  730. func GetUserMap(userIds string) (rs *[]map[string]interface{}) {
  731. query := fmt.Sprintf("select id,name from entniche_user where id in (%s)", userIds)
  732. rs = IC.MainMysql.SelectBySql(query)
  733. return rs
  734. }
  735. // CheckParticipateManager 验证项目id是否是该管理员企业下的参标项目
  736. func CheckParticipateManager(projectId string, entId int64, valid bool) (flag bool) {
  737. stateStr := "" // 是否需要验证是正在参标
  738. if valid {
  739. stateStr = " and state=0"
  740. }
  741. query := "SELECT count(id) FROM " + ParticipateUserTable + " where project_id=? and ent_id=?" + stateStr
  742. return IC.BaseMysql.CountBySql(query, projectId, entId) > 0
  743. }
  744. // CheckParticipateEntUser 验证项目id是否是该企业用户参标的项目
  745. func CheckParticipateEntUser(projectId string, entUserId int64, valid bool) (flag bool) {
  746. stateStr := "" // 是否需要验证是正在参标
  747. if valid {
  748. stateStr = " and state=0"
  749. }
  750. query := "SELECT count(id) FROM " + ParticipateUserTable + " where project_id=? and ent_user_id=?" + stateStr
  751. return IC.BaseMysql.CountBySql(query, projectId, entUserId) > 0
  752. }
  753. // CheckParticipatePersonal 查询项目id是否是该用户参标项目
  754. func CheckParticipatePersonal(projectId string, positionId int64, valid bool) (flag bool) {
  755. stateStr := "" // 是否需要验证是正在参标 终止参标的用户还能查看记录,但是不能更新状态
  756. if valid {
  757. stateStr = " and state=0"
  758. }
  759. query := "SELECT count(id) FROM " + ParticipateUserTable + " where project_id=? and position_id=?" + stateStr
  760. return IC.BaseMysql.CountBySql(query, projectId, positionId) > 0
  761. }