eta_business.go 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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. now := time.Now().Local()
  381. // 若当前时间不在首次签约时间内, 也算作已终止, 进入合约期时会由定时任务改为首次签约
  382. status := eta_business.EtaBusinessSigningStatusFirst
  383. isTerminate := true
  384. if now.Equal(signTime) || now.Equal(expiredTime) {
  385. isTerminate = false
  386. }
  387. if now.After(signTime) && now.Before(expiredTime) {
  388. isTerminate = false
  389. }
  390. if isTerminate {
  391. status = eta_business.EtaBusinessSigningStatusTerminate
  392. }
  393. // 新增商家和签约
  394. businessItem := new(eta_business.EtaBusiness)
  395. businessItem.BusinessName = req.BusinessName
  396. businessItem.BusinessCode = businessCode
  397. businessItem.CreditCode = req.CreditCode
  398. businessItem.RegionType = req.RegionType
  399. businessItem.Province = req.Province
  400. businessItem.City = req.City
  401. businessItem.Address = req.Province + req.City
  402. businessItem.SellerId = req.SellerId
  403. businessItem.SellerName = req.SellerName
  404. businessItem.Leader = req.Leader
  405. businessItem.IndustryId = req.IndustryId
  406. businessItem.IndustryName = req.IndustryName
  407. businessItem.CapitalScale = req.CapitalScale
  408. businessItem.ResearchTeamSize = req.ResearchTeamSize
  409. businessItem.UserMax = req.UserMax
  410. businessItem.Enable = 1
  411. businessItem.SigningStatus = status
  412. businessItem.SigningTime = signTime
  413. businessItem.ExpiredTime = expiredTime
  414. businessItem.CreateTime = now
  415. businessItem.ModifyTime = now
  416. contractItem := new(eta_business.EtaBusinessContract)
  417. contractItem.SigningTime = signTime
  418. contractItem.ExpiredTime = expiredTime
  419. contractItem.IsFirst = 1
  420. contractItem.CreateTime = now
  421. contractItem.ModifyTime = now
  422. if e = eta_business.CreateEtaBusinessAndContract(businessItem, contractItem); e != nil {
  423. br.Msg = "操作失败"
  424. br.ErrMsg = "新增商家和签约失败, Err: " + e.Error()
  425. return
  426. }
  427. // 操作日志
  428. go func() {
  429. recordOb := new(eta_business.EtaBusinessOperationRecord)
  430. recordOb.EtaBusinessId = businessItem.EtaBusinessId
  431. recordOb.SellerId = businessItem.SellerId
  432. recordOb.SysUserId = sysUser.AdminId
  433. recordOb.SysRealName = sysUser.RealName
  434. recordOb.OperationType = eta_business.EtaBusinessOperationTypeAdd
  435. recordOb.OperationRemark = fmt.Sprintf("%s新增商户", sysUser.RealName)
  436. recordOb.CreateTime = time.Now().Local()
  437. _ = recordOb.Create()
  438. }()
  439. br.Ret = 200
  440. br.Success = true
  441. br.Msg = "操作成功"
  442. }
  443. // Edit
  444. // @Title 编辑商家
  445. // @Description 编辑商家
  446. // @Param request body eta_business.EtaBusinessEditReq true "type json string"
  447. // @Success 200 Ret=200 操作成功
  448. // @router /edit [post]
  449. func (this *EtaBusinessController) Edit() {
  450. br := new(models.BaseResponse).Init()
  451. defer func() {
  452. if br.ErrMsg == "" {
  453. br.IsSendEmail = false
  454. }
  455. this.Data["json"] = br
  456. this.ServeJSON()
  457. }()
  458. sysUser := this.SysUser
  459. if sysUser == nil {
  460. br.Msg = "请登录"
  461. br.ErrMsg = "请登录,SysUser Is Empty"
  462. br.Ret = 408
  463. return
  464. }
  465. var req eta_business.EtaBusinessEditReq
  466. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  467. br.Msg = "参数解析异常!"
  468. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  469. return
  470. }
  471. if req.EtaBusinessId <= 0 {
  472. br.Msg = "参数有误"
  473. br.ErrMsg = "参数有误, 商家ID为空"
  474. return
  475. }
  476. req.Province = strings.TrimSpace(req.Province)
  477. if req.Province == "" {
  478. br.Msg = "省份不可为空"
  479. return
  480. }
  481. req.City = strings.TrimSpace(req.City)
  482. if req.City == "" {
  483. br.Msg = "城市不可为空"
  484. return
  485. }
  486. req.Leader = strings.TrimSpace(req.Leader)
  487. if req.Leader == "" {
  488. br.Msg = "决策人不可为空"
  489. return
  490. }
  491. if req.IndustryId <= 0 || req.IndustryName == "" {
  492. br.Msg = "所属行业不可为空"
  493. return
  494. }
  495. if req.ResearchTeamSize == "" {
  496. br.Msg = "研究团队规模不可为空"
  497. return
  498. }
  499. if req.UserMax <= 0 {
  500. br.Msg = "用户上限不可小于0"
  501. return
  502. }
  503. req.CapitalScale = strings.TrimSpace(req.CapitalScale)
  504. // 权限校验
  505. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  506. if e != nil {
  507. br.Msg = "操作失败"
  508. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  509. return
  510. }
  511. if !ok {
  512. br.Msg = "无权操作"
  513. return
  514. }
  515. ob := new(eta_business.EtaBusiness)
  516. item, e := ob.GetItemById(req.EtaBusinessId)
  517. if e != nil {
  518. if e.Error() == utils.ErrNoRow() {
  519. br.Msg = "商家不存在, 请刷新页面"
  520. return
  521. }
  522. br.Msg = "操作失败"
  523. br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
  524. return
  525. }
  526. item.Province = req.Province
  527. item.City = req.City
  528. item.Address = req.Province + req.City
  529. item.Leader = req.Leader
  530. item.IndustryId = req.IndustryId
  531. item.IndustryName = req.IndustryName
  532. item.CapitalScale = req.CapitalScale
  533. item.ResearchTeamSize = req.ResearchTeamSize
  534. item.UserMax = req.UserMax
  535. item.ModifyTime = time.Now().Local()
  536. cols := []string{
  537. "Province", "City", "Address", "Leader", "IndustryId", "IndustryName", "CapitalScale", "ResearchTeamSize", "UserMax", "ModifyTime",
  538. }
  539. if e := item.Update(cols); e != nil {
  540. br.Msg = "操作失败"
  541. br.ErrMsg = "编辑商家失败, Err: " + e.Error()
  542. return
  543. }
  544. // 操作日志
  545. go func() {
  546. recordOb := new(eta_business.EtaBusinessOperationRecord)
  547. recordOb.EtaBusinessId = item.EtaBusinessId
  548. recordOb.SellerId = item.SellerId
  549. recordOb.SysUserId = sysUser.AdminId
  550. recordOb.SysRealName = sysUser.RealName
  551. recordOb.OperationType = eta_business.EtaBusinessOperationTypeEdit
  552. recordOb.OperationRemark = fmt.Sprintf("%s编辑商户", sysUser.RealName)
  553. recordOb.CreateTime = time.Now().Local()
  554. _ = recordOb.Create()
  555. }()
  556. br.Ret = 200
  557. br.Success = true
  558. br.Msg = "操作成功"
  559. }
  560. // Signing
  561. // @Title 签约续约
  562. // @Description 签约续约(业务上仅用于续约, 兼容首次签约)
  563. // @Param request body eta_business.EtaBusinessSigningReq true "type json string"
  564. // @Success 200 Ret=200 操作成功
  565. // @router /signing [post]
  566. func (this *EtaBusinessController) Signing() {
  567. br := new(models.BaseResponse).Init()
  568. defer func() {
  569. if br.ErrMsg == "" {
  570. br.IsSendEmail = false
  571. }
  572. this.Data["json"] = br
  573. this.ServeJSON()
  574. }()
  575. sysUser := this.SysUser
  576. if sysUser == nil {
  577. br.Msg = "请登录"
  578. br.ErrMsg = "请登录,SysUser Is Empty"
  579. br.Ret = 408
  580. return
  581. }
  582. var req eta_business.EtaBusinessSigningReq
  583. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  584. br.Msg = "参数解析异常!"
  585. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  586. return
  587. }
  588. if req.EtaBusinessId <= 0 {
  589. br.Msg = "参数有误"
  590. br.ErrMsg = "参数有误, 商家ID为空"
  591. return
  592. }
  593. if req.SigningTime == "" {
  594. br.Msg = "签约时间不可为空"
  595. return
  596. }
  597. if req.ExpiredTime == "" {
  598. br.Msg = "到期时间不可为空"
  599. return
  600. }
  601. signTime, e := time.ParseInLocation(utils.FormatDate, req.SigningTime, time.Local)
  602. if e != nil {
  603. br.Msg = "签约时间格式有误"
  604. br.ErrMsg = "签约时间格式有误, Err: " + e.Error()
  605. return
  606. }
  607. expiredTime, e := time.ParseInLocation(utils.FormatDate, req.ExpiredTime, time.Local)
  608. if e != nil {
  609. br.Msg = "到期时间格式有误"
  610. br.ErrMsg = "到期时间格式有误, Err: " + e.Error()
  611. return
  612. }
  613. if !expiredTime.After(signTime) {
  614. br.Msg = "到期时间不得早于签约时间"
  615. return
  616. }
  617. // 权限校验
  618. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  619. if e != nil {
  620. br.Msg = "操作失败"
  621. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  622. return
  623. }
  624. if !ok {
  625. br.Msg = "无权操作"
  626. return
  627. }
  628. // 获取商家信息
  629. businessOb := new(eta_business.EtaBusiness)
  630. business, e := businessOb.GetItemById(req.EtaBusinessId)
  631. if e != nil {
  632. if e.Error() == utils.ErrNoRow() {
  633. br.Msg = "商家不存在, 请刷新页面"
  634. return
  635. }
  636. br.Msg = "操作失败"
  637. br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
  638. return
  639. }
  640. // 续约不可早于当前生效合约的到期时间
  641. if business.ContractId > 0 {
  642. {
  643. ob := new(eta_business.EtaBusinessContract)
  644. item, e := ob.GetItemById(business.ContractId)
  645. if e != nil {
  646. br.Msg = "操作失败"
  647. br.ErrMsg = "获取商家当前合同失败, Err: " + e.Error()
  648. return
  649. }
  650. if !signTime.After(item.ExpiredTime) {
  651. br.Msg = "签约时间不可早于当前合同的到期时间"
  652. return
  653. }
  654. }
  655. }
  656. // 获取历史签约, 签约日期不可交叠
  657. contract := new(eta_business.EtaBusinessContract)
  658. cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessContractColumns.EtaBusinessId)
  659. pars := make([]interface{}, 0)
  660. pars = append(pars, req.EtaBusinessId)
  661. contracts, e := contract.GetItemsByCondition(cond, pars, []string{}, "")
  662. if e != nil {
  663. br.Msg = "操作失败"
  664. br.ErrMsg = "获取商家签约列表失败, Err: " + e.Error()
  665. return
  666. }
  667. // 校验签约时间是否重叠
  668. isFirst := true // 是否为首次签约
  669. if len(contracts) > 0 {
  670. isFirst = false
  671. for _, c := range contracts {
  672. pass := false
  673. if expiredTime.Before(c.SigningTime) {
  674. pass = true
  675. }
  676. if signTime.After(c.ExpiredTime) {
  677. pass = true
  678. }
  679. if !pass {
  680. br.Msg = "签约时间在存续期内, 请检查"
  681. return
  682. }
  683. }
  684. }
  685. // 新增签约
  686. contract.EtaBusinessId = req.EtaBusinessId
  687. contract.SigningTime = signTime
  688. contract.ExpiredTime = expiredTime
  689. contract.CreateTime = time.Now().Local()
  690. contract.ModifyTime = time.Now().Local()
  691. if isFirst {
  692. contract.IsFirst = 1
  693. }
  694. if e = contract.Create(); e != nil {
  695. br.Msg = "操作失败"
  696. br.ErrMsg = "新增签约失败, Err: " + e.Error()
  697. return
  698. }
  699. // 签约后续操作
  700. go func() {
  701. _ = etaBusinessService.UpdateEtaBusinessAfterSigning(business.EtaBusinessId)
  702. }()
  703. // 续约操作日志
  704. if !isFirst {
  705. go func() {
  706. recordOb := new(eta_business.EtaBusinessOperationRecord)
  707. recordOb.EtaBusinessId = business.EtaBusinessId
  708. recordOb.SellerId = business.SellerId
  709. recordOb.SysUserId = sysUser.AdminId
  710. recordOb.SysRealName = sysUser.RealName
  711. recordOb.OperationType = eta_business.EtaBusinessOperationTypeRenewalContract
  712. recordOb.OperationRemark = fmt.Sprintf("%s添加续约", sysUser.RealName)
  713. recordOb.CreateTime = time.Now().Local()
  714. _ = recordOb.Create()
  715. }()
  716. }
  717. br.Ret = 200
  718. br.Success = true
  719. br.Msg = "操作成功"
  720. }
  721. // Enable
  722. // @Title 禁用启用
  723. // @Description 禁用启用
  724. // @Param request body eta_business.EtaBusinessEnableReq true "type json string"
  725. // @Success 200 Ret=200 操作成功
  726. // @router /enable [post]
  727. func (this *EtaBusinessController) Enable() {
  728. br := new(models.BaseResponse).Init()
  729. defer func() {
  730. if br.ErrMsg == "" {
  731. br.IsSendEmail = false
  732. }
  733. this.Data["json"] = br
  734. this.ServeJSON()
  735. }()
  736. sysUser := this.SysUser
  737. if sysUser == nil {
  738. br.Msg = "请登录"
  739. br.ErrMsg = "请登录,SysUser Is Empty"
  740. br.Ret = 408
  741. return
  742. }
  743. var req eta_business.EtaBusinessEnableReq
  744. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  745. br.Msg = "参数解析异常!"
  746. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  747. return
  748. }
  749. if req.EtaBusinessId <= 0 {
  750. br.Msg = "参数有误"
  751. br.ErrMsg = "参数有误, 商家ID为空"
  752. return
  753. }
  754. // 权限校验
  755. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  756. if e != nil {
  757. br.Msg = "操作失败"
  758. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  759. return
  760. }
  761. if !ok {
  762. br.Msg = "无权操作"
  763. return
  764. }
  765. // 获取商家信息
  766. businessOb := new(eta_business.EtaBusiness)
  767. business, e := businessOb.GetItemById(req.EtaBusinessId)
  768. if e != nil {
  769. if e.Error() == utils.ErrNoRow() {
  770. br.Msg = "商家不存在, 请刷新页面"
  771. return
  772. }
  773. br.Msg = "操作失败"
  774. br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
  775. return
  776. }
  777. // 禁启用
  778. typeMap := map[int]int{
  779. 0: eta_business.EtaBusinessOperationTypeEnable,
  780. 1: eta_business.EtaBusinessOperationTypeDisable,
  781. }
  782. typeRemarkMap := map[int]string{
  783. 0: "启用",
  784. 1: "禁用",
  785. }
  786. operateType := typeMap[business.Enable]
  787. operateRemark := typeRemarkMap[business.Enable]
  788. business.Enable = business.Enable ^ 1
  789. business.ModifyTime = time.Now().Local()
  790. cols := []string{"Enable", "ModifyTime"}
  791. if e = business.Update(cols); e != nil {
  792. br.Msg = "操作失败"
  793. br.ErrMsg = "更新商家状态失败, Err: " + e.Error()
  794. return
  795. }
  796. // 操作日志
  797. go func() {
  798. recordOb := new(eta_business.EtaBusinessOperationRecord)
  799. recordOb.EtaBusinessId = business.EtaBusinessId
  800. recordOb.SellerId = business.SellerId
  801. recordOb.SysUserId = sysUser.AdminId
  802. recordOb.SysRealName = sysUser.RealName
  803. recordOb.OperationType = operateType
  804. recordOb.OperationRemark = fmt.Sprintf("%s%s商户", sysUser.RealName, operateRemark)
  805. recordOb.CreateTime = time.Now().Local()
  806. _ = recordOb.Create()
  807. }()
  808. br.Ret = 200
  809. br.Success = true
  810. br.Msg = "操作成功"
  811. }
  812. // MoveSeller
  813. // @Title 移动销售
  814. // @Description 移动销售
  815. // @Param request body eta_business.EtaBusinessMoveSellerReq true "type json string"
  816. // @Success 200 Ret=200 操作成功
  817. // @router /move_seller [post]
  818. func (this *EtaBusinessController) MoveSeller() {
  819. br := new(models.BaseResponse).Init()
  820. defer func() {
  821. if br.ErrMsg == "" {
  822. br.IsSendEmail = false
  823. }
  824. this.Data["json"] = br
  825. this.ServeJSON()
  826. }()
  827. sysUser := this.SysUser
  828. if sysUser == nil {
  829. br.Msg = "请登录"
  830. br.ErrMsg = "请登录,SysUser Is Empty"
  831. br.Ret = 408
  832. return
  833. }
  834. var req eta_business.EtaBusinessMoveSellerReq
  835. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  836. br.Msg = "参数解析异常!"
  837. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  838. return
  839. }
  840. if req.EtaBusinessId <= 0 {
  841. br.Msg = "参数有误"
  842. br.ErrMsg = "参数有误, 商家ID为空"
  843. return
  844. }
  845. if req.SellerId <= 0 || req.SellerName == "" {
  846. br.Msg = "销售不可为空"
  847. return
  848. }
  849. // 权限校验
  850. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  851. if e != nil {
  852. br.Msg = "操作失败"
  853. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  854. return
  855. }
  856. if !ok {
  857. br.Msg = "无权操作"
  858. return
  859. }
  860. // 获取商家信息
  861. businessOb := new(eta_business.EtaBusiness)
  862. business, e := businessOb.GetItemById(req.EtaBusinessId)
  863. if e != nil {
  864. if e.Error() == utils.ErrNoRow() {
  865. br.Msg = "商家不存在, 请刷新页面"
  866. return
  867. }
  868. br.Msg = "操作失败"
  869. br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
  870. return
  871. }
  872. // 更新销售
  873. business.SellerId = req.SellerId
  874. business.SellerName = req.SellerName
  875. business.ModifyTime = time.Now().Local()
  876. cols := []string{"SellerId", "SellerName", "ModifyTime"}
  877. if e = business.Update(cols); e != nil {
  878. br.Msg = "操作失败"
  879. br.ErrMsg = "移动至销售失败, Err: " + e.Error()
  880. return
  881. }
  882. // 操作日志
  883. go func() {
  884. recordOb := new(eta_business.EtaBusinessOperationRecord)
  885. recordOb.EtaBusinessId = business.EtaBusinessId
  886. recordOb.SellerId = req.SellerId
  887. recordOb.SysUserId = sysUser.AdminId
  888. recordOb.SysRealName = sysUser.RealName
  889. recordOb.OperationType = eta_business.EtaBusinessOperationTypeMoveSeller
  890. recordOb.OperationRemark = fmt.Sprintf("%s移动到: %s", sysUser.RealName, req.SellerName)
  891. recordOb.CreateTime = time.Now().Local()
  892. _ = recordOb.Create()
  893. }()
  894. br.Ret = 200
  895. br.Success = true
  896. br.Msg = "操作成功"
  897. }
  898. // Detail
  899. // @Title 商家详情
  900. // @Description 商家详情
  901. // @Param EtaBusinessId query int true "商家ID"
  902. // @Success 200 Ret=200 获取成功
  903. // @router /detail [get]
  904. func (this *EtaBusinessController) Detail() {
  905. br := new(models.BaseResponse).Init()
  906. defer func() {
  907. if br.ErrMsg == "" {
  908. br.IsSendEmail = false
  909. }
  910. this.Data["json"] = br
  911. this.ServeJSON()
  912. }()
  913. sysUser := this.SysUser
  914. if sysUser == nil {
  915. br.Msg = "请登录"
  916. br.ErrMsg = "请登录,SysUser Is Empty"
  917. br.Ret = 408
  918. return
  919. }
  920. // 权限校验
  921. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  922. if e != nil {
  923. br.Msg = "获取失败"
  924. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  925. return
  926. }
  927. if !ok {
  928. br.Ret = 200
  929. br.Success = true
  930. br.Msg = "获取成功"
  931. return
  932. }
  933. businessId, _ := this.GetInt("EtaBusinessId", 0)
  934. if businessId <= 0 {
  935. br.Msg = "参数有误"
  936. br.ErrMsg = "参数有误, 商家ID"
  937. return
  938. }
  939. businessOb := new(eta_business.EtaBusiness)
  940. item, e := businessOb.GetItemById(businessId)
  941. if e != nil {
  942. if e.Error() == utils.ErrNoRow() {
  943. br.Msg = "商家不存在, 请刷新页面"
  944. return
  945. }
  946. br.Msg = "获取失败"
  947. br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
  948. return
  949. }
  950. resp := new(eta_business.EtaBusinessItem)
  951. resp.EtaBusinessId = item.EtaBusinessId
  952. resp.BusinessName = item.BusinessName
  953. resp.BusinessCode = item.BusinessCode
  954. resp.CreditCode = item.CreditCode
  955. resp.RegionType = item.RegionType
  956. resp.Province = item.Province
  957. resp.City = item.City
  958. resp.Address = item.Address
  959. resp.SellerId = item.SellerId
  960. resp.SellerName = item.SellerName
  961. resp.Leader = item.Leader
  962. resp.IndustryId = item.IndustryId
  963. resp.IndustryName = item.IndustryName
  964. resp.CapitalScale = item.CapitalScale
  965. resp.ResearchTeamSize = item.ResearchTeamSize
  966. resp.UserMax = item.UserMax
  967. resp.SigningStatus = item.SigningStatus
  968. resp.Enable = item.Enable
  969. resp.ContractId = item.ContractId
  970. resp.SigningTime = item.SigningTime.Format(utils.FormatDate)
  971. resp.ExpiredTime = item.ExpiredTime.Format(utils.FormatDate)
  972. resp.CreateTime = item.CreateTime.Format(utils.FormatDateTime)
  973. resp.ModifyTime = item.ModifyTime.Format(utils.FormatDateTime)
  974. br.Data = resp
  975. br.Ret = 200
  976. br.Success = true
  977. br.Msg = "获取成功"
  978. }
  979. // ContractList
  980. // @Title 商家签约列表
  981. // @Description 商家签约列表
  982. // @Param EtaBusinessId query int true "商家ID"
  983. // @Success 200 Ret=200 获取成功
  984. // @router /contract_list [get]
  985. func (this *EtaBusinessController) ContractList() {
  986. br := new(models.BaseResponse).Init()
  987. defer func() {
  988. if br.ErrMsg == "" {
  989. br.IsSendEmail = false
  990. }
  991. this.Data["json"] = br
  992. this.ServeJSON()
  993. }()
  994. sysUser := this.SysUser
  995. if sysUser == nil {
  996. br.Msg = "请登录"
  997. br.ErrMsg = "请登录,SysUser Is Empty"
  998. br.Ret = 408
  999. return
  1000. }
  1001. // 权限校验
  1002. resp := make([]*eta_business.EtaBusinessContractItem, 0)
  1003. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  1004. if e != nil {
  1005. br.Msg = "操作失败"
  1006. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  1007. return
  1008. }
  1009. if !ok {
  1010. br.Data = resp
  1011. br.Ret = 200
  1012. br.Success = true
  1013. br.Msg = "获取成功"
  1014. return
  1015. }
  1016. businessId, _ := this.GetInt("EtaBusinessId", 0)
  1017. if businessId <= 0 {
  1018. br.Msg = "参数有误"
  1019. br.ErrMsg = "参数有误, 商家ID"
  1020. return
  1021. }
  1022. cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessContractColumns.EtaBusinessId)
  1023. pars := make([]interface{}, 0)
  1024. pars = append(pars, businessId)
  1025. order := `signing_time DESC`
  1026. contractOb := new(eta_business.EtaBusinessContract)
  1027. list, e := contractOb.GetItemsByCondition(cond, pars, []string{}, order)
  1028. if e != nil {
  1029. br.Msg = "获取失败"
  1030. br.ErrMsg = "获取商家签约列表失败, Err: " + e.Error()
  1031. return
  1032. }
  1033. now := time.Now().Local()
  1034. for _, v := range list {
  1035. b := new(eta_business.EtaBusinessContractItem)
  1036. b.EtaBusinessContractId = v.EtaBusinessContractId
  1037. b.EtaBusinessId = v.EtaBusinessId
  1038. b.SigningTime = v.SigningTime.Format(utils.FormatDate)
  1039. b.ExpiredTime = v.ExpiredTime.Format(utils.FormatDate)
  1040. // 到期天数, 终止日按当天的23:59:59算
  1041. strEnd := v.ExpiredTime.Format(utils.FormatDate)
  1042. strEnd = strEnd + " 23:59:59"
  1043. endTime, e := time.ParseInLocation(utils.FormatDateTime, strEnd, time.Local)
  1044. if e != nil {
  1045. br.Msg = "获取失败"
  1046. br.ErrMsg = "签约日期有误, Err: " + e.Error()
  1047. return
  1048. }
  1049. b.ExpireDay = "-"
  1050. if now.After(v.SigningTime) && now.Before(endTime) {
  1051. diff := utils.GetDiffDays(v.ExpiredTime, now)
  1052. b.ExpireDay = strconv.Itoa(diff)
  1053. b.Using = true
  1054. }
  1055. resp = append(resp, b)
  1056. }
  1057. br.Data = resp
  1058. br.Ret = 200
  1059. br.Success = true
  1060. br.Msg = "获取成功"
  1061. }
  1062. // OperateRecordList
  1063. // @Title 操作日志列表
  1064. // @Description 操作日志列表
  1065. // @Param EtaBusinessId query int true "商家ID"
  1066. // @Success 200 Ret=200 获取成功
  1067. // @router /operate_record_list [get]
  1068. func (this *EtaBusinessController) OperateRecordList() {
  1069. br := new(models.BaseResponse).Init()
  1070. defer func() {
  1071. if br.ErrMsg == "" {
  1072. br.IsSendEmail = false
  1073. }
  1074. this.Data["json"] = br
  1075. this.ServeJSON()
  1076. }()
  1077. sysUser := this.SysUser
  1078. if sysUser == nil {
  1079. br.Msg = "请登录"
  1080. br.ErrMsg = "请登录,SysUser Is Empty"
  1081. br.Ret = 408
  1082. return
  1083. }
  1084. // 权限校验
  1085. resp := make([]*eta_business.EtaBusinessOperationRecordItem, 0)
  1086. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  1087. if e != nil {
  1088. br.Msg = "操作失败"
  1089. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  1090. return
  1091. }
  1092. if !ok {
  1093. br.Data = resp
  1094. br.Ret = 200
  1095. br.Success = true
  1096. br.Msg = "获取成功"
  1097. return
  1098. }
  1099. businessId, _ := this.GetInt("EtaBusinessId", 0)
  1100. if businessId <= 0 {
  1101. br.Msg = "参数有误"
  1102. br.ErrMsg = "参数有误, 商家ID"
  1103. return
  1104. }
  1105. cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessOperationRecordColumns.EtaBusinessId)
  1106. pars := make([]interface{}, 0)
  1107. pars = append(pars, businessId)
  1108. recordOb := new(eta_business.EtaBusinessOperationRecord)
  1109. list, e := recordOb.GetItemsByCondition(cond, pars, []string{}, "")
  1110. if e != nil {
  1111. br.Msg = "获取失败"
  1112. br.ErrMsg = "获取商家操作日志列表失败, Err: " + e.Error()
  1113. return
  1114. }
  1115. for _, v := range list {
  1116. r := new(eta_business.EtaBusinessOperationRecordItem)
  1117. r.EtaBusinessId = v.EtaBusinessId
  1118. r.SysUserId = v.SysUserId
  1119. r.SysRealName = v.SysRealName
  1120. r.OperationType = v.OperationType
  1121. r.OperationRemark = v.OperationRemark
  1122. r.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
  1123. resp = append(resp, r)
  1124. }
  1125. br.Data = resp
  1126. br.Ret = 200
  1127. br.Success = true
  1128. br.Msg = "获取成功"
  1129. }