register.go 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711
  1. package contract
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "github.com/go-playground/validator/v10"
  8. "github.com/shopspring/decimal"
  9. "github.com/tealeg/xlsx"
  10. "hongze/fms_api/controller/resp"
  11. "hongze/fms_api/global"
  12. "hongze/fms_api/models/base"
  13. "hongze/fms_api/models/crm"
  14. "hongze/fms_api/models/fms"
  15. "hongze/fms_api/models/system"
  16. "hongze/fms_api/services/alarm_msg"
  17. crmService "hongze/fms_api/services/crm"
  18. fmsService "hongze/fms_api/services/fms"
  19. "hongze/fms_api/utils"
  20. "net/http"
  21. "os"
  22. "path"
  23. "strconv"
  24. "strings"
  25. "time"
  26. )
  27. // RegisterController 合同登记
  28. type RegisterController struct{}
  29. // List
  30. // @Title 合同登记列表
  31. // @Description 合同登记列表
  32. // @Param Keyword query string false "关键词"
  33. // @Param StartDate query string false "合同开始日期"
  34. // @Param EndDate query string false "合同结束日期"
  35. // @Param ServiceType query int false "套餐类型"
  36. // @Param ContractType query int false "合同类型"
  37. // @Param RegisterStatus query int false "登记状态"
  38. // @Success 200 {object} fms.ContractRegisterItem
  39. // @router /contract/register/list [get]
  40. func (rg *RegisterController) List(c *gin.Context) {
  41. var req fms.ContractRegisterListReq
  42. if e := c.BindQuery(&req); e != nil {
  43. err, ok := e.(validator.ValidationErrors)
  44. if !ok {
  45. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  46. return
  47. }
  48. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  49. return
  50. }
  51. cond := `1 = 1`
  52. pars := make([]interface{}, 0)
  53. // 合同编号/客户姓名/销售/实际使用方
  54. if req.Keyword != "" {
  55. kw := "%" + req.Keyword + "%"
  56. cond += ` AND (company_name LIKE ? OR contract_code LIKE ? OR seller_name LIKE ? OR rai_seller_name LIKE ? OR actual_company_name LIKE ?)`
  57. pars = append(pars, kw, kw, kw, kw, kw)
  58. }
  59. if req.StartDate != "" && req.EndDate != "" {
  60. st := fmt.Sprint(req.StartDate, " 00:00:00")
  61. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  62. cond += ` AND (create_time BETWEEN ? AND ?)`
  63. pars = append(pars, st, ed)
  64. }
  65. if req.ContractType != 0 {
  66. cond += ` AND contract_type = ?`
  67. pars = append(pars, req.ContractType)
  68. }
  69. if req.RegisterStatus != 0 {
  70. cond += ` AND register_status = ?`
  71. pars = append(pars, req.RegisterStatus)
  72. }
  73. // 套餐筛选
  74. if req.ServiceTypes != "" {
  75. serviceTypes := strings.Split(req.ServiceTypes, ",")
  76. registerIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  77. if e != nil {
  78. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  79. return
  80. }
  81. if len(registerIds) > 0 {
  82. cond += ` AND contract_register_id IN ?`
  83. pars = append(pars, registerIds)
  84. } else {
  85. cond += ` AND 1 = 2`
  86. }
  87. }
  88. page := new(base.Page)
  89. page.SetPageSize(req.PageSize)
  90. page.SetCurrent(req.Current)
  91. page.AddOrderItem(base.OrderItem{Column: "create_time", Asc: false})
  92. total, list, e := fms.GetContractRegisterItemPageList(page, cond, pars)
  93. if e != nil {
  94. resp.FailMsg("获取失败", "获取合同登记列表失败, Err: "+e.Error(), c)
  95. return
  96. }
  97. registerIds := make([]int, 0)
  98. for i := range list {
  99. registerIds = append(registerIds, list[i].ContractRegisterId)
  100. }
  101. serviceMap := make(map[int]string, 0)
  102. invoiceMap := make(map[int][]*fms.ContractInvoiceItem, 0)
  103. paymentMap := make(map[int][]*fms.ContractInvoiceItem, 0)
  104. if len(registerIds) > 0 {
  105. // 获取所有的服务套餐
  106. serviceMap, _, e = fmsService.GetContractServiceNameFormat(registerIds)
  107. if e != nil {
  108. resp.FailMsg("获取失败", e.Error(), c)
  109. return
  110. }
  111. // 获取开票/到款列表
  112. invoiceCond := `contract_register_id IN ?`
  113. invoicePars := make([]interface{}, 0)
  114. invoicePars = append(invoicePars, registerIds)
  115. invoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  116. if e != nil {
  117. resp.FailMsg("获取失败", "获取开票/到款列表失败, Err: "+e.Error(), c)
  118. return
  119. }
  120. for i := range invoiceList {
  121. if invoiceMap[invoiceList[i].ContractRegisterId] == nil {
  122. invoiceMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoiceItem, 0)
  123. }
  124. if paymentMap[invoiceList[i].ContractRegisterId] == nil {
  125. paymentMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoiceItem, 0)
  126. }
  127. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  128. invoiceMap[invoiceList[i].ContractRegisterId] = append(invoiceMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  129. }
  130. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  131. paymentMap[invoiceList[i].ContractRegisterId] = append(paymentMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  132. }
  133. }
  134. }
  135. respList := make([]*fms.ContractRegisterList, 0)
  136. for i := range list {
  137. v := new(fms.ContractRegisterList)
  138. v.ContractRegisterItem = list[i]
  139. v.ServicesName = serviceMap[list[i].ContractRegisterId]
  140. v.InvoiceList = invoiceMap[list[i].ContractRegisterId]
  141. v.PaymentList = paymentMap[list[i].ContractRegisterId]
  142. respList = append(respList, v)
  143. }
  144. page.SetTotal(total)
  145. baseData := new(base.BaseData)
  146. baseData.SetPage(page)
  147. baseData.SetList(respList)
  148. resp.OkData("获取成功", baseData, c)
  149. }
  150. // Add
  151. // @Title 新增合同登记
  152. // @Description 新增合同登记
  153. // @Param request body fms.ContractRegisterAddReq true "type json string"
  154. // @Success 200 string "操作成功"
  155. // @router /contract/register/add [post]
  156. func (rg *RegisterController) Add(c *gin.Context) {
  157. req := new(fms.ContractRegisterAddReq)
  158. err := c.ShouldBind(&req)
  159. if err != nil {
  160. errs, ok := err.(validator.ValidationErrors)
  161. if !ok {
  162. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  163. return
  164. }
  165. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  166. return
  167. }
  168. claims, _ := c.Get("adminInfo")
  169. adminInfo := claims.(*system.SysAdmin)
  170. // 日期校验
  171. startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  172. if e != nil {
  173. resp.FailMsg("合同开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
  174. return
  175. }
  176. endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  177. if e != nil {
  178. resp.FailMsg("合同结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
  179. return
  180. }
  181. signDate, _ := time.ParseInLocation(utils.FormatDate, "0000-00-00", time.Local)
  182. if req.SignDate != "" {
  183. signDateTime, e := time.ParseInLocation(utils.FormatDate, req.SignDate, time.Local)
  184. if e != nil {
  185. resp.FailMsg("合同签订日期格式有误", "合同签订日期格式有误, Err: "+e.Error(), c)
  186. return
  187. }
  188. signDate = signDateTime
  189. }
  190. // 是否存在相同合同编号的登记
  191. ob := new(fms.ContractRegister)
  192. existCond := `contract_code = ?`
  193. existPars := make([]interface{}, 0)
  194. existPars = append(existPars, req.ContractCode)
  195. exist, e := ob.FetchByCondition(existCond, existPars)
  196. if e != nil && e != utils.ErrNoRow {
  197. resp.FailMsg("操作失败", "获取相同登记号失败, Err: "+e.Error(), c)
  198. return
  199. }
  200. if exist != nil && exist.ContractRegisterId > 0 {
  201. resp.Fail("合同编号已存在", c)
  202. return
  203. }
  204. // 货币及汇率
  205. rateList, e := fmsService.GetTodayCurrencyRateList()
  206. if e != nil {
  207. resp.FailMsg("操作失败", "获取今日货币汇率失败, Err: "+e.Error(), c)
  208. return
  209. }
  210. var rate float64
  211. for i := range rateList {
  212. if req.CurrencyUnit == rateList[i].Code {
  213. rate = rateList[i].RMBRate
  214. break
  215. }
  216. }
  217. if rate <= 0 {
  218. resp.FailMsg("操作失败", "货币汇率信息有误", c)
  219. return
  220. }
  221. nowTime := time.Now().Local()
  222. ob.ContractCode = req.ContractCode
  223. ob.RelateContractCode = req.RelateContractCode
  224. ob.CrmContractId = req.CrmContractId
  225. ob.ContractSource = req.ContractSource
  226. ob.CompanyName = req.CompanyName
  227. ob.ActualCompanyName = req.ActualCompanyName
  228. ob.ProductIds = req.ProductIds
  229. ob.SellerId = req.SellerId
  230. ob.SellerName = req.SellerName
  231. ob.RaiSellerId = req.RaiSellerId
  232. ob.RaiSellerName = req.RaiSellerName
  233. ob.ContractType = req.ContractType
  234. ob.ContractAmount = req.ContractAmount
  235. ob.CurrencyUnit = req.CurrencyUnit
  236. ob.RMBRate = rate
  237. ob.StartDate = startDate
  238. ob.EndDate = endDate
  239. ob.SignDate = signDate
  240. ob.AgreedPayTime = req.AgreedPayTime
  241. ob.ContractStatus = req.ContractStatus
  242. ob.RegisterStatus = fms.ContractRegisterStatusIng
  243. ob.Remark = req.Remark
  244. ob.ServiceRemark = req.ServiceRemark
  245. ob.HasPayment = req.HasPayment
  246. ob.NewCompany = req.NewCompany
  247. ob.Set()
  248. // 存在代付的直接完成登记, 且不允许进行开票/到款登记
  249. if req.HasPayment == 1 || req.ContractStatus == fms.ContractStatusEnd {
  250. ob.RegisterStatus = fms.ContractRegisterStatusComplete
  251. }
  252. // 新增套餐金额详情内容
  253. serviceAmountMap := make(map[int]float64)
  254. serviceAmountList, e := fmsService.HandleContractServiceAmount(req.ServiceAmount, serviceAmountMap, req.CurrencyUnit)
  255. if e != nil {
  256. resp.FailMsg("操作失败", "新增合同套餐金额信息失败, Err: "+e.Error(), c)
  257. return
  258. }
  259. // 套餐信息
  260. serviceList, e := fmsService.HandleContractServiceAndDetail(req.Services, true, serviceAmountMap)
  261. if e != nil {
  262. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  263. return
  264. }
  265. // 新增合同及套餐
  266. if req.Supplement == 1 {
  267. //新增到款信息
  268. if ob.HasPayment == 1 {
  269. resp.Fail("合同存在代付不允许添加开票/到款登记", c)
  270. return
  271. }
  272. //新增合同信息
  273. if e = fms.CreateContractRegisterAndServicesAndPayMent(ob, serviceList, req.PrePayId); e != nil {
  274. resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c)
  275. return
  276. }
  277. prePayOB := new(fms.ContractPrePayment)
  278. ppItem, e := prePayOB.Fetch(req.PrePayId)
  279. if e != nil {
  280. if e == utils.ErrNoRow {
  281. resp.Fail("预到款不存在或已被删除", c)
  282. return
  283. }
  284. resp.FailMsg("获取预到款记录失败", "Err:"+e.Error(), c)
  285. return
  286. }
  287. // 合同有效时长(计算付款方式)
  288. dayDiff := ob.EndDate.Sub(ob.StartDate).Hours() / 24
  289. // 获取销售分组信息
  290. sellerList, e := crmService.GetSellerDepartmentListWithGroupAndTeam()
  291. if e != nil {
  292. resp.FailData("获取销售失败", "Err:"+e.Error(), c)
  293. return
  294. }
  295. sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
  296. for i := range sellerList {
  297. sellerMap[sellerList[i].SellerId] = sellerList[i]
  298. }
  299. v := &fms.ContractInvoice{
  300. ContractRegisterId: ob.ContractRegisterId,
  301. ContractCode: ob.ContractCode,
  302. Amount: ppItem.Amount,
  303. OriginAmount: ppItem.OriginAmount,
  304. CurrencyUnit: ppItem.CurrencyUnit,
  305. InvoiceType: fms.ContractInvoiceTypePay,
  306. InvoiceDate: ppItem.CreateTime,
  307. AdminId: int(adminInfo.AdminId),
  308. AdminName: adminInfo.RealName,
  309. Remark: ppItem.Remark,
  310. IsPrePay: 1,
  311. }
  312. v.Set()
  313. // 到款登记-付款方式
  314. v.PayType = fmsService.CalculateContractPaymentType(ppItem.Amount, ob.ContractAmount, dayDiff)
  315. // 新增的记录
  316. logList := make([]*fms.ContractRegisterLog, 0)
  317. opData := ""
  318. opDataByte, e := json.Marshal(req)
  319. if e != nil {
  320. return
  321. }
  322. opData = string(opDataByte)
  323. opType := fms.ContractInvoiceTypePrePay
  324. newAmount := decimal.NewFromFloat(0).Round(2)
  325. a := decimal.NewFromFloat(v.Amount).Round(2)
  326. newAmount = newAmount.Add(a)
  327. ia, _ := newAmount.Round(2).Float64()
  328. logList = append(logList, &fms.ContractRegisterLog{
  329. ContractRegisterId: ob.ContractRegisterId,
  330. AdminId: int(adminInfo.AdminId),
  331. AdminName: adminInfo.RealName,
  332. OpData: opData,
  333. OpType: opType,
  334. CreateTime: nowTime,
  335. AmountRemark: fmt.Sprint("新增", fms.ContractInvoiceKeyNameMap[opType], "金额", ia, "元"),
  336. })
  337. if e := v.Create(); e != nil {
  338. resp.FailData("日期格式有误", "Err:"+e.Error(), c)
  339. return
  340. }
  341. //最后删除预到款记录
  342. e = ppItem.Delete()
  343. if e != nil {
  344. resp.FailMsg("删除预到款记录失败", "Err:"+e.Error(), c)
  345. return
  346. }
  347. // 校验金额-是否修改状态
  348. go fmsService.CheckContractRegisterAmount(ob.ContractRegisterId)
  349. // 开票到款汇总
  350. go fmsService.SummaryInvoicePaymentByContractRegisterId(ob.ContractRegisterId)
  351. // 操作日志
  352. go func() {
  353. logOB := new(fms.ContractRegisterLog)
  354. if e := logOB.AddInBatches(logList); e != nil {
  355. return
  356. }
  357. }()
  358. } else {
  359. if e = fms.CreateContractRegisterAndServices(ob, serviceList, serviceAmountList); e != nil {
  360. resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c)
  361. return
  362. }
  363. }
  364. // 操作日志
  365. go func() {
  366. opData := ""
  367. opDataByte, e := json.Marshal(req)
  368. if e != nil {
  369. return
  370. }
  371. opData = string(opDataByte)
  372. logItem := new(fms.ContractRegisterLog)
  373. logItem.ContractRegisterId = ob.ContractRegisterId
  374. logItem.AdminId = int(adminInfo.AdminId)
  375. logItem.AdminName = adminInfo.RealName
  376. logItem.OpData = opData
  377. logItem.OpType = fms.ContractRegisterOpTypeSave
  378. logItem.CreateTime = nowTime
  379. logItem.Remark = req.Remark
  380. if e = logItem.Create(); e != nil {
  381. return
  382. }
  383. }()
  384. resp.Ok("操作成功", c)
  385. }
  386. // Edit
  387. // @Title 编辑合同登记
  388. // @Description 编辑合同登记
  389. // @Param request body fms.ContractRegisterEditReq true "type json string"
  390. // @Success 200 string "操作成功"
  391. // @router /contract/register/edit [post]
  392. func (rg *RegisterController) Edit(c *gin.Context) {
  393. req := new(fms.ContractRegisterEditReq)
  394. err := c.ShouldBind(&req)
  395. if err != nil {
  396. errs, ok := err.(validator.ValidationErrors)
  397. if !ok {
  398. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  399. return
  400. }
  401. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  402. return
  403. }
  404. claims, _ := c.Get("adminInfo")
  405. adminInfo := claims.(*system.SysAdmin)
  406. // 日期校验
  407. startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  408. if e != nil {
  409. resp.FailMsg("合同开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
  410. return
  411. }
  412. endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  413. if e != nil {
  414. resp.FailMsg("合同结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
  415. return
  416. }
  417. signDate, _ := time.ParseInLocation(utils.FormatDate, "0000-00-00", time.Local)
  418. if req.SignDate != "" {
  419. signDateTime, e := time.ParseInLocation(utils.FormatDate, req.SignDate, time.Local)
  420. if e != nil {
  421. resp.FailMsg("合同签订日期格式有误", "合同签订日期格式有误, Err: "+e.Error(), c)
  422. return
  423. }
  424. signDate = signDateTime
  425. }
  426. ob := new(fms.ContractRegister)
  427. item, e := ob.Fetch(req.ContractRegisterId)
  428. if e != nil {
  429. if e == utils.ErrNoRow {
  430. resp.Fail("登记记录不存在或已被删除", c)
  431. return
  432. }
  433. resp.FailMsg("操作失败", "获取合同登记信息失败, Err:"+e.Error(), c)
  434. return
  435. }
  436. // 是否存在相同合同编号的登记
  437. existCond := `contract_code = ?`
  438. existPars := make([]interface{}, 0)
  439. existPars = append(existPars, req.ContractCode)
  440. exist, e := ob.FetchByCondition(existCond, existPars)
  441. if e != nil && e != utils.ErrNoRow {
  442. resp.FailMsg("操作失败", "获取相同登记号失败, Err: "+e.Error(), c)
  443. return
  444. }
  445. if exist != nil && exist.ContractRegisterId > 0 && exist.ContractRegisterId != item.ContractRegisterId {
  446. resp.Fail("合同编号已存在", c)
  447. return
  448. }
  449. originHasPayment := item.HasPayment
  450. updateCols := []string{
  451. "ProductIds","ContractCode", "RelateContractCode", "CrmContractId", "ContractSource", "CompanyName", "ActualCompanyName",
  452. "SellerId", "SellerName", "ContractType", "ContractAmount", "StartDate", "EndDate", "SignDate", "AgreedPayTime",
  453. "ContractStatus", "RegisterStatus", "Remark", "ServiceRemark", "HasPayment", "NewCompany", "ModifyTime",
  454. }
  455. nowTime := time.Now().Local()
  456. item.ProductIds = req.ProductIds
  457. item.ContractCode = req.ContractCode
  458. item.RelateContractCode = req.RelateContractCode
  459. item.CrmContractId = req.CrmContractId
  460. item.ContractSource = req.ContractSource
  461. item.CompanyName = req.CompanyName
  462. item.ActualCompanyName = req.ActualCompanyName
  463. item.SellerId = req.SellerId
  464. item.SellerName = req.SellerName
  465. item.RaiSellerId = req.RaiSellerId
  466. item.RaiSellerName = req.RaiSellerName
  467. item.ContractType = req.ContractType
  468. item.ContractAmount = req.ContractAmount
  469. item.StartDate = startDate
  470. item.EndDate = endDate
  471. item.SignDate = signDate
  472. item.AgreedPayTime = req.AgreedPayTime
  473. item.ContractStatus = req.ContractStatus
  474. item.RegisterStatus = fms.ContractRegisterStatusIng
  475. item.Remark = req.Remark
  476. item.ServiceRemark = req.ServiceRemark
  477. item.HasPayment = req.HasPayment
  478. item.NewCompany = req.NewCompany
  479. item.ModifyTime = nowTime
  480. // 存在代付的直接完成登记, 且不允许进行开票/到款登记
  481. if req.HasPayment == 1 {
  482. item.RegisterStatus = fms.ContractRegisterStatusComplete
  483. updateCols = append(updateCols, "RegisterStatus")
  484. }
  485. // 开票到款信息
  486. invoiceOB := new(fms.ContractInvoice)
  487. invoiceCond := `contract_register_id = ?`
  488. invoicePars := make([]interface{}, 0)
  489. invoicePars = append(invoicePars, req.ContractRegisterId)
  490. invoiceList, e := invoiceOB.List(invoiceCond, invoicePars, "")
  491. if e != nil {
  492. resp.FailMsg("操作失败", "获取合同开票到款列表失败, Err: "+e.Error(), c)
  493. return
  494. }
  495. invoiceUpdateCols := make([]string, 0)
  496. // 开票到款操作类型: 0-无; 1-更新; 2-删除;
  497. logRemark := req.Remark
  498. invoiceHandleType := 0
  499. if originHasPayment == 0 && req.HasPayment == 1 {
  500. // 若从无代付修改为有代付, 则删除无代付期间新增的所有开票到款登记
  501. invoiceHandleType = 2
  502. } else {
  503. // 修改了货币单位后,同步更新汇率及开票到款的换算金额
  504. if req.CurrencyUnit != item.CurrencyUnit {
  505. rateList, e := fmsService.GetTodayCurrencyRateList()
  506. if e != nil {
  507. resp.FailMsg("操作失败", "获取货币列表及汇率失败, Err: "+e.Error(), c)
  508. return
  509. }
  510. var rate float64
  511. for i := range rateList {
  512. if rateList[i].Code == req.CurrencyUnit {
  513. rate = rateList[i].RMBRate
  514. break
  515. }
  516. }
  517. if rate < 0 {
  518. resp.FailMsg("操作失败", "货币汇率有误", c)
  519. return
  520. }
  521. item.CurrencyUnit = req.CurrencyUnit
  522. item.RMBRate = rate
  523. updateCols = append(updateCols, "CurrencyUnit", "RMBRate")
  524. // 调整开票到款换算后的金额, 保留两位小数
  525. for i := range invoiceList {
  526. invoiceList[i].CurrencyUnit = req.CurrencyUnit
  527. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceList[i].OriginAmount/rate), 64)
  528. invoiceList[i].Amount = a
  529. }
  530. invoiceUpdateCols = append(invoiceUpdateCols, "CurrencyUnit", "Amount")
  531. invoiceHandleType = 1
  532. logRemark = fmt.Sprintf("金额单位由%s修改为%s\n%s", item.CurrencyUnit, req.CurrencyUnit, logRemark)
  533. }
  534. }
  535. // 新增套餐金额详情内容
  536. serviceAmountMap := make(map[int]float64)
  537. serviceAmountList, e := fmsService.HandleContractServiceAmount(req.ServiceAmount, serviceAmountMap, req.CurrencyUnit)
  538. if e != nil {
  539. resp.FailMsg("操作失败", "新增合同套餐金额信息失败, Err: "+e.Error(), c)
  540. return
  541. }
  542. // 套餐信息
  543. serviceList, e := fmsService.HandleContractServiceAndDetail(req.Services, true, serviceAmountMap)
  544. if e != nil {
  545. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  546. return
  547. }
  548. // 更新合同登记、套餐、开票到款
  549. if e = fms.UpdateContractRegister(item, updateCols, serviceList, invoiceList, invoiceUpdateCols, invoiceHandleType, serviceAmountList); e != nil {
  550. resp.FailMsg("操作失败", "更新合同及套餐失败, Err: "+e.Error(), c)
  551. return
  552. }
  553. // 校验金额-是否修改状态
  554. go fmsService.CheckContractRegisterAmount(item.ContractRegisterId)
  555. // 操作日志
  556. go func() {
  557. opData := ""
  558. opDataByte, e := json.Marshal(req)
  559. if e != nil {
  560. return
  561. }
  562. opData = string(opDataByte)
  563. logItem := new(fms.ContractRegisterLog)
  564. logItem.ContractRegisterId = item.ContractRegisterId
  565. logItem.AdminId = int(adminInfo.AdminId)
  566. logItem.AdminName = adminInfo.RealName
  567. logItem.OpData = opData
  568. logItem.OpType = fms.ContractRegisterOpTypeEdit
  569. logItem.CreateTime = nowTime
  570. logItem.Remark = logRemark
  571. if e = logItem.Create(); e != nil {
  572. return
  573. }
  574. }()
  575. resp.Ok("操作成功", c)
  576. }
  577. // Del
  578. // @Title 删除合同登记
  579. // @Description 删除合同登记
  580. // @Param request body fms.ContractRegisterDelReq true "type json string"
  581. // @Success 200 string "操作成功"
  582. // @router /contract/register/del [post]
  583. func (rg *RegisterController) Del(c *gin.Context) {
  584. req := new(fms.ContractRegisterDelReq)
  585. err := c.ShouldBind(&req)
  586. if err != nil {
  587. errs, ok := err.(validator.ValidationErrors)
  588. if !ok {
  589. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  590. return
  591. }
  592. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  593. return
  594. }
  595. claims, _ := c.Get("adminInfo")
  596. adminInfo := claims.(*system.SysAdmin)
  597. ob := new(fms.ContractRegister)
  598. item, e := ob.Fetch(req.ContractRegisterId)
  599. if e != nil {
  600. if e == utils.ErrNoRow {
  601. resp.Fail("合同登记不存在或已被删除", c)
  602. return
  603. }
  604. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  605. return
  606. }
  607. nowTime := time.Now().Local()
  608. item.IsDeleted = 1
  609. item.ModifyTime = nowTime
  610. updateCols := []string{"IsDeleted", "ModifyTime"}
  611. if e = item.Update(updateCols); e != nil {
  612. resp.FailMsg("操作失败", "更新合同登记失败, Err:"+e.Error(), c)
  613. return
  614. }
  615. // 删除对应的开票到款登记
  616. go func() {
  617. if e = fms.DeleteContractInvoicesByRegisterId(item.ContractRegisterId); e != nil {
  618. alarm_msg.SendAlarmMsg(fmt.Sprintf("删除合同登记%d后, 删除开票到款记录失败, ErrMsg: %s", item.ContractRegisterId, e.Error()), 3)
  619. return
  620. }
  621. if e = fms.DeleteInvoicePaymentSummaryByRegisterId(item.ContractRegisterId); e != nil {
  622. alarm_msg.SendAlarmMsg(fmt.Sprintf("删除合同登记%d后, 删除开票到款汇总数据失败, ErrMsg: %s", item.ContractRegisterId, e.Error()), 3)
  623. return
  624. }
  625. }()
  626. // 操作日志
  627. go func() {
  628. opData := ""
  629. opDataByte, e := json.Marshal(req)
  630. if e != nil {
  631. return
  632. }
  633. opData = string(opDataByte)
  634. logItem := new(fms.ContractRegisterLog)
  635. logItem.ContractRegisterId = req.ContractRegisterId
  636. logItem.AdminId = int(adminInfo.AdminId)
  637. logItem.AdminName = adminInfo.RealName
  638. logItem.OpData = opData
  639. logItem.OpType = fms.ContractRegisterOpTypeDel
  640. logItem.CreateTime = nowTime
  641. if e = logItem.Create(); e != nil {
  642. return
  643. }
  644. }()
  645. resp.Ok("操作成功", c)
  646. }
  647. // Detail
  648. // @Title 合同登记详情
  649. // @Description 合同登记详情
  650. // @Param ContractRegisterId query int false "合同登记ID"
  651. // @Success 200 {object} fms.ContractRegisterDetail
  652. // @router /contract/register/detail [get]
  653. func (rg *RegisterController) Detail(c *gin.Context) {
  654. var req fms.ContractRegisterDetailReq
  655. if e := c.BindQuery(&req); e != nil {
  656. err, ok := e.(validator.ValidationErrors)
  657. if !ok {
  658. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  659. return
  660. }
  661. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  662. return
  663. }
  664. result := new(fms.ContractRegisterDetail)
  665. // 合同登记信息
  666. item, e := fms.GetContractRegisterItemById(req.ContractRegisterId)
  667. if e != nil {
  668. resp.FailData("获取失败", "获取合同登记详情失败, Err:"+e.Error(), c)
  669. return
  670. }
  671. result.ContractRegisterItem = item
  672. // 套餐信息
  673. serviceAmountTempList, e := fms.GetContractServiceAmountByRegisterId(req.ContractRegisterId)
  674. if e != nil {
  675. resp.FailData("获取失败", "获取合同套餐信息失败, Err: "+e.Error(), c)
  676. return
  677. }
  678. serviceAmountList := make([]*fms.ContractServiceAmountItem, 0)
  679. for _, v := range serviceAmountTempList {
  680. tmp := &fms.ContractServiceAmountItem{
  681. ContractRegisterId: v.ContractRegisterId,
  682. ProductId: v.ProductId,
  683. ServiceAmount: v.ServiceAmount,
  684. CurrencyUnit: v.CurrencyUnit,
  685. }
  686. serviceAmountList = append(serviceAmountList, tmp)
  687. }
  688. result.ServiceAmountList = serviceAmountList
  689. // 套餐金额信息
  690. serviceList, e := fmsService.GetContractServiceAndDetail(req.ContractRegisterId)
  691. if e != nil {
  692. resp.FailData("获取失败", "获取合同套餐信息失败, Err: "+e.Error(), c)
  693. return
  694. }
  695. result.ServiceList = serviceList
  696. // 开票/到款信息
  697. invoiceCond := `contract_register_id = ?`
  698. invoicePars := make([]interface{}, 0)
  699. invoicePars = append(invoicePars, req.ContractRegisterId)
  700. invoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  701. if e != nil {
  702. resp.FailData("获取失败", "获取合同开票/到款信息失败, Err: "+e.Error(), c)
  703. return
  704. }
  705. result.InvoiceList = make([]*fms.ContractInvoiceItem, 0)
  706. result.PaymentList = make([]*fms.ContractInvoiceItem, 0)
  707. for i := range invoiceList {
  708. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  709. result.InvoiceList = append(result.InvoiceList, invoiceList[i])
  710. continue
  711. }
  712. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  713. result.PaymentList = append(result.PaymentList, invoiceList[i])
  714. }
  715. }
  716. // 合同登记进度
  717. logCond := `contract_register_id = ?`
  718. logPars := make([]interface{}, 0)
  719. logPars = append(logPars, req.ContractRegisterId)
  720. logList, e := fms.GetContractRegisterLogItemList(logCond, logPars)
  721. if e != nil {
  722. resp.FailData("获取失败", "获取合同登记进度失败, Err: "+e.Error(), c)
  723. return
  724. }
  725. result.Logs = logList
  726. resp.OkData("获取成功", result, c)
  727. }
  728. // UpdateStatus
  729. // @Title 修改合同状态
  730. // @Description 修改合同状态
  731. // @Param request body fms.ContractRegisterUpdateStatusReq true "type json string"
  732. // @Success 200 string "操作成功"
  733. // @router /contract/register/update_status [post]
  734. func (rg *RegisterController) UpdateStatus(c *gin.Context) {
  735. req := new(fms.ContractRegisterUpdateStatusReq)
  736. err := c.ShouldBind(&req)
  737. if err != nil {
  738. errs, ok := err.(validator.ValidationErrors)
  739. if !ok {
  740. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  741. return
  742. }
  743. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  744. return
  745. }
  746. claims, _ := c.Get("adminInfo")
  747. adminInfo := claims.(*system.SysAdmin)
  748. ob := new(fms.ContractRegister)
  749. item, e := ob.Fetch(req.ContractRegisterId)
  750. if e != nil {
  751. if e == utils.ErrNoRow {
  752. resp.Fail("合同登记不存在或已被删除", c)
  753. return
  754. }
  755. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  756. return
  757. }
  758. nowTime := time.Now().Local()
  759. item.ContractStatus = req.ContractStatus
  760. item.ModifyTime = nowTime
  761. updateCols := []string{"ContractStatus", "ModifyTime"}
  762. if e = item.Update(updateCols); e != nil {
  763. resp.FailMsg("操作失败", "更新合同登记失败, Err:"+e.Error(), c)
  764. return
  765. }
  766. // 校验金额-是否修改状态
  767. go fmsService.CheckContractRegisterAmount(req.ContractRegisterId)
  768. // 操作日志
  769. go func() {
  770. opData := ""
  771. opDataByte, e := json.Marshal(req)
  772. if e != nil {
  773. return
  774. }
  775. opData = string(opDataByte)
  776. logItem := new(fms.ContractRegisterLog)
  777. logItem.ContractRegisterId = req.ContractRegisterId
  778. logItem.AdminId = int(adminInfo.AdminId)
  779. logItem.AdminName = adminInfo.RealName
  780. logItem.OpData = opData
  781. logItem.OpType = fms.ContractRegisterOpTypeStatus
  782. logItem.CreateTime = nowTime
  783. if e = logItem.Create(); e != nil {
  784. return
  785. }
  786. }()
  787. resp.Ok("操作成功", c)
  788. }
  789. // Invoice
  790. // @Title 开票/到款登记
  791. // @Description 开票/到款登记
  792. // @Param request body fms.ContractInvoiceSaveReq true "type json string"
  793. // @Success 200 string "操作成功"
  794. // @router /contract/register/invoice [post]
  795. func (rg *RegisterController) Invoice(c *gin.Context) {
  796. req := new(fms.ContractInvoiceSaveReq)
  797. err := c.ShouldBind(&req)
  798. if err != nil {
  799. errs, ok := err.(validator.ValidationErrors)
  800. if !ok {
  801. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  802. return
  803. }
  804. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  805. return
  806. }
  807. claims, _ := c.Get("adminInfo")
  808. adminInfo := claims.(*system.SysAdmin)
  809. registerOB := new(fms.ContractRegister)
  810. item, e := registerOB.Fetch(req.ContractRegisterId)
  811. if e != nil {
  812. if e == utils.ErrNoRow {
  813. resp.Fail("合同登记不存在或已被删除", c)
  814. return
  815. }
  816. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  817. return
  818. }
  819. // 合同存在代付不允许开票/到款登记
  820. if item.HasPayment == 1 {
  821. resp.Fail("合同存在代付不允许添加开票/到款登记", c)
  822. return
  823. }
  824. noChangeInvoiceIds := make([]int, 0)
  825. newInvoice := make([]*fms.ContractInvoice, 0)
  826. if len(req.AmountList) > 0 {
  827. // 合同有效时长(计算付款方式)
  828. dayDiff := item.EndDate.Sub(item.StartDate).Hours() / 24
  829. // 获取销售分组信息
  830. sellerList, e := crmService.GetSellerDepartmentListWithGroupAndTeam()
  831. if e != nil {
  832. resp.FailData("获取销售失败", "Err:"+e.Error(), c)
  833. return
  834. }
  835. sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
  836. for i := range sellerList {
  837. sellerMap[sellerList[i].SellerId] = sellerList[i]
  838. }
  839. for i := range req.AmountList {
  840. if req.AmountList[i].Amount <= 0 {
  841. resp.Fail("登记金额有误", c)
  842. return
  843. }
  844. if req.AmountList[i].InvoiceDate == "" {
  845. resp.Fail("请选择日期", c)
  846. return
  847. }
  848. // 开票登记销售必填
  849. if req.InvoiceType == fms.ContractInvoiceTypeMake && req.AmountList[i].SellerId <= 0 {
  850. resp.Fail("请选择销售", c)
  851. return
  852. }
  853. t, e := time.ParseInLocation(utils.FormatDate, req.AmountList[i].InvoiceDate, time.Local)
  854. if e != nil {
  855. resp.FailData("日期格式有误", "Err:"+e.Error(), c)
  856. return
  857. }
  858. if req.AmountList[i].InvoiceId > 0 {
  859. noChangeInvoiceIds = append(noChangeInvoiceIds, req.AmountList[i].InvoiceId)
  860. continue
  861. }
  862. if req.AmountList[i].InvoiceId == 0 {
  863. v := &fms.ContractInvoice{
  864. ContractRegisterId: req.ContractRegisterId,
  865. ContractCode: item.ContractCode,
  866. Amount: req.AmountList[i].Amount,
  867. OriginAmount: req.AmountList[i].OriginAmount,
  868. CurrencyUnit: item.CurrencyUnit,
  869. InvoiceType: req.InvoiceType,
  870. ServiceProductId: req.AmountList[i].ServiceProductId,
  871. InvoiceDate: t,
  872. AdminId: int(adminInfo.AdminId),
  873. AdminName: adminInfo.RealName,
  874. Remark: req.AmountList[i].Remark,
  875. }
  876. v.Set()
  877. // 开票登记-销售信息
  878. if req.InvoiceType == fms.ContractInvoiceTypeMake {
  879. sellerItem := sellerMap[req.AmountList[i].SellerId]
  880. if sellerItem == nil {
  881. resp.Fail("销售信息异常", c)
  882. return
  883. }
  884. if req.AmountList[i].ServiceProductId == crm.CompanyProductFicc && sellerItem.DepartmentId != crm.SellerDepartmentId {
  885. resp.Fail("销售类型和所选套餐类型不一致", c)
  886. return
  887. }
  888. if req.AmountList[i].ServiceProductId == crm.CompanyProductRai && sellerItem.DepartmentId != crm.RaiSellerDepartmentId {
  889. resp.Fail("销售类型和所选套餐类型不一致", c)
  890. return
  891. }
  892. v.SellerId = sellerItem.SellerId
  893. v.SellerName = sellerItem.SellerName
  894. v.SellerGroupId = sellerItem.GroupId
  895. v.SellerGroupName = sellerItem.GroupName
  896. v.SellerTeamId = sellerItem.TeamId
  897. v.SellerTeamName = sellerItem.TeamName
  898. }
  899. // 到款登记-付款方式
  900. if req.InvoiceType == fms.ContractInvoiceTypePay {
  901. v.PayType = fmsService.CalculateContractPaymentType(req.AmountList[i].Amount, item.ContractAmount, dayDiff)
  902. }
  903. newInvoice = append(newInvoice, v)
  904. }
  905. }
  906. }
  907. // 获取原有的登记信息
  908. invoiceCond := `contract_register_id = ? AND invoice_type = ? `
  909. invoicePars := make([]interface{}, 0)
  910. invoicePars = append(invoicePars, req.ContractRegisterId, req.InvoiceType)
  911. originInvoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  912. if e != nil {
  913. resp.FailMsg("获取失败", "获取开票/到款列表失败, Err: "+e.Error(), c)
  914. return
  915. }
  916. // 比对原有和现有的登记信息
  917. logList := make([]*fms.ContractRegisterLog, 0)
  918. opData := ""
  919. opDataByte, e := json.Marshal(req)
  920. if e != nil {
  921. return
  922. }
  923. opData = string(opDataByte)
  924. opType := fms.ContractRegisterOpTypeInvoice
  925. if req.InvoiceType == fms.ContractInvoiceTypePay {
  926. opType = fms.ContractRegisterOpTypePayment
  927. }
  928. nowTime := time.Now().Local()
  929. // 需要删除的记录
  930. deleteInvoiceIds := make([]int, 0)
  931. for i := range originInvoiceList {
  932. if !utils.InArrayByInt(noChangeInvoiceIds, originInvoiceList[i].ContractInvoiceId) {
  933. deleteInvoiceIds = append(deleteInvoiceIds, originInvoiceList[i].ContractInvoiceId)
  934. logList = append(logList, &fms.ContractRegisterLog{
  935. ContractRegisterId: req.ContractRegisterId,
  936. AdminId: int(adminInfo.AdminId),
  937. AdminName: adminInfo.RealName,
  938. OpData: opData,
  939. OpType: opType,
  940. CreateTime: nowTime,
  941. AmountRemark: fmt.Sprint("删除", fms.ContractInvoiceKeyNameMap[opType], "金额", originInvoiceList[i].Amount, "元"),
  942. })
  943. }
  944. }
  945. // todo 判断是否符合均分金额的条件,如果符合,需要生成金额分配记录表
  946. // 新增的记录
  947. if len(newInvoice) > 0 {
  948. newAmount := decimal.NewFromFloat(0).Round(2)
  949. for i := range newInvoice {
  950. a := decimal.NewFromFloat(newInvoice[i].Amount).Round(2)
  951. newAmount = newAmount.Add(a)
  952. }
  953. ia, _ := newAmount.Round(2).Float64()
  954. logList = append(logList, &fms.ContractRegisterLog{
  955. ContractRegisterId: req.ContractRegisterId,
  956. AdminId: int(adminInfo.AdminId),
  957. AdminName: adminInfo.RealName,
  958. OpData: opData,
  959. OpType: opType,
  960. CreateTime: nowTime,
  961. AmountRemark: fmt.Sprint("新增", fms.ContractInvoiceKeyNameMap[opType], "金额", ia, "元"),
  962. })
  963. }
  964. // 删除并新增登记
  965. ob := new(fms.ContractInvoice)
  966. if e := ob.DeleteAndCreateNewInvoice(req.ContractRegisterId, req.InvoiceType, deleteInvoiceIds, newInvoice); e != nil {
  967. resp.FailData("日期格式有误", "Err:"+e.Error(), c)
  968. return
  969. }
  970. // 校验金额-是否修改状态
  971. go fmsService.CheckContractRegisterAmount(req.ContractRegisterId)
  972. // 开票到款汇总
  973. go fmsService.SummaryInvoicePaymentByContractRegisterId(req.ContractRegisterId)
  974. // 操作日志
  975. go func() {
  976. logOB := new(fms.ContractRegisterLog)
  977. if e := logOB.AddInBatches(logList); e != nil {
  978. return
  979. }
  980. }()
  981. resp.Ok("操作成功", c)
  982. }
  983. // Export
  984. // @Title 合同登记-导出
  985. // @Description 合同登记-导出
  986. // @Param Keyword query string false "关键词"
  987. // @Param StartDate query string false "合同开始日期"
  988. // @Param EndDate query string false "合同结束日期"
  989. // @Param ServiceType query int false "套餐类型"
  990. // @Param ContractType query int false "合同类型"
  991. // @Param RegisterStatus query int false "登记状态"
  992. // @Success 200 string "操作成功"
  993. // @router /contract/register/export [get]
  994. func (rg *RegisterController) Export(c *gin.Context) {
  995. var req fms.ContractRegisterListReq
  996. if e := c.BindQuery(&req); e != nil {
  997. err, ok := e.(validator.ValidationErrors)
  998. if !ok {
  999. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1000. return
  1001. }
  1002. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1003. return
  1004. }
  1005. cond := `1 = 1`
  1006. pars := make([]interface{}, 0)
  1007. // 合同编号/客户姓名/销售/实际使用方
  1008. if req.Keyword != "" {
  1009. kw := "%" + req.Keyword + "%"
  1010. cond += ` AND (company_name LIKE ? OR contract_code LIKE ? OR seller_name LIKE ? OR actual_company_name LIKE ?)`
  1011. pars = append(pars, kw, kw, kw, kw)
  1012. }
  1013. if req.StartDate != "" && req.EndDate != "" {
  1014. st := fmt.Sprint(req.StartDate, " 00:00:00")
  1015. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1016. cond += ` AND (create_time BETWEEN ? AND ?)`
  1017. pars = append(pars, st, ed)
  1018. }
  1019. if req.ContractType != 0 {
  1020. cond += ` AND contract_type = ?`
  1021. pars = append(pars, req.ContractType)
  1022. }
  1023. if req.RegisterStatus != 0 {
  1024. cond += ` AND register_status = ?`
  1025. pars = append(pars, req.RegisterStatus)
  1026. }
  1027. if req.ServiceTypes != "" {
  1028. serviceTypes := strings.Split(req.ServiceTypes, ",")
  1029. registerIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  1030. if e != nil {
  1031. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  1032. return
  1033. }
  1034. if len(registerIds) > 0 {
  1035. cond += ` AND contract_register_id IN ?`
  1036. pars = append(pars, registerIds)
  1037. } else {
  1038. cond += ` AND 1 = 2`
  1039. }
  1040. }
  1041. // 获取列表数据
  1042. cr := new(fms.ContractRegister)
  1043. list, e := cr.List(cond, pars)
  1044. if e != nil {
  1045. resp.FailData("获取合同列表失败", "Err:"+e.Error(), c)
  1046. return
  1047. }
  1048. if len(list) == 0 {
  1049. resp.Fail("无有效数据可导出", c)
  1050. return
  1051. }
  1052. registerIds := make([]int, 0)
  1053. for i := range list {
  1054. registerIds = append(registerIds, list[i].ContractRegisterId)
  1055. }
  1056. // 获取小套餐品种
  1057. cpCond := `product_id = ? AND permission_name <> ?`
  1058. cpPars := make([]interface{}, 0)
  1059. cpPars = append(cpPars, crm.CompanyProductFicc, crm.ChartPermissionStrategyName)
  1060. cp := new(crm.ChartPermission)
  1061. permissionList, e := cp.List(cpCond, cpPars)
  1062. if e != nil {
  1063. resp.FailData("获取小套餐品种失败", "Err:"+e.Error(), c)
  1064. return
  1065. }
  1066. permissionLen := len(permissionList)
  1067. permissionNameIdMap := make(map[string]int)
  1068. for i := range permissionList {
  1069. permissionNameIdMap[permissionList[i].PermissionName] = permissionList[i].ChartPermissionId
  1070. }
  1071. // 获取所有FICC套餐服务
  1072. serviceList, e := fms.GetContractServiceTemplateMapByProductId(crm.CompanyProductFicc)
  1073. if e != nil {
  1074. resp.FailData("获取套餐服务失败", "Err:"+e.Error(), c)
  1075. return
  1076. }
  1077. // 除大小套餐外的其他套餐(下面的导出需要以此动态处理)
  1078. otherService := make([]*fms.ContractServiceTemplateItem, 0)
  1079. for i := range serviceList {
  1080. if serviceList[i].Title != "FICC大套餐" && serviceList[i].Title != "FICC小套餐" {
  1081. otherService = append(otherService, serviceList[i])
  1082. }
  1083. }
  1084. otherServiceLen := len(otherService)
  1085. // 获取所有权益套餐服务
  1086. raiServiceList, e := fms.GetContractServiceTemplateMapByProductId(crm.CompanyProductRai)
  1087. if e != nil {
  1088. resp.FailData("获取权益套餐服务失败", "Err:"+e.Error(), c)
  1089. return
  1090. }
  1091. // 除大小套餐外的其他套餐(下面的导出需要以此动态处理)
  1092. raiOtherService := make([]*fms.ContractServiceTemplateItem, 0)
  1093. for i := range raiServiceList {
  1094. //获取二级套餐
  1095. secondServiceList, e := fms.GetContractServiceTemplateMapByParentId(raiServiceList[i].ServiceTemplateId)
  1096. if e != nil {
  1097. resp.FailData("获取二级权益套餐服务失败", "Err:"+e.Error(), c)
  1098. return
  1099. }
  1100. if len(secondServiceList) > 0 {
  1101. for _, sv := range secondServiceList {
  1102. //获取三级套餐
  1103. thirdServiceList, e := fms.GetContractServiceTemplateMapByParentId(sv.ServiceTemplateId)
  1104. if e != nil {
  1105. resp.FailData("获取三级权益套餐服务失败", "Err:"+e.Error(), c)
  1106. return
  1107. }
  1108. if len(thirdServiceList) > 0 {
  1109. for _, tv := range thirdServiceList {
  1110. raiOtherService = append(raiOtherService, tv)
  1111. }
  1112. }else{
  1113. raiOtherService = append(raiOtherService, sv)
  1114. }
  1115. }
  1116. }else{
  1117. raiOtherService = append(raiOtherService, raiServiceList[i])
  1118. }
  1119. }
  1120. otherServiceLen += len(raiOtherService)
  1121. //套餐金额map
  1122. serviceAmountMap := make(map[int]map[int]*fms.ContractServiceAmount)
  1123. // 套餐/开票/到款列表
  1124. serviceMap := make(map[int][]*fms.ContractService)
  1125. serviceChartPermissionsMap := make(map[int][]int)
  1126. invoiceMap := make(map[int][]*fms.ContractInvoice)
  1127. paymentMap := make(map[int][]*fms.ContractInvoice)
  1128. maxInvoice := 0
  1129. maxPayment := 0
  1130. if len(registerIds) > 0 {
  1131. // 获取套餐信息
  1132. csCond := `contract_register_id IN ?`
  1133. csPars := make([]interface{}, 0)
  1134. csPars = append(csPars, registerIds)
  1135. cs := new(fms.ContractService)
  1136. serviceList, e := cs.List(csCond, csPars)
  1137. if e != nil {
  1138. resp.FailData("获取合同套餐列表失败", "Err:"+e.Error(), c)
  1139. return
  1140. }
  1141. for i := range serviceList {
  1142. cid := serviceList[i].ContractRegisterId
  1143. if serviceMap[cid] == nil {
  1144. serviceMap[cid] = make([]*fms.ContractService, 0)
  1145. }
  1146. serviceMap[cid] = append(serviceMap[cid], serviceList[i])
  1147. // 小套餐权限
  1148. if serviceChartPermissionsMap[cid] == nil {
  1149. serviceChartPermissionsMap[cid] = make([]int, 0)
  1150. }
  1151. if serviceList[i].ChartPermissionIds != "" {
  1152. ids := utils.JoinStr2IntArr(serviceList[i].ChartPermissionIds, ",")
  1153. serviceChartPermissionsMap[cid] = append(serviceChartPermissionsMap[cid], ids...)
  1154. }
  1155. }
  1156. // 获取合同的套餐金额信息
  1157. serviceAmountList, e := fms.GetContractServiceAmountByContractRegisterIds(registerIds)
  1158. if e != nil {
  1159. resp.FailData("获取合同的套餐金额信息失败", "Err:"+e.Error(), c)
  1160. return
  1161. }
  1162. for _, v := range serviceAmountList {
  1163. if _, ok := serviceAmountMap[v.ContractRegisterId]; !ok {
  1164. serviceAmountMap[v.ContractRegisterId] = make(map[int]*fms.ContractServiceAmount)
  1165. }
  1166. serviceAmountMap[v.ContractRegisterId][v.ProductId] = v
  1167. }
  1168. // 获取开票/到款详情, 并取最大的开票/到款数(用于动态扩展第二列表头)
  1169. ci := new(fms.ContractInvoice)
  1170. invoiceList, e := ci.List(csCond, csPars, "")
  1171. if e != nil {
  1172. resp.FailData("获取开票/到款列表失败", "Err:"+e.Error(), c)
  1173. return
  1174. }
  1175. for k := range invoiceList {
  1176. cid := invoiceList[k].ContractRegisterId
  1177. if invoiceMap[cid] == nil {
  1178. invoiceMap[cid] = make([]*fms.ContractInvoice, 0)
  1179. }
  1180. if paymentMap[cid] == nil {
  1181. paymentMap[cid] = make([]*fms.ContractInvoice, 0)
  1182. }
  1183. if invoiceList[k].InvoiceType == fms.ContractInvoiceTypeMake {
  1184. invoiceMap[cid] = append(invoiceMap[cid], invoiceList[k])
  1185. continue
  1186. }
  1187. if invoiceList[k].InvoiceType == fms.ContractInvoiceTypePay {
  1188. paymentMap[cid] = append(paymentMap[cid], invoiceList[k])
  1189. continue
  1190. }
  1191. }
  1192. // 取最大开票/到款数
  1193. for j := range invoiceMap {
  1194. if len(invoiceMap[j]) > maxInvoice {
  1195. maxInvoice = len(invoiceMap[j])
  1196. }
  1197. }
  1198. for p := range paymentMap {
  1199. if len(paymentMap[p]) > maxPayment {
  1200. maxPayment = len(paymentMap[p])
  1201. }
  1202. }
  1203. }
  1204. // 生成Excel文件
  1205. xlsxFile := xlsx.NewFile()
  1206. style := xlsx.NewStyle()
  1207. alignment := xlsx.Alignment{
  1208. Horizontal: "center",
  1209. Vertical: "center",
  1210. WrapText: true,
  1211. }
  1212. style.Alignment = alignment
  1213. style.ApplyAlignment = true
  1214. sheetName := "财务列表"
  1215. sheet, err := xlsxFile.AddSheet(sheetName)
  1216. if err != nil {
  1217. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  1218. return
  1219. }
  1220. // 1行表头
  1221. hInt := 0
  1222. titleRow := sheet.AddRow()
  1223. titleRow.SetHeight(40)
  1224. // 1行1列-右合并两格, 下合并1行
  1225. cell1 := titleRow.AddCell()
  1226. cell1.HMerge = 4
  1227. cell1.VMerge = 1
  1228. cell1.SetString("FICC客户签约表")
  1229. cell1.SetStyle(style)
  1230. // 右增两列空白格用于第一列合并, 否则后续单元格合并会有问题
  1231. titleRow.AddCell().SetString("")
  1232. titleRow.AddCell().SetString("")
  1233. titleRow.AddCell().SetString("")
  1234. titleRow.AddCell().SetString("")
  1235. hInt += 5
  1236. // 1行2列
  1237. cell2 := titleRow.AddCell()
  1238. cell2.VMerge = 1
  1239. cell2.SetString("FICC大套餐")
  1240. cell2.SetStyle(style)
  1241. hInt ++
  1242. // 1行3列-右合并小套餐数
  1243. if permissionLen >= 1 {
  1244. cell3 := titleRow.AddCell()
  1245. hInt ++
  1246. cell3.HMerge = permissionLen - 1
  1247. cell3.VMerge = 1
  1248. cell3.SetString("FICC小套餐")
  1249. cell3.SetStyle(style)
  1250. // 同上右增单元格小套餐数-1的空白单元格用于合并
  1251. for i := 0; i < permissionLen-1; i++ {
  1252. titleRow.AddCell().SetString("")
  1253. hInt ++
  1254. }
  1255. }
  1256. for i := range otherService {
  1257. cellOther := titleRow.AddCell()
  1258. cellOther.VMerge = 1
  1259. hInt ++
  1260. cellOther.SetString(otherService[i].Title)
  1261. cellOther.SetStyle(style)
  1262. }
  1263. //权益大套餐
  1264. cell4 := titleRow.AddCell()
  1265. cell4.HMerge = 1
  1266. cell4.VMerge = 1
  1267. cell4.SetString("权益大套餐")
  1268. cell4.SetStyle(style)
  1269. titleRow.AddCell().SetString("")
  1270. hInt +=2
  1271. //权益分行业套餐
  1272. cell5 := titleRow.AddCell()
  1273. cell5.HMerge = 8
  1274. cell5.SetString("权益分行业套餐")
  1275. cell5.SetStyle(style)
  1276. titleRow.AddCell().SetString("")
  1277. titleRow.AddCell().SetString("")
  1278. titleRow.AddCell().SetString("")
  1279. titleRow.AddCell().SetString("")
  1280. titleRow.AddCell().SetString("")
  1281. titleRow.AddCell().SetString("")
  1282. titleRow.AddCell().SetString("")
  1283. titleRow.AddCell().SetString("")
  1284. //第二行,前面几个单元格用于第一行的合并
  1285. titleRow2 := sheet.AddRow()
  1286. titleRow2.SetHeight(30)
  1287. for i:= 0; i<hInt;i++ {
  1288. titleRow2.AddCell().SetString("")
  1289. }
  1290. //权益分行业套餐
  1291. row2Cell1 := titleRow2.AddCell()
  1292. row2Cell1.HMerge = 1
  1293. row2Cell1.SetString("医药")
  1294. row2Cell1.SetStyle(style)
  1295. titleRow2.AddCell().SetString("")
  1296. row2Cell2 := titleRow2.AddCell()
  1297. row2Cell2.HMerge = 1
  1298. row2Cell2.SetString("消费")
  1299. row2Cell2.SetStyle(style)
  1300. titleRow2.AddCell().SetString("")
  1301. row2Cell3 := titleRow2.AddCell()
  1302. row2Cell3.HMerge = 1
  1303. row2Cell3.SetString("科技")
  1304. row2Cell3.SetStyle(style)
  1305. titleRow2.AddCell().SetString("")
  1306. row2Cell4 := titleRow2.AddCell()
  1307. row2Cell4.HMerge = 1
  1308. row2Cell4.SetString("智造")
  1309. row2Cell4.SetStyle(style)
  1310. titleRow2.AddCell().SetString("")
  1311. row2Cell5 := titleRow2.AddCell()
  1312. row2Cell5.SetString("策略")
  1313. row2Cell5.SetStyle(style)
  1314. // 第三行表头
  1315. titleRow3 := sheet.AddRow()
  1316. titleRow3.SetHeight(60)
  1317. row3Title := make([]string, 0)
  1318. row3Title = append(row3Title, "客户名称", "新客户\n0-是\n1-否", "合同类型\n续约-0\n新增-1\n代付-2\n补充协议-3", "FICC销售", "权益销售", "FICC大套餐")
  1319. for i := range permissionList {
  1320. row3Title = append(row3Title, permissionList[i].PermissionName)
  1321. }
  1322. // 其他套餐
  1323. for i := range otherService {
  1324. row3Title = append(row3Title, otherService[i].Title)
  1325. }
  1326. // 权益三级套餐
  1327. for i := range raiOtherService {
  1328. row3Title = append(row3Title, raiOtherService[i].Title)
  1329. }
  1330. row3Title = append(row3Title, "套餐备注", "开始时间", "到期时间", "2022年合同金额", "金额单位", "FICC套餐总金额", "权益套餐总金额", "约定付款时间", "签订日", "合同状态",
  1331. "合同编号", "合规备注")
  1332. // 设置表头
  1333. for i := range row3Title {
  1334. v := titleRow3.AddCell()
  1335. v.SetString(row3Title[i])
  1336. v.SetStyle(style)
  1337. }
  1338. // 第二行表头-开票/收款(动态添加)
  1339. invoiceTitle := []string{"开票日", "开票金额", "套餐类型", "销售", "备注"}
  1340. for i := 0; i < maxInvoice; i++ {
  1341. n := i + 1
  1342. for ii := range invoiceTitle {
  1343. c := titleRow3.AddCell()
  1344. t := fmt.Sprintf("%s%d", invoiceTitle[ii], n)
  1345. c.SetString(t)
  1346. c.SetStyle(style)
  1347. row3Title = append(row3Title, t)
  1348. }
  1349. }
  1350. paymentTitle := []string{"到款日", "到款金额", "套餐类型", "备注"}
  1351. for i := 0; i < maxPayment; i++ {
  1352. n := i + 1
  1353. for ii := range paymentTitle {
  1354. c := titleRow2.AddCell()
  1355. t := fmt.Sprintf("%s%d", paymentTitle[ii], n)
  1356. c.SetString(t)
  1357. c.SetStyle(style)
  1358. row3Title = append(row3Title, t)
  1359. }
  1360. }
  1361. // 此处取第二行标题NameKeyMap, 后面的动态匹配
  1362. row2NameKeyMap := make(map[string]int)
  1363. for i := range row3Title {
  1364. row2NameKeyMap[row3Title[i]] = i
  1365. }
  1366. newCompanyMap := map[int]string{0: "1", 1: "0"}
  1367. contractTMap := map[int]int{
  1368. fms.ContractTypeNew: 1,
  1369. fms.ContractTypeRenew: 0,
  1370. fms.ContractTypeAgentPay: 2, // 代付合同
  1371. fms.ContractTypePlus: 3, // 补充协议
  1372. }
  1373. for _, v := range list {
  1374. k := -1 // 套餐匹配用
  1375. dataRow := sheet.AddRow()
  1376. dataRow.SetHeight(20)
  1377. k += 4
  1378. dataRow.AddCell().SetString(v.CompanyName)
  1379. dataRow.AddCell().SetString(newCompanyMap[v.NewCompany])
  1380. dataRow.AddCell().SetString(fmt.Sprint(contractTMap[v.ContractType]))
  1381. dataRow.AddCell().SetString(v.SellerName)
  1382. dataRow.AddCell().SetString(v.RaiSellerName)
  1383. // 大套餐
  1384. k += 1
  1385. col4Name := row3Title[k]
  1386. svList := serviceMap[v.ContractRegisterId]
  1387. col4 := ""
  1388. if svList != nil && len(svList) > 0 {
  1389. for isv := range svList {
  1390. if svList[isv].Title == col4Name {
  1391. // todo 判断权益套餐权限时会有问题,需要同时判断父级name
  1392. col4 = "是"
  1393. break
  1394. }
  1395. }
  1396. }
  1397. dataRow.AddCell().SetString(col4)
  1398. // 小套餐
  1399. serviceChartPermissionIds := serviceChartPermissionsMap[v.ContractRegisterId]
  1400. for i := 0; i < permissionLen; i++ {
  1401. k += 1
  1402. colName := row3Title[k]
  1403. chartPermissionId := permissionNameIdMap[colName]
  1404. if utils.InArray(chartPermissionId, serviceChartPermissionIds) {
  1405. dataRow.AddCell().SetString("是")
  1406. } else {
  1407. dataRow.AddCell().SetString("")
  1408. }
  1409. }
  1410. // 除大小套餐外的其他套餐(处理方式其实跟上面的大套餐一样, 只是中间隔了小套餐按照顺序要这么处理=_=!)
  1411. for i := 0; i < otherServiceLen; i++ {
  1412. k += 1
  1413. otherColName := row3Title[k]
  1414. otherCol := ""
  1415. if svList != nil && len(svList) > 0 {
  1416. for isv := range svList {
  1417. if svList[isv].Title == otherColName {
  1418. otherCol = "是"
  1419. break
  1420. }
  1421. }
  1422. }
  1423. dataRow.AddCell().SetString(otherCol)
  1424. }
  1425. // 其他信息
  1426. ficcAmount, _ := serviceAmountMap[v.ContractRegisterId][crm.CompanyProductFicc]
  1427. raiAmount, _ := serviceAmountMap[v.ContractRegisterId][crm.CompanyProductRai]
  1428. dataRow.AddCell().SetString(v.ServiceRemark) // 套餐备注
  1429. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.StartDate)) // 开始时间
  1430. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.EndDate)) // 到期时间
  1431. dataRow.AddCell().SetString(fmt.Sprint("¥", v.ContractAmount)) // 2022年合同金额
  1432. dataRow.AddCell().SetString(v.CurrencyUnit) // 货币单位
  1433. dataRow.AddCell().SetString(fmt.Sprint("¥", ficcAmount.ServiceAmount)) // FICC套餐总金额
  1434. dataRow.AddCell().SetString(fmt.Sprint("¥", raiAmount.ServiceAmount)) // 权益套餐总金额
  1435. dataRow.AddCell().SetString(v.AgreedPayTime) // 约定付款时间
  1436. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.SignDate)) // 签订日
  1437. dataRow.AddCell().SetString(fms.ContractStatusKeyNameMap[v.ContractStatus]) // 合同状态
  1438. dataRow.AddCell().SetString(v.ContractCode) // 合同编号
  1439. dataRow.AddCell().SetString(v.Remark) // 合规备注
  1440. // 开票/到款信息
  1441. ivList := invoiceMap[v.ContractRegisterId]
  1442. ivListLen := len(ivList)
  1443. for ia := 0; ia < maxInvoice; ia++ {
  1444. if ia < ivListLen {
  1445. if ivList != nil && ivList[ia] != nil {
  1446. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", ivList[ia].InvoiceDate)) // 开票日
  1447. dataRow.AddCell().SetString(fmt.Sprint(ivList[ia].OriginAmount)) // 开票金额
  1448. // todo 开票中的套餐类型
  1449. dataRow.AddCell().SetString("")
  1450. dataRow.AddCell().SetString(ivList[ia].SellerName) // 销售名称
  1451. dataRow.AddCell().SetString(ivList[ia].Remark) // 开票备注
  1452. continue
  1453. }
  1454. }
  1455. // 这里要把不够的填充为空
  1456. dataRow.AddCell().SetString("")
  1457. dataRow.AddCell().SetString("")
  1458. dataRow.AddCell().SetString("")
  1459. dataRow.AddCell().SetString("")
  1460. dataRow.AddCell().SetString("")
  1461. }
  1462. pyList := paymentMap[v.ContractRegisterId]
  1463. pyListLen := len(pyList)
  1464. for ib := 0; ib < maxPayment; ib++ {
  1465. if ib < pyListLen {
  1466. if pyList != nil && pyList[ib] != nil {
  1467. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", pyList[ib].InvoiceDate)) // 收款日
  1468. dataRow.AddCell().SetString(fmt.Sprint(pyList[ib].OriginAmount)) // 收款金额
  1469. //todo 到款的套餐类型
  1470. dataRow.AddCell().SetString("") // 套餐类型
  1471. dataRow.AddCell().SetString(pyList[ib].Remark) // 收款备注
  1472. continue
  1473. }
  1474. }
  1475. // 这里要把不够的填充为空
  1476. dataRow.AddCell().SetString("")
  1477. dataRow.AddCell().SetString("")
  1478. dataRow.AddCell().SetString("")
  1479. dataRow.AddCell().SetString("")
  1480. }
  1481. }
  1482. // 输出文件
  1483. var buffer bytes.Buffer
  1484. _ = xlsxFile.Write(&buffer)
  1485. content := bytes.NewReader(buffer.Bytes())
  1486. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1487. fileName := sheetName + randStr + ".xlsx"
  1488. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  1489. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1490. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  1491. }
  1492. // InvoiceList
  1493. // @Title 开票/到款列表
  1494. // @Description 开票/到款列表
  1495. // @Param InvoiceType query int false "类型: 1-开票登记; 2-到款登记"
  1496. // @Param ContractCode query string false "合同编号"
  1497. // @Param StartDate query string false "开始日期"
  1498. // @Param EndDate query string false "结束日期"
  1499. // @Param MinAmount query float64 false "开票金额区间-最小值"
  1500. // @Param MaxAmount query float64 false "开票金额区间-最大值"
  1501. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  1502. // @Success 200 {object} fms.ContractInvoiceItem
  1503. // @router /contract/register/invoice_list [get]
  1504. func (rg *RegisterController) InvoiceList(c *gin.Context) {
  1505. var req fms.ContractInvoiceListReq
  1506. if e := c.BindQuery(&req); e != nil {
  1507. err, ok := e.(validator.ValidationErrors)
  1508. if !ok {
  1509. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1510. return
  1511. }
  1512. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1513. return
  1514. }
  1515. pageSize := req.PageSize
  1516. pageIndex := req.Current
  1517. if pageSize <= 0 {
  1518. pageSize = utils.PageSize20
  1519. }
  1520. if pageIndex <= 0 {
  1521. pageIndex = 1
  1522. }
  1523. cond := `invoice_type = ?`
  1524. pars := make([]interface{}, 0)
  1525. pars = append(pars, req.InvoiceType)
  1526. // 合同编号
  1527. if req.ContractCode != "" {
  1528. kw := fmt.Sprint("%", req.ContractCode, "%")
  1529. pars = append(pars, kw)
  1530. // 开票列表同时模糊查询销售名称
  1531. if req.InvoiceType == fms.ContractInvoiceTypeMake {
  1532. cond += ` AND (contract_code LIKE ? )`
  1533. pars = append(pars, kw)
  1534. } else {
  1535. cond += ` AND contract_code LIKE ?`
  1536. pars = append(pars, kw)
  1537. }
  1538. }
  1539. if req.StartDate != "" && req.EndDate != "" {
  1540. st := fmt.Sprint(req.StartDate, " 00:00:00")
  1541. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1542. cond += ` AND (invoice_time BETWEEN ? AND ?)`
  1543. pars = append(pars, st, ed)
  1544. }
  1545. if req.MinAmount > 0 {
  1546. cond += ` AND amount >= ?`
  1547. pars = append(pars, req.MinAmount)
  1548. }
  1549. if req.MaxAmount > 0 {
  1550. cond += ` AND amount <= ?`
  1551. pars = append(pars, req.MaxAmount)
  1552. }
  1553. // 套餐类型
  1554. if req.ServiceProductId > 0 {
  1555. cond += ` AND service_product_id = ?`
  1556. pars = append(pars, req.ServiceProductId)
  1557. }
  1558. // 货币列表
  1559. currencyOB := new(fms.CurrencyUnit)
  1560. currencyCond := `enable = 1`
  1561. currencyPars := make([]interface{}, 0)
  1562. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  1563. if e != nil {
  1564. resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
  1565. return
  1566. }
  1567. unitMap := make(map[string]string)
  1568. currencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  1569. for i := range currencyList {
  1570. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  1571. currencyTotals = append(currencyTotals, &fms.InvoiceListCurrencyTotal{
  1572. Name: currencyList[i].Name,
  1573. UnitName: currencyList[i].UnitName,
  1574. Code: currencyList[i].Code,
  1575. FlagImg: currencyList[i].FlagImg,
  1576. })
  1577. }
  1578. page := new(base.Page)
  1579. page.SetPageSize(pageSize)
  1580. page.SetCurrent(pageIndex)
  1581. page.AddOrderItem(base.OrderItem{Column: "invoice_time", Asc: false})
  1582. if req.IsExport == 1 {
  1583. page.SetPageSize(10000)
  1584. page.SetCurrent(1)
  1585. }
  1586. total, list, e := fms.GetContractInvoiceItemPageList(page, cond, pars)
  1587. if e != nil {
  1588. resp.FailMsg("获取失败", "获取合同开票/到款列表失败, Err: "+e.Error(), c)
  1589. return
  1590. }
  1591. page.SetTotal(total)
  1592. contractRegisterIds := make([]int, 0)
  1593. contractStatusMap := make(map[int]int)
  1594. for i := range list {
  1595. list[i].UnitName = unitMap[list[i].CurrencyUnit]
  1596. contractRegisterIds = append(contractRegisterIds, list[i].ContractRegisterId)
  1597. }
  1598. if len(contractRegisterIds) > 0 {
  1599. contractRegisters, err := fms.GetContractInfoByRegisterIds(contractRegisterIds)
  1600. if err != nil {
  1601. resp.FailMsg("查询合同信息失败", "查询合同信息失败, Err: "+err.Error(), c)
  1602. return
  1603. }
  1604. for _, v := range contractRegisters {
  1605. contractStatusMap[v.ContractRegisterId] = v.ContractStatus
  1606. }
  1607. }
  1608. for i := range list {
  1609. list[i].ContractStatus = contractStatusMap[list[i].ContractRegisterId]
  1610. }
  1611. // 分币种合计金额
  1612. var amountTotal float64
  1613. sumList, e := fms.GetInvoiceListCurrencySum(cond, pars, "currency_unit")
  1614. if e != nil {
  1615. resp.FailMsg("获取失败", "获取开票/到款列表合计金额失败, Err: "+e.Error(), c)
  1616. return
  1617. }
  1618. sumMap := make(map[string]float64)
  1619. for i := range sumList {
  1620. amountTotal += sumList[i].AmountTotal
  1621. sumMap[sumList[i].CurrencyUnit] = sumList[i].OriginAmountTotal
  1622. }
  1623. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountTotal), 64)
  1624. for i := range currencyTotals {
  1625. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", sumMap[currencyTotals[i].Code]), 64)
  1626. currencyTotals[i].Amount = a
  1627. }
  1628. respData := &fms.InvoiceListRespData{
  1629. Page: page,
  1630. List: list,
  1631. AmountTotal: amountTotal,
  1632. CurrencyTotal: currencyTotals,
  1633. }
  1634. // 是否导出
  1635. if req.IsExport == 1 {
  1636. ExportInvoiceList(c, req, respData)
  1637. return
  1638. }
  1639. resp.OkData("获取成功", respData, c)
  1640. }
  1641. // ExportInvoiceList 导出开票/到款列表
  1642. func ExportInvoiceList(c *gin.Context, req fms.ContractInvoiceListReq, results *fms.InvoiceListRespData) {
  1643. list := make([]*fms.ContractInvoiceItem, 0)
  1644. if val, ok := results.List.([]*fms.ContractInvoiceItem); ok {
  1645. list = val
  1646. } else {
  1647. resp.Fail("列表数据有误", c)
  1648. return
  1649. }
  1650. if len(list) == 0 {
  1651. resp.Fail("列表数据为空", c)
  1652. return
  1653. }
  1654. listName := "开票"
  1655. if req.InvoiceType == fms.ContractInvoiceTypePay {
  1656. listName = "到款"
  1657. }
  1658. // 生成Excel文件
  1659. xlsxFile := xlsx.NewFile()
  1660. style := xlsx.NewStyle()
  1661. alignment := xlsx.Alignment{
  1662. Horizontal: "center",
  1663. Vertical: "center",
  1664. WrapText: true,
  1665. }
  1666. style.Alignment = alignment
  1667. style.ApplyAlignment = true
  1668. sheet, err := xlsxFile.AddSheet(fmt.Sprintf("%s列表", listName))
  1669. if err != nil {
  1670. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  1671. return
  1672. }
  1673. // 前两行显示合计金额, 第三行空出与列表数据隔一行
  1674. rowA := sheet.AddRow()
  1675. cellAA := rowA.AddCell()
  1676. cellAA.SetString(fmt.Sprintf("已开票合计金额(换算后):%.2f(元)", results.AmountTotal))
  1677. rowBData := "已开票金额:"
  1678. for _, v := range results.CurrencyTotal {
  1679. rowBData += fmt.Sprintf("%s%.2f(%s) ", v.Name, v.Amount, v.UnitName)
  1680. }
  1681. rowB := sheet.AddRow()
  1682. rowB.AddCell().SetString(rowBData)
  1683. sheet.AddRow()
  1684. // 列表数据表头
  1685. titles := []string{"合同编号", fmt.Sprintf("%s金额", listName), "金额单位", "换算金额(元)",
  1686. fmt.Sprintf("%s日期", listName), "销售", "备注"}
  1687. titleRow := sheet.AddRow()
  1688. titleRow.SetHeight(40)
  1689. for i := range titles {
  1690. c := titleRow.AddCell()
  1691. c.SetString(titles[i])
  1692. c.SetStyle(style)
  1693. }
  1694. // 单元格赋值
  1695. for _, v := range list {
  1696. dataRow := sheet.AddRow()
  1697. dataRow.SetHeight(20)
  1698. dataRow.AddCell().SetString(v.ContractCode) // 合同编号
  1699. dataRow.AddCell().SetString(fmt.Sprint(v.OriginAmount)) // 开票金额
  1700. dataRow.AddCell().SetString(v.UnitName) // 金额单位
  1701. dataRow.AddCell().SetString(fmt.Sprint(v.Amount)) // 换算金额(元)
  1702. dataRow.AddCell().SetString(v.InvoiceDate) // 开票日
  1703. dataRow.AddCell().SetString(v.SellerName) // 销售
  1704. dataRow.AddCell().SetString(v.Remark) // 备注
  1705. }
  1706. // 输出文件
  1707. var buffer bytes.Buffer
  1708. _ = xlsxFile.Write(&buffer)
  1709. content := bytes.NewReader(buffer.Bytes())
  1710. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1711. fileName := fmt.Sprintf("%s列表_%s.xlsx", listName, randStr)
  1712. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  1713. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1714. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  1715. }
  1716. // Import
  1717. // @Title 合同登记-导入
  1718. // @Description 合同登记-导入
  1719. // @Success 200 string "操作成功"
  1720. // @router /contract/register/import [post]
  1721. func (rg *RegisterController) Import(c *gin.Context) {
  1722. h, err := c.FormFile("File")
  1723. if err != nil {
  1724. resp.FailData("获取文件失败", "Err:"+err.Error(), c)
  1725. return
  1726. }
  1727. ext := path.Ext(h.Filename)
  1728. if ext != ".xlsx" && ext != ".xls" {
  1729. resp.Fail("请上传Excel文件", c)
  1730. return
  1731. }
  1732. uploadDir := "static/xls"
  1733. err = os.MkdirAll(uploadDir, 766)
  1734. if err != nil {
  1735. resp.FailData("存储目录创建失败", "Err:"+err.Error(), c)
  1736. return
  1737. }
  1738. uploadPath := uploadDir + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + h.Filename
  1739. err = c.SaveUploadedFile(h, uploadPath)
  1740. if err != nil {
  1741. resp.FailData("保存本地文件失败", "Err:"+err.Error(), c)
  1742. return
  1743. }
  1744. defer func() {
  1745. _ = os.Remove(uploadPath)
  1746. }()
  1747. xlFile, err := xlsx.OpenFile(uploadPath)
  1748. if err != nil {
  1749. resp.FailData("打开文件失败", "Err:"+err.Error(), c)
  1750. return
  1751. }
  1752. // 获取所有已登记,根据合同编号去重
  1753. contractCodeArr := make([]string, 0)
  1754. registerOB := new(fms.ContractRegister)
  1755. registerCond := ``
  1756. registerPars := make([]interface{}, 0)
  1757. registerList, e := registerOB.List(registerCond, registerPars)
  1758. if e != nil {
  1759. resp.FailData("获取合同登记列表失败", "Err:"+e.Error(), c)
  1760. return
  1761. }
  1762. for i := range registerList {
  1763. contractCodeArr = append(contractCodeArr, registerList[i].ContractCode)
  1764. }
  1765. // 获取所有销售Map
  1766. sellerList, e := crmService.GetSellerDepartmentListWithGroupAndTeam()
  1767. if e != nil {
  1768. resp.FailData("获取销售列表失败", "Err:"+e.Error(), c)
  1769. return
  1770. }
  1771. sellerMap := make(map[string]*crm.SellerAdminWithGroupTeam)
  1772. for i := range sellerList {
  1773. sellerMap[sellerList[i].SellerName] = sellerList[i]
  1774. }
  1775. // 获取品种权限Map
  1776. chartPermissionNameIdMap := make(map[string]int)
  1777. cpCond := `product_id = ?`
  1778. cpPars := make([]interface{}, 0)
  1779. cpPars = append(cpPars, crm.CompanyProductFicc)
  1780. cp := new(crm.ChartPermission)
  1781. permissionList, e := cp.List(cpCond, cpPars)
  1782. if e != nil {
  1783. resp.FailData("获取权限列表失败", "Err:"+e.Error(), c)
  1784. return
  1785. }
  1786. for i := range permissionList {
  1787. chartPermissionNameIdMap[permissionList[i].PermissionName] = permissionList[i].ChartPermissionId
  1788. }
  1789. // 获取所有套餐模板
  1790. serviceTempCond := ``
  1791. serviceTempPars := make([]interface{}, 0)
  1792. serviceTempOB := new(fms.ContractServiceTemplate)
  1793. serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
  1794. if e != nil {
  1795. resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
  1796. return
  1797. }
  1798. serviceTempNameMap := make(map[string]*fms.ContractServiceTemplate)
  1799. serviceTempFullNameMap := make(map[string]*fms.ContractServiceTemplate)
  1800. serviceTempIdMap := make(map[int]*fms.ContractServiceTemplate)
  1801. for i := range serviceTempList {
  1802. serviceTempIdMap[serviceTempList[i].ServiceTemplateId] = serviceTempList[i]
  1803. serviceTempNameMap[serviceTempList[i].Title] = serviceTempList[i]
  1804. }
  1805. for i := range serviceTempList {
  1806. fullName := serviceTempList[i].Title
  1807. if serviceTempList[i].Pid > 0 {
  1808. parentItem := serviceTempIdMap[serviceTempList[i].Pid]
  1809. fullName += "_"+parentItem.Title
  1810. if parentItem.Pid > 0 {
  1811. fullName += "_"+serviceTempIdMap[parentItem.Pid].Title
  1812. }
  1813. }
  1814. serviceTempFullNameMap[fullName] = serviceTempList[i]
  1815. }
  1816. //权益行业套餐名称
  1817. raiIndustryMap := map[int]string{
  1818. 36:"医药",
  1819. 37:"医药",
  1820. 38:"消费",
  1821. 39:"消费",
  1822. 40:"科技",
  1823. 41:"科技",
  1824. 42:"智造",
  1825. 43:"智造",
  1826. 44:"策略",
  1827. }
  1828. // 获取货币列表及汇率(汇率为导入日的汇率)
  1829. rateList, e := fmsService.GetTodayCurrencyRateList()
  1830. if e != nil {
  1831. resp.FailData("获取货币列表及汇率失败", "Err:"+e.Error(), c)
  1832. return
  1833. }
  1834. rateMap := make(map[string]float64)
  1835. for i := range rateList {
  1836. rateMap[rateList[i].Code] = rateList[i].RMBRate
  1837. }
  1838. titleMap := make(map[int]string)
  1839. newIds := make([]int, 0)
  1840. newCompanyArr := []string{"0", "1"}
  1841. newCompanyMap := map[string]int{"0": 1, "1": 0}
  1842. contractTypeArr := []string{"0", "1", "2", "3"}
  1843. contractTypeMap := map[string]int{
  1844. "0": fms.ContractTypeRenew,
  1845. "1": fms.ContractTypeNew,
  1846. "2": fms.ContractTypeAgentPay,
  1847. "3": fms.ContractTypePlus,
  1848. }
  1849. // 更新登记状态
  1850. defer func() {
  1851. if len(newIds) > 0 {
  1852. go func() {
  1853. for i := range newIds {
  1854. fmsService.CheckContractRegisterAmount(newIds[i])
  1855. fmsService.SummaryInvoicePaymentByContractRegisterId(newIds[i])
  1856. }
  1857. }()
  1858. }
  1859. }()
  1860. invoiceMax := 3
  1861. checkDate := time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local)
  1862. for _, sheet := range xlFile.Sheets {
  1863. // 遍历行读取
  1864. maxRow := sheet.MaxRow
  1865. for i := 0; i < maxRow; i++ {
  1866. // 第三行开始读取表头
  1867. if i == 2 {
  1868. row := sheet.Row(i)
  1869. cells := row.Cells
  1870. for k, cell := range cells {
  1871. text := cell.String()
  1872. titleMap[k] = text
  1873. // 只检验前面部分表头, 后面很多动态列
  1874. if k == 0 {
  1875. if text != "客户名称" {
  1876. resp.Fail("【客户名称】列名称有误, 请参考模板导入", c)
  1877. return
  1878. }
  1879. continue
  1880. } else if k == 1 {
  1881. if !strings.Contains(text, "新客户") {
  1882. resp.Fail("【新客户】列名称有误, 请参考模板导入", c)
  1883. return
  1884. }
  1885. continue
  1886. } else if k == 2 {
  1887. if !strings.Contains(text, "合同类型") {
  1888. resp.Fail("【合同类型】列名称有误, 请参考模板导入", c)
  1889. return
  1890. }
  1891. continue
  1892. } else if k == 3 {
  1893. if text != "FICC销售" {
  1894. resp.Fail("【FICC销售】列名称有误, 请参考模板导入", c)
  1895. return
  1896. }
  1897. continue
  1898. } else if k == 4 {
  1899. if text != "权益销售" {
  1900. resp.Fail("【权益销售】列名称有误, 请参考模板导入", c)
  1901. return
  1902. }
  1903. continue
  1904. } else if k == 5 {
  1905. if text != "FICC大套餐" {
  1906. resp.Fail("【FICC大套餐】列名称有误, 请参考模板导入", c)
  1907. return
  1908. }
  1909. continue
  1910. }
  1911. }
  1912. }else if i >= 3 {
  1913. // 数据行
  1914. row := sheet.Row(i)
  1915. cells := row.Cells
  1916. // 登记信息
  1917. rowRegister := new(fms.ContractRegister)
  1918. // 套餐
  1919. rowServices := make([]*fms.ContractService, 0)
  1920. // 套餐金额
  1921. rowServiceAmount := make([]*fms.ContractServiceAmount, 0)
  1922. // 开票/到款
  1923. rowInvoices := make([]*fms.ContractInvoice, 0)
  1924. for ir := 0; ir < invoiceMax; ir++ {
  1925. rowInvoices = append(rowInvoices, new(fms.ContractInvoice))
  1926. }
  1927. rowPayments := make([]*fms.ContractInvoice, 0)
  1928. for ir := 0; ir < invoiceMax; ir++ {
  1929. rowPayments = append(rowPayments, new(fms.ContractInvoice))
  1930. }
  1931. // 小套餐权限
  1932. rowChartPermissionIdArr := make([]string, 0)
  1933. productIds := make(map[int]struct{})
  1934. isSkip := false
  1935. for k, cell := range cells {
  1936. v := utils.TrimStr(cell.String())
  1937. // 客户名称
  1938. if k == 0 {
  1939. if v == "" {
  1940. resp.Fail(fmt.Sprintf("第%d行客户名称不可为空, 请按模板导入", i+1), c)
  1941. return
  1942. }
  1943. rowRegister.CompanyName = v
  1944. continue
  1945. }
  1946. // 是否新客户
  1947. if k == 1 {
  1948. if !utils.InArrayByStr(newCompanyArr, v) {
  1949. resp.Fail(fmt.Sprintf("第%d行新客户有误, 请按模板导入", i+1), c)
  1950. return
  1951. }
  1952. rowRegister.NewCompany = newCompanyMap[v]
  1953. continue
  1954. }
  1955. // 合同类型
  1956. if k == 2 {
  1957. if !utils.InArrayByStr(contractTypeArr, v) {
  1958. resp.Fail(fmt.Sprintf("第%d行合同类型有误, 请按模板导入", i+1), c)
  1959. return
  1960. }
  1961. rowRegister.ContractType = contractTypeMap[v]
  1962. if rowRegister.ContractType == 0 {
  1963. resp.Fail(fmt.Sprintf("第%d行合同类型匹配有误, 请按模板导入", i+1), c)
  1964. return
  1965. }
  1966. continue
  1967. }
  1968. // FICC销售
  1969. if k == 3 {
  1970. if v != "" {
  1971. /*resp.Fail(fmt.Sprintf("第%d行销售名称不可为空, 请按模板导入", i+1), c)
  1972. return*/
  1973. sellerItem := sellerMap[v]
  1974. if sellerItem == nil {
  1975. resp.Fail(fmt.Sprintf("第%d行销售名称与系统销售不匹配, 请核对名称后导入", i+1), c)
  1976. return
  1977. }
  1978. rowRegister.SellerId = sellerItem.SellerId
  1979. rowRegister.SellerName = sellerItem.SellerName
  1980. }
  1981. continue
  1982. }
  1983. // 权益销售
  1984. if k == 4 {
  1985. if v == "" && rowRegister.SellerName == "" {
  1986. resp.Fail(fmt.Sprintf("第FICC销售名称和权益销售名称不可都为空, 请按模板导入", i+1), c)
  1987. return
  1988. }
  1989. sellerItem := sellerMap[v]
  1990. if sellerItem == nil {
  1991. resp.Fail(fmt.Sprintf("第%d行权益销售名称与系统销售不匹配, 请核对名称后导入", i+1), c)
  1992. return
  1993. }
  1994. rowRegister.RaiSellerId = sellerItem.SellerId
  1995. rowRegister.RaiSellerName = sellerItem.SellerName
  1996. continue
  1997. }
  1998. // FICC大套餐
  1999. if k == 5 {
  2000. if v == "是" {
  2001. tempItem := serviceTempNameMap[titleMap[k]]
  2002. if tempItem == nil {
  2003. resp.Fail(fmt.Sprintf("第%d行套餐名称不匹配, 请按模板导入", i+1), c)
  2004. return
  2005. }
  2006. cs := &fms.ContractService{
  2007. ProductId: crm.CompanyProductFicc,
  2008. ServiceTemplateId: tempItem.ServiceTemplateId,
  2009. Title: tempItem.Title,
  2010. Value: tempItem.Value,
  2011. TableValue: tempItem.TableValue,
  2012. ChartPermissionId: tempItem.ChartPermissionId,
  2013. }
  2014. cs.Set()
  2015. rowServices = append(rowServices, cs)
  2016. productIds[crm.CompanyProductFicc] = struct{}{}
  2017. }
  2018. continue
  2019. }
  2020. // FICC小套餐
  2021. if k >= 6 && k <= 26 {
  2022. // 小套餐权限
  2023. if v == "是" {
  2024. rowChartPermissionIdArr = append(rowChartPermissionIdArr, strconv.Itoa(chartPermissionNameIdMap[titleMap[k]]))
  2025. productIds[crm.CompanyProductFicc] = struct{}{}
  2026. }
  2027. continue
  2028. }
  2029. // 其他类型套餐
  2030. if k >= 27 && k <= 33 {
  2031. if v == "是" {
  2032. tempItem := serviceTempNameMap[titleMap[k]]
  2033. if tempItem == nil {
  2034. resp.Fail(fmt.Sprintf("第%d行套餐名称不匹配, 请按模板导入", i+1), c)
  2035. return
  2036. }
  2037. cs := &fms.ContractService{
  2038. ProductId: crm.CompanyProductFicc,
  2039. ServiceTemplateId: tempItem.ServiceTemplateId,
  2040. Title: tempItem.Title,
  2041. Value: tempItem.Value,
  2042. TableValue: tempItem.TableValue,
  2043. ChartPermissionId: tempItem.ChartPermissionId,
  2044. }
  2045. cs.Set()
  2046. rowServices = append(rowServices, cs)
  2047. productIds[crm.CompanyProductFicc] = struct{}{}
  2048. }
  2049. continue
  2050. }
  2051. // todo 权益行业套餐
  2052. if k >=34 && k<=44 {
  2053. if v == "是" {
  2054. // 权益大套餐
  2055. if k <= 35 {
  2056. //新增
  2057. parentName := "权益大套餐"
  2058. tempItem := serviceTempNameMap[titleMap[k]]
  2059. if tempItem == nil {
  2060. resp.Fail(fmt.Sprintf("第%d行套餐名称不匹配, 请按模板导入", i+1), c)
  2061. return
  2062. }
  2063. cs := &fms.ContractService{
  2064. ProductId: crm.CompanyProductFicc,
  2065. ServiceTemplateId: tempItem.ServiceTemplateId,
  2066. Title: tempItem.Title,
  2067. Value: tempItem.Value,
  2068. TableValue: tempItem.TableValue,
  2069. ChartPermissionId: tempItem.ChartPermissionId,
  2070. }
  2071. rowServices = append(rowServices, cs)
  2072. tempItem = serviceTempNameMap[parentName]
  2073. if tempItem == nil {
  2074. resp.Fail(fmt.Sprintf("第%d行套餐名称不匹配, 请按模板导入", i+1), c)
  2075. return
  2076. }
  2077. cs = &fms.ContractService{
  2078. ProductId: crm.CompanyProductRai,
  2079. ServiceTemplateId: tempItem.ServiceTemplateId,
  2080. Title: tempItem.Title,
  2081. Value: tempItem.Value,
  2082. TableValue: tempItem.TableValue,
  2083. ChartPermissionId: tempItem.ChartPermissionId,
  2084. }
  2085. rowServices = append(rowServices, cs)
  2086. } else {
  2087. rootName := "行业套餐"
  2088. // 新增三条套餐信息
  2089. parentName, _ := raiIndustryMap[k]
  2090. childName := titleMap[k]
  2091. fullName := childName+"_"+parentName+"_"+rootName
  2092. if fullName != "" {
  2093. //增加三级权限
  2094. tempItem := serviceTempFullNameMap[fullName]
  2095. if tempItem == nil {
  2096. resp.Fail(fmt.Sprintf("第%d行套餐名称不匹配, 请按模板导入", i+1), c)
  2097. return
  2098. }
  2099. cs := &fms.ContractService{
  2100. ProductId: crm.CompanyProductRai,
  2101. ServiceTemplateId: tempItem.ServiceTemplateId,
  2102. Title: tempItem.Title,
  2103. Value: tempItem.Value,
  2104. TableValue: tempItem.TableValue,
  2105. ChartPermissionId: tempItem.ChartPermissionId,
  2106. }
  2107. rowServices = append(rowServices, cs)
  2108. //增加二级权限
  2109. tempItem = serviceTempFullNameMap[parentName+"_"+rootName]
  2110. if tempItem == nil {
  2111. resp.Fail(fmt.Sprintf("第%d行套餐名称不匹配, 请按模板导入", i+1), c)
  2112. return
  2113. }
  2114. cs = &fms.ContractService{
  2115. ProductId: crm.CompanyProductRai,
  2116. ServiceTemplateId: tempItem.ServiceTemplateId,
  2117. Title: tempItem.Title,
  2118. Value: tempItem.Value,
  2119. TableValue: tempItem.TableValue,
  2120. ChartPermissionId: tempItem.ChartPermissionId,
  2121. }
  2122. rowServices = append(rowServices, cs)
  2123. //增加一级权限
  2124. tempItem = serviceTempFullNameMap[rootName]
  2125. if tempItem == nil {
  2126. resp.Fail(fmt.Sprintf("第%d行套餐名称不匹配, 请按模板导入", i+1), c)
  2127. return
  2128. }
  2129. cs = &fms.ContractService{
  2130. ProductId: crm.CompanyProductRai,
  2131. ServiceTemplateId: tempItem.ServiceTemplateId,
  2132. Title: tempItem.Title,
  2133. Value: tempItem.Value,
  2134. TableValue: tempItem.TableValue,
  2135. ChartPermissionId: tempItem.ChartPermissionId,
  2136. }
  2137. rowServices = append(rowServices, cs)
  2138. }
  2139. }
  2140. productIds[crm.CompanyProductRai] = struct{}{}
  2141. }
  2142. continue
  2143. }
  2144. // 权益研选等套餐
  2145. if k >= 45 && k<=49 {
  2146. if v == "是" {
  2147. tempItem := serviceTempNameMap[titleMap[k]]
  2148. if tempItem == nil {
  2149. resp.Fail(fmt.Sprintf("第%d行套餐名称不匹配, 请按模板导入", i+1), c)
  2150. return
  2151. }
  2152. cs := &fms.ContractService{
  2153. ProductId: crm.CompanyProductRai,
  2154. ServiceTemplateId: tempItem.ServiceTemplateId,
  2155. Title: tempItem.Title,
  2156. Value: tempItem.Value,
  2157. TableValue: tempItem.TableValue,
  2158. ChartPermissionId: tempItem.ChartPermissionId,
  2159. }
  2160. cs.Set()
  2161. rowServices = append(rowServices, cs)
  2162. productIds[crm.CompanyProductRai] = struct{}{}
  2163. }
  2164. continue
  2165. }
  2166. // 套餐备注
  2167. if k == 50 {
  2168. rowRegister.ServiceRemark = v
  2169. continue
  2170. }
  2171. // 开始时间/到期时间
  2172. if k == 51 {
  2173. // 转换失败可能是因为格式为Excel日期格式, 读取出来会是一串数字, 将其转换成日期字符串再处理
  2174. va := cell.Value
  2175. if va == "" {
  2176. resp.Fail(fmt.Sprintf("第%d行开始时间不可为空, 请按模板导入", i+1), c)
  2177. return
  2178. }
  2179. startDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  2180. if e != nil {
  2181. d := utils.ConvertToFormatDay(va, "2006/01/02")
  2182. startDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  2183. if e != nil {
  2184. resp.Fail(fmt.Sprintf("第%d行开始时间格式转换有误, 请按模板导入", i+1), c)
  2185. return
  2186. }
  2187. }
  2188. // 转换后的日期小于1900-01-01表示当前生成的日期是有问题的
  2189. if startDate.Before(checkDate) {
  2190. resp.Fail(fmt.Sprintf("第%d行开始时间格式有误, 请按模板导入", i+1), c)
  2191. return
  2192. }
  2193. rowRegister.StartDate = startDate
  2194. continue
  2195. }
  2196. if k == 52 {
  2197. va := cell.Value
  2198. if va == "" {
  2199. resp.Fail(fmt.Sprintf("第%d行到期时间不可为空, 请按模板导入", i+1), c)
  2200. return
  2201. }
  2202. endDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  2203. if e != nil {
  2204. d := utils.ConvertToFormatDay(va, "2006/01/02")
  2205. endDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  2206. if e != nil {
  2207. resp.Fail(fmt.Sprintf("第%d行到期时间格式转换有误, 请按模板导入", i+1), c)
  2208. return
  2209. }
  2210. }
  2211. if endDate.Before(checkDate) {
  2212. resp.Fail(fmt.Sprintf("第%d行到期时间格式有误, 请按模板导入", i+1), c)
  2213. return
  2214. }
  2215. rowRegister.EndDate = endDate
  2216. continue
  2217. }
  2218. // 合同金额
  2219. if k == 53 {
  2220. amountStr := v
  2221. amount, e := strconv.ParseFloat(amountStr, 64)
  2222. if e != nil {
  2223. resp.Fail(fmt.Sprintf("第%d行合同金额有误, 请按模板导入", i+1), c)
  2224. return
  2225. }
  2226. rowRegister.ContractAmount = amount
  2227. continue
  2228. }
  2229. // 金额单位
  2230. if k == 54 {
  2231. rate := rateMap[v]
  2232. if rate <= 0 {
  2233. resp.Fail(fmt.Sprintf("第%d行金额单位有误, 请按模板导入", i+1), c)
  2234. return
  2235. }
  2236. rowRegister.CurrencyUnit = v
  2237. rowRegister.RMBRate = rate
  2238. continue
  2239. }
  2240. // todo FICC套餐总金额
  2241. if k == 55 {
  2242. amountStr := v
  2243. amount, e := strconv.ParseFloat(amountStr, 64)
  2244. if e != nil {
  2245. resp.Fail(fmt.Sprintf("第%d行FICC套餐总金额有误, 请按模板导入", i+1), c)
  2246. return
  2247. }
  2248. if amount > 0 {
  2249. tmp := &fms.ContractServiceAmount{
  2250. ProductId: crm.CompanyProductFicc,
  2251. ServiceAmount: amount,
  2252. CurrencyUnit: rowRegister.CurrencyUnit,
  2253. TimeBase: base.TimeBase{},
  2254. }
  2255. rowServiceAmount = append(rowServiceAmount, tmp)
  2256. }
  2257. continue
  2258. }
  2259. // todo 权益套餐总金额
  2260. if k == 56 {
  2261. amountStr := v
  2262. amount, e := strconv.ParseFloat(amountStr, 64)
  2263. if e != nil {
  2264. resp.Fail(fmt.Sprintf("第%d行权益套餐总金额有误, 请按模板导入", i+1), c)
  2265. return
  2266. }
  2267. if amount > 0 {
  2268. tmp := &fms.ContractServiceAmount{
  2269. ProductId: crm.CompanyProductRai,
  2270. ServiceAmount: amount,
  2271. CurrencyUnit: rowRegister.CurrencyUnit,
  2272. TimeBase: base.TimeBase{},
  2273. }
  2274. rowServiceAmount = append(rowServiceAmount, tmp)
  2275. }
  2276. continue
  2277. }
  2278. // 约定付款日期
  2279. if k == 57 {
  2280. rowRegister.AgreedPayTime = v
  2281. continue
  2282. }
  2283. // 签订日
  2284. if k == 58 {
  2285. va := cell.Value
  2286. if va == "" {
  2287. continue
  2288. }
  2289. signDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  2290. if e != nil {
  2291. d := utils.ConvertToFormatDay(va, "2006/01/02")
  2292. signDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  2293. if e != nil {
  2294. resp.Fail(fmt.Sprintf("第%d行签订日格式转换有误, 请按模板导入", i+1), c)
  2295. return
  2296. }
  2297. }
  2298. if signDate.Before(checkDate) {
  2299. resp.Fail(fmt.Sprintf("第%d行签订日格式有误, 请按模板导入", i+1), c)
  2300. return
  2301. }
  2302. rowRegister.SignDate = signDate
  2303. continue
  2304. }
  2305. // 合同状态
  2306. if k == 59 {
  2307. rowRegister.ContractStatus = fms.ContractStatusNameKeyMap[v]
  2308. if rowRegister.ContractStatus == 0 {
  2309. resp.Fail(fmt.Sprintf("第%d行合同状态不匹配, 请按模板导入", i+1), c)
  2310. return
  2311. }
  2312. continue
  2313. }
  2314. // 合同编号
  2315. if k == 60 {
  2316. rowContractCode := v
  2317. if rowContractCode == "" {
  2318. resp.Fail(fmt.Sprintf("第%d行合同编号不可为空, 请按模板导入", i+1), c)
  2319. return
  2320. }
  2321. if utils.InArrayByStr(contractCodeArr, rowContractCode) {
  2322. // 此合同已登记, 跳过本行的读取
  2323. isSkip = true
  2324. break
  2325. }
  2326. rowRegister.ContractCode = rowContractCode
  2327. continue
  2328. }
  2329. // 合规备注
  2330. if k == 61 {
  2331. rowRegister.Remark = v
  2332. continue
  2333. }
  2334. // 开票列表
  2335. k2 := 62
  2336. for ir := 0; ir < invoiceMax; ir++ {
  2337. n := ir + 1
  2338. // 开票日
  2339. k2 += 1
  2340. if k == k2 {
  2341. if v != "" {
  2342. va := cell.Value
  2343. invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  2344. if e != nil {
  2345. d := utils.ConvertToFormatDay(va, "2006/01/02")
  2346. invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  2347. if e != nil {
  2348. resp.Fail(fmt.Sprintf("第%d行开票时间%d格式转换有误, 请按模板导入", i+1, n), c)
  2349. return
  2350. }
  2351. }
  2352. if invoiceDate.Before(checkDate) {
  2353. resp.Fail(fmt.Sprintf("第%d行开票时间%d格式有误, 请按模板导入", i+1, n), c)
  2354. return
  2355. }
  2356. rowInvoices[ir].InvoiceDate = invoiceDate
  2357. rowInvoices[ir].ContractCode = rowRegister.ContractCode
  2358. rowInvoices[ir].InvoiceType = fms.ContractInvoiceTypeMake
  2359. }
  2360. continue
  2361. }
  2362. // 开票金额
  2363. k2 += 1
  2364. if k == k2 {
  2365. if v != "" {
  2366. amountStr := v
  2367. amount, e := strconv.ParseFloat(amountStr, 64)
  2368. if e != nil {
  2369. resp.Fail(fmt.Sprintf("第%d行开票金额%d有误, 请按模板导入", i+1, n), c)
  2370. return
  2371. }
  2372. if rowRegister.RMBRate <= 0 {
  2373. resp.Fail(fmt.Sprintf("第%d行开票金额换算%d有误, 请按模板导入金额单位", i+1, n), c)
  2374. return
  2375. }
  2376. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", amount/rowRegister.RMBRate), 64)
  2377. rowInvoices[ir].OriginAmount = amount
  2378. rowInvoices[ir].Amount = a
  2379. }
  2380. continue
  2381. }
  2382. // todo 开票套餐类型
  2383. k2 += 1
  2384. if k == k2 {
  2385. if v != "" {
  2386. }
  2387. continue
  2388. }
  2389. // 开票销售
  2390. k2 += 1
  2391. if k == k2 {
  2392. if v != "" {
  2393. sellerItem := sellerMap[v]
  2394. if sellerItem == nil {
  2395. resp.Fail(fmt.Sprintf("第%d行开票销售名称与系统销售不匹配, 请核对名称后导入", i+1), c)
  2396. return
  2397. }
  2398. rowInvoices[ir].SellerId = sellerItem.SellerId
  2399. rowInvoices[ir].SellerName = sellerItem.SellerName
  2400. rowInvoices[ir].SellerGroupId = sellerItem.GroupId
  2401. rowInvoices[ir].SellerGroupName = sellerItem.GroupName
  2402. rowInvoices[ir].SellerTeamId = sellerItem.TeamId
  2403. rowInvoices[ir].SellerTeamName = sellerItem.TeamName
  2404. }
  2405. continue
  2406. }
  2407. // 备注
  2408. k2 += 1
  2409. if k == k2 {
  2410. if v != "" {
  2411. rowInvoices[ir].Remark = v
  2412. }
  2413. continue
  2414. }
  2415. }
  2416. // 到款列表
  2417. for ir := 0; ir < invoiceMax; ir++ {
  2418. n := ir + 1
  2419. // 到款日
  2420. k2 += 1
  2421. if k == k2 {
  2422. if v != "" {
  2423. va := cell.Value
  2424. invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  2425. if e != nil {
  2426. d := utils.ConvertToFormatDay(va, "2006/01/02")
  2427. invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  2428. if e != nil {
  2429. resp.Fail(fmt.Sprintf("第%d行到款时间%d格式转换有误, 请按模板导入", i+1, n), c)
  2430. return
  2431. }
  2432. }
  2433. if invoiceDate.Before(checkDate) {
  2434. resp.Fail(fmt.Sprintf("第%d行到款时间%d格式有误, 请按模板导入", i+1, n), c)
  2435. return
  2436. }
  2437. rowPayments[ir].InvoiceDate = invoiceDate
  2438. rowPayments[ir].ContractCode = rowRegister.ContractCode
  2439. rowPayments[ir].InvoiceType = fms.ContractInvoiceTypePay
  2440. }
  2441. continue
  2442. }
  2443. // 到款金额
  2444. k2 += 1
  2445. if k == k2 {
  2446. if v != "" {
  2447. amountStr := v
  2448. amount, e := strconv.ParseFloat(amountStr, 64)
  2449. if e != nil {
  2450. resp.Fail(fmt.Sprintf("第%d行到款金额%d有误, 请按模板导入", i+1, n), c)
  2451. return
  2452. }
  2453. if rowRegister.RMBRate <= 0 {
  2454. resp.Fail(fmt.Sprintf("第%d行到款金额换算%d有误, 请按模板导入金额单位", i+1, n), c)
  2455. return
  2456. }
  2457. // 付款方式
  2458. dayDiff := rowRegister.EndDate.Sub(rowRegister.StartDate).Hours() / 24
  2459. contractAmount := rowRegister.ContractAmount
  2460. payType := fmsService.CalculateContractPaymentType(amount, contractAmount, dayDiff)
  2461. rowPayments[ir].PayType = payType
  2462. rowPayments[ir].OriginAmount = amount
  2463. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", amount/rowRegister.RMBRate), 64)
  2464. rowPayments[ir].Amount = a
  2465. }
  2466. continue
  2467. }
  2468. // todo 到款套餐类型
  2469. k2 += 1
  2470. if k == k2 {
  2471. if v != "" {
  2472. }
  2473. continue
  2474. }
  2475. // 备注
  2476. k2 += 1
  2477. if k == k2 {
  2478. if v != "" {
  2479. rowPayments[ir].Remark = v
  2480. }
  2481. continue
  2482. }
  2483. }
  2484. }
  2485. if isSkip {
  2486. continue
  2487. }
  2488. // 小套餐
  2489. if len(rowChartPermissionIdArr) > 0 {
  2490. // 说明有小套餐
  2491. tempItem := serviceTempNameMap["FICC小套餐"]
  2492. if tempItem == nil {
  2493. resp.Fail(fmt.Sprintf("第%d行小套餐名称不匹配, 请按模板导入", i+1), c)
  2494. return
  2495. }
  2496. rowChartPermissionIds := strings.Join(rowChartPermissionIdArr, ",")
  2497. cs := &fms.ContractService{
  2498. ProductId: crm.CompanyProductFicc,
  2499. ServiceTemplateId: tempItem.ServiceTemplateId,
  2500. Title: tempItem.Title,
  2501. Value: tempItem.Value,
  2502. TableValue: tempItem.TableValue,
  2503. ChartPermissionId: tempItem.ChartPermissionId,
  2504. ChartPermissionIds: rowChartPermissionIds,
  2505. }
  2506. cs.Set()
  2507. rowServices = append(rowServices, cs)
  2508. }
  2509. // 如果导入的最后一条合同编号为空并且合同编号之后的字段均为空
  2510. // excel这个包读行的时候不会再往后面没数据的地方读取, 所以此处需要重新判断一次
  2511. if rowRegister.ContractCode == "" {
  2512. resp.Fail(fmt.Sprintf("第%d行合同编号为空", i+1), c)
  2513. return
  2514. }
  2515. rowRegister.RegisterStatus = fms.ContractRegisterStatusIng
  2516. rowInvoiceList := make([]*fms.ContractInvoice, 0)
  2517. // 过滤信息不完整的开票到款
  2518. for l := range rowInvoices {
  2519. if rowInvoices[l].ContractCode != "" {
  2520. if rowInvoices[l].OriginAmount <= 0 || rowInvoices[l].SellerId == 0 {
  2521. resp.Fail(fmt.Sprintf("第%d行开票信息必填项不完整", i+1), c)
  2522. return
  2523. }
  2524. rowInvoiceList = append(rowInvoiceList, rowInvoices[l])
  2525. }
  2526. }
  2527. for l := range rowPayments {
  2528. if rowPayments[l].ContractCode != "" {
  2529. if rowPayments[l].OriginAmount <= 0 {
  2530. resp.Fail(fmt.Sprintf("第%d行到款信息必填项不完整", i+1), c)
  2531. return
  2532. }
  2533. rowInvoiceList = append(rowInvoiceList, rowPayments[l])
  2534. }
  2535. }
  2536. // 新增登记、套餐、开票到款信息
  2537. for proId, _ := range productIds {
  2538. rowRegister.ProductIds += strconv.Itoa(proId)+","
  2539. }
  2540. rowRegister.ProductIds = strings.Trim(rowRegister.ProductIds, ",")
  2541. newId, e := fms.CreateImportContractRegister(rowRegister, rowServices, rowInvoiceList, rowServiceAmount)
  2542. if e != nil {
  2543. resp.FailData(fmt.Sprintf("第%d行导入失败", i+1), "新增导入登记失败, Err: "+e.Error(), c)
  2544. return
  2545. }
  2546. newIds = append(newIds, newId)
  2547. }
  2548. }
  2549. }
  2550. resp.Ok("操作成功", c)
  2551. }
  2552. // CurrencyList
  2553. // @Title 货币单位列表
  2554. // @Description 货币单位列表
  2555. // @Success 200 {object} fms.CurrencyUnitItem
  2556. // @router /contract/register/currency_list [get]
  2557. func (rg *RegisterController) CurrencyList(c *gin.Context) {
  2558. list, e := fmsService.GetTodayCurrencyRateList()
  2559. if e != nil {
  2560. resp.FailData("获取失败", "获取今日货币汇率列表失败, Err: "+e.Error(), c)
  2561. return
  2562. }
  2563. resp.OkData("获取成功", list, c)
  2564. }