eta_business.go 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  1. package eta_business
  2. import (
  3. "encoding/json"
  4. "eta/eta_forum_admin/controllers"
  5. "eta/eta_forum_admin/models"
  6. "eta/eta_forum_admin/models/eta_business"
  7. "eta/eta_forum_admin/models/help_doc"
  8. etaBusinessService "eta/eta_forum_admin/services/eta_business"
  9. "eta/eta_forum_admin/utils"
  10. "fmt"
  11. "github.com/rdlucklib/rdluck_tools/paging"
  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. }
  1427. // @Title 设置社区图表分类权限
  1428. // @Description 设置社区图表分类权限
  1429. // @Param request body eta_business.AddBusinessChartClassifyPermissionReq true "type json string"
  1430. // @Success 200 {object} system.SysRoleListResp
  1431. // @router /add_chart_permission [post]
  1432. func (this *EtaBusinessController) AddChartPermission() {
  1433. br := new(models.BaseResponse).Init()
  1434. defer func() {
  1435. this.Data["json"] = br
  1436. this.ServeJSON()
  1437. }()
  1438. sysUser := this.SysUser
  1439. if sysUser == nil {
  1440. br.Msg = "请登录"
  1441. br.ErrMsg = "请登录,SysUser Is Empty"
  1442. return
  1443. }
  1444. var req eta_business.AddBusinessChartClassifyPermissionReq
  1445. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1446. if err != nil {
  1447. br.Msg = "参数解析异常!"
  1448. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1449. return
  1450. }
  1451. if req.EtaBusinessId <= 0 {
  1452. br.Msg = "请选择商户"
  1453. return
  1454. }
  1455. if req.ChartClassifyIdStr == "" {
  1456. br.Msg = "请选择图表分类"
  1457. return
  1458. }
  1459. // 权限校验
  1460. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  1461. if e != nil {
  1462. br.Msg = "操作失败"
  1463. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  1464. return
  1465. }
  1466. if !ok {
  1467. br.Msg = "无权操作"
  1468. return
  1469. }
  1470. ob := new(eta_business.EtaBusiness)
  1471. businessInfo, e := ob.GetItemById(req.EtaBusinessId)
  1472. if e != nil {
  1473. if e.Error() == utils.ErrNoRow() {
  1474. br.Msg = "商家不存在, 请刷新页面"
  1475. return
  1476. }
  1477. br.Msg = "获取商家信息失败"
  1478. br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
  1479. return
  1480. }
  1481. chartClassifyIdArr := strings.Split(req.ChartClassifyIdStr, ",")
  1482. addList := make([]*eta_business.BusinessChartClassifyPermission, 0)
  1483. for _, v := range chartClassifyIdArr {
  1484. classifyId, e := strconv.Atoi(v)
  1485. if e != nil {
  1486. br.Msg = "设置社区权限失败"
  1487. br.ErrMsg = "classifyId 解析失败,Err:" + e.Error()
  1488. return
  1489. }
  1490. //item := new(system.SysRoleMenu)
  1491. item := new(eta_business.BusinessChartClassifyPermission)
  1492. item.ChartClassifyId = classifyId
  1493. item.BusinessCode = businessInfo.BusinessCode
  1494. item.CreateTime = time.Now()
  1495. item.ModifyTime = time.Now()
  1496. item.AdminId = sysUser.AdminId
  1497. item.Source = 1
  1498. addList = append(addList, item)
  1499. }
  1500. err = eta_business.AddBusinessChartClassifyPermission(addList, businessInfo.BusinessCode)
  1501. if err != nil {
  1502. br.Msg = "设置社区权限失败"
  1503. br.ErrMsg = "设置社区权限失败,Err:" + err.Error()
  1504. return
  1505. }
  1506. br.Ret = 200
  1507. br.Success = true
  1508. br.IsAddLog = true
  1509. br.Msg = "设置成功"
  1510. }
  1511. // PageList
  1512. // @Title 商家列表
  1513. // @Description 商家列表
  1514. // @Param Keyword query string false "关键词: 商家名称/社会信用码/商家编码"
  1515. // @Success 200 Ret=200 获取成功
  1516. // @router /list [get]
  1517. func (this *EtaBusinessController) AllList() {
  1518. br := new(models.BaseResponse).Init()
  1519. defer func() {
  1520. if br.ErrMsg == "" {
  1521. br.IsSendEmail = false
  1522. }
  1523. this.Data["json"] = br
  1524. this.ServeJSON()
  1525. }()
  1526. sysUser := this.SysUser
  1527. if sysUser == nil {
  1528. br.Msg = "请登录"
  1529. br.ErrMsg = "请登录,SysUser Is Empty"
  1530. br.Ret = 408
  1531. return
  1532. }
  1533. // 权限校验
  1534. resp := new(eta_business.EtaBusinessAllListResp)
  1535. ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
  1536. if e != nil {
  1537. br.Msg = "操作失败"
  1538. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  1539. return
  1540. }
  1541. if !ok {
  1542. resp.List = make([]*eta_business.EtaBusinessItem, 0)
  1543. br.Data = resp
  1544. br.Ret = 200
  1545. br.Success = true
  1546. br.Msg = "获取成功"
  1547. return
  1548. }
  1549. cond := ``
  1550. pars := make([]interface{}, 0)
  1551. // 筛选项
  1552. {
  1553. // 关键词
  1554. keyword := this.GetString("Keyword", "")
  1555. keyword = strings.TrimSpace(keyword)
  1556. if keyword != "" {
  1557. kw := fmt.Sprint("%", keyword, "%")
  1558. cond += fmt.Sprintf(` AND (%s LIKE ? OR %s LIKE ? OR %s LIKE ?)`, eta_business.EtaBusinessColumns.BusinessName, eta_business.EtaBusinessColumns.BusinessCode, eta_business.EtaBusinessColumns.CreditCode)
  1559. pars = append(pars, kw, kw, kw)
  1560. }
  1561. }
  1562. order := ``
  1563. // 排序
  1564. {
  1565. fieldArr := []int{1, 2, 3, 4}
  1566. typeArr := []int{1, 2}
  1567. fieldMap := map[int]string{
  1568. 1: "signing_time",
  1569. 2: "expired_time",
  1570. 3: "create_time",
  1571. 4: "user_max",
  1572. }
  1573. typeMap := map[int]string{
  1574. 1: "ASC",
  1575. 2: "DESC",
  1576. }
  1577. sortParam, _ := this.GetInt("SortParam", 0)
  1578. sortType, _ := this.GetInt("SortType", 0)
  1579. if utils.InArrayByInt(fieldArr, sortParam) && utils.InArrayByInt(typeArr, sortType) {
  1580. order = fmt.Sprintf("%s %s", fieldMap[sortParam], typeMap[sortType])
  1581. }
  1582. fmt.Println("order: ", order)
  1583. }
  1584. // 获取列表
  1585. businessOb := new(eta_business.EtaBusiness)
  1586. list, e := businessOb.GetItemsByCondition(cond, pars, []string{}, order)
  1587. if e != nil {
  1588. br.Msg = "获取失败"
  1589. br.ErrMsg = "获取商家列表失败, Err: " + e.Error()
  1590. return
  1591. }
  1592. items := make([]*eta_business.EtaBusinessItem, 0)
  1593. for _, v := range list {
  1594. b := new(eta_business.EtaBusinessItem)
  1595. b.EtaBusinessId = v.EtaBusinessId
  1596. b.BusinessName = v.BusinessName
  1597. b.BusinessCode = v.BusinessCode
  1598. b.CreditCode = v.CreditCode
  1599. b.RegionType = v.RegionType
  1600. b.Province = v.Province
  1601. b.City = v.City
  1602. b.Address = v.Address
  1603. b.SellerId = v.SellerId
  1604. b.SellerName = v.SellerName
  1605. b.Leader = v.Leader
  1606. b.IndustryId = v.IndustryId
  1607. b.IndustryName = v.IndustryName
  1608. b.CapitalScale = v.CapitalScale
  1609. b.ResearchTeamSize = v.ResearchTeamSize
  1610. b.UserMax = v.UserMax
  1611. b.SigningStatus = v.SigningStatus
  1612. b.Enable = v.Enable
  1613. b.ContractId = v.ContractId
  1614. b.SigningTime = utils.TimeTransferString(utils.FormatDate, v.SigningTime)
  1615. b.ExpiredTime = utils.TimeTransferString(utils.FormatDate, v.ExpiredTime)
  1616. b.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
  1617. b.ModifyTime = v.ModifyTime.Format(utils.FormatDateTime)
  1618. b.Nation = v.Nation
  1619. items = append(items, b)
  1620. }
  1621. resp.List = items
  1622. br.Data = resp
  1623. br.Ret = 200
  1624. br.Success = true
  1625. br.Msg = "获取成功"
  1626. }