participateBid.go 32 KB

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