eta_business.go 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  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 = utils.TimeTransferString(utils.FormatDate, v.SigningTime)
  215. b.ExpiredTime = utils.TimeTransferString(utils.FormatDate, v.ExpiredTime)
  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. // 如果仅校验不新增, 那么不做第二页签约时间的校验(已废弃, 20230919签约时间非必填)
  305. var signTime, expiredTime time.Time
  306. if !req.IsCheck {
  307. if req.SigningTime != "" && req.ExpiredTime != "" {
  308. signTime, e = time.ParseInLocation(utils.FormatDate, req.SigningTime, time.Local)
  309. if e != nil {
  310. br.Msg = "签约时间格式有误"
  311. br.ErrMsg = "签约时间格式有误, Err: " + e.Error()
  312. return
  313. }
  314. expiredTime, e = time.ParseInLocation(utils.FormatDate, req.ExpiredTime, time.Local)
  315. if e != nil {
  316. br.Msg = "到期时间格式有误"
  317. br.ErrMsg = "到期时间格式有误, Err: " + e.Error()
  318. return
  319. }
  320. if !expiredTime.After(signTime) {
  321. br.Msg = "到期时间不得早于签约时间"
  322. return
  323. }
  324. }
  325. }
  326. // 重名校验
  327. {
  328. item := new(eta_business.EtaBusiness)
  329. cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessColumns.BusinessName)
  330. pars := make([]interface{}, 0)
  331. pars = append(pars, req.BusinessName)
  332. exist, e := item.GetItemByCondition(cond, pars)
  333. if e != nil && e.Error() != utils.ErrNoRow() {
  334. br.Msg = "操作失败"
  335. br.ErrMsg = "获取重名商家失败, Err: " + e.Error()
  336. return
  337. }
  338. if exist != nil {
  339. br.Msg = "商家名称已存在"
  340. return
  341. }
  342. }
  343. // 社会信用码重复校验
  344. {
  345. item := new(eta_business.EtaBusiness)
  346. cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessColumns.CreditCode)
  347. pars := make([]interface{}, 0)
  348. pars = append(pars, req.CreditCode)
  349. exist, e := item.GetItemByCondition(cond, pars)
  350. if e != nil && e.Error() != utils.ErrNoRow() {
  351. br.Msg = "操作失败"
  352. br.ErrMsg = "获取重复信用码商家失败, Err: " + e.Error()
  353. return
  354. }
  355. if exist != nil {
  356. br.Msg = "商家社会信用码已存在"
  357. return
  358. }
  359. }
  360. // 如果只做校验不新增, 此处校验通过后直接返回true
  361. if req.IsCheck {
  362. br.Data = true
  363. br.Ret = 200
  364. br.Success = true
  365. br.Msg = "校验通过"
  366. return
  367. }
  368. // 商家编码
  369. businessCode, e := eta_business.CreateEtaBusinessCode()
  370. if e != nil {
  371. br.Msg = "操作失败"
  372. br.ErrMsg = "生成商家编码失败, Err: " + e.Error()
  373. return
  374. }
  375. codeEncrypt := utils.MD5(fmt.Sprintf("%s%s", businessCode, utils.BusinessCodeSalt))
  376. now := time.Now().Local()
  377. status := eta_business.EtaBusinessSigningStatusWait // 默认待签约
  378. // 若当前时间不在首次签约时间内, 也算作已终止, 进入合约期时会由定时任务改为首次签约
  379. if !signTime.IsZero() && !expiredTime.IsZero() {
  380. if !now.Before(signTime) && !now.After(expiredTime) {
  381. status = eta_business.EtaBusinessSigningStatusFirst // 首次签约
  382. } else {
  383. status = eta_business.EtaBusinessSigningStatusTerminate // 已终止
  384. }
  385. }
  386. // 新增商家和签约
  387. businessItem := new(eta_business.EtaBusiness)
  388. businessItem.BusinessName = req.BusinessName
  389. businessItem.BusinessCode = businessCode
  390. businessItem.CodeEncrypt = codeEncrypt
  391. businessItem.CreditCode = req.CreditCode
  392. businessItem.RegionType = req.RegionType
  393. businessItem.Province = req.Province
  394. businessItem.City = req.City
  395. businessItem.Address = req.Province + req.City
  396. businessItem.SellerId = req.SellerId
  397. businessItem.SellerName = req.SellerName
  398. businessItem.Leader = req.Leader
  399. businessItem.IndustryId = req.IndustryId
  400. businessItem.IndustryName = req.IndustryName
  401. businessItem.CapitalScale = req.CapitalScale
  402. businessItem.ResearchTeamSize = req.ResearchTeamSize
  403. businessItem.UserMax = req.UserMax
  404. businessItem.Enable = 1
  405. businessItem.SigningStatus = status
  406. businessItem.SigningTime = signTime
  407. businessItem.ExpiredTime = expiredTime
  408. businessItem.CreateTime = now
  409. businessItem.ModifyTime = now
  410. contractItem := new(eta_business.EtaBusinessContract)
  411. if !signTime.IsZero() && !expiredTime.IsZero() {
  412. contractItem.SigningTime = signTime
  413. contractItem.ExpiredTime = expiredTime
  414. contractItem.IsFirst = 1
  415. contractItem.CreateTime = now
  416. contractItem.ModifyTime = now
  417. } else {
  418. contractItem = nil
  419. }
  420. if e = eta_business.CreateEtaBusinessAndContract(businessItem, contractItem); e != nil {
  421. br.Msg = "操作失败"
  422. br.ErrMsg = "新增商家和签约失败, Err: " + e.Error()
  423. return
  424. }
  425. // 给商家帮助文档所有分类的权限
  426. if e = help_doc.AddAllHelpDocClassifyVisible(businessItem.EtaBusinessId); e != nil {
  427. br.Msg = "操作失败"
  428. br.ErrMsg = "新增商家分类文档权限失败, Err: " + e.Error()
  429. return
  430. }
  431. // 操作日志
  432. go func() {
  433. recordOb := new(eta_business.EtaBusinessOperationRecord)
  434. recordOb.EtaBusinessId = businessItem.EtaBusinessId
  435. recordOb.SellerId = businessItem.SellerId
  436. recordOb.SysUserId = sysUser.AdminId
  437. recordOb.SysRealName = sysUser.RealName
  438. recordOb.OperationType = eta_business.EtaBusinessOperationTypeAdd
  439. recordOb.OperationRemark = fmt.Sprintf("%s新增商户", sysUser.RealName)
  440. recordOb.CreateTime = time.Now().Local()
  441. _ = recordOb.Create()
  442. }()
  443. br.Ret = 200
  444. br.Success = true
  445. br.Msg = "操作成功"
  446. }
  447. // Edit
  448. // @Title 编辑商家
  449. // @Description 编辑商家
  450. // @Param request body eta_business.EtaBusinessEditReq true "type json string"
  451. // @Success 200 Ret=200 操作成功
  452. // @router /edit [post]
  453. func (this *EtaBusinessController) Edit() {
  454. br := new(models.BaseResponse).Init()
  455. defer func() {
  456. if br.ErrMsg == "" {
  457. br.IsSendEmail = false
  458. }
  459. this.Data["json"] = br
  460. this.ServeJSON()
  461. }()
  462. sysUser := this.SysUser
  463. if sysUser == nil {
  464. br.Msg = "请登录"
  465. br.ErrMsg = "请登录,SysUser Is Empty"
  466. br.Ret = 408
  467. return
  468. }
  469. var req eta_business.EtaBusinessEditReq
  470. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  471. br.Msg = "参数解析异常!"
  472. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  473. return
  474. }
  475. if req.EtaBusinessId <= 0 {
  476. br.Msg = "参数有误"
  477. br.ErrMsg = "参数有误, 商家ID为空"
  478. return
  479. }
  480. req.Province = strings.TrimSpace(req.Province)
  481. if req.Province == "" {
  482. br.Msg = "省份不可为空"
  483. return
  484. }
  485. req.City = strings.TrimSpace(req.City)
  486. if req.City == "" {
  487. br.Msg = "城市不可为空"
  488. return
  489. }
  490. req.Leader = strings.TrimSpace(req.Leader)
  491. if req.Leader == "" {
  492. br.Msg = "决策人不可为空"
  493. return
  494. }
  495. if req.IndustryId <= 0 || req.IndustryName == "" {
  496. br.Msg = "所属行业不可为空"
  497. return
  498. }
  499. if req.ResearchTeamSize == "" {
  500. br.Msg = "研究团队规模不可为空"
  501. return
  502. }
  503. if req.UserMax <= 0 {
  504. br.Msg = "用户上限不可小于0"
  505. return
  506. }
  507. req.CapitalScale = strings.TrimSpace(req.CapitalScale)
  508. // 权限校验
  509. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  510. if e != nil {
  511. br.Msg = "操作失败"
  512. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  513. return
  514. }
  515. if !ok {
  516. br.Msg = "无权操作"
  517. return
  518. }
  519. ob := new(eta_business.EtaBusiness)
  520. item, e := ob.GetItemById(req.EtaBusinessId)
  521. if e != nil {
  522. if e.Error() == utils.ErrNoRow() {
  523. br.Msg = "商家不存在, 请刷新页面"
  524. return
  525. }
  526. br.Msg = "操作失败"
  527. br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
  528. return
  529. }
  530. item.Province = req.Province
  531. item.City = req.City
  532. item.Address = req.Province + req.City
  533. item.Leader = req.Leader
  534. item.IndustryId = req.IndustryId
  535. item.IndustryName = req.IndustryName
  536. item.CapitalScale = req.CapitalScale
  537. item.ResearchTeamSize = req.ResearchTeamSize
  538. item.UserMax = req.UserMax
  539. item.ModifyTime = time.Now().Local()
  540. cols := []string{
  541. "Province", "City", "Address", "Leader", "IndustryId", "IndustryName", "CapitalScale", "ResearchTeamSize", "UserMax", "ModifyTime",
  542. }
  543. if e := item.Update(cols); e != nil {
  544. br.Msg = "操作失败"
  545. br.ErrMsg = "编辑商家失败, Err: " + e.Error()
  546. return
  547. }
  548. // 操作日志
  549. go func() {
  550. recordOb := new(eta_business.EtaBusinessOperationRecord)
  551. recordOb.EtaBusinessId = item.EtaBusinessId
  552. recordOb.SellerId = item.SellerId
  553. recordOb.SysUserId = sysUser.AdminId
  554. recordOb.SysRealName = sysUser.RealName
  555. recordOb.OperationType = eta_business.EtaBusinessOperationTypeEdit
  556. recordOb.OperationRemark = fmt.Sprintf("%s编辑商户", sysUser.RealName)
  557. recordOb.CreateTime = time.Now().Local()
  558. _ = recordOb.Create()
  559. }()
  560. br.Ret = 200
  561. br.Success = true
  562. br.Msg = "操作成功"
  563. }
  564. // Signing
  565. // @Title 签约续约
  566. // @Description 签约续约(业务上仅用于续约, 兼容首次签约)
  567. // @Param request body eta_business.EtaBusinessSigningReq true "type json string"
  568. // @Success 200 Ret=200 操作成功
  569. // @router /signing [post]
  570. func (this *EtaBusinessController) Signing() {
  571. br := new(models.BaseResponse).Init()
  572. defer func() {
  573. if br.ErrMsg == "" {
  574. br.IsSendEmail = false
  575. }
  576. this.Data["json"] = br
  577. this.ServeJSON()
  578. }()
  579. sysUser := this.SysUser
  580. if sysUser == nil {
  581. br.Msg = "请登录"
  582. br.ErrMsg = "请登录,SysUser Is Empty"
  583. br.Ret = 408
  584. return
  585. }
  586. var req eta_business.EtaBusinessSigningReq
  587. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  588. br.Msg = "参数解析异常!"
  589. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  590. return
  591. }
  592. if req.EtaBusinessId <= 0 {
  593. br.Msg = "参数有误"
  594. br.ErrMsg = "参数有误, 商家ID为空"
  595. return
  596. }
  597. if req.SigningTime == "" {
  598. br.Msg = "签约时间不可为空"
  599. return
  600. }
  601. if req.ExpiredTime == "" {
  602. br.Msg = "到期时间不可为空"
  603. return
  604. }
  605. signTime, e := time.ParseInLocation(utils.FormatDate, req.SigningTime, time.Local)
  606. if e != nil {
  607. br.Msg = "签约时间格式有误"
  608. br.ErrMsg = "签约时间格式有误, Err: " + e.Error()
  609. return
  610. }
  611. expiredTime, e := time.ParseInLocation(utils.FormatDate, req.ExpiredTime, time.Local)
  612. if e != nil {
  613. br.Msg = "到期时间格式有误"
  614. br.ErrMsg = "到期时间格式有误, Err: " + e.Error()
  615. return
  616. }
  617. if !expiredTime.After(signTime) {
  618. br.Msg = "到期时间不得早于签约时间"
  619. return
  620. }
  621. // 权限校验
  622. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  623. if e != nil {
  624. br.Msg = "操作失败"
  625. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  626. return
  627. }
  628. if !ok {
  629. br.Msg = "无权操作"
  630. return
  631. }
  632. // 获取商家信息
  633. businessOb := new(eta_business.EtaBusiness)
  634. business, e := businessOb.GetItemById(req.EtaBusinessId)
  635. if e != nil {
  636. if e.Error() == utils.ErrNoRow() {
  637. br.Msg = "商家不存在, 请刷新页面"
  638. return
  639. }
  640. br.Msg = "操作失败"
  641. br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
  642. return
  643. }
  644. // 续约不可早于当前生效合约的到期时间
  645. if business.ContractId > 0 {
  646. {
  647. ob := new(eta_business.EtaBusinessContract)
  648. item, e := ob.GetItemById(business.ContractId)
  649. if e != nil {
  650. br.Msg = "操作失败"
  651. br.ErrMsg = "获取商家当前合同失败, Err: " + e.Error()
  652. return
  653. }
  654. if !signTime.After(item.ExpiredTime) {
  655. br.Msg = "签约时间不可早于当前合同的到期时间"
  656. return
  657. }
  658. }
  659. }
  660. // 获取历史签约, 签约日期不可交叠
  661. contract := new(eta_business.EtaBusinessContract)
  662. cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessContractColumns.EtaBusinessId)
  663. pars := make([]interface{}, 0)
  664. pars = append(pars, req.EtaBusinessId)
  665. contracts, e := contract.GetItemsByCondition(cond, pars, []string{}, "signing_time ASC")
  666. if e != nil {
  667. br.Msg = "操作失败"
  668. br.ErrMsg = "获取商家签约列表失败, Err: " + e.Error()
  669. return
  670. }
  671. // 校验签约时间是否重叠
  672. isFirst := true // 是否为首次签约
  673. changeFirst := false // 是否需要更新首次签约合同
  674. if len(contracts) > 0 {
  675. isFirst = false
  676. for k, c := range contracts {
  677. pass := false
  678. if expiredTime.Before(c.SigningTime) {
  679. pass = true
  680. }
  681. if signTime.After(c.ExpiredTime) {
  682. pass = true
  683. }
  684. if !pass {
  685. br.Msg = "签约时间在存续期内, 请检查"
  686. return
  687. }
  688. // 若该合同签约时间早于第一份合同(业务上未限制所以可能会出现这种情况, contracts已排序), 则更新为首次签约合同
  689. if k == 0 && signTime.Before(c.SigningTime) {
  690. changeFirst = true
  691. isFirst = true
  692. }
  693. }
  694. }
  695. // 新增签约
  696. contract.EtaBusinessId = req.EtaBusinessId
  697. contract.SigningTime = signTime
  698. contract.ExpiredTime = expiredTime
  699. contract.CreateTime = time.Now().Local()
  700. contract.ModifyTime = time.Now().Local()
  701. if isFirst {
  702. contract.IsFirst = 1
  703. }
  704. if e = contract.CreateMaybeUpdateFirst(contract, changeFirst); e != nil {
  705. br.Msg = "操作失败"
  706. br.ErrMsg = "新增签约失败, Err: " + e.Error()
  707. return
  708. }
  709. // 签约后续操作
  710. go func() {
  711. _ = etaBusinessService.UpdateEtaBusinessAfterSigning(business.EtaBusinessId)
  712. }()
  713. // 续约操作日志
  714. if !isFirst {
  715. go func() {
  716. recordOb := new(eta_business.EtaBusinessOperationRecord)
  717. recordOb.EtaBusinessId = business.EtaBusinessId
  718. recordOb.SellerId = business.SellerId
  719. recordOb.SysUserId = sysUser.AdminId
  720. recordOb.SysRealName = sysUser.RealName
  721. recordOb.OperationType = eta_business.EtaBusinessOperationTypeRenewalContract
  722. recordOb.OperationRemark = fmt.Sprintf("%s添加续约", sysUser.RealName)
  723. recordOb.CreateTime = time.Now().Local()
  724. _ = recordOb.Create()
  725. }()
  726. }
  727. br.Ret = 200
  728. br.Success = true
  729. br.Msg = "操作成功"
  730. }
  731. // Enable
  732. // @Title 禁用启用
  733. // @Description 禁用启用
  734. // @Param request body eta_business.EtaBusinessEnableReq true "type json string"
  735. // @Success 200 Ret=200 操作成功
  736. // @router /enable [post]
  737. func (this *EtaBusinessController) Enable() {
  738. br := new(models.BaseResponse).Init()
  739. defer func() {
  740. if br.ErrMsg == "" {
  741. br.IsSendEmail = false
  742. }
  743. this.Data["json"] = br
  744. this.ServeJSON()
  745. }()
  746. sysUser := this.SysUser
  747. if sysUser == nil {
  748. br.Msg = "请登录"
  749. br.ErrMsg = "请登录,SysUser Is Empty"
  750. br.Ret = 408
  751. return
  752. }
  753. var req eta_business.EtaBusinessEnableReq
  754. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  755. br.Msg = "参数解析异常!"
  756. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  757. return
  758. }
  759. if req.EtaBusinessId <= 0 {
  760. br.Msg = "参数有误"
  761. br.ErrMsg = "参数有误, 商家ID为空"
  762. return
  763. }
  764. // 权限校验
  765. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  766. if e != nil {
  767. br.Msg = "操作失败"
  768. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  769. return
  770. }
  771. if !ok {
  772. br.Msg = "无权操作"
  773. return
  774. }
  775. // 获取商家信息
  776. businessOb := new(eta_business.EtaBusiness)
  777. business, e := businessOb.GetItemById(req.EtaBusinessId)
  778. if e != nil {
  779. if e.Error() == utils.ErrNoRow() {
  780. br.Msg = "商家不存在, 请刷新页面"
  781. return
  782. }
  783. br.Msg = "操作失败"
  784. br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
  785. return
  786. }
  787. // 禁启用
  788. typeMap := map[int]int{
  789. 0: eta_business.EtaBusinessOperationTypeEnable,
  790. 1: eta_business.EtaBusinessOperationTypeDisable,
  791. }
  792. typeRemarkMap := map[int]string{
  793. 0: "启用",
  794. 1: "禁用",
  795. }
  796. operateType := typeMap[business.Enable]
  797. operateRemark := typeRemarkMap[business.Enable]
  798. business.Enable = business.Enable ^ 1
  799. business.ModifyTime = time.Now().Local()
  800. cols := []string{"Enable", "ModifyTime"}
  801. if e = business.Update(cols); e != nil {
  802. br.Msg = "操作失败"
  803. br.ErrMsg = "更新商家状态失败, Err: " + e.Error()
  804. return
  805. }
  806. // 操作日志
  807. go func() {
  808. recordOb := new(eta_business.EtaBusinessOperationRecord)
  809. recordOb.EtaBusinessId = business.EtaBusinessId
  810. recordOb.SellerId = business.SellerId
  811. recordOb.SysUserId = sysUser.AdminId
  812. recordOb.SysRealName = sysUser.RealName
  813. recordOb.OperationType = operateType
  814. recordOb.OperationRemark = fmt.Sprintf("%s%s商户", sysUser.RealName, operateRemark)
  815. recordOb.CreateTime = time.Now().Local()
  816. _ = recordOb.Create()
  817. }()
  818. br.Ret = 200
  819. br.Success = true
  820. br.Msg = "操作成功"
  821. }
  822. // MoveSeller
  823. // @Title 移动销售
  824. // @Description 移动销售
  825. // @Param request body eta_business.EtaBusinessMoveSellerReq true "type json string"
  826. // @Success 200 Ret=200 操作成功
  827. // @router /move_seller [post]
  828. func (this *EtaBusinessController) MoveSeller() {
  829. br := new(models.BaseResponse).Init()
  830. defer func() {
  831. if br.ErrMsg == "" {
  832. br.IsSendEmail = false
  833. }
  834. this.Data["json"] = br
  835. this.ServeJSON()
  836. }()
  837. sysUser := this.SysUser
  838. if sysUser == nil {
  839. br.Msg = "请登录"
  840. br.ErrMsg = "请登录,SysUser Is Empty"
  841. br.Ret = 408
  842. return
  843. }
  844. var req eta_business.EtaBusinessMoveSellerReq
  845. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  846. br.Msg = "参数解析异常!"
  847. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  848. return
  849. }
  850. if req.EtaBusinessId <= 0 {
  851. br.Msg = "参数有误"
  852. br.ErrMsg = "参数有误, 商家ID为空"
  853. return
  854. }
  855. if req.SellerId <= 0 || req.SellerName == "" {
  856. br.Msg = "销售不可为空"
  857. return
  858. }
  859. // 权限校验
  860. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  861. if e != nil {
  862. br.Msg = "操作失败"
  863. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  864. return
  865. }
  866. if !ok {
  867. br.Msg = "无权操作"
  868. return
  869. }
  870. // 获取商家信息
  871. businessOb := new(eta_business.EtaBusiness)
  872. business, e := businessOb.GetItemById(req.EtaBusinessId)
  873. if e != nil {
  874. if e.Error() == utils.ErrNoRow() {
  875. br.Msg = "商家不存在, 请刷新页面"
  876. return
  877. }
  878. br.Msg = "操作失败"
  879. br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
  880. return
  881. }
  882. // 更新销售
  883. business.SellerId = req.SellerId
  884. business.SellerName = req.SellerName
  885. business.ModifyTime = time.Now().Local()
  886. cols := []string{"SellerId", "SellerName", "ModifyTime"}
  887. if e = business.Update(cols); e != nil {
  888. br.Msg = "操作失败"
  889. br.ErrMsg = "移动至销售失败, Err: " + e.Error()
  890. return
  891. }
  892. // 操作日志
  893. go func() {
  894. recordOb := new(eta_business.EtaBusinessOperationRecord)
  895. recordOb.EtaBusinessId = business.EtaBusinessId
  896. recordOb.SellerId = req.SellerId
  897. recordOb.SysUserId = sysUser.AdminId
  898. recordOb.SysRealName = sysUser.RealName
  899. recordOb.OperationType = eta_business.EtaBusinessOperationTypeMoveSeller
  900. recordOb.OperationRemark = fmt.Sprintf("%s移动到: %s", sysUser.RealName, req.SellerName)
  901. recordOb.CreateTime = time.Now().Local()
  902. _ = recordOb.Create()
  903. }()
  904. br.Ret = 200
  905. br.Success = true
  906. br.Msg = "操作成功"
  907. }
  908. // Detail
  909. // @Title 商家详情
  910. // @Description 商家详情
  911. // @Param EtaBusinessId query int true "商家ID"
  912. // @Success 200 Ret=200 获取成功
  913. // @router /detail [get]
  914. func (this *EtaBusinessController) Detail() {
  915. br := new(models.BaseResponse).Init()
  916. defer func() {
  917. if br.ErrMsg == "" {
  918. br.IsSendEmail = false
  919. }
  920. this.Data["json"] = br
  921. this.ServeJSON()
  922. }()
  923. sysUser := this.SysUser
  924. if sysUser == nil {
  925. br.Msg = "请登录"
  926. br.ErrMsg = "请登录,SysUser Is Empty"
  927. br.Ret = 408
  928. return
  929. }
  930. // 权限校验
  931. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  932. if e != nil {
  933. br.Msg = "获取失败"
  934. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  935. return
  936. }
  937. if !ok {
  938. br.Ret = 200
  939. br.Success = true
  940. br.Msg = "获取成功"
  941. return
  942. }
  943. businessId, _ := this.GetInt("EtaBusinessId", 0)
  944. if businessId <= 0 {
  945. br.Msg = "参数有误"
  946. br.ErrMsg = "参数有误, 商家ID"
  947. return
  948. }
  949. businessOb := new(eta_business.EtaBusiness)
  950. item, e := businessOb.GetItemById(businessId)
  951. if e != nil {
  952. if e.Error() == utils.ErrNoRow() {
  953. br.Msg = "商家不存在, 请刷新页面"
  954. return
  955. }
  956. br.Msg = "获取失败"
  957. br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
  958. return
  959. }
  960. resp := new(eta_business.EtaBusinessItem)
  961. resp.EtaBusinessId = item.EtaBusinessId
  962. resp.BusinessName = item.BusinessName
  963. resp.BusinessCode = item.BusinessCode
  964. resp.CreditCode = item.CreditCode
  965. resp.RegionType = item.RegionType
  966. resp.Province = item.Province
  967. resp.City = item.City
  968. resp.Address = item.Address
  969. resp.SellerId = item.SellerId
  970. resp.SellerName = item.SellerName
  971. resp.Leader = item.Leader
  972. resp.IndustryId = item.IndustryId
  973. resp.IndustryName = item.IndustryName
  974. resp.CapitalScale = item.CapitalScale
  975. resp.ResearchTeamSize = item.ResearchTeamSize
  976. resp.UserMax = item.UserMax
  977. resp.SigningStatus = item.SigningStatus
  978. resp.Enable = item.Enable
  979. resp.ContractId = item.ContractId
  980. resp.SigningTime = utils.TimeTransferString(utils.FormatDate, item.SigningTime)
  981. resp.ExpiredTime = utils.TimeTransferString(utils.FormatDate, item.ExpiredTime)
  982. resp.CreateTime = item.CreateTime.Format(utils.FormatDateTime)
  983. resp.ModifyTime = item.ModifyTime.Format(utils.FormatDateTime)
  984. br.Data = resp
  985. br.Ret = 200
  986. br.Success = true
  987. br.Msg = "获取成功"
  988. }
  989. // ContractList
  990. // @Title 商家签约列表
  991. // @Description 商家签约列表
  992. // @Param EtaBusinessId query int true "商家ID"
  993. // @Success 200 Ret=200 获取成功
  994. // @router /contract_list [get]
  995. func (this *EtaBusinessController) ContractList() {
  996. br := new(models.BaseResponse).Init()
  997. defer func() {
  998. if br.ErrMsg == "" {
  999. br.IsSendEmail = false
  1000. }
  1001. this.Data["json"] = br
  1002. this.ServeJSON()
  1003. }()
  1004. sysUser := this.SysUser
  1005. if sysUser == nil {
  1006. br.Msg = "请登录"
  1007. br.ErrMsg = "请登录,SysUser Is Empty"
  1008. br.Ret = 408
  1009. return
  1010. }
  1011. // 权限校验
  1012. resp := make([]*eta_business.EtaBusinessContractItem, 0)
  1013. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  1014. if e != nil {
  1015. br.Msg = "操作失败"
  1016. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  1017. return
  1018. }
  1019. if !ok {
  1020. br.Data = resp
  1021. br.Ret = 200
  1022. br.Success = true
  1023. br.Msg = "获取成功"
  1024. return
  1025. }
  1026. businessId, _ := this.GetInt("EtaBusinessId", 0)
  1027. if businessId <= 0 {
  1028. br.Msg = "参数有误"
  1029. br.ErrMsg = "参数有误, 商家ID"
  1030. return
  1031. }
  1032. cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessContractColumns.EtaBusinessId)
  1033. pars := make([]interface{}, 0)
  1034. pars = append(pars, businessId)
  1035. order := `signing_time ASC`
  1036. contractOb := new(eta_business.EtaBusinessContract)
  1037. list, e := contractOb.GetItemsByCondition(cond, pars, []string{}, order)
  1038. if e != nil {
  1039. br.Msg = "获取失败"
  1040. br.ErrMsg = "获取商家签约列表失败, Err: " + e.Error()
  1041. return
  1042. }
  1043. now := time.Now().Local()
  1044. for _, v := range list {
  1045. b := new(eta_business.EtaBusinessContractItem)
  1046. b.EtaBusinessContractId = v.EtaBusinessContractId
  1047. b.EtaBusinessId = v.EtaBusinessId
  1048. b.SigningTime = utils.TimeTransferString(utils.FormatDate, v.SigningTime)
  1049. b.ExpiredTime = utils.TimeTransferString(utils.FormatDate, v.ExpiredTime)
  1050. // 到期天数, 终止日按当天的23:59:59算
  1051. strEnd := v.ExpiredTime.Format(utils.FormatDate)
  1052. strEnd = strEnd + " 23:59:59"
  1053. endTime, e := time.ParseInLocation(utils.FormatDateTime, strEnd, time.Local)
  1054. if e != nil {
  1055. br.Msg = "获取失败"
  1056. br.ErrMsg = "签约日期有误, Err: " + e.Error()
  1057. return
  1058. }
  1059. b.ExpireDay = "-"
  1060. if now.After(v.SigningTime) && now.Before(endTime) {
  1061. diff := utils.GetDiffDays(v.ExpiredTime, now)
  1062. b.ExpireDay = strconv.Itoa(diff)
  1063. b.Using = true
  1064. }
  1065. resp = append(resp, b)
  1066. }
  1067. br.Data = resp
  1068. br.Ret = 200
  1069. br.Success = true
  1070. br.Msg = "获取成功"
  1071. }
  1072. // OperateRecordList
  1073. // @Title 操作日志列表
  1074. // @Description 操作日志列表
  1075. // @Param EtaBusinessId query int true "商家ID"
  1076. // @Success 200 Ret=200 获取成功
  1077. // @router /operate_record_list [get]
  1078. func (this *EtaBusinessController) OperateRecordList() {
  1079. br := new(models.BaseResponse).Init()
  1080. defer func() {
  1081. if br.ErrMsg == "" {
  1082. br.IsSendEmail = false
  1083. }
  1084. this.Data["json"] = br
  1085. this.ServeJSON()
  1086. }()
  1087. sysUser := this.SysUser
  1088. if sysUser == nil {
  1089. br.Msg = "请登录"
  1090. br.ErrMsg = "请登录,SysUser Is Empty"
  1091. br.Ret = 408
  1092. return
  1093. }
  1094. // 权限校验
  1095. resp := make([]*eta_business.EtaBusinessOperationRecordItem, 0)
  1096. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  1097. if e != nil {
  1098. br.Msg = "操作失败"
  1099. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  1100. return
  1101. }
  1102. if !ok {
  1103. br.Data = resp
  1104. br.Ret = 200
  1105. br.Success = true
  1106. br.Msg = "获取成功"
  1107. return
  1108. }
  1109. businessId, _ := this.GetInt("EtaBusinessId", 0)
  1110. if businessId <= 0 {
  1111. br.Msg = "参数有误"
  1112. br.ErrMsg = "参数有误, 商家ID"
  1113. return
  1114. }
  1115. cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessOperationRecordColumns.EtaBusinessId)
  1116. pars := make([]interface{}, 0)
  1117. pars = append(pars, businessId)
  1118. recordOb := new(eta_business.EtaBusinessOperationRecord)
  1119. list, e := recordOb.GetItemsByCondition(cond, pars, []string{}, "")
  1120. if e != nil {
  1121. br.Msg = "获取失败"
  1122. br.ErrMsg = "获取商家操作日志列表失败, Err: " + e.Error()
  1123. return
  1124. }
  1125. for _, v := range list {
  1126. r := new(eta_business.EtaBusinessOperationRecordItem)
  1127. r.EtaBusinessId = v.EtaBusinessId
  1128. r.SysUserId = v.SysUserId
  1129. r.SysRealName = v.SysRealName
  1130. r.OperationType = v.OperationType
  1131. r.OperationRemark = v.OperationRemark
  1132. r.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
  1133. resp = append(resp, r)
  1134. }
  1135. br.Data = resp
  1136. br.Ret = 200
  1137. br.Success = true
  1138. br.Msg = "获取成功"
  1139. }
  1140. // EditSign
  1141. // @Title 编辑签约
  1142. // @Description 编辑签约
  1143. // @Param request body eta_business.EtaBusinessEditSignReq true "type json string"
  1144. // @Success 200 Ret=200 操作成功
  1145. // @router /edit_sign [post]
  1146. func (this *EtaBusinessController) EditSign() {
  1147. br := new(models.BaseResponse).Init()
  1148. defer func() {
  1149. if br.ErrMsg == "" {
  1150. br.IsSendEmail = false
  1151. }
  1152. this.Data["json"] = br
  1153. this.ServeJSON()
  1154. }()
  1155. sysUser := this.SysUser
  1156. if sysUser == nil {
  1157. br.Msg = "请登录"
  1158. br.ErrMsg = "请登录,SysUser Is Empty"
  1159. br.Ret = 408
  1160. return
  1161. }
  1162. var req eta_business.EtaBusinessEditSignReq
  1163. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1164. br.Msg = "参数解析异常!"
  1165. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  1166. return
  1167. }
  1168. if req.EtaBusinessContractId <= 0 {
  1169. br.Msg = "参数有误"
  1170. br.ErrMsg = "参数有误, 商家签约ID为空"
  1171. return
  1172. }
  1173. if req.SigningTime == "" {
  1174. br.Msg = "签约时间不可为空"
  1175. return
  1176. }
  1177. if req.ExpiredTime == "" {
  1178. br.Msg = "到期时间不可为空"
  1179. return
  1180. }
  1181. signTime, e := time.ParseInLocation(utils.FormatDate, req.SigningTime, time.Local)
  1182. if e != nil {
  1183. br.Msg = "签约时间格式有误"
  1184. br.ErrMsg = "签约时间格式有误, Err: " + e.Error()
  1185. return
  1186. }
  1187. expiredTime, e := time.ParseInLocation(utils.FormatDate, req.ExpiredTime, time.Local)
  1188. if e != nil {
  1189. br.Msg = "到期时间格式有误"
  1190. br.ErrMsg = "到期时间格式有误, Err: " + e.Error()
  1191. return
  1192. }
  1193. if !expiredTime.After(signTime) {
  1194. br.Msg = "到期时间不得早于签约时间"
  1195. return
  1196. }
  1197. // 权限校验
  1198. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  1199. if e != nil {
  1200. br.Msg = "操作失败"
  1201. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  1202. return
  1203. }
  1204. if !ok {
  1205. br.Msg = "无权操作"
  1206. return
  1207. }
  1208. contractOb := new(eta_business.EtaBusinessContract)
  1209. contract, e := contractOb.GetItemById(req.EtaBusinessContractId)
  1210. if e != nil {
  1211. if e.Error() == utils.ErrNoRow() {
  1212. br.Msg = "签约信息不存在, 请刷新页面"
  1213. return
  1214. }
  1215. br.Msg = "操作失败"
  1216. br.ErrMsg = "获取商家签约信息失败, Err: " + e.Error()
  1217. return
  1218. }
  1219. businessOb := new(eta_business.EtaBusiness)
  1220. business, e := businessOb.GetItemById(contract.EtaBusinessId)
  1221. if e != nil {
  1222. if e.Error() == utils.ErrNoRow() {
  1223. br.Msg = "商家不存在, 请刷新页面"
  1224. return
  1225. }
  1226. br.Msg = "操作失败"
  1227. br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
  1228. return
  1229. }
  1230. // 续约不可早于当前生效合约的到期时间
  1231. if business.ContractId > 0 && business.ContractId != req.EtaBusinessContractId {
  1232. {
  1233. ob := new(eta_business.EtaBusinessContract)
  1234. item, e := ob.GetItemById(business.ContractId)
  1235. if e != nil {
  1236. br.Msg = "操作失败"
  1237. br.ErrMsg = "获取商家当前合同失败, Err: " + e.Error()
  1238. return
  1239. }
  1240. if !signTime.After(item.ExpiredTime) {
  1241. br.Msg = "签约时间不可早于当前合同的到期时间"
  1242. return
  1243. }
  1244. }
  1245. }
  1246. // 获取历史签约, 签约日期不可交叠
  1247. cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessContractColumns.EtaBusinessId)
  1248. pars := make([]interface{}, 0)
  1249. pars = append(pars, contract.EtaBusinessId)
  1250. contracts, e := contractOb.GetItemsByCondition(cond, pars, []string{}, "signing_time ASC")
  1251. if e != nil {
  1252. br.Msg = "操作失败"
  1253. br.ErrMsg = "获取商家签约列表失败, Err: " + e.Error()
  1254. return
  1255. }
  1256. // 校验签约时间是否重叠
  1257. isFirst := true // 是否为首次签约
  1258. changeFirst := false // 是否需要更新首次签约合同
  1259. if len(contracts) > 0 {
  1260. isFirst = false
  1261. for k, c := range contracts {
  1262. if c.EtaBusinessContractId == req.EtaBusinessContractId {
  1263. continue
  1264. }
  1265. pass := false
  1266. if expiredTime.Before(c.SigningTime) {
  1267. pass = true
  1268. }
  1269. if signTime.After(c.ExpiredTime) {
  1270. pass = true
  1271. }
  1272. if !pass {
  1273. br.Msg = "签约时间在存续期内, 请检查"
  1274. return
  1275. }
  1276. // 若该合同签约时间早于第一份合同(业务上未限制所以可能会出现这种情况, contracts已排序), 则更新为首次签约合同
  1277. if k == 0 && signTime.Before(c.SigningTime) {
  1278. changeFirst = true
  1279. isFirst = true
  1280. }
  1281. }
  1282. }
  1283. // 新增签约
  1284. contract.SigningTime = signTime
  1285. contract.ExpiredTime = expiredTime
  1286. contract.ModifyTime = time.Now().Local()
  1287. if isFirst {
  1288. contract.IsFirst = 1
  1289. }
  1290. updateCols := []string{"SigningTime", "ExpiredTime", "ModifyTime"}
  1291. if e = contract.UpdateMaybeUpdateFirst(contract, changeFirst, updateCols); e != nil {
  1292. br.Msg = "操作失败"
  1293. br.ErrMsg = "编辑签约失败, Err: " + e.Error()
  1294. return
  1295. }
  1296. // 签约后续操作
  1297. go func() {
  1298. _ = etaBusinessService.UpdateEtaBusinessAfterSigning(business.EtaBusinessId)
  1299. }()
  1300. // 续约操作日志
  1301. if !isFirst {
  1302. go func() {
  1303. recordOb := new(eta_business.EtaBusinessOperationRecord)
  1304. recordOb.EtaBusinessId = business.EtaBusinessId
  1305. recordOb.SellerId = business.SellerId
  1306. recordOb.SysUserId = sysUser.AdminId
  1307. recordOb.SysRealName = sysUser.RealName
  1308. recordOb.OperationType = eta_business.EtaBusinessOperationTypeEditContract
  1309. recordOb.OperationRemark = fmt.Sprintf("%s编辑续约", sysUser.RealName)
  1310. recordOb.CreateTime = time.Now().Local()
  1311. _ = recordOb.Create()
  1312. }()
  1313. }
  1314. br.Ret = 200
  1315. br.Success = true
  1316. br.Msg = "操作成功"
  1317. }
  1318. // RemoveSign
  1319. // @Title 删除签约
  1320. // @Description 删除签约
  1321. // @Param request body eta_business.EtaBusinessRemoveSignReq true "type json string"
  1322. // @Success 200 Ret=200 操作成功
  1323. // @router /remove_sign [post]
  1324. func (this *EtaBusinessController) RemoveSign() {
  1325. br := new(models.BaseResponse).Init()
  1326. defer func() {
  1327. if br.ErrMsg == "" {
  1328. br.IsSendEmail = false
  1329. }
  1330. this.Data["json"] = br
  1331. this.ServeJSON()
  1332. }()
  1333. sysUser := this.SysUser
  1334. if sysUser == nil {
  1335. br.Msg = "请登录"
  1336. br.ErrMsg = "请登录,SysUser Is Empty"
  1337. br.Ret = 408
  1338. return
  1339. }
  1340. var req eta_business.EtaBusinessRemoveSignReq
  1341. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1342. br.Msg = "参数解析异常!"
  1343. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  1344. return
  1345. }
  1346. if req.EtaBusinessContractId <= 0 {
  1347. br.Msg = "参数有误"
  1348. br.ErrMsg = "参数有误, 商家签约ID为空"
  1349. return
  1350. }
  1351. // 权限校验
  1352. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  1353. if e != nil {
  1354. br.Msg = "操作失败"
  1355. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  1356. return
  1357. }
  1358. if !ok {
  1359. br.Msg = "无权操作"
  1360. return
  1361. }
  1362. // 获取商家信息
  1363. contractOb := new(eta_business.EtaBusinessContract)
  1364. contract, e := contractOb.GetItemById(req.EtaBusinessContractId)
  1365. if e != nil {
  1366. if e.Error() == utils.ErrNoRow() {
  1367. br.Msg = "签约信息不存在, 请刷新页面"
  1368. return
  1369. }
  1370. br.Msg = "操作失败"
  1371. br.ErrMsg = "获取商家签约信息失败, Err: " + e.Error()
  1372. return
  1373. }
  1374. // 获取商家信息
  1375. businessOb := new(eta_business.EtaBusiness)
  1376. business, e := businessOb.GetItemById(contract.EtaBusinessId)
  1377. if e != nil {
  1378. if e.Error() == utils.ErrNoRow() {
  1379. br.Msg = "商家不存在, 请检查"
  1380. return
  1381. }
  1382. br.Msg = "操作失败"
  1383. br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
  1384. return
  1385. }
  1386. // 删除签约
  1387. if e = contract.Del(); e != nil {
  1388. br.Msg = "操作失败"
  1389. br.ErrMsg = "删除签约失败, Err: " + e.Error()
  1390. return
  1391. }
  1392. // 签约后续操作
  1393. go func() {
  1394. _ = etaBusinessService.UpdateEtaBusinessAfterSigning(business.EtaBusinessId)
  1395. }()
  1396. // 操作日志
  1397. go func() {
  1398. recordOb := new(eta_business.EtaBusinessOperationRecord)
  1399. recordOb.EtaBusinessId = business.EtaBusinessId
  1400. recordOb.SellerId = business.SellerId
  1401. recordOb.SysUserId = sysUser.AdminId
  1402. recordOb.SysRealName = sysUser.RealName
  1403. recordOb.OperationType = eta_business.EtaBusinessOperationTypeRemoveContract
  1404. recordOb.OperationRemark = fmt.Sprintf("%s删除签约", sysUser.RealName)
  1405. recordOb.CreateTime = time.Now().Local()
  1406. _ = recordOb.Create()
  1407. }()
  1408. br.Ret = 200
  1409. br.Success = true
  1410. br.Msg = "操作成功"
  1411. }