eta_business.go 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  1. package eta_business
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "hongze/hz_crm_api/controllers"
  7. "hongze/hz_crm_api/models"
  8. "hongze/hz_crm_api/models/eta_business"
  9. etaBusinessService "hongze/hz_crm_api/services/eta_business"
  10. "hongze/hz_crm_api/utils"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // EtaBusinessController ETA商家
  16. type EtaBusinessController struct {
  17. controllers.BaseAuthController
  18. }
  19. // PageList
  20. // @Title 商家列表-分页
  21. // @Description 商家列表-分页
  22. // @Param Keyword query string false "关键词: 商家名称/社会信用码/商家编码"
  23. // @Param SellerIds query string false "销售IDs"
  24. // @Param SigningStatus query string false "签约状态: 1-首次签约; 2-续约中; 3-已终止"
  25. // @Param Province query string false "省份筛选"
  26. // @Param City query string false "城市筛选"
  27. // @Param SortParam query int false "排序字段: 1-签约时间; 2-到期时间; 3-创建时间; 4-用户上限"
  28. // @Param SortType query int false "排序类型: 1-正序; 2-倒序"
  29. // @Success 200 Ret=200 获取成功
  30. // @router /page_list [get]
  31. func (this *EtaBusinessController) PageList() {
  32. br := new(models.BaseResponse).Init()
  33. defer func() {
  34. if br.ErrMsg == "" {
  35. br.IsSendEmail = false
  36. }
  37. this.Data["json"] = br
  38. this.ServeJSON()
  39. }()
  40. sysUser := this.SysUser
  41. if sysUser == nil {
  42. br.Msg = "请登录"
  43. br.ErrMsg = "请登录,SysUser Is Empty"
  44. br.Ret = 408
  45. return
  46. }
  47. // 分页
  48. pageSize, _ := this.GetInt("PageSize")
  49. currentIndex, _ := this.GetInt("CurrentIndex")
  50. var startSize int
  51. if pageSize <= 0 {
  52. pageSize = utils.PageSize20
  53. }
  54. if currentIndex <= 0 {
  55. currentIndex = 1
  56. }
  57. startSize = utils.StartIndex(currentIndex, pageSize)
  58. // 权限校验
  59. resp := new(eta_business.EtaBusinessListResp)
  60. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  61. if e != nil {
  62. br.Msg = "操作失败"
  63. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  64. return
  65. }
  66. if !ok {
  67. resp.Paging = paging.GetPaging(currentIndex, pageSize, 0)
  68. br.Data = resp
  69. br.Ret = 200
  70. br.Success = true
  71. br.Msg = "获取成功"
  72. return
  73. }
  74. cond := ``
  75. pars := make([]interface{}, 0)
  76. // 筛选项
  77. {
  78. // 关键词
  79. keyword := this.GetString("Keyword", "")
  80. keyword = strings.TrimSpace(keyword)
  81. if keyword != "" {
  82. kw := fmt.Sprint("%", keyword, "%")
  83. cond += fmt.Sprintf(` AND (%s LIKE ? OR %s LIKE ? OR %s LIKE ?)`, eta_business.EtaBusinessColumns.BusinessName, eta_business.EtaBusinessColumns.BusinessCode, eta_business.EtaBusinessColumns.CreditCode)
  84. pars = append(pars, kw, kw, kw)
  85. }
  86. // 销售
  87. sellerIds := this.GetString("SellerIds", "")
  88. if sellerIds != "" {
  89. arr := strings.Split(sellerIds, ",")
  90. ids := make([]int, 0)
  91. for _, s := range arr {
  92. v, e := strconv.Atoi(s)
  93. if e != nil {
  94. br.Msg = "销售ID有误"
  95. return
  96. }
  97. ids = append(ids, v)
  98. }
  99. if len(ids) == 0 {
  100. resp.Paging = paging.GetPaging(currentIndex, pageSize, 0)
  101. br.Data = resp
  102. br.Ret = 200
  103. br.Success = true
  104. br.Msg = "获取成功"
  105. return
  106. }
  107. cond += fmt.Sprintf(` AND %s IN (%s)`, eta_business.EtaBusinessColumns.SellerId, utils.GetOrmInReplace(len(ids)))
  108. pars = append(pars, ids)
  109. }
  110. // 签约状态
  111. signingStatus, _ := this.GetInt("SigningStatus", 0)
  112. if signingStatus > 0 {
  113. cond += fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessColumns.SigningStatus)
  114. pars = append(pars, signingStatus)
  115. }
  116. // 商家地址
  117. province := this.GetString("Province", "")
  118. province = strings.TrimSpace(province)
  119. if province != "" {
  120. provinceArr := strings.Split(province, ",")
  121. if len(provinceArr) == 0 {
  122. resp.Paging = paging.GetPaging(currentIndex, pageSize, 0)
  123. br.Data = resp
  124. br.Ret = 200
  125. br.Success = true
  126. br.Msg = "获取成功"
  127. return
  128. }
  129. cond += fmt.Sprintf(` AND %s IN (%s)`, eta_business.EtaBusinessColumns.Province, utils.GetOrmInReplace(len(provinceArr)))
  130. pars = append(pars, provinceArr)
  131. }
  132. city := this.GetString("City", "")
  133. city = strings.TrimSpace(city)
  134. if city != "" {
  135. cityArr := strings.Split(city, ",")
  136. if len(cityArr) == 0 {
  137. resp.Paging = paging.GetPaging(currentIndex, pageSize, 0)
  138. br.Data = resp
  139. br.Ret = 200
  140. br.Success = true
  141. br.Msg = "获取成功"
  142. return
  143. }
  144. cond += fmt.Sprintf(` AND %s IN (%s)`, eta_business.EtaBusinessColumns.City, utils.GetOrmInReplace(len(cityArr)))
  145. pars = append(pars, cityArr)
  146. }
  147. }
  148. order := ``
  149. // 排序
  150. {
  151. fieldArr := []int{1, 2, 3, 4}
  152. typeArr := []int{1, 2}
  153. fieldMap := map[int]string{
  154. 1: "signing_time",
  155. 2: "expired_time",
  156. 3: "create_time",
  157. 4: "user_max",
  158. }
  159. typeMap := map[int]string{
  160. 1: "ASC",
  161. 2: "DESC",
  162. }
  163. sortParam, _ := this.GetInt("SortParam", 0)
  164. sortType, _ := this.GetInt("SortType", 0)
  165. if utils.InArrayByInt(fieldArr, sortParam) && utils.InArrayByInt(typeArr, sortType) {
  166. order = fmt.Sprintf("%s %s", fieldMap[sortParam], typeMap[sortType])
  167. }
  168. fmt.Println("order: ", order)
  169. }
  170. // 获取列表
  171. businessOb := new(eta_business.EtaBusiness)
  172. total, e := businessOb.GetCountByCondition(cond, pars)
  173. if e != nil {
  174. br.Msg = "获取失败"
  175. br.ErrMsg = "获取商家总数失败, Err: " + e.Error()
  176. return
  177. }
  178. list, e := businessOb.GetPageItemsByCondition(cond, pars, []string{}, order, startSize, pageSize)
  179. if e != nil {
  180. br.Msg = "获取失败"
  181. br.ErrMsg = "获取商家列表失败, Err: " + e.Error()
  182. return
  183. }
  184. items := make([]*eta_business.EtaBusinessItem, 0)
  185. for _, v := range list {
  186. b := new(eta_business.EtaBusinessItem)
  187. b.EtaBusinessId = v.EtaBusinessId
  188. b.BusinessName = v.BusinessName
  189. b.BusinessCode = v.BusinessCode
  190. b.CreditCode = v.CreditCode
  191. b.RegionType = v.RegionType
  192. b.Address = v.Address
  193. b.SellerId = v.SellerId
  194. b.SellerName = v.SellerName
  195. b.Leader = v.Leader
  196. b.IndustryId = v.IndustryId
  197. b.IndustryName = v.IndustryName
  198. b.CapitalScale = v.CapitalScale
  199. b.ResearchTeamSize = v.ResearchTeamSize
  200. b.UserMax = v.UserMax
  201. b.SigningStatus = v.SigningStatus
  202. b.Enable = v.Enable
  203. b.SigningTime = v.SigningTime.Format(utils.FormatDate)
  204. b.ExpiredTime = v.ExpiredTime.Format(utils.FormatDate)
  205. b.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
  206. b.ModifyTime = v.ModifyTime.Format(utils.FormatDateTime)
  207. items = append(items, b)
  208. }
  209. resp.List = items
  210. resp.Paging = paging.GetPaging(currentIndex, pageSize, total)
  211. br.Data = resp
  212. br.Ret = 200
  213. br.Success = true
  214. br.Msg = "获取成功"
  215. }
  216. // Add
  217. // @Title 新增商家
  218. // @Description 新增商家
  219. // @Param request body eta_business.EtaBusinessAddReq true "type json string"
  220. // @Success 200 Ret=200 操作成功
  221. // @router /add [post]
  222. func (this *EtaBusinessController) Add() {
  223. br := new(models.BaseResponse).Init()
  224. defer func() {
  225. if br.ErrMsg == "" {
  226. br.IsSendEmail = false
  227. }
  228. this.Data["json"] = br
  229. this.ServeJSON()
  230. }()
  231. sysUser := this.SysUser
  232. if sysUser == nil {
  233. br.Msg = "请登录"
  234. br.ErrMsg = "请登录,SysUser Is Empty"
  235. br.Ret = 408
  236. return
  237. }
  238. // 操作权限校验
  239. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  240. if e != nil {
  241. br.Msg = "操作失败"
  242. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  243. return
  244. }
  245. if !ok {
  246. br.Msg = "无权操作"
  247. return
  248. }
  249. // 参数校验
  250. var req eta_business.EtaBusinessAddReq
  251. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  252. br.Msg = "参数解析异常!"
  253. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  254. return
  255. }
  256. req.BusinessName = strings.TrimSpace(req.BusinessName)
  257. if req.BusinessName == "" {
  258. br.Msg = "商家名称不可为空"
  259. return
  260. }
  261. if req.CreditCode == "" {
  262. br.Msg = "社会统一信用码不可为空"
  263. return
  264. }
  265. req.Province = strings.TrimSpace(req.Province)
  266. if req.Province == "" {
  267. br.Msg = "省份不可为空"
  268. return
  269. }
  270. req.City = strings.TrimSpace(req.City)
  271. if req.City == "" {
  272. br.Msg = "城市不可为空"
  273. return
  274. }
  275. req.Leader = strings.TrimSpace(req.Leader)
  276. if req.Leader == "" {
  277. br.Msg = "决策人不可为空"
  278. return
  279. }
  280. if req.IndustryId <= 0 || req.IndustryName == "" {
  281. br.Msg = "所属行业不可为空"
  282. return
  283. }
  284. if req.ResearchTeamSize == "" {
  285. br.Msg = "研究团队规模不可为空"
  286. return
  287. }
  288. if req.UserMax <= 0 {
  289. br.Msg = "用户上限不可小于0"
  290. return
  291. }
  292. req.CapitalScale = strings.TrimSpace(req.CapitalScale)
  293. // 如果仅校验不新增, 那么不做第二页签约时间的校验
  294. var signTime, expiredTime time.Time
  295. if !req.IsCheck {
  296. if req.SigningTime == "" {
  297. br.Msg = "签约时间不可为空"
  298. return
  299. }
  300. if req.ExpiredTime == "" {
  301. br.Msg = "到期时间不可为空"
  302. return
  303. }
  304. signTime, e = time.ParseInLocation(utils.FormatDate, req.SigningTime, time.Local)
  305. if e != nil {
  306. br.Msg = "签约时间格式有误"
  307. br.ErrMsg = "签约时间格式有误, Err: " + e.Error()
  308. return
  309. }
  310. expiredTime, e = time.ParseInLocation(utils.FormatDate, req.ExpiredTime, time.Local)
  311. if e != nil {
  312. br.Msg = "到期时间格式有误"
  313. br.ErrMsg = "到期时间格式有误, Err: " + e.Error()
  314. return
  315. }
  316. if !expiredTime.After(signTime) {
  317. br.Msg = "到期时间不得早于签约时间"
  318. return
  319. }
  320. }
  321. // 重名校验
  322. {
  323. item := new(eta_business.EtaBusiness)
  324. cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessColumns.BusinessName)
  325. pars := make([]interface{}, 0)
  326. pars = append(pars, req.BusinessName)
  327. exist, e := item.GetItemByCondition(cond, pars)
  328. if e != nil && e.Error() != utils.ErrNoRow() {
  329. br.Msg = "操作失败"
  330. br.ErrMsg = "获取重名商家失败, Err: " + e.Error()
  331. return
  332. }
  333. if exist != nil {
  334. br.Msg = "商家名称已存在"
  335. return
  336. }
  337. }
  338. // 社会信用码重复校验
  339. {
  340. item := new(eta_business.EtaBusiness)
  341. cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessColumns.CreditCode)
  342. pars := make([]interface{}, 0)
  343. pars = append(pars, req.CreditCode)
  344. exist, e := item.GetItemByCondition(cond, pars)
  345. if e != nil && e.Error() != utils.ErrNoRow() {
  346. br.Msg = "操作失败"
  347. br.ErrMsg = "获取重复信用码商家失败, Err: " + e.Error()
  348. return
  349. }
  350. if exist != nil {
  351. br.Msg = "商家社会信用码已存在"
  352. return
  353. }
  354. }
  355. // 如果只做校验不新增, 此处校验通过后直接返回true
  356. if req.IsCheck {
  357. br.Data = true
  358. br.Ret = 200
  359. br.Success = true
  360. br.Msg = "校验通过"
  361. return
  362. }
  363. // 商家编码
  364. businessCode, e := eta_business.CreateEtaBusinessCode()
  365. if e != nil {
  366. br.Msg = "操作失败"
  367. br.ErrMsg = "生成商家编码失败, Err: " + e.Error()
  368. return
  369. }
  370. now := time.Now().Local()
  371. // 若当前时间不在首次签约时间内, 也算作已终止, 进入合约期时会由定时任务改为首次签约
  372. status := eta_business.EtaBusinessSigningStatusFirst
  373. isTerminate := true
  374. if now.Equal(signTime) || now.Equal(expiredTime) {
  375. isTerminate = false
  376. }
  377. if now.After(signTime) && now.Before(expiredTime) {
  378. isTerminate = false
  379. }
  380. if isTerminate {
  381. status = eta_business.EtaBusinessSigningStatusTerminate
  382. }
  383. // 新增商家和签约
  384. businessItem := new(eta_business.EtaBusiness)
  385. businessItem.BusinessName = req.BusinessName
  386. businessItem.BusinessCode = businessCode
  387. businessItem.CreditCode = req.CreditCode
  388. businessItem.RegionType = req.RegionType
  389. businessItem.Province = req.Province
  390. businessItem.City = req.City
  391. businessItem.Address = req.Province + req.City
  392. businessItem.SellerId = req.SellerId
  393. businessItem.SellerName = req.SellerName
  394. businessItem.Leader = req.Leader
  395. businessItem.IndustryId = req.IndustryId
  396. businessItem.IndustryName = req.IndustryName
  397. businessItem.CapitalScale = req.CapitalScale
  398. businessItem.ResearchTeamSize = req.ResearchTeamSize
  399. businessItem.UserMax = req.UserMax
  400. businessItem.Enable = 1
  401. businessItem.SigningStatus = status
  402. businessItem.SigningTime = signTime
  403. businessItem.ExpiredTime = expiredTime
  404. businessItem.CreateTime = now
  405. businessItem.ModifyTime = now
  406. contractItem := new(eta_business.EtaBusinessContract)
  407. contractItem.SigningTime = signTime
  408. contractItem.ExpiredTime = expiredTime
  409. contractItem.IsFirst = 1
  410. contractItem.CreateTime = now
  411. contractItem.ModifyTime = now
  412. if e = eta_business.CreateEtaBusinessAndContract(businessItem, contractItem); e != nil {
  413. br.Msg = "操作失败"
  414. br.ErrMsg = "新增商家和签约失败, Err: " + e.Error()
  415. return
  416. }
  417. // 操作日志
  418. go func() {
  419. recordOb := new(eta_business.EtaBusinessOperationRecord)
  420. recordOb.EtaBusinessId = businessItem.EtaBusinessId
  421. recordOb.SellerId = businessItem.SellerId
  422. recordOb.SysUserId = sysUser.AdminId
  423. recordOb.SysRealName = sysUser.RealName
  424. recordOb.OperationType = eta_business.EtaBusinessOperationTypeAdd
  425. recordOb.OperationRemark = fmt.Sprintf("%s新增商户", sysUser.RealName)
  426. recordOb.CreateTime = time.Now().Local()
  427. _ = recordOb.Create()
  428. }()
  429. br.Ret = 200
  430. br.Success = true
  431. br.Msg = "操作成功"
  432. }
  433. // Edit
  434. // @Title 编辑商家
  435. // @Description 编辑商家
  436. // @Param request body eta_business.EtaBusinessEditReq true "type json string"
  437. // @Success 200 Ret=200 操作成功
  438. // @router /edit [post]
  439. func (this *EtaBusinessController) Edit() {
  440. br := new(models.BaseResponse).Init()
  441. defer func() {
  442. if br.ErrMsg == "" {
  443. br.IsSendEmail = false
  444. }
  445. this.Data["json"] = br
  446. this.ServeJSON()
  447. }()
  448. sysUser := this.SysUser
  449. if sysUser == nil {
  450. br.Msg = "请登录"
  451. br.ErrMsg = "请登录,SysUser Is Empty"
  452. br.Ret = 408
  453. return
  454. }
  455. var req eta_business.EtaBusinessEditReq
  456. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  457. br.Msg = "参数解析异常!"
  458. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  459. return
  460. }
  461. if req.EtaBusinessId <= 0 {
  462. br.Msg = "参数有误"
  463. br.ErrMsg = "参数有误, 商家ID为空"
  464. return
  465. }
  466. req.Province = strings.TrimSpace(req.Province)
  467. if req.Province == "" {
  468. br.Msg = "省份不可为空"
  469. return
  470. }
  471. req.City = strings.TrimSpace(req.City)
  472. if req.City == "" {
  473. br.Msg = "城市不可为空"
  474. return
  475. }
  476. req.Leader = strings.TrimSpace(req.Leader)
  477. if req.Leader == "" {
  478. br.Msg = "决策人不可为空"
  479. return
  480. }
  481. if req.IndustryId <= 0 || req.IndustryName == "" {
  482. br.Msg = "所属行业不可为空"
  483. return
  484. }
  485. if req.ResearchTeamSize == "" {
  486. br.Msg = "研究团队规模不可为空"
  487. return
  488. }
  489. if req.UserMax <= 0 {
  490. br.Msg = "用户上限不可小于0"
  491. return
  492. }
  493. req.CapitalScale = strings.TrimSpace(req.CapitalScale)
  494. // 权限校验
  495. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  496. if e != nil {
  497. br.Msg = "操作失败"
  498. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  499. return
  500. }
  501. if !ok {
  502. br.Msg = "无权操作"
  503. return
  504. }
  505. ob := new(eta_business.EtaBusiness)
  506. item, e := ob.GetItemById(req.EtaBusinessId)
  507. if e != nil {
  508. if e.Error() == utils.ErrNoRow() {
  509. br.Msg = "商家不存在, 请刷新页面"
  510. return
  511. }
  512. br.Msg = "操作失败"
  513. br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
  514. return
  515. }
  516. item.Province = req.Province
  517. item.City = req.City
  518. item.Address = req.Province + req.City
  519. item.Leader = req.Leader
  520. item.IndustryId = req.IndustryId
  521. item.IndustryName = req.IndustryName
  522. item.CapitalScale = req.CapitalScale
  523. item.ResearchTeamSize = req.ResearchTeamSize
  524. item.UserMax = req.UserMax
  525. item.ModifyTime = time.Now().Local()
  526. cols := []string{
  527. "Province", "City", "Address", "Leader", "IndustryId", "IndustryName", "CapitalScale", "ResearchTeamSize", "UserMax", "ModifyTime",
  528. }
  529. if e := item.Update(cols); e != nil {
  530. br.Msg = "操作失败"
  531. br.ErrMsg = "编辑商家失败, Err: " + e.Error()
  532. return
  533. }
  534. // 操作日志
  535. go func() {
  536. recordOb := new(eta_business.EtaBusinessOperationRecord)
  537. recordOb.EtaBusinessId = item.EtaBusinessId
  538. recordOb.SellerId = item.SellerId
  539. recordOb.SysUserId = sysUser.AdminId
  540. recordOb.SysRealName = sysUser.RealName
  541. recordOb.OperationType = eta_business.EtaBusinessOperationTypeEdit
  542. recordOb.OperationRemark = fmt.Sprintf("%s编辑商户", sysUser.RealName)
  543. recordOb.CreateTime = time.Now().Local()
  544. _ = recordOb.Create()
  545. }()
  546. br.Ret = 200
  547. br.Success = true
  548. br.Msg = "操作成功"
  549. }
  550. // Signing
  551. // @Title 签约续约
  552. // @Description 签约续约(业务上仅用于续约, 兼容首次签约)
  553. // @Param request body eta_business.EtaBusinessSigningReq true "type json string"
  554. // @Success 200 Ret=200 操作成功
  555. // @router /signing [post]
  556. func (this *EtaBusinessController) Signing() {
  557. br := new(models.BaseResponse).Init()
  558. defer func() {
  559. if br.ErrMsg == "" {
  560. br.IsSendEmail = false
  561. }
  562. this.Data["json"] = br
  563. this.ServeJSON()
  564. }()
  565. sysUser := this.SysUser
  566. if sysUser == nil {
  567. br.Msg = "请登录"
  568. br.ErrMsg = "请登录,SysUser Is Empty"
  569. br.Ret = 408
  570. return
  571. }
  572. var req eta_business.EtaBusinessSigningReq
  573. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  574. br.Msg = "参数解析异常!"
  575. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  576. return
  577. }
  578. if req.EtaBusinessId <= 0 {
  579. br.Msg = "参数有误"
  580. br.ErrMsg = "参数有误, 商家ID为空"
  581. return
  582. }
  583. if req.SigningTime == "" {
  584. br.Msg = "签约时间不可为空"
  585. return
  586. }
  587. if req.ExpiredTime == "" {
  588. br.Msg = "到期时间不可为空"
  589. return
  590. }
  591. signTime, e := time.ParseInLocation(utils.FormatDate, req.SigningTime, time.Local)
  592. if e != nil {
  593. br.Msg = "签约时间格式有误"
  594. br.ErrMsg = "签约时间格式有误, Err: " + e.Error()
  595. return
  596. }
  597. expiredTime, e := time.ParseInLocation(utils.FormatDate, req.ExpiredTime, time.Local)
  598. if e != nil {
  599. br.Msg = "到期时间格式有误"
  600. br.ErrMsg = "到期时间格式有误, Err: " + e.Error()
  601. return
  602. }
  603. if !expiredTime.After(signTime) {
  604. br.Msg = "到期时间不得早于签约时间"
  605. return
  606. }
  607. // 权限校验
  608. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  609. if e != nil {
  610. br.Msg = "操作失败"
  611. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  612. return
  613. }
  614. if !ok {
  615. br.Msg = "无权操作"
  616. return
  617. }
  618. // 获取商家信息
  619. businessOb := new(eta_business.EtaBusiness)
  620. business, e := businessOb.GetItemById(req.EtaBusinessId)
  621. if e != nil {
  622. if e.Error() == utils.ErrNoRow() {
  623. br.Msg = "商家不存在, 请刷新页面"
  624. return
  625. }
  626. br.Msg = "操作失败"
  627. br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
  628. return
  629. }
  630. // 续约不可早于当前生效合约的到期时间
  631. if business.ContractId > 0 {
  632. {
  633. ob := new(eta_business.EtaBusinessContract)
  634. item, e := ob.GetItemById(business.ContractId)
  635. if e != nil {
  636. br.Msg = "操作失败"
  637. br.ErrMsg = "获取商家当前合同失败, Err: " + e.Error()
  638. return
  639. }
  640. if !signTime.After(item.ExpiredTime) {
  641. br.Msg = "签约时间不可早于当前合同的到期时间"
  642. return
  643. }
  644. }
  645. }
  646. // 获取历史签约, 签约日期不可交叠
  647. contract := new(eta_business.EtaBusinessContract)
  648. cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessContractColumns.EtaBusinessId)
  649. pars := make([]interface{}, 0)
  650. pars = append(pars, req.EtaBusinessId)
  651. contracts, e := contract.GetItemsByCondition(cond, pars, []string{}, "")
  652. if e != nil {
  653. br.Msg = "操作失败"
  654. br.ErrMsg = "获取商家签约列表失败, Err: " + e.Error()
  655. return
  656. }
  657. // 校验签约时间是否重叠
  658. isFirst := true // 是否为首次签约
  659. if len(contracts) > 0 {
  660. isFirst = false
  661. for _, c := range contracts {
  662. pass := false
  663. if expiredTime.Before(c.SigningTime) {
  664. pass = true
  665. }
  666. if signTime.After(c.ExpiredTime) {
  667. pass = true
  668. }
  669. if !pass {
  670. br.Msg = "签约时间在存续期内, 请检查"
  671. return
  672. }
  673. }
  674. }
  675. // 新增签约
  676. contract.EtaBusinessId = req.EtaBusinessId
  677. contract.SigningTime = signTime
  678. contract.ExpiredTime = expiredTime
  679. contract.CreateTime = time.Now().Local()
  680. contract.ModifyTime = time.Now().Local()
  681. if isFirst {
  682. contract.IsFirst = 1
  683. }
  684. if e = contract.Create(); e != nil {
  685. br.Msg = "操作失败"
  686. br.ErrMsg = "新增签约失败, Err: " + e.Error()
  687. return
  688. }
  689. // 签约后续操作
  690. go func() {
  691. _ = etaBusinessService.UpdateEtaBusinessAfterSigning(business.EtaBusinessId)
  692. }()
  693. // 续约操作日志
  694. if !isFirst {
  695. go func() {
  696. recordOb := new(eta_business.EtaBusinessOperationRecord)
  697. recordOb.EtaBusinessId = business.EtaBusinessId
  698. recordOb.SellerId = business.SellerId
  699. recordOb.SysUserId = sysUser.AdminId
  700. recordOb.SysRealName = sysUser.RealName
  701. recordOb.OperationType = eta_business.EtaBusinessOperationTypeRenewalContract
  702. recordOb.OperationRemark = fmt.Sprintf("%s添加续约", sysUser.RealName)
  703. recordOb.CreateTime = time.Now().Local()
  704. _ = recordOb.Create()
  705. }()
  706. }
  707. br.Ret = 200
  708. br.Success = true
  709. br.Msg = "操作成功"
  710. }
  711. // Enable
  712. // @Title 禁用启用
  713. // @Description 禁用启用
  714. // @Param request body eta_business.EtaBusinessEnableReq true "type json string"
  715. // @Success 200 Ret=200 操作成功
  716. // @router /enable [post]
  717. func (this *EtaBusinessController) Enable() {
  718. br := new(models.BaseResponse).Init()
  719. defer func() {
  720. if br.ErrMsg == "" {
  721. br.IsSendEmail = false
  722. }
  723. this.Data["json"] = br
  724. this.ServeJSON()
  725. }()
  726. sysUser := this.SysUser
  727. if sysUser == nil {
  728. br.Msg = "请登录"
  729. br.ErrMsg = "请登录,SysUser Is Empty"
  730. br.Ret = 408
  731. return
  732. }
  733. var req eta_business.EtaBusinessEnableReq
  734. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  735. br.Msg = "参数解析异常!"
  736. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  737. return
  738. }
  739. if req.EtaBusinessId <= 0 {
  740. br.Msg = "参数有误"
  741. br.ErrMsg = "参数有误, 商家ID为空"
  742. return
  743. }
  744. // 权限校验
  745. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  746. if e != nil {
  747. br.Msg = "操作失败"
  748. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  749. return
  750. }
  751. if !ok {
  752. br.Msg = "无权操作"
  753. return
  754. }
  755. // 获取商家信息
  756. businessOb := new(eta_business.EtaBusiness)
  757. business, e := businessOb.GetItemById(req.EtaBusinessId)
  758. if e != nil {
  759. if e.Error() == utils.ErrNoRow() {
  760. br.Msg = "商家不存在, 请刷新页面"
  761. return
  762. }
  763. br.Msg = "操作失败"
  764. br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
  765. return
  766. }
  767. // 禁启用
  768. typeMap := map[int]int{
  769. 0: eta_business.EtaBusinessOperationTypeEnable,
  770. 1: eta_business.EtaBusinessOperationTypeDisable,
  771. }
  772. typeRemarkMap := map[int]string{
  773. 0: "启用",
  774. 1: "禁用",
  775. }
  776. operateType := typeMap[business.Enable]
  777. operateRemark := typeRemarkMap[business.Enable]
  778. business.Enable = business.Enable ^ 1
  779. business.ModifyTime = time.Now().Local()
  780. cols := []string{"Enable", "ModifyTime"}
  781. if e = business.Update(cols); e != nil {
  782. br.Msg = "操作失败"
  783. br.ErrMsg = "更新商家状态失败, Err: " + e.Error()
  784. return
  785. }
  786. // 操作日志
  787. go func() {
  788. recordOb := new(eta_business.EtaBusinessOperationRecord)
  789. recordOb.EtaBusinessId = business.EtaBusinessId
  790. recordOb.SellerId = business.SellerId
  791. recordOb.SysUserId = sysUser.AdminId
  792. recordOb.SysRealName = sysUser.RealName
  793. recordOb.OperationType = operateType
  794. recordOb.OperationRemark = fmt.Sprintf("%s%s商户", sysUser.RealName, operateRemark)
  795. recordOb.CreateTime = time.Now().Local()
  796. _ = recordOb.Create()
  797. }()
  798. br.Ret = 200
  799. br.Success = true
  800. br.Msg = "操作成功"
  801. }
  802. // MoveSeller
  803. // @Title 移动销售
  804. // @Description 移动销售
  805. // @Param request body eta_business.EtaBusinessMoveSellerReq true "type json string"
  806. // @Success 200 Ret=200 操作成功
  807. // @router /move_seller [post]
  808. func (this *EtaBusinessController) MoveSeller() {
  809. br := new(models.BaseResponse).Init()
  810. defer func() {
  811. if br.ErrMsg == "" {
  812. br.IsSendEmail = false
  813. }
  814. this.Data["json"] = br
  815. this.ServeJSON()
  816. }()
  817. sysUser := this.SysUser
  818. if sysUser == nil {
  819. br.Msg = "请登录"
  820. br.ErrMsg = "请登录,SysUser Is Empty"
  821. br.Ret = 408
  822. return
  823. }
  824. var req eta_business.EtaBusinessMoveSellerReq
  825. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  826. br.Msg = "参数解析异常!"
  827. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  828. return
  829. }
  830. if req.EtaBusinessId <= 0 {
  831. br.Msg = "参数有误"
  832. br.ErrMsg = "参数有误, 商家ID为空"
  833. return
  834. }
  835. if req.SellerId <= 0 || req.SellerName == "" {
  836. br.Msg = "销售不可为空"
  837. return
  838. }
  839. // 权限校验
  840. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  841. if e != nil {
  842. br.Msg = "操作失败"
  843. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  844. return
  845. }
  846. if !ok {
  847. br.Msg = "无权操作"
  848. return
  849. }
  850. // 获取商家信息
  851. businessOb := new(eta_business.EtaBusiness)
  852. business, e := businessOb.GetItemById(req.EtaBusinessId)
  853. if e != nil {
  854. if e.Error() == utils.ErrNoRow() {
  855. br.Msg = "商家不存在, 请刷新页面"
  856. return
  857. }
  858. br.Msg = "操作失败"
  859. br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
  860. return
  861. }
  862. // 更新销售
  863. business.SellerId = req.SellerId
  864. business.SellerName = req.SellerName
  865. business.ModifyTime = time.Now().Local()
  866. cols := []string{"SellerId", "SellerName", "ModifyTime"}
  867. if e = business.Update(cols); e != nil {
  868. br.Msg = "操作失败"
  869. br.ErrMsg = "移动至销售失败, Err: " + e.Error()
  870. return
  871. }
  872. // 操作日志
  873. go func() {
  874. recordOb := new(eta_business.EtaBusinessOperationRecord)
  875. recordOb.EtaBusinessId = business.EtaBusinessId
  876. recordOb.SellerId = req.SellerId
  877. recordOb.SysUserId = sysUser.AdminId
  878. recordOb.SysRealName = sysUser.RealName
  879. recordOb.OperationType = eta_business.EtaBusinessOperationTypeMoveSeller
  880. recordOb.OperationRemark = fmt.Sprintf("%s移动到: %s", sysUser.RealName, req.SellerName)
  881. recordOb.CreateTime = time.Now().Local()
  882. _ = recordOb.Create()
  883. }()
  884. br.Ret = 200
  885. br.Success = true
  886. br.Msg = "操作成功"
  887. }
  888. // Detail
  889. // @Title 商家详情
  890. // @Description 商家详情
  891. // @Param EtaBusinessId query int true "商家ID"
  892. // @Success 200 Ret=200 获取成功
  893. // @router /detail [get]
  894. func (this *EtaBusinessController) Detail() {
  895. br := new(models.BaseResponse).Init()
  896. defer func() {
  897. if br.ErrMsg == "" {
  898. br.IsSendEmail = false
  899. }
  900. this.Data["json"] = br
  901. this.ServeJSON()
  902. }()
  903. sysUser := this.SysUser
  904. if sysUser == nil {
  905. br.Msg = "请登录"
  906. br.ErrMsg = "请登录,SysUser Is Empty"
  907. br.Ret = 408
  908. return
  909. }
  910. // 权限校验
  911. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  912. if e != nil {
  913. br.Msg = "操作失败"
  914. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  915. return
  916. }
  917. if !ok {
  918. br.Ret = 200
  919. br.Success = true
  920. br.Msg = "获取成功"
  921. return
  922. }
  923. businessId, _ := this.GetInt("EtaBusinessId", 0)
  924. if businessId <= 0 {
  925. br.Msg = "参数有误"
  926. br.ErrMsg = "参数有误, 商家ID"
  927. return
  928. }
  929. businessOb := new(eta_business.EtaBusiness)
  930. item, e := businessOb.GetItemById(businessId)
  931. if e != nil {
  932. if e.Error() == utils.ErrNoRow() {
  933. br.Msg = "商家不存在, 请刷新页面"
  934. return
  935. }
  936. br.Msg = "获取失败"
  937. br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
  938. return
  939. }
  940. resp := new(eta_business.EtaBusinessItem)
  941. resp.EtaBusinessId = item.EtaBusinessId
  942. resp.BusinessName = item.BusinessName
  943. resp.BusinessCode = item.BusinessCode
  944. resp.CreditCode = item.CreditCode
  945. resp.RegionType = item.RegionType
  946. resp.Address = item.Address
  947. resp.SellerId = item.SellerId
  948. resp.SellerName = item.SellerName
  949. resp.Leader = item.Leader
  950. resp.IndustryId = item.IndustryId
  951. resp.IndustryName = item.IndustryName
  952. resp.CapitalScale = item.CapitalScale
  953. resp.ResearchTeamSize = item.ResearchTeamSize
  954. resp.UserMax = item.UserMax
  955. resp.SigningStatus = item.SigningStatus
  956. resp.Enable = item.Enable
  957. resp.SigningTime = item.SigningTime.Format(utils.FormatDate)
  958. resp.ExpiredTime = item.ExpiredTime.Format(utils.FormatDate)
  959. resp.CreateTime = item.CreateTime.Format(utils.FormatDateTime)
  960. resp.ModifyTime = item.ModifyTime.Format(utils.FormatDateTime)
  961. br.Data = resp
  962. br.Ret = 200
  963. br.Success = true
  964. br.Msg = "获取成功"
  965. }
  966. // ContractList
  967. // @Title 商家签约列表
  968. // @Description 商家签约列表
  969. // @Param EtaBusinessId query int true "商家ID"
  970. // @Success 200 Ret=200 获取成功
  971. // @router /contract_list [get]
  972. func (this *EtaBusinessController) ContractList() {
  973. br := new(models.BaseResponse).Init()
  974. defer func() {
  975. if br.ErrMsg == "" {
  976. br.IsSendEmail = false
  977. }
  978. this.Data["json"] = br
  979. this.ServeJSON()
  980. }()
  981. sysUser := this.SysUser
  982. if sysUser == nil {
  983. br.Msg = "请登录"
  984. br.ErrMsg = "请登录,SysUser Is Empty"
  985. br.Ret = 408
  986. return
  987. }
  988. // 权限校验
  989. resp := make([]*eta_business.EtaBusinessContractItem, 0)
  990. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  991. if e != nil {
  992. br.Msg = "操作失败"
  993. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  994. return
  995. }
  996. if !ok {
  997. br.Data = resp
  998. br.Ret = 200
  999. br.Success = true
  1000. br.Msg = "获取成功"
  1001. return
  1002. }
  1003. businessId, _ := this.GetInt("EtaBusinessId", 0)
  1004. if businessId <= 0 {
  1005. br.Msg = "参数有误"
  1006. br.ErrMsg = "参数有误, 商家ID"
  1007. return
  1008. }
  1009. cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessContractColumns.EtaBusinessId)
  1010. pars := make([]interface{}, 0)
  1011. pars = append(pars, businessId)
  1012. order := `signing_time DESC`
  1013. contractOb := new(eta_business.EtaBusinessContract)
  1014. list, e := contractOb.GetItemsByCondition(cond, pars, []string{}, order)
  1015. if e != nil {
  1016. br.Msg = "获取失败"
  1017. br.ErrMsg = "获取商家签约列表失败, Err: " + e.Error()
  1018. return
  1019. }
  1020. now := time.Now().Local()
  1021. for _, v := range list {
  1022. b := new(eta_business.EtaBusinessContractItem)
  1023. b.EtaBusinessContractId = v.EtaBusinessContractId
  1024. b.EtaBusinessId = v.EtaBusinessId
  1025. b.SigningTime = v.SigningTime.Format(utils.FormatDate)
  1026. b.ExpiredTime = v.ExpiredTime.Format(utils.FormatDate)
  1027. // 到期天数, 终止日按当天的23:59:59算
  1028. strEnd := v.ExpiredTime.Format(utils.FormatDate)
  1029. strEnd = strEnd + " 23:59:59"
  1030. endTime, e := time.ParseInLocation(utils.FormatDateTime, strEnd, time.Local)
  1031. if e != nil {
  1032. br.Msg = "获取失败"
  1033. br.ErrMsg = "签约日期有误, Err: " + e.Error()
  1034. return
  1035. }
  1036. b.ExpireDay = "-"
  1037. if now.After(v.SigningTime) && now.Before(endTime) {
  1038. diff := utils.GetDiffDays(v.ExpiredTime, now)
  1039. b.ExpireDay = strconv.Itoa(diff)
  1040. b.Using = true
  1041. }
  1042. resp = append(resp, b)
  1043. }
  1044. br.Data = resp
  1045. br.Ret = 200
  1046. br.Success = true
  1047. br.Msg = "获取成功"
  1048. }
  1049. // OperateRecordList
  1050. // @Title 操作日志列表
  1051. // @Description 操作日志列表
  1052. // @Param EtaBusinessId query int true "商家ID"
  1053. // @Success 200 Ret=200 获取成功
  1054. // @router /operate_record_list [get]
  1055. func (this *EtaBusinessController) OperateRecordList() {
  1056. br := new(models.BaseResponse).Init()
  1057. defer func() {
  1058. if br.ErrMsg == "" {
  1059. br.IsSendEmail = false
  1060. }
  1061. this.Data["json"] = br
  1062. this.ServeJSON()
  1063. }()
  1064. sysUser := this.SysUser
  1065. if sysUser == nil {
  1066. br.Msg = "请登录"
  1067. br.ErrMsg = "请登录,SysUser Is Empty"
  1068. br.Ret = 408
  1069. return
  1070. }
  1071. // 权限校验
  1072. resp := make([]*eta_business.EtaBusinessOperationRecordItem, 0)
  1073. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  1074. if e != nil {
  1075. br.Msg = "操作失败"
  1076. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  1077. return
  1078. }
  1079. if !ok {
  1080. br.Data = resp
  1081. br.Ret = 200
  1082. br.Success = true
  1083. br.Msg = "获取成功"
  1084. return
  1085. }
  1086. businessId, _ := this.GetInt("EtaBusinessId", 0)
  1087. if businessId <= 0 {
  1088. br.Msg = "参数有误"
  1089. br.ErrMsg = "参数有误, 商家ID"
  1090. return
  1091. }
  1092. cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessOperationRecordColumns.EtaBusinessId)
  1093. pars := make([]interface{}, 0)
  1094. pars = append(pars, businessId)
  1095. recordOb := new(eta_business.EtaBusinessOperationRecord)
  1096. list, e := recordOb.GetItemsByCondition(cond, pars, []string{}, "")
  1097. if e != nil {
  1098. br.Msg = "获取失败"
  1099. br.ErrMsg = "获取商家操作日志列表失败, Err: " + e.Error()
  1100. return
  1101. }
  1102. for _, v := range list {
  1103. r := new(eta_business.EtaBusinessOperationRecordItem)
  1104. r.EtaBusinessId = v.EtaBusinessId
  1105. r.SysUserId = v.SysUserId
  1106. r.SysRealName = v.SysRealName
  1107. r.OperationType = v.OperationType
  1108. r.OperationRemark = v.OperationRemark
  1109. r.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
  1110. resp = append(resp, r)
  1111. }
  1112. br.Data = resp
  1113. br.Ret = 200
  1114. br.Success = true
  1115. br.Msg = "获取成功"
  1116. }