eta_business.go 41 KB

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