order_dal.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. package dal
  2. import (
  3. "database/sql"
  4. "errors"
  5. "fmt"
  6. "gopkg.in/guregu/null.v3"
  7. "log"
  8. "regexp"
  9. "strings"
  10. "time"
  11. "xiaoniaokuaiyan.com/xiaoniao/entity"
  12. "xiaoniaokuaiyan.com/xiaoniao/util"
  13. )
  14. type Order struct {
  15. }
  16. func (odal *Order) Update(oitem *entity.Order) (interface{}, error) {
  17. db := util.GetWriteSqlDB()
  18. sqlStr, kvm := util.GenerateUpdateSqlFromStruct("t_order", oitem, " where id='"+oitem.Id+"'")
  19. result, err := db.NamedExec(sqlStr, kvm)
  20. if err != nil {
  21. return nil, err
  22. }
  23. ra, err := result.RowsAffected()
  24. if err != nil {
  25. return nil, err
  26. }
  27. if ra <= 0 {
  28. return nil, errors.New("update failed(can not found order record)")
  29. }
  30. return oitem, nil
  31. }
  32. func (odal *Order) Get(id string) (*entity.OrderDB, error) {
  33. db := util.GetSqlDB()
  34. oitem := entity.OrderDB{}
  35. err := db.Get(&oitem, "select * FROM t_order WHERE id = ?", id)
  36. if err != nil {
  37. if err == sql.ErrNoRows {
  38. return nil, nil
  39. }
  40. return nil, err
  41. }
  42. return &oitem, nil
  43. }
  44. func (odal *Order) GetByCode(bcode string) (interface{}, error) {
  45. db := util.GetSqlDB()
  46. oitem := struct {
  47. entity.OrderDB
  48. SendbackTpl string `db:"sendback_tpl"`
  49. ProductName string `db:"product_name"`
  50. }{}
  51. err := db.Get(&oitem, "select t1.*, bloodtest_id, need_emptiness,report_period,pressure_pipe,blood_address,notice FROM t_order t1 left join t_order_extra t2 on t1.id = t2.order_id WHERE blood_codes like ? limit 1", "%"+bcode+"%")
  52. if err != nil {
  53. if err == sql.ErrNoRows {
  54. return nil, nil
  55. }
  56. return nil, err
  57. }
  58. db.Get(&oitem, "select sendback_tpl, product_name from t_product tp right join (select product_id, product_name from t_order t1 left join t_order_product t2 on t1.id = t2.order_id where id = ?) t3 on tp.id = t3.product_id limit 1;", oitem.Id)
  59. return &oitem, nil
  60. }
  61. func (odal *Order) List(customId, pageIndex, pageSize, status int, mobile string, isZFB, isHis bool) (interface{}, error) {
  62. var result = OrderListResult{}
  63. strSql := "select t2.* from (select id, mobile from t_custom where id = ?) t1, t_order t2 "
  64. whereStr := " where (t1.id = t2.custom_id or t1.mobile = t2.mobile) "
  65. whereValue := []interface{}{
  66. customId,
  67. }
  68. pinfo := Pager{
  69. PageIndex: pageIndex,
  70. PageSize: pageSize,
  71. }
  72. // 20230629 增加支付宝特殊处理
  73. if isZFB {
  74. whereStr += " and t2.source='sp_zfb' "
  75. }
  76. if status > 0 {
  77. whereStr += " and status = ? "
  78. whereValue = append(whereValue, status)
  79. } else if status == -1 {
  80. whereStr += " and status in(6,11) "
  81. } else {
  82. //whereStr += " and status <> 8 "
  83. whereStr += " and is_delete='N'"
  84. }
  85. if mobile != "" {
  86. whereStr += " and t2.mobile = '" + mobile + "' "
  87. }
  88. whereStr += " and t2.retype in ('100','110')"
  89. //20230830 拆分历史表
  90. if isHis {
  91. strSql = util.ChangeOrderTableName(strSql)
  92. }
  93. rel, err := List(&result, "("+strSql+whereStr+") t3", strSql+whereStr, " order by created_at desc ", "", whereValue, pinfo)
  94. if err != nil {
  95. return nil, err
  96. }
  97. err = result.FetchProduct()
  98. if err != nil {
  99. return nil, err
  100. }
  101. return rel, nil
  102. }
  103. func (odal *Order) Detail(oid string) (interface{}, error) {
  104. var result = &OrderResult{}
  105. //strSql := "select * from t_order t1 left join t_order_extra t2 on t1.id = t2.order_id where t1.id = ?"
  106. strSql := ` select t1.id,t1.name,t1.gender,t1.age,t1.birthday,t1.address,t1.detail_address,t1.visit_date,t1.visit_time_range,t1.status,t1.work_fee,t1.free_fee,t1.repeat_item_fee,t1.payment,t1.actual_payment,t1.payment_time,t1.pay_type,t1.pay_no,t1.custom_mobile,t1.mobile,t1.created_at,t1.is_view_report,t1.report_picture,t2.is_notice,t2.express,t2.expressnumber,t1.delay_deadtime,t1.linker_id,t1.source,t2.report_period,t2.pressure_pipe,t2.blood_address,t2.notice,t1.remark,t1.id_num,t2.e_visit_date,t2.e_visit_time_range,t1.zone,t1.vcode,t1.jd_order_id,t1.complete_time,t1.share_source from t_order t1 left join t_order_extra t2 on t1.id = t2.order_id where t1.id = ?
  107. union
  108. select t3.id,t3.name,t3.gender,t3.age,t3.birthday,t3.address,t3.detail_address,t3.visit_date,t3.visit_time_range,t3.status,t3.work_fee,t3.free_fee,t3.repeat_item_fee,t3.payment,t3.actual_payment,t3.payment_time,t3.pay_type,t3.pay_no,t3.custom_mobile,t3.mobile,t3.created_at,t3.is_view_report,t3.report_picture,t4.is_notice,t4.express,t4.expressnumber,t3.delay_deadtime,t3.linker_id,t3.source,t4.report_period,t4.pressure_pipe,t4.blood_address,t4.notice,t3.remark,t3.id_num,t4.e_visit_date,t4.e_visit_time_range,t3.zone,t3.vcode,t3.jd_order_id,t3.complete_time,t3.share_source from t_order_history t3 left join t_order_extra_history t4 on t3.id = t4.order_id where t3.id = ? limit 1`
  109. db := util.GetSqlDB()
  110. err := db.Get(result, strSql, oid, oid)
  111. if err != nil {
  112. return nil, err
  113. }
  114. err = result.FetchProduct()
  115. if err != nil {
  116. return nil, err
  117. }
  118. return result, nil
  119. }
  120. type tempPitem struct {
  121. ProductId int `db:"product_id"`
  122. ProductName string `db:"product_name"`
  123. Price int `db:"price"`
  124. Quantity int `db:"quantity"`
  125. SaleMode int `db:"sale_mode"`
  126. Picture string `db:"picture"`
  127. SubIds sql.NullString `db:"sub_ids"`
  128. Tube sql.NullString `db:"tube"`
  129. }
  130. func (odal *Order) CheckAndSplitOrder(oid string) (bool, error) {
  131. //是否拆单了
  132. var isSplited bool = false
  133. //是否全部邮寄 状态200
  134. var isAllyouji200 = true //for 循环中,有一个100 就为false
  135. //是否包含抗原
  136. var isExistKangyuan = false //for 循环中, 有一个抗原 就为true
  137. //抗原产品名
  138. var kangyuanProductName string
  139. db := util.GetWriteSqlDB()
  140. strSql := "select t1.product_id, t1.product_name, t1.price, t1.picture,t1.quantity, t2.sale_mode, t2.sub_ids, t2.tube from t_order_product t1 left join t_product t2 on t1.product_id = t2.id where t1.order_id=? order by quantity desc;"
  141. plist := []tempPitem{}
  142. err := db.Select(&plist, strSql, oid)
  143. if err != nil {
  144. return isSplited, err
  145. }
  146. strSql = "select * from t_order t1 left join t_order_extra t2 on t1.id = t2.order_id where t1.id = ?"
  147. oitem := entity.OrderDB{}
  148. err = db.Get(&oitem, strSql, oid)
  149. if err != nil {
  150. return isSplited, err
  151. }
  152. //20221229如果每样下了一单,暂不拆单
  153. //20221229拆单逻辑调整 如果只有邮寄产品且包括抗原 发送短信27; 如果只有邮寄产品且不包括抗原,不发送短信;包含上门产品,发送短信16
  154. if len(plist) > 0 && plist[0].Quantity <= 1 {
  155. for idx := range plist {
  156. //20221229 判断全邮寄产品逻辑
  157. if isAllyouji200 { //如果目前全都是邮寄
  158. //如果有一个不是 200 isAllyouji200 就为false
  159. isAllyouji200 = plist[idx].SaleMode == 200
  160. }
  161. //20221229 判断是否有抗原逻辑
  162. if util.ProductIsKangYuan(plist[idx].ProductId) {
  163. isExistKangyuan = true
  164. kangyuanProductName += plist[idx].ProductName + ","
  165. }
  166. //20220427 邮寄产品直接准备耗材
  167. if len(plist) == 1 && util.ProductStatusJumpTo10(plist[idx].ProductId) {
  168. db.MustExec("update t_order set status = 10 where id = ?", oid)
  169. } else if len(plist) == 1 && util.ProductWenZhen(plist[idx].ProductId) {
  170. db.MustExec("update t_order set status = 6 where id = ?", oid)
  171. }
  172. //20210225 1149 拆单修改,防止用户只下一单不走下面的流程
  173. //odal.SplitOrderV2(oid, plist[idx].ProductId)
  174. //20220620 拆单改良版
  175. odal.SplitOrderV3(oid, plist[idx].ProductId)
  176. }
  177. if isAllyouji200 && isExistKangyuan {
  178. SendKangYuanSms(oitem.Mobile, kangyuanProductName[0:len(kangyuanProductName)-1])
  179. }
  180. /* 20221229 修改逻辑
  181. //20220427 邮寄产品直接准备耗材
  182. if len(plist) == 1 && util.ProductStatusJumpTo10(plist[0].ProductId) {
  183. db.MustExec("update t_order set status = 10 where id = ?", oid)
  184. }
  185. //20210225 1149 拆单修改,防止用户只下一单不走下面的流程
  186. odal.SplitOrderV2(oid, plist[0].ProductId)
  187. //20220620 拆单改良版
  188. odal.SplitOrderV3(oid, plist[0].ProductId)
  189. */
  190. return isSplited, nil
  191. }
  192. tx := db.MustBegin()
  193. isSplited = true
  194. prefixReg := regexp.MustCompile(`\d`)
  195. prefix := prefixReg.ReplaceAllString(oitem.Id, "")
  196. var (
  197. leftPayment int = oitem.Payment
  198. leftVActualPayment int = int(oitem.ActualPayment.Int64)
  199. leftFreeFee int = oitem.FreeFee
  200. )
  201. //20210129 拆单后的oid 于产品的 map
  202. oidList := map[string]int{}
  203. for idx, pitem := range plist {
  204. //20221229 判断全邮寄产品逻辑
  205. if isAllyouji200 { //如果目前全都是邮寄
  206. //如果有一个不是 200 isAllyouji200 就为false
  207. isAllyouji200 = plist[idx].SaleMode == 200
  208. }
  209. //20221229 判断是否有抗原逻辑
  210. if util.ProductIsKangYuan(plist[idx].ProductId) {
  211. isExistKangyuan = true
  212. kangyuanProductName += plist[idx].ProductName + ","
  213. }
  214. //20220427 邮寄产品直接准备耗材
  215. status := oitem.Status
  216. if util.ProductStatusJumpTo10(pitem.ProductId) {
  217. status = 10
  218. } else if util.ProductWenZhen(pitem.ProductId) {
  219. status = 6
  220. }
  221. for i := 0; i < pitem.Quantity; i++ {
  222. if idx == len(plist)-1 && i == pitem.Quantity-1 {
  223. break
  224. }
  225. newOItem := &entity.Order{
  226. Id: util.GenerateOrderNo(prefix),
  227. CreatedAt: time.Now().Format("2006-01-02 15:04:05"),
  228. Name: oitem.Name,
  229. Gender: oitem.Gender,
  230. Birthday: oitem.Birthday,
  231. Address: oitem.Address,
  232. DetailAddress: oitem.DetailAddress,
  233. VisitDate: oitem.VisitDate.String,
  234. VisitTimeRange: oitem.VisitTimeRange.String,
  235. Source: oitem.Source,
  236. Status: status, //oitem.Status,
  237. CustomId: int(oitem.CustomId.Int64),
  238. WorkFee: 0,
  239. Mobile: oitem.Mobile,
  240. CustomMobile: oitem.CustomMobile,
  241. Payment: pitem.Price,
  242. FreeFee: int(oitem.FreeFee * pitem.Price / oitem.Payment),
  243. VActualPayment: int(int(oitem.ActualPayment.Int64) * pitem.Price / oitem.Payment),
  244. ServiceRemark: fmt.Sprintf("由订单%s自动拆单生成", oitem.Id),
  245. JdOrderId: oitem.JdOrderId.String,
  246. Coordinates: oitem.Coordinates.String,
  247. }
  248. leftFreeFee -= newOItem.FreeFee
  249. leftPayment -= newOItem.Payment
  250. leftVActualPayment -= newOItem.VActualPayment
  251. //20210129 添加 拆单后的oid,以便1149 拆单
  252. oidList[newOItem.Id] = pitem.ProductId
  253. strSql, mkv := util.GenerateInsertSqlFromStruct("t_order", newOItem)
  254. _, err = tx.NamedExec(strSql, mkv)
  255. if err != nil {
  256. fmt.Println("split order ", oitem.Id, err)
  257. tx.Tx.Rollback()
  258. return isSplited, err
  259. }
  260. strSql = "insert into t_order_product(order_id, product_id, product_name, price, picture) values (?,?,?,?,?)"
  261. sqlResult := tx.MustExec(strSql, newOItem.Id, pitem.ProductId, pitem.ProductName, pitem.Price, pitem.Picture)
  262. if ra, _ := sqlResult.RowsAffected(); ra <= 0 {
  263. tx.Tx.Rollback()
  264. return isSplited, err
  265. }
  266. tx.MustExec("insert into t_order_extra(order_id, pressure_pipe, relationship, is_yunxue, is_dfguomin) values (?,?,?,?,?)", newOItem.Id, pitem.Tube.String, oitem.Relationship.String, oitem.IsYunxue.Int64, oitem.IsDfguomin.Int64)
  267. }
  268. strSql = "delete from t_order_product where order_id = ? and product_id = ?"
  269. sqlResult := tx.MustExec(strSql, oitem.Id, pitem.ProductId)
  270. if ra, _ := sqlResult.RowsAffected(); ra <= 0 {
  271. tx.Tx.Rollback()
  272. break
  273. }
  274. if idx == len(plist)-1 {
  275. strSql = "insert into t_order_product(order_id, product_id, product_name, price, picture) values (?,?,?,?,?)"
  276. sqlResult = tx.MustExec(strSql, oitem.Id, pitem.ProductId, pitem.ProductName, pitem.Price, pitem.Picture)
  277. if ra, _ := sqlResult.RowsAffected(); ra <= 0 {
  278. tx.Tx.Rollback()
  279. return isSplited, err
  280. }
  281. tx.MustExec("update t_order_extra set pressure_pipe = ? where order_id = ?", pitem.Tube.String, oitem.Id)
  282. //tx.MustExec("update t_order set payment = ?, v_actual_payment = ?, free_fee =? where id = ?;", leftPayment, leftVActualPayment, leftFreeFee, oitem.Id)
  283. tx.MustExec("update t_order set payment = ?, v_actual_payment = ?, free_fee =?,status = ? where id = ?;", leftPayment, leftVActualPayment, leftFreeFee, status, oitem.Id)
  284. //添加最开始的单据
  285. oidList[oitem.Id] = pitem.ProductId
  286. }
  287. }
  288. if isSplited {
  289. tx.Tx.Commit()
  290. //todo 发送短信 提示添加信息 20210105
  291. strSql = "select content from t_sms_tpl where id = 16;"
  292. Content := ""
  293. db.Get(&Content, strSql)
  294. if len(Content) > 0 && !isAllyouji200 {
  295. util.SendSMS(oitem.Mobile, Content)
  296. }
  297. //拆单后2次拆单
  298. for k, v := range oidList {
  299. //odal.SplitOrderV2(k, v)
  300. odal.SplitOrderV3(k, v)
  301. }
  302. }
  303. if isAllyouji200 && isExistKangyuan {
  304. go SendKangYuanSms(oitem.Mobile, kangyuanProductName[0:len(kangyuanProductName)-1])
  305. }
  306. return isSplited, nil
  307. }
  308. // 20220620 拆单改良版 结合拆出不同产品,以及拆出多个数量
  309. func (dal *Order) SplitOrderV3(orderId string, productId int) {
  310. db := util.GetWriteSqlDB()
  311. //查询拆单关系
  312. tpsInfoList := []struct {
  313. ProductId int `db:"product_id" `
  314. ChildId int `db:"child_id"`
  315. Num int `db:"num"`
  316. Name string `db:"name"`
  317. Price int `db:"price"`
  318. }{}
  319. //sqlSearch := "select t1.*,t2.name as name,t2.price as price from t_product_split t1 left join t_product t2 on t1.child_id = t2.id where t1.product_id =? order by t1.is_main desc"
  320. sqlSearch := "select t1.product_id,t1.child_id,t1.num,if (t2.name is null,'',t2.name) as name,if (t2.price is null ,0,t2.price) as price from t_product_split t1 left join t_product t2 on t1.child_id = t2.id where t1.product_id =? order by t1.is_main desc"
  321. err := db.Select(&tpsInfoList, sqlSearch, productId)
  322. if err != nil || len(tpsInfoList) <= 0 {
  323. log.Println(fmt.Sprintf("订单%s,产品编号为%d 不需要拆单", orderId, productId))
  324. return
  325. }
  326. //查询订单,
  327. orderDb, err := dal.Get(orderId)
  328. fmt.Println(orderDb)
  329. //查询订单 产品关联
  330. plist := struct {
  331. OrderId string `db:"order_id"`
  332. ProductId string `db:"product_id"`
  333. ProductName string `db:"product_name"`
  334. Price int `db:"price"`
  335. Picture string `db:"picture"`
  336. Quantity int `db:"quantity"`
  337. IsPersonal int `db:"is_personal"`
  338. }{}
  339. strSql := "select * from t_order_product where order_id = ? and product_id = ?"
  340. err = db.Get(&plist, strSql, orderId, productId)
  341. tx := db.MustBegin()
  342. prefixReg := regexp.MustCompile(`\d`)
  343. prefix := prefixReg.ReplaceAllString(orderId, "")
  344. firstEver := true
  345. for i := 0; i < len(tpsInfoList); i++ {
  346. //todo sth
  347. for j := 0; j < tpsInfoList[i].Num; j++ {
  348. if firstEver {
  349. //order 不用更新
  350. //更新order_product
  351. result := tx.MustExec("update t_order_product set product_id = ?,product_name = ?,price=? where order_id = ? and product_id = ?", tpsInfoList[i].ChildId, tpsInfoList[i].Name, tpsInfoList[i].Price, orderId, productId)
  352. if ra, _ := result.RowsAffected(); ra <= 0 {
  353. tx.Tx.Rollback()
  354. log.Println(fmt.Sprintf("订单%s,产品编号为%d 拆单失败 更新 t_order_product ", orderId, productId))
  355. return
  356. }
  357. tx.MustExec("update t_order set payment = ? where id = ?", tpsInfoList[i].Price, orderId)
  358. } else {
  359. //生成订单号
  360. orderNewId := util.GenerateOrderNo(prefix)
  361. //fmt.Println(orderNewId)
  362. //插入 t_order
  363. orderDb.Id = orderNewId
  364. orderDb.Payment = tpsInfoList[i].Price
  365. orderDb.ServiceRemark = null.StringFrom(fmt.Sprintf("由%s拆单", orderId))
  366. orderDb.ActualPayment = null.IntFrom(0)
  367. strSql, mkv := util.GenerateInsertSqlFromStruct("t_order", orderDb)
  368. osqlResult, err := tx.NamedExec(strSql, mkv)
  369. if err != nil {
  370. tx.Tx.Rollback()
  371. log.Println(fmt.Sprintf("订单%s,产品编号为%d 拆单失败 插入 t_order %s", orderId, productId, orderNewId))
  372. return
  373. }
  374. if ra, _ := osqlResult.RowsAffected(); ra <= 0 {
  375. tx.Tx.Rollback()
  376. log.Println(fmt.Sprintf("订单%s,产品编号为%d 拆单失败 插入 t_order %s", orderId, productId, orderNewId))
  377. return
  378. }
  379. //todo 查询product
  380. //插入 t_order_product 注意 金额
  381. var fields = map[string]interface{}{
  382. "order_id": orderNewId,
  383. "product_id": tpsInfoList[i].ChildId,
  384. "product_name": tpsInfoList[i].Name,
  385. "price": tpsInfoList[i].Price,
  386. "picture": plist.Picture,
  387. "quantity": plist.Quantity,
  388. "is_personal": plist.IsPersonal,
  389. }
  390. strSql = util.GenerateInsertSql("t_order_product", fields)
  391. _, err = tx.NamedExec(strSql, fields)
  392. if err != nil {
  393. tx.Tx.Rollback()
  394. log.Println(fmt.Sprintf("订单%s,产品编号为%d 拆单失败,新建 t_order_product %s", orderId, productId, orderNewId))
  395. return
  396. }
  397. //复制 t_order_extra
  398. result := tx.MustExec(fmt.Sprintf("insert into t_order_extra(`order_id`, `need_emptiness`, `report_period`, `pressure_pipe`, `blood_address`, `notice`, `updated_at`, `remark1`, `cancel_reason`, `is_notice`, `relationship`, `is_yunxue`, `is_dfguomin`, `bloodtest_id`, `reportresult`, `paynurse`, `isaddpro`, `sample`, `jicai_name`, `express`, `expressnumber`, `expressstatus`, `expressinfo`, `express_user`, `expressnumber_user`) select '%s',`need_emptiness`, `report_period`, `pressure_pipe`, `blood_address`, `notice`, `updated_at`, `remark1`, `cancel_reason`, `is_notice`, `relationship`, `is_yunxue`, `is_dfguomin`, `bloodtest_id`, `reportresult`, `paynurse`, `isaddpro`, `sample`, `jicai_name`, `express`, `expressnumber`, `expressstatus`, `expressinfo`, `express_user`, `expressnumber_user` from t_order_extra where order_id =?", orderNewId), orderId)
  399. if ra, _ := result.RowsAffected(); ra <= 0 {
  400. tx.Tx.Rollback()
  401. log.Println(fmt.Sprintf("订单%s,产品编号为%d 拆单失败,新建 t_order_extra %s", orderId, productId, orderNewId))
  402. return
  403. }
  404. }
  405. }
  406. firstEver = false
  407. }
  408. tx.Commit()
  409. }
  410. func SendKangYuanSms(mobile, productNames string) {
  411. strSql := "select content from t_sms_tpl where id = 27;"
  412. Content := ""
  413. db := util.GetSqlDB()
  414. db.Get(&Content, strSql)
  415. if len(Content) > 0 {
  416. Content = strings.Replace(Content, "{product}", productNames, 1)
  417. util.SendSMS(mobile, Content)
  418. }
  419. }
  420. func (odal *Order) CommitSurvey(osItem *entity.OrderSurvey) (bool, error) {
  421. db := util.GetWriteSqlDB()
  422. if osItem.ServiceScore == "200" {
  423. strSql := "select count(order_id) as ex from t_order_sat_survey where order_id = ?"
  424. var count uint8
  425. err := db.Get(&count, strSql, osItem.OrderId)
  426. if err != nil {
  427. return false, err
  428. }
  429. if count > 0 {
  430. return true, nil
  431. }
  432. return false, nil
  433. }
  434. strSql, mkv := util.GenerateInsertSqlFromStruct("t_order_sat_survey", osItem)
  435. _, err := db.NamedExec(strSql, mkv)
  436. if err != nil {
  437. return false, err
  438. }
  439. return true, nil
  440. }
  441. func (odal *Order) IsFirst(mobile string) (bool, error) {
  442. db := util.GetSqlDB()
  443. var count int
  444. err := db.Get(&count, "select count(id) from t_order where custom_mobile =? and status not in(1,9) and retype in ('100','110');", mobile)
  445. if err != nil {
  446. return false, err
  447. }
  448. if count > 1 {
  449. return false, nil
  450. }
  451. return true, nil
  452. }
  453. var DefaultOrderDal = &Order{}
  454. func (odal *Order) ListCustomFile(customFileId, pageIndex, pageSize, status int, mobile string, isZFB, isHis bool) (interface{}, error) {
  455. var result = OrderListResult{}
  456. strSql := "select t2.* from (select id, mobile from t_custom_file where id = ?) t1, t_order t2 "
  457. whereStr := " where (t1.id = t2.custom_file_id or t1.mobile = t2.mobile) "
  458. whereValue := []interface{}{}
  459. if customFileId == 0 { //传0 代表是查看自己的报告
  460. strSql = "select t2.* from t_order t2 "
  461. whereStr = " where 1=1 "
  462. } else {
  463. whereValue = append(whereValue, customFileId)
  464. }
  465. pinfo := Pager{
  466. PageIndex: pageIndex,
  467. PageSize: pageSize,
  468. }
  469. // 20230629 增加支付宝特殊处理
  470. if isZFB {
  471. whereStr += " and t2.source='sp_zfb' "
  472. }
  473. if status > 0 {
  474. whereStr += " and status = ? "
  475. whereValue = append(whereValue, status)
  476. } else if status == -1 {
  477. whereStr += " and status in(6,11) "
  478. } else {
  479. //whereStr += " and status <> 8 "
  480. whereStr += " and is_delete='N'"
  481. }
  482. if mobile != "" {
  483. whereStr += " and t2.mobile = '" + mobile + "' "
  484. }
  485. whereStr += " and t2.retype in ('100','110')"
  486. if isHis {
  487. strSql = util.ChangeOrderTableName(strSql)
  488. }
  489. rel, err := List(&result, "("+strSql+whereStr+") t3", strSql+whereStr, " order by complete_time desc ", "", whereValue, pinfo)
  490. if err != nil {
  491. return nil, err
  492. }
  493. err = result.FetchProduct()
  494. if err != nil {
  495. return nil, err
  496. }
  497. return rel, nil
  498. }