participateBid.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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"` //投标类型1:直接投标;2:渠道投标
  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. EntnicheUserTable = "entniche_user" // 企业用户表
  40. )
  41. //划转参标信息
  42. func TransferParticipateInfo(projectId string, in *bxcore.ParticipateActionReq) error {
  43. defer MC.Catch()
  44. //保存或更新新跟踪人
  45. if !IC.BaseMysql.ExecTx("划转参标信息", func(tx *sql.Tx) bool {
  46. var (
  47. b1 = true
  48. b2, b3 bool
  49. now = time.Now()
  50. content = "%s划转给%s%s"
  51. lastNotes = ",保留原参标人"
  52. fromEntUserNames, toEntUserNames, toEntUserIds []string
  53. ids []int
  54. )
  55. partInfo := IC.BaseMysql.SelectBySqlByTx(tx, "SELECT id,position_id FROM "+ParticipateUserTable+" WHERE project_id = ? AND ent_id = ? AND state > -1", projectId, in.EntId)
  56. if partInfo == nil || len(*partInfo) == 0 {
  57. logx.Info("当前项目不满足划转条件")
  58. return false
  59. } else {
  60. for _, v := range *partInfo {
  61. ids = append(ids, MC.IntAll(v["id"]))
  62. positionId := MC.Int64All(v["position_id"])
  63. userInfo := IC.Middleground.UserCenter.IdentityByPositionId(positionId)
  64. if userInfo.EntUserName != "" {
  65. fromEntUserNames = append(fromEntUserNames, userInfo.EntUserName)
  66. }
  67. }
  68. }
  69. if len(fromEntUserNames) == 0 {
  70. logx.Info("原参标人信息查询有误")
  71. return false
  72. }
  73. //是否保留原参标人
  74. if !in.IsRetain {
  75. lastNotes = ""
  76. //不保留 原参标人,获取把原参标人信息
  77. //当前项目有参标人 更新参标人状态
  78. b1 = IC.BaseMysql.UpdateByTx(tx, ParticipateUserTable, map[string]interface{}{
  79. "ent_id": in.EntId,
  80. "project_id": projectId,
  81. }, map[string]interface{}{
  82. "state": -1,
  83. "mark": -2, //0:参标;1:被划入;-1:终止参标;-2:被划走
  84. "update_date": date.FormatDate(&now, date.Date_Full_Layout),
  85. })
  86. }
  87. //移动端 划转对象是多选
  88. //划转对象entuserid 解密
  89. for _, toEntUserId := range strings.Split(in.ToEntUserId, ",") {
  90. toEntUserId = encrypt.SE.Decode4HexByCheck(toEntUserId)
  91. if toEntUserId == "" {
  92. logx.Info("划转对象不能为空", in.ProjectIds, in.EntId)
  93. continue
  94. }
  95. //查询划转人信息
  96. entUserId, _ := strconv.ParseInt(toEntUserId, 10, 64)
  97. userInfo := IC.Middleground.UserCenter.IdentityByEntUserId(entUserId)
  98. positionId := userInfo.PositionId
  99. //保存参标--participate_user
  100. //查看是否参标过当前项目
  101. 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 {
  102. //更新
  103. b3 = IC.BaseMysql.UpdateByTx(tx, ParticipateUserTable, map[string]interface{}{
  104. "position_id": positionId,
  105. "project_id": projectId,
  106. "ent_id": in.EntId,
  107. }, map[string]interface{}{
  108. "state": 0,
  109. "mark": 1, //0:参标;1:被划入;-1:终止参标;-2:被划走
  110. "update_date": date.FormatDate(&now, date.Date_Full_Layout),
  111. })
  112. } else {
  113. //保存
  114. b3 = IC.BaseMysql.InsertByTx(tx, ParticipateUserTable, map[string]interface{}{
  115. "ent_id": in.EntId,
  116. "ent_user_id": entUserId,
  117. "position_id": positionId,
  118. "project_id": projectId,
  119. "user_id": in.MgoUserId,
  120. "state": 0,
  121. "mark": 1, //0:参标;1:被划入;-1:终止参标;-2:被划走
  122. "create_date": date.FormatDate(&now, date.Date_Full_Layout),
  123. "update_date": date.FormatDate(&now, date.Date_Full_Layout),
  124. }) > 0
  125. }
  126. if b3 {
  127. toEntUserIds = append(toEntUserIds, toEntUserId)
  128. toEntUserNames = append(toEntUserNames, userInfo.EntUserName)
  129. }
  130. }
  131. //划转记录
  132. b2 = IC.BaseMysql.InsertByTx(tx, ParticipateBidRecordsTable, map[string]interface{}{
  133. "ent_id": in.EntId,
  134. "ent_user_id": in.EntUserId,
  135. "position_id": in.PositionId,
  136. "project_id": projectId,
  137. "record_type": 0,
  138. "transfer_ent_user_id": strings.Join(toEntUserIds, ","),
  139. "record_content": fmt.Sprintf(content, strings.Join(fromEntUserNames, "、"), strings.Join(toEntUserNames, "、"), lastNotes),
  140. "create_date": date.FormatDate(&now, date.Date_Full_Layout),
  141. }) > 0
  142. return b1 && b2 && b3
  143. }) {
  144. logx.Info(in.PositionId, "---终止---", projectId)
  145. return fmt.Errorf("终止参标更新信息出错")
  146. }
  147. return nil
  148. }
  149. //终止参标
  150. func CancelParticipateInfo(in *bxcore.ParticipateActionReq, roleId int64) error {
  151. defer MC.Catch()
  152. if !IC.BaseMysql.ExecTx("终止参标", func(tx *sql.Tx) bool {
  153. var (
  154. b1, b2 bool
  155. now = time.Now()
  156. tip = "终止参标(被)"
  157. )
  158. //管理员终止:当前项目 其他参标人也被终止
  159. query := map[string]interface{}{
  160. "project_id": in.ProjectIds,
  161. "ent_id": in.EntId,
  162. }
  163. //个人终止:仅仅终止本人参标项目
  164. if roleId == 0 {
  165. query["position_id"] = in.PositionId
  166. tip = "终止参标"
  167. }
  168. insert := map[string]interface{}{
  169. "state": -1,
  170. "mark": -1, //0:参标;1:被划入;-1:终止参标;-2:被划走
  171. "update_date": date.FormatDate(&now, date.Date_Full_Layout),
  172. }
  173. //更新参标participate_user
  174. b1 = IC.BaseMysql.UpdateByTx(tx, ParticipateUserTable, query, insert)
  175. //保存参标记录--participate_bid_records
  176. b2 = IC.BaseMysql.InsertByTx(tx, ParticipateBidRecordsTable, map[string]interface{}{
  177. "ent_id": in.EntId,
  178. "ent_user_id": in.EntUserId,
  179. "position_id": in.PositionId,
  180. "project_id": in.ProjectIds,
  181. "record_type": 0,
  182. "record_content": tip,
  183. "create_date": date.FormatDate(&now, date.Date_Full_Layout),
  184. }) > 0
  185. return b1 && b2
  186. }) {
  187. logx.Info(in.PositionId, "---终止---", in.ProjectIds)
  188. return fmt.Errorf("终止参标更新信息出错")
  189. }
  190. return nil
  191. }
  192. //保存参标信息
  193. func SaveParticipateInfo(in *bxcore.ParticipateActionReq) error {
  194. defer MC.Catch()
  195. if !IC.BaseMysql.ExecTx("保存|更新参标信息及保存参标记录", func(tx *sql.Tx) bool {
  196. var (
  197. b1, b2, b3 bool
  198. now = time.Now()
  199. )
  200. //保存参标--participate_user
  201. //查看是否参标过当前项目
  202. 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 {
  203. //更新
  204. b1 = IC.BaseMysql.UpdateByTx(tx, ParticipateUserTable, map[string]interface{}{
  205. "position_id": in.PositionId,
  206. "ent_id": in.EntId,
  207. "project_id": in.ProjectIds,
  208. }, map[string]interface{}{
  209. "state": 0,
  210. "mark": 0,
  211. "update_date": date.FormatDate(&now, date.Date_Full_Layout),
  212. })
  213. } else {
  214. //保存
  215. b1 = IC.BaseMysql.InsertByTx(tx, ParticipateUserTable, 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. "user_id": in.MgoUserId,
  221. "state": 0,
  222. "mark": 0,
  223. "create_date": date.FormatDate(&now, date.Date_Full_Layout),
  224. "update_date": date.FormatDate(&now, date.Date_Full_Layout),
  225. }) > 0
  226. }
  227. if !b1 {
  228. return false
  229. }
  230. //保存参标记录participate_bid_records
  231. b2 = IC.BaseMysql.InsertByTx(tx, ParticipateBidRecordsTable, map[string]interface{}{
  232. "ent_id": in.EntId,
  233. "ent_user_id": in.EntUserId,
  234. "position_id": in.PositionId,
  235. "project_id": in.ProjectIds,
  236. "record_type": 0,
  237. "record_content": "参标",
  238. "create_date": date.FormatDate(&now, date.Date_Full_Layout),
  239. }) > 0
  240. if !b2 {
  241. return false
  242. }
  243. //保存或更新项目信息
  244. //有问题 其他回滚,项目信息不用回滚
  245. b3 = UpdateProjectInfo(in.ProjectIds, es.GetProjectInfo(in.ProjectIds)) == nil
  246. return b1 && b2 && b3
  247. }) {
  248. logx.Info(in.PositionId, "---保存---", in.ProjectIds)
  249. return fmt.Errorf("保存参标信息出错")
  250. }
  251. return nil
  252. }
  253. //查询当前招标信息是否已被参标
  254. func IsParticipatedByBidId(in *bxcore.ParticipateActionReq) bool {
  255. defer MC.Catch()
  256. //如果不允许多人参标 当前项目是否已经有企业其他人员参标
  257. query := fmt.Sprintf(`SELECT count(id) FROM `+ParticipateUserTable+` WHERE %s AND project_id = %s AND state >-1`, "%s", in.ProjectIds)
  258. if in.PositionType > 0 { //企业版
  259. query = fmt.Sprintf(query, fmt.Sprintf("ent_id = %d", in.EntId))
  260. } else { //个人版
  261. query = fmt.Sprintf(query, fmt.Sprintf("position_id = %d", in.PositionId))
  262. }
  263. return IC.BaseMysql.CountBySql(query) > 0
  264. }
  265. //获取参标权限
  266. func GetParticipateIsAllow(query map[string]interface{}) (b bool) {
  267. defer MC.Catch()
  268. if info, ok := IC.Mgo.FindOne(PartTable, query); ok {
  269. if info != nil {
  270. if (*info)["i_isallow"] != nil {
  271. b = MC.IntAll((*info)["i_isallow"]) > 0
  272. }
  273. }
  274. }
  275. return
  276. }
  277. //更新设置信息
  278. func UpdateParticipateSetInfo(in *bxcore.ParticipateSetUpInfoReq) error {
  279. defer MC.Catch()
  280. query := map[string]interface{}{
  281. "i_positionid": in.PositionId,
  282. }
  283. if in.PositionType > 0 {
  284. //企业版 判断是否是管理员
  285. //判断用户身份
  286. userInfo := IC.Middleground.PowerCheckCenter.Check(in.AppId, in.UserId, in.NewUserId, in.AccountId, in.EntId, in.PositionType, in.PositionId)
  287. if userInfo.Ent.EntRoleId == 0 {
  288. return fmt.Errorf("当前企业身份无权限")
  289. }
  290. query["i_entid"] = in.EntId
  291. }
  292. upsert := map[string]interface{}{
  293. "i_entid": in.EntId,
  294. "i_entuserid": in.EntUserId,
  295. "i_positionid": in.PositionId,
  296. "l_createtime": time.Now().Unix(),
  297. }
  298. if in.IsAllow != "" {
  299. isAllow, _ := strconv.Atoi(in.IsAllow)
  300. upsert["i_isallow"] = isAllow
  301. }
  302. if len(in.BidType) > 0 {
  303. upsert["o_bidtype"] = in.BidType
  304. }
  305. if len(in.RemindRule) > 0 {
  306. upsert["o_remindrule"] = in.RemindRule
  307. }
  308. if ok := IC.Mgo.Update(PartTable, query, map[string]interface{}{
  309. "$set": upsert,
  310. }, true, false); ok {
  311. return nil
  312. }
  313. return fmt.Errorf("更新失败")
  314. }
  315. //查询企业|个人参标设置信息
  316. func GetParticipateSetInfo(in *bxcore.ParticipateSetUpInfoReq) (*bxcore.ParticipateSetUpInfo, error) {
  317. defer MC.Catch()
  318. query := map[string]interface{}{
  319. "i_positionid": in.PositionId,
  320. }
  321. if in.PositionType > 0 {
  322. query["i_entid"] = in.EntId
  323. }
  324. if setInfo, ok := IC.Mgo.FindOne(PartTable, query); ok {
  325. var (
  326. isAllow = ""
  327. bidType []*bxcore.BidTypeReq
  328. remindRule []*bxcore.RemindRuleReq
  329. )
  330. bidType = append(bidType, &bxcore.BidTypeReq{
  331. Name: "直接投标",
  332. Content: []string{"未报名", "已报名", "投标决策", "编制投标文件", "递交投标文件", "中标公示", "签合同", "已结束"},
  333. }, &bxcore.BidTypeReq{
  334. Name: "渠道投标",
  335. Content: []string{"已报名", "签合同", "已结束"},
  336. })
  337. remindRule = append(remindRule, &bxcore.RemindRuleReq{
  338. BidState: "直接投标",
  339. Remainder: 72,
  340. Node: "编制投标文件",
  341. })
  342. if setInfo != nil {
  343. if (*setInfo)["i_isallow"] != nil {
  344. isAllow = strconv.Itoa(MC.IntAll((*setInfo)["i_isallow"]))
  345. }
  346. if (*setInfo)["bidType"] != nil {
  347. if sbb, err := json.Marshal((*setInfo)["o_bidtype"]); err == nil {
  348. if err := json.Unmarshal(sbb, &bidType); err != nil {
  349. logx.Info("bidType json un err:", err.Error())
  350. return nil, err
  351. }
  352. } else {
  353. logx.Info("bidType json err:", err.Error())
  354. return nil, err
  355. }
  356. }
  357. if (*setInfo)["o_remindrule"] != nil {
  358. if sbr, err := json.Marshal((*setInfo)["o_remindrule"]); err == nil {
  359. if err := json.Unmarshal(sbr, &remindRule); err != nil {
  360. logx.Info("remindRule json un err:", err.Error())
  361. return nil, err
  362. }
  363. } else {
  364. logx.Info("remindRule json err:", err.Error())
  365. return nil, err
  366. }
  367. }
  368. }
  369. return &bxcore.ParticipateSetUpInfo{
  370. IsAllow: isAllow,
  371. BidType: bidType,
  372. RemindRule: remindRule,
  373. }, nil
  374. }
  375. return nil, nil
  376. }
  377. //保存或更新tidb 项目信息
  378. func UpdateProjectInfo(id string, pInfo map[string]interface{}) error {
  379. //id 项目id
  380. //name 项目名称
  381. //area 省份
  382. //city 城市
  383. //buyer 采购单位
  384. //budget 预算
  385. //bid_open_time 开标时间
  386. //zbtime 招标时间
  387. //bid_end_time 开标结束时间 bidding表 由 数据组 重新生索引到project表
  388. //pici 批次 轮询更新数据
  389. //
  390. projectInfo := map[string]interface{}{
  391. "id": id,
  392. "name": MC.ObjToString(pInfo["projectname"]),
  393. "area": MC.ObjToString(pInfo["area"]),
  394. "city": MC.ObjToString(pInfo["city"]),
  395. "buyer": MC.ObjToString(pInfo["buyer"]),
  396. "budget": MC.Int64All(pInfo["budget"]),
  397. }
  398. if pInfo["bidopentime"] != nil {
  399. openTime := pInfo["bidopentime"]
  400. projectInfo["bid_open_time"] = date.FormatDateWithObj(&openTime, date.Date_Full_Layout)
  401. }
  402. if pInfo["pici"] != nil {
  403. pici := pInfo["pici"]
  404. projectInfo["pici"] = date.FormatDateWithObj(&pici, date.Date_Full_Layout)
  405. }
  406. // 项目表:zbtime 招标时间是 biding表:publishtime发布时间
  407. if pInfo["zbtime"] != nil {
  408. bidTime := pInfo["zbtime"]
  409. projectInfo["bid_time"] = date.FormatDateWithObj(&bidTime, date.Date_Full_Layout)
  410. }
  411. if pInfo["bidendtime"] != nil {
  412. bidEndTime := pInfo["bidendtime"]
  413. projectInfo["bid_end_time"] = date.FormatDateWithObj(&bidEndTime, date.Date_Full_Layout)
  414. }
  415. if c := IC.BaseMysql.CountBySql(`SELECT COUNT(id) FROM project WHERE id = ?`, id); c > 0 {
  416. if ok := IC.BaseMysql.Update("project", map[string]interface{}{
  417. "id": id,
  418. }, projectInfo); !ok {
  419. return fmt.Errorf("项目信息更新异常", id)
  420. }
  421. } else {
  422. if i := IC.BaseMysql.Insert("project", projectInfo); i < 0 {
  423. return fmt.Errorf("项目信息插入异常", id)
  424. }
  425. }
  426. return nil
  427. }
  428. //参标列表其他条件
  429. func ParticipateListSql(in *bxcore.ParticipateListReq) string {
  430. //b project表
  431. //a participate_user表
  432. now := time.Now()
  433. nowDate := date.FormatDate(&now, date.Date_Full_Layout)
  434. //查询tidb base_service.project
  435. conditionSql := ` WHERE 1=1 `
  436. //地区
  437. if in.Area != "" {
  438. conditionSql += fmt.Sprintf(" AND pt.area IN ('%s') ", strings.ReplaceAll(in.Area, ",", "','"))
  439. }
  440. //城市
  441. if in.City != "" {
  442. conditionSql += fmt.Sprintf(" AND pt.city IN ('%s') ", strings.ReplaceAll(in.City, ",", "','"))
  443. }
  444. //关键词
  445. if in.Keywords != "" {
  446. kSql := ` AND (`
  447. for kk, kv := range strings.Split(in.Keywords, " ") {
  448. if kk > 0 {
  449. kSql += " OR "
  450. }
  451. kSql += " pt.name like '%" + kv + "%'"
  452. }
  453. kSql += `)`
  454. conditionSql += kSql
  455. }
  456. //招标日期
  457. if in.BidTime != "" && strings.Contains(in.BidTime, "-") {
  458. startTime := strings.Split(in.BidTime, "-")[0]
  459. entTime := strings.Split(in.BidTime, "-")[1]
  460. if startTime != "" {
  461. startTimeInt, _ := strconv.ParseInt(startTime, 10, 64)
  462. conditionSql += ` AND pt.bid_time > '` + date.FormatDateByInt64(&startTimeInt, date.Date_Full_Layout) + `'`
  463. }
  464. if entTime != "" {
  465. entTimeInt, _ := strconv.ParseInt(entTime, 10, 64)
  466. conditionSql += ` AND pt.bid_time < '` + date.FormatDateByInt64(&entTimeInt, date.Date_Full_Layout) + `'`
  467. }
  468. }
  469. //招标截止日期
  470. if in.BidEndTime != "" {
  471. //投标截止日期规则:
  472. //1、开始时间小于当前时间 ,结束时间大于当前时间,投标截止状态按钮未截止和已截止可用;
  473. //2、结束时间小于当前时间|开始时间大于当前时间,投标截止状态按钮未截止和已截止不可用;
  474. //3、需要前端做成连动
  475. startTime := strings.Split(in.BidEndTime, "-")[0]
  476. endTime := strings.Split(in.BidEndTime, "-")[1]
  477. startTimeInt, _ := strconv.ParseInt(startTime, 10, 64)
  478. endTimeInt, _ := strconv.ParseInt(endTime, 10, 64)
  479. if startTimeInt > 0 && endTimeInt > 0 && startTimeInt > endTimeInt {
  480. logx.Info(fmt.Sprintf("投标截止日期 %d 开始时间 大于 结束时间%d!!!", startTimeInt, endTimeInt))
  481. } else {
  482. switch in.BidEndStatus {
  483. case 0:
  484. if startTimeInt > 0 {
  485. conditionSql += ` AND pt.bid_end_time > '` + date.FormatDateByInt64(&startTimeInt, date.Date_Full_Layout) + `'`
  486. }
  487. if endTimeInt > 0 {
  488. conditionSql += ` AND pt.bid_end_time < '` + date.FormatDateByInt64(&endTimeInt, date.Date_Full_Layout) + `'`
  489. }
  490. case 1: //投标截止状态:1:未截止;2:已截止;3:终止参标
  491. //未截止:
  492. var (
  493. endBool = true
  494. )
  495. //如果结束时间存在且小于当前时间,投标截止日期 范围都是已截止 不会存在未截止的数据
  496. if endTimeInt > 0 {
  497. conditionSql += ` AND pt.bid_end_time < '` + date.FormatDateByInt64(&endTimeInt, date.Date_Full_Layout) + `'`
  498. endBool = endTimeInt > now.Unix()
  499. }
  500. //开始时间小于 当前时间
  501. if endBool && now.Unix() > startTimeInt {
  502. startTimeInt = now.Unix()
  503. }
  504. //存在开始时间为0的情况
  505. if startTimeInt > 0 {
  506. conditionSql += ` AND pt.bid_end_time > '` + date.FormatDateByInt64(&startTimeInt, date.Date_Full_Layout) + `'`
  507. }
  508. case 2: //投标截止状态:1:未截止;2:已截止;3:终止参标
  509. //如果开始时间存在且大于当前时间,投标截止日期 范围都是未截止 不会存在已截止的数据
  510. var (
  511. startBool = true
  512. )
  513. if startTimeInt > 0 {
  514. conditionSql += ` AND pt.bid_end_time > '` + date.FormatDateByInt64(&startTimeInt, date.Date_Full_Layout) + `'`
  515. startBool = startTimeInt < now.Unix()
  516. }
  517. if startBool && (endTimeInt == 0 || now.Unix() < endTimeInt) {
  518. endTimeInt = now.Unix()
  519. }
  520. //存在结束时间为0的情况
  521. if endTimeInt > 0 {
  522. conditionSql += ` AND pt.bid_end_time < '` + date.FormatDateByInt64(&endTimeInt, date.Date_Full_Layout) + `'`
  523. }
  524. case 3:
  525. conditionSql += ` AND pug.state < 0 `
  526. }
  527. }
  528. } else if in.BidEndStatus > 0 { //投标截止状态1:未截止;2:已截止;3:终止参标
  529. switch in.BidEndStatus {
  530. case 1:
  531. conditionSql += ` AND pt.bid_end_time > '` + nowDate + `'`
  532. case 2:
  533. conditionSql += ` AND pt.bid_end_time < '` + nowDate + `'`
  534. case 3:
  535. conditionSql += ` AND pug.state < 0 `
  536. }
  537. }
  538. //开标时间
  539. if in.BidOpenTime != "" {
  540. startTime := strings.Split(in.BidOpenTime, "-")[0]
  541. entTime := strings.Split(in.BidOpenTime, "-")[1]
  542. if startTime != "" {
  543. startTimeInt, _ := strconv.ParseInt(startTime, 10, 64)
  544. conditionSql += ` AND pt.bid_open_time > '` + date.FormatDateByInt64(&startTimeInt, date.Date_Full_Layout) + `'`
  545. }
  546. if entTime != "" {
  547. entTimeInt, _ := strconv.ParseInt(entTime, 10, 64)
  548. conditionSql += ` AND pt.bid_open_time < '` + date.FormatDateByInt64(&entTimeInt, date.Date_Full_Layout) + `'`
  549. }
  550. }
  551. //开标状态1:未开标;2:已开标
  552. if in.BidOpenStatus > 0 {
  553. switch in.BidOpenStatus {
  554. case 1:
  555. conditionSql += ` AND pt.bid_open_time > '` + nowDate + `'`
  556. case 2:
  557. conditionSql += ` AND pt.bid_open_time < '` + nowDate + `'`
  558. }
  559. }
  560. //参标人 管理员权限
  561. if in.EntUserIds != "" && in.PositionType > 0 {
  562. conditionSql += ` AND (`
  563. for k, v := range strings.Split(in.EntUserIds, ",") {
  564. if k > 0 {
  565. conditionSql += " OR "
  566. }
  567. conditionSql += ` FIND_IN_SET(` + v + ` , pug.ent_user_id) `
  568. }
  569. conditionSql += `)`
  570. }
  571. //默认按照投标截止日期正序排列、1:开标时间正序、2:更新状态时间倒序
  572. switch in.OrderNum {
  573. case 1:
  574. conditionSql += ` ORDER BY pt.bid_open_time ASC`
  575. case 2:
  576. conditionSql += ` ORDER BY pug.update_date DESC`
  577. default:
  578. conditionSql += ` ORDER BY pt.bid_end_time ASC`
  579. }
  580. logx.Info(conditionSql)
  581. return conditionSql
  582. }
  583. //个人或员工查询参标列表
  584. func SingleParticipateList(in *bxcore.ParticipateListReq, conditionSql string) (data *bxcore.ParticipateData, err error) {
  585. defer MC.Catch()
  586. data = &bxcore.ParticipateData{
  587. NowTime: time.Now().Unix(),
  588. Count: 0,
  589. List: []*bxcore.ParticipateList{},
  590. }
  591. //员工|个人列表
  592. singlePersonSql := `SELECT %s FROM ` + ParticipateUserTable + ` pug LEFT JOIN project pt ON pug.project_id = pt.id WHERE pug.position_id = ? `
  593. singlePersonSql += conditionSql
  594. countSql := fmt.Sprintf(singlePersonSql, " COUNT(pt.id) ")
  595. count := IC.BaseMysql.CountBySql(countSql, in.PositionId)
  596. if count > 0 {
  597. data.Count = count
  598. listSql := fmt.Sprintf(singlePersonSql, " pug.update_date,pt.* ")
  599. //分页
  600. listSql += fmt.Sprintf(` LIMIT %d,%d`, in.PageNum, in.PageSize)
  601. list := IC.BaseMysql.SelectBySql(listSql, in.PositionId)
  602. if list != nil && len(*list) > 0 {
  603. for _, v := range *list {
  604. bidTimeStr := MC.ObjToString(v["bid_time"])
  605. bidEndTimeStr := MC.ObjToString(v["bid_end_time"])
  606. bidOpenTimeStr := MC.ObjToString(v["bid_open_time"])
  607. updateStatusTimeStr := MC.ObjToString(v["update_date"])
  608. var bidTime, bidEndTime, bidOpenTime, updateStatusTime int64
  609. if bidTimeStr != "" {
  610. bidTime_, _ := time.ParseInLocation(date.Date_Full_Layout, bidTimeStr, time.Local)
  611. bidTime = bidTime_.Unix()
  612. }
  613. if bidEndTimeStr != "" {
  614. bidEndTime_, _ := time.ParseInLocation(date.Date_Full_Layout, bidEndTimeStr, time.Local)
  615. bidEndTime = bidEndTime_.Unix()
  616. }
  617. if bidOpenTimeStr != "" {
  618. bidOpenTime_, _ := time.ParseInLocation(date.Date_Full_Layout, bidOpenTimeStr, time.Local)
  619. bidOpenTime = bidOpenTime_.Unix()
  620. }
  621. if updateStatusTimeStr != "" {
  622. updateStatusTime_, _ := time.ParseInLocation(date.Date_Full_Layout, updateStatusTimeStr, time.Local)
  623. updateStatusTime = updateStatusTime_.Unix()
  624. }
  625. data.List = append(data.List, &bxcore.ParticipateList{
  626. Id: encrypt.EncodeArticleId2ByCheck(MC.ObjToString(v["id"])),
  627. ProjectName: MC.ObjToString(v["name"]),
  628. Buyer: MC.ObjToString(v["buyer"]),
  629. Budget: MC.ObjToString(v["budget"]),
  630. BidTime: bidTime,
  631. BidEndTime: bidEndTime,
  632. BidOpenTime: bidOpenTime,
  633. UpdateStatusTime: updateStatusTime,
  634. UpdateStatusCon: GetParticipateContent("s", in.PositionId, MC.ObjToString(v["id"])), //查询最后一次 投标状态更新,
  635. })
  636. }
  637. return data, nil
  638. }
  639. return nil, fmt.Errorf("数据异常")
  640. }
  641. return data, nil
  642. }
  643. //管理员获取参标列表数据
  644. func AdminParticipateList(in *bxcore.ParticipateListReq, conditionSql string) (data *bxcore.ParticipateData, err error) {
  645. defer MC.Catch()
  646. data = &bxcore.ParticipateData{
  647. NowTime: time.Now().Unix(),
  648. Count: 0,
  649. List: []*bxcore.ParticipateList{},
  650. }
  651. 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`
  652. adminCountSql := fmt.Sprintf(adminSql, "COUNT(pt.id)") + conditionSql
  653. log.Println(adminCountSql)
  654. count := IC.BaseMysql.CountBySql(adminCountSql, in.EntId)
  655. if count > 0 {
  656. data.Count = count
  657. in.PageNum = (in.PageNum - 1) * in.PageSize
  658. adminListSql := fmt.Sprintf(adminSql, " pt.*, pug.ent_user_id,pug.update_date ") + conditionSql + fmt.Sprintf(" LIMIT %d,%d", in.PageNum, in.PageSize)
  659. list := IC.BaseMysql.SelectBySql(adminListSql, in.EntId)
  660. if list != nil && len(*list) > 0 {
  661. for _, v := range *list {
  662. bidTimeStr := MC.ObjToString(v["bid_time"])
  663. bidEndTimeStr := MC.ObjToString(v["bid_end_time"])
  664. bidOpenTimeStr := MC.ObjToString(v["bid_open_time"])
  665. updateStatusTimeStr := MC.ObjToString(v["update_date"])
  666. var bidTime, bidEndTime, bidOpenTime, updateStatusTime int64
  667. if bidTimeStr != "" {
  668. bidTime_, _ := time.ParseInLocation(date.Date_Full_Layout, bidTimeStr, time.Local)
  669. bidTime = bidTime_.Unix()
  670. }
  671. if bidEndTimeStr != "" {
  672. bidEndTime_, _ := time.ParseInLocation(date.Date_Full_Layout, bidEndTimeStr, time.Local)
  673. bidEndTime = bidEndTime_.Unix()
  674. }
  675. if bidOpenTimeStr != "" {
  676. bidOpenTime_, _ := time.ParseInLocation(date.Date_Full_Layout, bidOpenTimeStr, time.Local)
  677. bidOpenTime = bidOpenTime_.Unix()
  678. }
  679. if updateStatusTimeStr != "" {
  680. updateStatusTime_, _ := time.ParseInLocation(date.Date_Full_Layout, updateStatusTimeStr, time.Local)
  681. updateStatusTime = updateStatusTime_.Unix()
  682. }
  683. data.List = append(data.List, &bxcore.ParticipateList{
  684. Id: encrypt.EncodeArticleId2ByCheck(MC.ObjToString(v["id"])),
  685. ProjectName: MC.ObjToString(v["name"]),
  686. Buyer: MC.ObjToString(v["buyer"]),
  687. Budget: MC.ObjToString(v["budget"]),
  688. BidTime: bidTime,
  689. BidEndTime: bidEndTime,
  690. BidOpenTime: bidOpenTime,
  691. UpdateStatusTime: updateStatusTime,
  692. UpdateStatusCon: GetParticipateContent("e", in.EntId, MC.ObjToString(v["id"])), //查询最后一次 投标状态更新
  693. Participants: GetParticipateUserName(MC.ObjToString(v["ent_user_id"])), //参标人信息
  694. })
  695. }
  696. return data, nil
  697. }
  698. return nil, fmt.Errorf("数据异常")
  699. }
  700. return data, nil
  701. }
  702. //获取最新参标 更新内容
  703. func GetParticipateContent(s string, id int64, projectId string) string {
  704. identitySql := `ent_id = ?`
  705. if s == "s" {
  706. identitySql = `position_id = ?`
  707. }
  708. recordsSql := `SELECT record_content,record_type FROM ` + ParticipateBidRecordsTable + ` WHERE ` + identitySql + ` AND project_id = ? ORDER BY create_date DESC LIMIT 1;`
  709. records := IC.BaseMysql.SelectBySql(recordsSql, id, projectId)
  710. if records != nil && len(*records) > 0 {
  711. rec := (*records)[0]
  712. switch MC.IntAll(rec["record_type"]) {
  713. case 0:
  714. return MC.ObjToString(rec["record_content"])
  715. case 1:
  716. recordContent := *MC.ObjToMap(rec["record_content"])
  717. rb, err := json.Marshal(recordContent)
  718. if err != nil {
  719. log.Println(err.Error())
  720. return ""
  721. }
  722. var rc = RecordsContent{
  723. After: PartStatusContent{},
  724. Before: PartStatusContent{},
  725. }
  726. err1 := json.Unmarshal(rb, &rc)
  727. if err1 == nil {
  728. return rc.Content
  729. }
  730. }
  731. }
  732. return ""
  733. }
  734. //根据ent_user_id 获取参标人昵称,企业管理员现在都是“我”
  735. func GetParticipateUserName(entUserId string) string {
  736. if entUserId != "" {
  737. var userNames []string
  738. for _, v := range strings.Split(entUserId, ",") {
  739. entUserInfos := IC.MainMysql.SelectBySql(`SELECT * FROM entniche_user WHERE id = ?`, v)
  740. if entUserInfos != nil && len(*entUserInfos) > 0 {
  741. entUserInfo := (*entUserInfos)[0]
  742. if entUserInfo["name"] != nil {
  743. if userName := MC.ObjToString(entUserInfo["name"]); userName != "" {
  744. userNames = append(userNames, userName)
  745. }
  746. }
  747. }
  748. }
  749. return strings.Join(userNames, ",")
  750. }
  751. return ""
  752. }
  753. // GetBidContentEnt 企业版 获取投标状态更新内容
  754. func GetBidContentEnt(projectId string, entId int64) *[]map[string]interface{} {
  755. // record_type '默认0:参标、划转、取消参标;1:投标状态更新存储'
  756. query := "SELECT * FROM " + ParticipateBidRecordsTable + " where project_id=? and ent_id=? and record_type=1 order by create_date desc limit 1; "
  757. return IC.BaseMysql.SelectBySql(query, projectId, entId)
  758. }
  759. // GetBidContentPersonal 个人版 获取投标状态更新内容
  760. func GetBidContentPersonal(projectId string, positionId int64) *[]map[string]interface{} {
  761. query := "SELECT * FROM " + ParticipateBidRecordsTable + " where project_id=? and position_id=? and record_type=1 order by create_date desc limit 1;"
  762. return IC.BaseMysql.SelectBySql(query, projectId, positionId)
  763. }
  764. // UpdateBidContent 更新投标状态信息以及操作记录
  765. func UpdateBidContent(recordData map[string]interface{}) (flag bool) {
  766. r2 := IC.BaseMysql.Insert(ParticipateBidRecordsTable, recordData)
  767. return r2 > 0
  768. }
  769. // InsertBidContent 新增投标状态信息及操作记录
  770. func InsertBidContent(recordData map[string]interface{}) (flag bool) {
  771. r2 := IC.BaseMysql.Insert(ParticipateBidRecordsTable, recordData)
  772. return r2 > 0
  773. }
  774. // GetBidRecordsEnt 获取操作记录列表企业
  775. func GetBidRecordsEnt(projectId string, entId, page, pageSize int64) (rs *[]map[string]interface{}, total int64) {
  776. query := "SELECT * FROM " + ParticipateBidRecordsTable + " where project_id=? and ent_id=? order by create_date desc limit ?,?"
  777. countQuery := "SELECT count(id) FROM " + ParticipateBidRecordsTable + " where project_id=? and ent_id=? ;"
  778. rs = IC.BaseMysql.SelectBySql(query, projectId, entId, (page-1)*pageSize, pageSize)
  779. total = IC.BaseMysql.CountBySql(countQuery, projectId, entId)
  780. return rs, total
  781. }
  782. // GetBidRecordsPersonal 获取操作记录列表个人
  783. func GetBidRecordsPersonal(projectId string, positionId, page, pageSize int64) (rs *[]map[string]interface{}, total int64) {
  784. query := "SELECT * FROM " + ParticipateBidRecordsTable + " where project_id=? and position_id=? order by create_date desc limit ?,?;"
  785. countQuery := "SELECT count(id) FROM " + ParticipateBidRecordsTable + " where project_id=? and position_id=? ;"
  786. rs = IC.BaseMysql.SelectBySql(query, projectId, positionId, (page-1)*pageSize, pageSize)
  787. total = IC.BaseMysql.CountBySql(countQuery, projectId, positionId)
  788. return rs, total
  789. }
  790. // GetUserMap 查询用户id的姓名
  791. func GetUserMap(userIds string) (rs *[]map[string]interface{}) {
  792. query := fmt.Sprintf("select id,name from entniche_user where id in (%s)", userIds)
  793. rs = IC.MainMysql.SelectBySql(query)
  794. return rs
  795. }
  796. // CheckParticipateManager 验证项目id是否是该管理员企业下的参标项目
  797. func CheckParticipateManager(projectId string, entId int64, valid bool) (flag bool) {
  798. stateStr := "" // 是否需要验证是正在参标
  799. if valid {
  800. stateStr = " and state=0"
  801. }
  802. query := "SELECT count(id) FROM " + ParticipateUserTable + " where project_id=? and ent_id=?" + stateStr
  803. return IC.BaseMysql.CountBySql(query, projectId, entId) > 0
  804. }
  805. // CheckParticipateEntUser 验证项目id是否是该企业用户参标的项目
  806. func CheckParticipateEntUser(projectId string, entUserId int64, valid bool) (flag bool) {
  807. stateStr := "" // 是否需要验证是正在参标
  808. if valid {
  809. stateStr = " and state=0"
  810. }
  811. query := "SELECT count(id) FROM " + ParticipateUserTable + " where project_id=? and ent_user_id=?" + stateStr
  812. return IC.BaseMysql.CountBySql(query, projectId, entUserId) > 0
  813. }
  814. // CheckParticipatePersonal 查询项目id是否是该用户参标项目
  815. func CheckParticipatePersonal(projectId string, positionId int64, valid bool) (flag bool) {
  816. stateStr := "" // 是否需要验证是正在参标 终止参标的用户还能查看记录,但是不能更新状态
  817. if valid {
  818. stateStr = " and state=0"
  819. }
  820. query := "SELECT count(id) FROM " + ParticipateUserTable + " where project_id=? and position_id=?" + stateStr
  821. return IC.BaseMysql.CountBySql(query, projectId, positionId) > 0
  822. }
  823. // GetNameByUserIds 获取用户名字符串
  824. // 参数:逗号分割的用户id "11,22,333..."
  825. // 返回: "张三,李四,王五..."
  826. func GetNameByUserIds(ids string) *[]map[string]interface{} {
  827. query := "select group_concat(name) as name from " + EntnicheUserTable + " where id in (" + ids + ") "
  828. rs := IC.MainMysql.SelectBySql(query)
  829. return rs
  830. }
  831. // ParticipateProjectPersonal 查询给定项目id中已经参标的项目id
  832. func ParticipateProjectPersonal(positionId int64, projectId []string) *[]map[string]interface{} {
  833. // 1. 查询出已经参标的
  834. var arg []string
  835. var value []interface{}
  836. value = append(value, positionId)
  837. for i := 0; i < len(projectId); i++ {
  838. arg = append(arg, "?")
  839. value = append(value, projectId[i])
  840. }
  841. argStr := strings.Join(arg, ",")
  842. query := "select project_id from " + ParticipateUserTable + " where position_id = ? and project_id in (%s) and state=0"
  843. rs := IC.BaseMysql.SelectBySql(fmt.Sprintf(query, argStr), value...)
  844. return rs
  845. }
  846. // ParticipateProjectEnt 查询给定项目id中已经参标的项目id
  847. func ParticipateProjectEnt(entId int64, projectId []string) *[]map[string]interface{} {
  848. // 1. 查询出已经参标的
  849. var arg []string
  850. var value []interface{}
  851. value = append(value, entId)
  852. for i := 0; i < len(projectId); i++ {
  853. arg = append(arg, "?")
  854. value = append(value, projectId[i])
  855. }
  856. argStr := strings.Join(arg, ",")
  857. query := "select GROUP_CONCAT(ent_user_id) as personIds ,project_id from " + ParticipateUserTable + " where ent_id=? and project_id in (%s) and state=0 group by project_id "
  858. rs := IC.BaseMysql.SelectBySql(fmt.Sprintf(query, argStr), value...)
  859. return rs
  860. }
  861. //查询企业人员信息
  862. func GetPersonInfo(entId, entUserId int64, participateMap map[int64]bool) []*bxcore.ParticipatePerson {
  863. r := IC.MainMysql.SelectBySql(`SELECT a.id,a.pid,a.name,c.id as user_id,c.name as user_name,c.phone as user_phone,c.power as user_power,e.name as role from entniche_department a
  864. INNER JOIN entniche_department_user b on (a.ent_id=? and a.id=b.dept_id)
  865. INNER JOIN entniche_user c on (b.user_id=c.id)
  866. LEFT JOIN entniche_user_role d on (c.id=d.user_id)
  867. LEFT JOIN entniche_role e on (d.role_id=e.id)
  868. order by a.id,convert(c.name using gbk) COLLATE gbk_chinese_ci asc`, entId)
  869. var (
  870. list []*bxcore.ParticipatePerson
  871. prevId int64 = 0
  872. )
  873. for _, v := range *r {
  874. if entUserId == MC.Int64All(v["user_id"]) {
  875. continue
  876. }
  877. id := MC.Int64All(v["id"])
  878. userId := strconv.FormatInt(MC.Int64All(v["user_id"]), 10)
  879. user := &bxcore.ParticipatePerson{
  880. Id: encrypt.SE.Encode2HexByCheck(userId),
  881. Power: MC.Int64All(v["user_power"]),
  882. Name: MC.ObjToString(v["user_name"]),
  883. Phone: MC.ObjToString(v["user_phone"]),
  884. Role: MC.ObjToString(v["role"]),
  885. }
  886. if participateMap != nil {
  887. if participateMap[MC.Int64All(v["user_id"])] {
  888. user.IsPart = 1
  889. }
  890. }
  891. if prevId == id {
  892. users := list[len(list)-1].Users
  893. users = append(users, user)
  894. list[len(list)-1].Users = users
  895. } else {
  896. seId := strconv.FormatInt(id, 10)
  897. list = append(list, &bxcore.ParticipatePerson{
  898. Id: encrypt.SE.Encode2HexByCheck(seId),
  899. Name: MC.ObjToString(v["name"]),
  900. Pid: MC.Int64All(v["pid"]),
  901. Users: []*bxcore.ParticipatePerson{user},
  902. })
  903. }
  904. prevId = id
  905. }
  906. return list
  907. }