eta_business.go 32 KB

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