eta_business.go 32 KB

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