course.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. package manage
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/url"
  6. "qfw/coreconfig"
  7. cutil "qfw/coreutil"
  8. qutil "qfw/util"
  9. "qfw/util/mongodb"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/go-xweb/log"
  14. "github.com/go-xweb/xweb"
  15. "github.com/pkg/errors"
  16. "gopkg.in/mgo.v2/bson"
  17. )
  18. //招投标课程相关接口
  19. type CourseManage struct {
  20. *xweb.Action
  21. index xweb.Mapper `xweb:"/manage/course/index"` //课程首页
  22. list xweb.Mapper `xweb:"/manage/course/list"` //请求课程列表
  23. createPage xweb.Mapper `xweb:"/manage/course/page/(\\w+)/(\\w+)"` //课程三级页
  24. createApi xweb.Mapper `xweb:"/manage/course/doRequest"` //课程三级页接口
  25. changeStatus xweb.Mapper `xweb:"/manage/course/changeStatus"` //上线or下线
  26. preview xweb.Mapper `xweb:"/manage/course/preview/(\\w+)/(\\w+)"` //课程预览
  27. previewGetDate xweb.Mapper `xweb:"/manage/course/previewGetDate"` //课程预览
  28. //课程订单
  29. orderIndex xweb.Mapper `xweb:"/manage/courseOrder/index"` //课程订单列表
  30. orderDetail xweb.Mapper `xweb:"/manage/courseOrder/detail/(\\w+)"` //课程订单详情
  31. orderList xweb.Mapper `xweb:"/manage/courseOrder/list"` //请求课程订单列表
  32. getDetail xweb.Mapper `xweb:"/manage/courseOrder/getDetail"` //请求课程订单详情
  33. //线上课程
  34. onLineIndex xweb.Mapper `xweb:"/manage/course/onLineIndex"` //线上课程首页
  35. createOnLineApi xweb.Mapper `xweb:"/manage/course/OnLineRequest"` //线上课程新增、修改
  36. onLineClickTradeRate xweb.Mapper `xweb:"/manage/course/OnLineClickTradeRate"` //线上课程点击量、购买量
  37. onLineList xweb.Mapper `xweb:"/manage/course/OnLineList"` //线上课程列表
  38. onLineChangeStatus xweb.Mapper `xweb:"/manage/course/OnLineChangeStatus"` //上线or下线
  39. onLineDetail xweb.Mapper `xweb:"/manage/course/OnLineDetail/(\\w+)/(\\w+)"` //课程详情
  40. }
  41. type FuncResult struct {
  42. Success bool //是否成功
  43. Err error //错误
  44. Data map[string]interface{}
  45. }
  46. func (f *FuncResult) Format() *map[string]interface{} {
  47. errStr := ""
  48. if f.Err != nil {
  49. errStr = f.Err.Error()
  50. }
  51. return &map[string]interface{}{
  52. "success": f.Success,
  53. "errMsg": errStr,
  54. "data": f.Data,
  55. }
  56. }
  57. func init() {
  58. xweb.AddAction(&CourseManage{})
  59. }
  60. func (this *CourseManage) Preview(flag string, id string) {
  61. this.T["id"] = id
  62. if flag == "mobile" {
  63. this.Render("/manage/course/preview/course_detail_mobile.html")
  64. } else {
  65. this.Render("/manage/course/preview/course_detail_pc.html")
  66. }
  67. }
  68. func (this *CourseManage) PreviewGetDate() {
  69. _id := this.GetString("_id") //课程id
  70. showAbout := this.GetString("aboutFlag") //是否需要相关课程
  71. r := func() *FuncResult {
  72. if _id == "" {
  73. return &FuncResult{false, errors.New("信息id为空"), nil}
  74. }
  75. //课程基本信息
  76. rData := mongodb.FindById("jy_course", _id, `{"i_type":1,"s_address":1,"i_status":1,"s_name":1,"i_price":1,"l_starttime":1,"l_endtime":1,"s_content":1,"s_discountPlan":1,"_id":1}`)
  77. if rData == nil || len(*rData) == 0 {
  78. return &FuncResult{false, errors.New("未找到此课程的信息"), nil}
  79. }
  80. resultMap := map[string]interface{}{}
  81. (*rData)["l_starttime"] = time.Unix(qutil.Int64All((*rData)["l_starttime"]), 0).Format("2006-01-02 15:04")
  82. (*rData)["l_endtime"] = time.Unix(qutil.Int64All((*rData)["l_endtime"]), 0).Format(qutil.Date_Short_Layout)
  83. resultMap["detail"] = rData
  84. i_type := qutil.IntAll((*rData)["i_type"])
  85. if showAbout != "" {
  86. //相关课程信息
  87. s_name := (*rData)["s_name"]
  88. queryAbout := bson.M{"i_type": i_type, "s_name": s_name, "i_status": bson.M{"$in": []int{1, -1}}, "_id": bson.M{"$ne": bson.ObjectIdHex(_id)}} //查询已发布or已结束的课程
  89. aboutList := mongodb.Find("jy_course", queryAbout, `{"l_publishtime":-1}`,
  90. `{"_id":1,"i_type":1,"s_address":1,"i_status":1,"s_name":1,"i_price":1,"l_starttime":1}`, false, 0, 3)
  91. for _, v := range *aboutList {
  92. v["l_starttime"] = time.Unix(qutil.Int64All(v["l_starttime"]), 0).Format(qutil.Date_Short_Layout)
  93. v["l_endtime"] = time.Unix(qutil.Int64All(v["l_endtime"]), 0).Format(qutil.Date_Short_Layout)
  94. v["l_publishtime"] = time.Unix(qutil.Int64All(v["l_publishtime"]), 0).Format(qutil.Date_Short_Layout)
  95. v["_id"] = qutil.EncodeArticleId2ByCheck(qutil.BsonIdToSId(v["_id"]))
  96. }
  97. resultMap["aboutList"] = aboutList
  98. }
  99. return &FuncResult{true, nil, resultMap}
  100. }()
  101. if r.Err != nil {
  102. log.Printf(" CourseContent err:%v\n", r.Err.Error())
  103. }
  104. this.ServeJson(r.Format())
  105. }
  106. //
  107. func (this *CourseManage) GetDetail() error {
  108. orderCode := this.GetString("orderCode")
  109. if orderCode != "" {
  110. dotype := this.GetString("rt")
  111. qMap := map[string]interface{}{}
  112. qMap["order_code"] = orderCode
  113. res := cutil.Mysql.FindOne("dataexport_order", qMap, "", "create_time desc")
  114. log.Println("res:", (*res)["filter"])
  115. var filter = qutil.ObjToMap((*res)["filter"])
  116. if dotype == "1" { //获取订单信息
  117. if len(*res) > 0 {
  118. resInv := cutil.Mysql.FindOne("apply_invoice", map[string]interface{}{"order_id": (*res)["id"]}, "", "apply_date desc")
  119. pay_way := (*res)["pay_way"]
  120. var dataBase = "ali_pay"
  121. if strings.Index(pay_way.(string), "wx") > -1 {
  122. dataBase = "weixin_pay"
  123. }
  124. payMap := map[string]interface{}{}
  125. payMap["out_trade_no"] = (*res)["out_trade_no"]
  126. payInfo := cutil.Mysql.FindOne(dataBase, payMap, "", "create_time desc")
  127. if payInfo != nil {
  128. (*res)["transaction_id"] = (*payInfo)["transaction_id"]
  129. }
  130. filter_id := qutil.ObjToString((*res)["filter_id"])
  131. if strings.Contains(filter_id, "ABC") {
  132. filter_id = qutil.DecodeArticleId2ByCheck(filter_id)[0]
  133. }
  134. resCour := mongodb.FindById("jy_course", filter_id, nil)
  135. this.ServeJson(map[string]interface{}{
  136. "res": res,
  137. "resInv": resInv,
  138. "resCour": resCour,
  139. })
  140. }
  141. } else if dotype == "2" { //确认退款
  142. nw := time.Now().Unix()
  143. (*filter)["adminRefundTime"] = qutil.FormatDateByInt64(&nw, qutil.Date_Full_Layout)
  144. datailMsg, _ := json.Marshal(*filter)
  145. id := cutil.Mysql.UpdateOrDeleteBySql("UPDATE dataexport_order SET course_status = 6,filter='" + string(datailMsg) + "' WHERE order_code = " + orderCode)
  146. this.ServeJson(map[string]interface{}{
  147. "id": id,
  148. })
  149. } else if dotype == "3" { //审核凭证提交
  150. nw := time.Now().Unix()
  151. agreeN := this.GetString("agreeN")
  152. order_status := "0"
  153. course_status := "2"
  154. if agreeN == "1" { //不通过
  155. course_status = "3"
  156. } else {
  157. course_status = "4"
  158. order_status = "1"
  159. }
  160. (*filter)["adminTransferVTime"] = qutil.FormatDateByInt64(&nw, qutil.Date_Full_Layout)
  161. datailMsg, _ := json.Marshal(*filter)
  162. updateSql := "UPDATE dataexport_order SET course_status = " + course_status + ",filter_publishtime=" + strconv.FormatInt(nw, 10) + ",order_status=" + order_status
  163. updateSql += ",filter = '" + string(datailMsg) + "'"
  164. if agreeN == "2" {
  165. updateSql += ",pay_time='" + qutil.FormatDateByInt64(&nw, qutil.Date_Full_Layout) + "'"
  166. }
  167. updateSql += " WHERE order_code = " + orderCode
  168. id := cutil.Mysql.UpdateOrDeleteBySql(updateSql)
  169. this.ServeJson(map[string]interface{}{
  170. "id": id,
  171. "pay_time": qutil.FormatDateByInt64(&nw, qutil.Date_Full_Layout),
  172. })
  173. }
  174. }
  175. return nil
  176. }
  177. //课程订单详情
  178. func (this *CourseManage) OrderDetail(orderCode string) error {
  179. this.T["orderCode"] = orderCode
  180. return this.Render("/manage/course/order_detail.html")
  181. }
  182. //课程订单列表
  183. func (this *CourseManage) OrderIndex() error {
  184. return this.Render("/manage/course/order_index.html")
  185. }
  186. //课程首页
  187. func (this *CourseManage) Index() error {
  188. return this.Render("/manage/course/index.html")
  189. }
  190. //请求课程列表
  191. func (this *CourseManage) OrderList() {
  192. currentPage, _ := this.GetInteger("currentPage")
  193. perPage, _ := this.GetInteger("perPage")
  194. c_status := this.GetString("strSel1") //订单状态 0:未支付 1:已支付 -1:已删除 -2:已取消
  195. c_type := this.GetString("strSel2") //请求类型 0:订单编号 1:课程名称 2:联系人(订单表中 user_nickname 暂存联系人信息) 3:联系人手机号
  196. c_content := this.GetString("strCont")
  197. var sql = ``
  198. var c_sql = `SELECT order_status,order_code,pay_time,filter,applybill_status,pay_money,order_money,course_status,pay_way FROM dataexport_order WHERE product_type in ('招投标课程','线上课程') and `
  199. var c_sql_c = `SELECT count(*) FROM dataexport_order WHERE product_type in ('招投标课程','线上课程') and `
  200. if c_status != "" {
  201. sql += `order_status =` + fmt.Sprint(c_status)
  202. } else {
  203. c_status = "-1"
  204. sql += `order_status !=` + fmt.Sprint(c_status)
  205. }
  206. if c_type == "1" {
  207. query := map[string]interface{}{
  208. "s_name": c_content,
  209. }
  210. res := mongodb.Find("jy_course", query, `{"l_publishtime":-1}`, `{"_id":1}`, false, -1, -1)
  211. log.Println("res:", *res)
  212. var a_id = ""
  213. for _, v := range *res {
  214. if a_id != "" {
  215. a_id += ","
  216. }
  217. a_id += "'" + qutil.BsonIdToSId(v["_id"]) + "'"
  218. }
  219. sql += ` and filter_id in (` + a_id + `)`
  220. } else if c_type == "0" {
  221. sql += ` and order_code ='` + c_content + `'`
  222. } else if c_type == "2" {
  223. sql += ` and filter like '%"name":"` + c_content + `"%'`
  224. } else if c_type == "3" {
  225. sql += ` and user_phone ='` + c_content + `'`
  226. }
  227. c_sql = c_sql + sql
  228. c_sql += ` order by create_time desc limit ` + fmt.Sprint((currentPage-1)*perPage) + `,` + fmt.Sprint(perPage)
  229. log.Println("sql:", c_sql)
  230. rData := cutil.Mysql.SelectBySql(c_sql)
  231. if len(*rData) > 0 {
  232. for _, v := range *rData {
  233. filter := qutil.ObjToMap(v["filter"])
  234. v["courseType"] = (*filter)["courseType"]
  235. }
  236. }
  237. c_sql_c = c_sql_c + sql
  238. count := cutil.Mysql.CountBySql(c_sql_c)
  239. this.ServeJson(map[string]interface{}{
  240. "currentPage": currentPage,
  241. "data": rData,
  242. "totalRows": count,
  243. })
  244. }
  245. //请求课程列表
  246. func (this *CourseManage) List() {
  247. currentPage, _ := this.GetInteger("currentPage")
  248. perPage, _ := this.GetInteger("perPage")
  249. c_status := this.GetString("strSel1") //报名状态 1 报名中 2 结束 0未发布
  250. c_type := this.GetString("strSel2") //课程类型 1 招标 2 投标
  251. queryStr := this.GetString("query")
  252. query := map[string]interface{}{}
  253. if c_status != "" {
  254. query["i_status"] = qutil.IntAll(c_status)
  255. }
  256. if c_type != "" {
  257. query["i_type"] = qutil.IntAll(c_type)
  258. }
  259. if queryStr != "" {
  260. query["s_name"] = bson.M{"$regex": queryStr}
  261. }
  262. //课程类型不等于3
  263. query1 := map[string]int32{}
  264. query1["$ne"] = 3
  265. query["i_type"] = query1
  266. rData := mongodb.Find("jy_course", query, `{"l_publishtime":-1}`, `{"_id":1,"i_type":1,"s_address":1,"i_status":1,"s_name":1,"l_publishtime":1}`, false, (currentPage-1)*perPage, perPage)
  267. count := mongodb.Count("jy_course", query)
  268. this.ServeJson(map[string]interface{}{
  269. "currentPage": currentPage,
  270. "data": rData,
  271. "totalRows": count,
  272. })
  273. }
  274. //课程三级页
  275. func (this *CourseManage) CreatePage(flag, id string) error {
  276. if flag == "edit" || flag == "copy" {
  277. //获取文章详情
  278. if id == "" {
  279. return errors.New("请求参数有误")
  280. }
  281. rData := mongodb.FindById("jy_course", id, "")
  282. this.T["type"] = (*rData)["i_type"]
  283. this.T["name"] = (*rData)["s_name"]
  284. this.T["address"] = (*rData)["s_address"]
  285. this.T["price"] = (*rData)["i_price"]
  286. this.T["publishtime"] = time.Unix(qutil.Int64All((*rData)["l_publishtime"]), 0).Format(qutil.Date_Full_Layout)
  287. this.T["starttime"] = time.Unix(qutil.Int64All((*rData)["l_starttime"]), 0).Format(qutil.Date_Full_Layout)
  288. this.T["endtime"] = time.Unix(qutil.Int64All((*rData)["l_endtime"]), 0).Format(qutil.Date_Full_Layout)
  289. this.T["content"] = (*rData)["s_content"]
  290. this.T["status"] = (*rData)["i_status"]
  291. this.T["id"] = id
  292. }
  293. this.T["doType"] = flag
  294. this.Render("/manage/course/content.html")
  295. return nil
  296. }
  297. func (this *CourseManage) CreateApi() {
  298. c_type, _ := this.GetInteger("c_type") //课程类型
  299. name := this.GetString("name")
  300. address := this.GetString("address")
  301. price, _ := this.GetInt("price")
  302. s_starttime := this.GetString("starttime")
  303. s_endtime := this.GetString("endtime")
  304. s_publishtime := this.GetString("publishtime")
  305. d_content := this.GetString("content")
  306. Dotype := this.GetString("dotype") //克隆 or 新增 or 修改
  307. courseName := "招标管理课程"
  308. if c_type == 2 {
  309. courseName = "投标实务课程"
  310. }
  311. log.Println(coreconfig.DiscountConfig)
  312. dplan := coreconfig.DiscountConfig
  313. var pmap []map[string]interface{}
  314. if dplan != nil {
  315. pmap = qutil.ObjArrToMapArr(dplan[courseName].([]interface{}))
  316. } else {
  317. log.Println("添加优惠信息有误")
  318. }
  319. // discountPlan := this.GetString("discountPlan")
  320. // err := json.Unmarshal([]byte(discountPlan), &pmap)
  321. // if err != nil {
  322. // log.Println("err:", err)
  323. // }
  324. r := func() *FuncResult {
  325. //数据校验
  326. if name == "" || address == "" || d_content == "" || Dotype == "" || c_type == 0 ||
  327. price == 0 || s_publishtime == "" || s_starttime == "" || s_endtime == "" {
  328. return &FuncResult{false, errors.New("参数不完整"), nil}
  329. }
  330. publishtime, err := time.ParseInLocation(qutil.Date_Full_Layout, s_publishtime, time.Local)
  331. if err != nil {
  332. return &FuncResult{false, errors.New("发布时间异常"), nil}
  333. }
  334. log.Println("start:", s_starttime)
  335. starttime, err := time.ParseInLocation(qutil.Date_Full_Layout, s_starttime, time.Local)
  336. if err != nil {
  337. return &FuncResult{false, errors.New("课程时间异常"), nil}
  338. }
  339. log.Println("s_endtime:", s_endtime)
  340. endtime, err := time.ParseInLocation(qutil.Date_Full_Layout, s_endtime, time.Local)
  341. if err != nil {
  342. return &FuncResult{false, errors.New("课程时间异常"), nil}
  343. }
  344. if endtime.Before(starttime) {
  345. return &FuncResult{false, errors.New("时间异常"), nil}
  346. }
  347. //
  348. d1_content, err := url.QueryUnescape(d_content)
  349. if err != nil {
  350. return &FuncResult{false, errors.New("课程内容解析异常"), nil}
  351. }
  352. content, err := url.QueryUnescape(d1_content)
  353. if err != nil {
  354. return &FuncResult{false, errors.New("课程内容解析异常"), nil}
  355. }
  356. if !(price > 0) {
  357. return &FuncResult{false, errors.New("课程金额异常"), nil}
  358. }
  359. //数据校验
  360. mData := map[string]interface{}{
  361. "s_name": name,
  362. "i_type": c_type,
  363. "s_address": address,
  364. "i_price": price,
  365. "l_publishtime": publishtime.Unix(),
  366. "l_starttime": starttime.Unix(),
  367. "l_endtime": endtime.Unix(),
  368. "s_content": content,
  369. "s_discountPlan": pmap, //课程优惠内容
  370. }
  371. if Dotype == "add" || Dotype == "copy" {
  372. flag := this.GetString("flag") //保存 or 立即发布
  373. if flag == "publish" {
  374. mData["i_status"] = 1 //立即发布
  375. if time.Now().After(starttime) {
  376. return &FuncResult{false, errors.New("开课时间已到,禁止发布"), nil}
  377. }
  378. } else {
  379. mData["i_status"] = 0 //暂时保存
  380. }
  381. if mongodb.Save("jy_course", mData) == "" {
  382. return &FuncResult{false, errors.New("增加课程失败"), nil}
  383. } else {
  384. return &FuncResult{true, nil, nil}
  385. }
  386. } else if Dotype == "edit" {
  387. id := this.GetString("_id")
  388. if id == "" {
  389. return &FuncResult{false, errors.New("操作信息id为空"), nil}
  390. }
  391. updateFlag := mongodb.Update("jy_course", &map[string]interface{}{
  392. "_id": qutil.StringTOBsonId(id),
  393. }, &map[string]interface{}{
  394. "$set": mData,
  395. }, false, false)
  396. if !updateFlag {
  397. return &FuncResult{false, errors.New("修改课程失败"), nil}
  398. } else {
  399. return &FuncResult{true, nil, nil}
  400. }
  401. }
  402. return &FuncResult{false, errors.New("未知操作"), nil}
  403. }()
  404. if r.Err != nil {
  405. log.Printf("CreateApi err:%v\n", r.Err.Error())
  406. }
  407. this.ServeJson(r.Format())
  408. }
  409. func (this *CourseManage) ChangeStatus() {
  410. flag := this.GetString("flag")
  411. _id := this.GetString("id")
  412. r := func() *FuncResult {
  413. if !(flag == "off" || flag == "on" || flag == "del") {
  414. return &FuncResult{false, errors.New("未知操作"), nil}
  415. }
  416. if _id == "" {
  417. return &FuncResult{false, errors.New("未获取到信息_id"), nil}
  418. }
  419. rData := mongodb.FindById("jy_course", _id, `"i_status":1,"l_starttime":1`)
  420. if rData == nil || len(*rData) == 0 {
  421. return &FuncResult{false, errors.New("未知信息_id"), nil}
  422. }
  423. if (*rData)["i_status"] == -2 {
  424. return &FuncResult{false, errors.New("当前信息状态不能更改"), nil}
  425. }
  426. if flag == "off" { //下线
  427. if (*rData)["i_status"] == 1 {
  428. ok := mongodb.Update("jy_course", &map[string]interface{}{
  429. "_id": qutil.StringTOBsonId(_id),
  430. }, &map[string]interface{}{
  431. "$set": bson.M{"i_status": -1},
  432. }, false, false)
  433. //通过课程id查询该课程相关的订单
  434. var updateSql = `UPDATE dataexport_order SET order_status = -2 WHERE order_status = 0 AND product_type = '招投标课程' AND filter_id = '` + _id + `'`
  435. go cutil.Mysql.UpdateOrDeleteBySql(updateSql)
  436. if ok {
  437. return &FuncResult{true, nil, nil}
  438. }
  439. }
  440. } else if flag == "on" { //发布
  441. //校验开课时间是否已到
  442. if time.Unix(qutil.Int64All((*rData)["l_starttime"]), 0).Before(time.Now()) {
  443. return &FuncResult{false, errors.New("开课时间已到,禁止发布"), nil}
  444. }
  445. if (*rData)["i_status"] == 0 {
  446. ok := mongodb.Update("jy_course", &map[string]interface{}{
  447. "_id": qutil.StringTOBsonId(_id),
  448. }, &map[string]interface{}{
  449. "$set": bson.M{"i_status": 1},
  450. }, false, false)
  451. if ok {
  452. return &FuncResult{true, nil, nil}
  453. }
  454. }
  455. } else if flag == "del" {
  456. status := []int{0, -1} //只能删除已下线或者未发布的
  457. ok := mongodb.Update("jy_course", &map[string]interface{}{
  458. "_id": qutil.StringTOBsonId(_id),
  459. "i_status": bson.M{"$in": status},
  460. }, &map[string]interface{}{
  461. "$set": bson.M{"i_status": -2},
  462. }, false, false)
  463. //通过课程id查询该课程相关的订单
  464. var updateSql = `UPDATE dataexport_order SET order_status = -2 WHERE order_status = 0 AND product_type = '招投标课程' AND filter_id = '` + _id + `'`
  465. go cutil.Mysql.UpdateOrDeleteBySql(updateSql)
  466. if ok {
  467. return &FuncResult{true, nil, nil}
  468. }
  469. }
  470. return &FuncResult{false, errors.New("信息状态修改非法"), nil}
  471. }()
  472. if r.Err != nil {
  473. log.Printf("CreateApi err:%v\n", r.Err.Error())
  474. }
  475. this.ServeJson(r.Format())
  476. }
  477. func (this *CourseManage) CreateOnLineApi() {
  478. c_type, _ := this.GetInteger("type") //课程类型:线下课程(1招标管理课程、2投标实物课程)、3线上课程
  479. name := this.GetString("name")//课程名称
  480. picUrl := this.GetString("picUrl")//头图
  481. price := this.GetString("price")//课程金额
  482. if price == "" {
  483. price = "0"
  484. }
  485. price2 := fmt.Sprintf("%.2f", qutil.Float64All(price))
  486. pos := strings.Index(price2, ".")
  487. price3 := qutil.Int64All(price2[:pos] + price2[pos+1:])
  488. videoUrl := this.GetString("videoUrl")//课程视频
  489. fileUrl := this.GetString("fileUrl")//课件
  490. s_publishtime := this.GetString("publishtime")//发布时间/上线时间
  491. d_content := this.GetString("content")//课程详情
  492. Dotype := this.GetString("dotype") //新增add or 修改edit
  493. //status := this.GetString("status") //1上线、0草稿、-1未发布
  494. courseDate := this.GetString("courseDate")//课程时长
  495. endTime := this.GetString("endTime")//课程有效截止时间
  496. associatedCourses := this.GetString("associatedCourses")//关联课程
  497. //updateTime := this.GetString("updateTime")//课程内容更新时间
  498. //createTime := time.Now().Unix()
  499. //clickRate := 0//点击量默认0次
  500. //tradeRate := 0//购买量默认0次
  501. r := func() *FuncResult {
  502. //数据校验
  503. if name == "" || picUrl == "" || videoUrl == "" || c_type != 3 ||
  504. courseDate == "" || endTime == "" {
  505. return &FuncResult{false, errors.New("参数不完整"), nil}
  506. }
  507. /*if picUrl != "" {
  508. //生成访问地址
  509. b, _, m := cutil.FileRedis(picUrl,"img")
  510. if b == false{
  511. return &FuncResult{false, errors.New(fmt.Sprintf("%s", m)), nil}
  512. }
  513. }
  514. if videoUrl != "" {
  515. //生成访问地址
  516. b, _, m := cutil.FileRedis(videoUrl,"")
  517. if b == false{
  518. return &FuncResult{false, errors.New(fmt.Sprintf("%s", m)), nil}
  519. }
  520. }
  521. if fileUrl != "" {
  522. //生成访问地址
  523. b, _, m := cutil.FileRedis(fileUrl,"file")
  524. if b == false{
  525. return &FuncResult{false, errors.New(fmt.Sprintf("%s", m)), nil}
  526. }
  527. }*/
  528. if videoUrl != "" {
  529. //生成访问地址
  530. b, st := cutil.SetTxVideoRedis(videoUrl)
  531. if b == false{
  532. return &FuncResult{false, errors.New(fmt.Sprintf("%s", st)), nil}
  533. }
  534. }
  535. publishtime, err := time.ParseInLocation(qutil.Date_Full_Layout, s_publishtime, time.Local)
  536. if err != nil {
  537. //return &FuncResult{false, errors.New("发布时间异常"), nil}
  538. publishtime , err = time.ParseInLocation(qutil.Date_Full_Layout, time.Now().Format("2006-01-02 15:04:05"), time.Local)
  539. }
  540. l_endTime, err := time.ParseInLocation(qutil.Date_Full_Layout, endTime, time.Local)
  541. if err != nil {
  542. return &FuncResult{false, errors.New("课程截止时间异常"), nil}
  543. }
  544. //
  545. d1_content, err := url.QueryUnescape(d_content)
  546. if err != nil {
  547. return &FuncResult{false, errors.New("课程内容解析异常"), nil}
  548. }
  549. content, err := url.QueryUnescape(d1_content)
  550. if err != nil {
  551. return &FuncResult{false, errors.New("课程内容解析异常"), nil}
  552. }
  553. /*if err1 != nil {
  554. return &FuncResult{false, errors.New("课程金额异常"), nil}
  555. }*/
  556. //数据校验
  557. mData := map[string]interface{}{
  558. "s_name": name,
  559. "i_type": c_type,
  560. "s_picUrl": picUrl,
  561. "s_videoUrl": videoUrl,
  562. "s_fileUrl": fileUrl,
  563. "i_price": price3,
  564. "l_publishtime": publishtime.Unix(),
  565. //"l_createtime": createTime,
  566. "l_endtime": l_endTime.Unix(),
  567. "s_content": content,
  568. "s_courseDate": courseDate,
  569. "s_associatedCourses": associatedCourses,
  570. //"i_clickRate": clickRate,
  571. //"i_tradeRate": tradeRate,
  572. }
  573. if Dotype == "add" || Dotype == "copy" {
  574. mData["i_clickRate"] = 0
  575. mData["i_tradeRate"] = 0
  576. flag := this.GetString("flag") //保存 or 立即发布
  577. if flag == "publish" {
  578. mData["i_status"] = 1 //立即发布
  579. if time.Now().After(l_endTime) {
  580. return &FuncResult{false, errors.New("课程截止时间已到,禁止发布"), nil}
  581. }
  582. } else {
  583. mData["i_status"] = 0 //暂时保存
  584. }
  585. mData["l_createTime"] = time.Now().Unix()
  586. if mongodb.Save("jy_course", mData) == "" {
  587. return &FuncResult{false, errors.New("增加课程失败"), nil}
  588. } else {
  589. return &FuncResult{true, nil, nil}
  590. }
  591. } else if Dotype == "edit" {
  592. id := this.GetString("_id")
  593. if id == "" {
  594. return &FuncResult{false, errors.New("操作课程信息id为空"), nil}
  595. }
  596. flag := this.GetString("flag") //保存 or 立即发布
  597. if flag == "publish" {
  598. mData["i_status"] = 1 //立即发布
  599. if time.Now().After(l_endTime) {
  600. return &FuncResult{false, errors.New("课程截止时间已到,禁止发布"), nil}
  601. }
  602. } else {
  603. mData["i_status"] = 0 //暂时保存
  604. }
  605. mData["l_updateTime"] = time.Now().Unix()
  606. updateFlag := mongodb.Update("jy_course", &map[string]interface{}{
  607. "_id": qutil.StringTOBsonId(id),
  608. }, &map[string]interface{}{
  609. "$set": mData,
  610. }, false, false)
  611. if !updateFlag {
  612. return &FuncResult{false, errors.New("修改课程失败"), nil}
  613. } else {
  614. return &FuncResult{true, nil, nil}
  615. }
  616. }
  617. return &FuncResult{false, errors.New("未知操作"), nil}
  618. }()
  619. if r.Err != nil {
  620. log.Printf("CreateApi err:%v\n", r.Err.Error())
  621. }
  622. this.ServeJson(r.Format())
  623. }
  624. func (this *CourseManage) OnLineClickTradeRate() {
  625. _id := this.GetString("_id")//课程id
  626. s_type := this.GetString("type")//类型click:点击量、trade:购买量
  627. r := func() *FuncResult {
  628. if _id != "" {
  629. rData := mongodb.FindById("jy_course", _id, `"i_status":1,"l_starttime":1`)
  630. if rData == nil || len(*rData) == 0 {
  631. return &FuncResult{false, errors.New("未知信息_id"), nil}
  632. }
  633. if (*rData)["i_status"] == -1 {
  634. return &FuncResult{false, errors.New("当状态下的信息不能增加购买量或点击量"), nil}
  635. }
  636. if s_type == "click" {
  637. ok := mongodb.Update("jy_course", &map[string]interface{}{
  638. "_id": qutil.StringTOBsonId(_id),
  639. }, &map[string]interface{}{
  640. "$inc": bson.M{"i_clickRate": 1},
  641. }, false, false)
  642. if ok {
  643. return &FuncResult{true, nil, nil}
  644. }
  645. }
  646. if s_type == "trade" {
  647. ok := mongodb.Update("jy_course", &map[string]interface{}{
  648. "_id": qutil.StringTOBsonId(_id),
  649. }, &map[string]interface{}{
  650. "$inc": bson.M{"i_tradeRate": 1},
  651. }, false, false)
  652. if ok {
  653. return &FuncResult{true, nil, nil}
  654. }
  655. }
  656. }else {
  657. return &FuncResult{false, errors.New("课程id不能为空"), nil}
  658. }
  659. return &FuncResult{false, errors.New("未知操作"), nil}
  660. }()
  661. if r.Err != nil {
  662. log.Printf("CreateApi err:%v\n", r.Err.Error())
  663. }
  664. this.ServeJson(r.Format())
  665. }
  666. func (this *CourseManage) OnLineList() {
  667. currentPage, _ := this.GetInteger("currentPage")
  668. perPage, _ := this.GetInteger("perPage")
  669. queryStr := this.GetString("query")
  670. flag := this.GetString("flag")
  671. infoId := this.GetString("infoId")
  672. query := map[string]interface{}{}
  673. if queryStr != "" {
  674. query["s_name"] = bson.M{"$regex": queryStr}
  675. }
  676. if flag == "gl" {
  677. now := time.Now().Unix()
  678. query["i_status"] = 1
  679. query["l_publishtime"] = bson.M{"$lt":now}
  680. query["l_endtime"] = bson.M{"$gt":now}
  681. if infoId != ""{
  682. query["_id"] = bson.M{"$ne": bson.ObjectIdHex(infoId)}
  683. }
  684. }
  685. query["i_type"] = 3
  686. rData := mongodb.Find("jy_course", query, `{"l_publishtime":-1}`, `{"_id":1,"s_courseDate":1,"i_price":1,"i_status":1,"s_name":1,"l_publishtime":1}`, false, (currentPage-1)*perPage, perPage)
  687. count := mongodb.Count("jy_course", query)
  688. this.ServeJson(map[string]interface{}{
  689. "currentPage": currentPage,
  690. "data": rData,
  691. "totalRows": count,
  692. })
  693. }
  694. func (this *CourseManage) OnLineChangeStatus() {
  695. flag := this.GetString("flag")
  696. _id := this.GetString("id")
  697. r := func() *FuncResult {
  698. if !(flag == "off" || flag == "on") {
  699. return &FuncResult{false, errors.New("未知操作"), nil}
  700. }
  701. if _id == "" {
  702. return &FuncResult{false, errors.New("未获取到信息_id"), nil}
  703. }
  704. rData := mongodb.FindById("jy_course", _id, `"i_status":1,"l_endtime":1`)
  705. if rData == nil || len(*rData) == 0 {
  706. return &FuncResult{false, errors.New("未知信息_id"), nil}
  707. }
  708. if (*rData)["i_status"] == -2 {
  709. return &FuncResult{false, errors.New("当前信息状态不能更改"), nil}
  710. }
  711. if flag == "off" { //下线
  712. if (*rData)["i_status"] == 1 {
  713. ok := mongodb.Update("jy_course", &map[string]interface{}{
  714. "_id": qutil.StringTOBsonId(_id),
  715. }, &map[string]interface{}{
  716. "$set": bson.M{"i_status": -1},
  717. }, false, false)
  718. //通过课程id查询该课程相关的订单
  719. var updateSql = `UPDATE dataexport_order SET order_status = -2 WHERE order_status = 0 AND product_type = '线上课程' AND filter_id = '` + _id + `'`
  720. go cutil.Mysql.UpdateOrDeleteBySql(updateSql)
  721. if ok {
  722. return &FuncResult{true, nil, nil}
  723. }
  724. }
  725. } else if flag == "on" { //发布
  726. //校验开课时间是否已到
  727. if time.Unix(qutil.Int64All((*rData)["l_endtime"]), 0).Before(time.Now()) {
  728. return &FuncResult{false, errors.New("课程截止时间已到,禁止上线"), nil}
  729. }
  730. if (*rData)["i_status"] == 0 || (*rData)["i_status"] == -1{
  731. ok := mongodb.Update("jy_course", &map[string]interface{}{
  732. "_id": qutil.StringTOBsonId(_id),
  733. }, &map[string]interface{}{
  734. "$set": bson.M{"i_status": 1},
  735. }, false, false)
  736. if ok {
  737. return &FuncResult{true, nil, nil}
  738. }
  739. }
  740. }
  741. return &FuncResult{false, errors.New("信息状态修改非法"), nil}
  742. }()
  743. if r.Err != nil {
  744. log.Printf("CreateApi err:%v\n", r.Err.Error())
  745. }
  746. this.ServeJson(r.Format())
  747. }
  748. func (this *CourseManage) OnLineDetail(flag, id string) error{
  749. if flag == "edit" {
  750. //获取文章详情
  751. if id == "" {
  752. return errors.New("请求参数有误")
  753. }
  754. rData := mongodb.FindById("jy_course", id, `{"_id":1,"i_status":1,"s_content":1,"s_name":1,"s_picUrl":1,"i_price":1,"l_publishtime":1,"l_endtime":1,"s_associatedCourses":1,"i_clickRate":1,"s_videoUrl":1,"s_fileUrl":1,"s_courseDate":1,"i_tradeRate":1}`)
  755. this.T["picUrl"] = (*rData)["s_picUrl"]
  756. this.T["name"] = (*rData)["s_name"]
  757. this.T["videoUrl"] = (*rData)["s_videoUrl"]
  758. this.T["fileUrl"] = (*rData)["s_fileUrl"]
  759. this.T["price"] = fmt.Sprintf("%.2f", qutil.Float64All((*rData)["i_price"])/100)
  760. this.T["endTime"] = time.Unix(qutil.Int64All((*rData)["l_endtime"]), 0).Format(qutil.Date_Full_Layout)
  761. this.T["courseDate"] = (*rData)["s_courseDate"]
  762. this.T["content"] = (*rData)["s_content"]
  763. this.T["status"] = (*rData)["i_status"]
  764. ids := (*rData)["s_associatedCourses"]
  765. var idSlice []bson.ObjectId
  766. if ids != "" {
  767. arr := strings.Split(ids.(string), ",")
  768. for _, as := range arr{
  769. idSlice = append(idSlice, bson.ObjectIdHex(as))
  770. }
  771. }
  772. now := time.Now().Unix()
  773. queryAbout := bson.M{"i_type": 3, "i_status": 1, "_id": bson.M{"$in":idSlice}, "l_publishtime": bson.M{"$lt":now}, "l_endtime": bson.M{"$gt":now}}
  774. aboutList := mongodb.Find("jy_course", queryAbout, `{"l_publishtime":-1}`,
  775. `{"_id":1,"s_name":1}`, false, 0, 0)
  776. this.T["associatedCourses"] = aboutList
  777. this.T["publishtime"] = time.Unix(qutil.Int64All((*rData)["l_publishtime"]), 0).Format(qutil.Date_Full_Layout)
  778. this.T["id"] = id
  779. }
  780. this.T["doType"] = flag
  781. this.Render("/manage/course/onLineContent.html")
  782. return nil
  783. /*_id := this.GetString("_id") //课程id
  784. r := func() *FuncResult {
  785. if _id == "" {
  786. return &FuncResult{false, errors.New("信息id为空"), nil}
  787. }
  788. //课程基本信息
  789. rData := mongodb.FindById("jy_course", _id, `{"_id":1,"s_content":1,"s_name":1,"s_picUrl":1,"i_price":1,"l_publishtime":1,"l_endtime":1,"s_associatedCourses":1,"i_clickRate":1,"s_videoUrl":1,"s_fileUrl":1,"s_courseDate":1,"i_tradeRate":1}`)
  790. if rData == nil || len(*rData) == 0 {
  791. return &FuncResult{false, errors.New("未找到此课程的信息"), nil}
  792. }
  793. resultMap := map[string]interface{}{}
  794. (*rData)["l_starttime"] = time.Unix(qutil.Int64All((*rData)["l_publishtime"]), 0).Format("2006-01-02 15:04")
  795. (*rData)["l_endtime"] = time.Unix(qutil.Int64All((*rData)["l_endtime"]), 0).Format(qutil.Date_Short_Layout)
  796. resultMap["detail"] = rData
  797. if (*rData)["s_associatedCourses"] != "" {
  798. //相关课程信息
  799. ids := (*rData)["s_associatedCourses"]
  800. arr := strings.Split(ids.(string), ",")
  801. var idSlice []bson.ObjectId
  802. for _, as := range arr{
  803. idSlice = append(idSlice, bson.ObjectIdHex(as))
  804. }
  805. now := time.Now().Unix()
  806. queryAbout := bson.M{"i_type": 3, "i_status": 1, "_id": bson.M{"$in":idSlice}, "l_publishtime": bson.M{"$lt":now}, "l_endtime": bson.M{"$gt":now}}
  807. aboutList := mongodb.Find("jy_course", queryAbout, `{"l_publishtime":-1}`,
  808. `{"_id":1,"s_name":1}`, false, 0, 0)
  809. resultMap["aboutList"] = aboutList
  810. }
  811. return &FuncResult{true, nil, resultMap}
  812. }()
  813. if r.Err != nil {
  814. log.Printf(" CourseContent err:%v\n", r.Err.Error())
  815. }
  816. this.ServeJson(r.Format())*/
  817. }
  818. //线上课程首页
  819. func (this *CourseManage) OnLineIndex() error {
  820. return this.Render("/manage/course/onLineIndex.html")
  821. }