register.go 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
  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. fmsService "hongze/fms_api/services/fms"
  18. "hongze/fms_api/utils"
  19. "net/http"
  20. "os"
  21. "path"
  22. "strconv"
  23. "strings"
  24. "time"
  25. )
  26. // RegisterController 合同登记
  27. type RegisterController struct{}
  28. // List
  29. // @Title 合同登记列表
  30. // @Description 合同登记列表
  31. // @Param Keyword query string false "关键词"
  32. // @Param StartDate query string false "合同开始日期"
  33. // @Param EndDate query string false "合同结束日期"
  34. // @Param ServiceType query int false "套餐类型"
  35. // @Param ContractType query int false "合同类型"
  36. // @Param RegisterStatus query int false "登记状态"
  37. // @Success 200 {object} fms.ContractRegisterItem
  38. // @router /contract/register/list [get]
  39. func (rg *RegisterController) List(c *gin.Context) {
  40. var req fms.ContractRegisterListReq
  41. if e := c.BindQuery(&req); e != nil {
  42. err, ok := e.(validator.ValidationErrors)
  43. if !ok {
  44. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  45. return
  46. }
  47. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  48. return
  49. }
  50. cond := `1 = 1`
  51. pars := make([]interface{}, 0)
  52. // 合同编号/客户姓名/销售/实际使用方
  53. if req.Keyword != "" {
  54. kw := "%" + req.Keyword + "%"
  55. cond += ` AND (company_name LIKE ? OR contract_code LIKE ? OR seller_name LIKE ? OR actual_company_name LIKE ?)`
  56. pars = append(pars, kw, kw, kw, kw)
  57. }
  58. if req.StartDate != "" && req.EndDate != "" {
  59. st := fmt.Sprint(req.StartDate, " 00:00:00")
  60. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  61. cond += ` AND (create_time BETWEEN ? AND ?)`
  62. pars = append(pars, st, ed)
  63. }
  64. if req.ContractType != 0 {
  65. cond += ` AND contract_type = ?`
  66. pars = append(pars, req.ContractType)
  67. }
  68. if req.RegisterStatus != 0 {
  69. cond += ` AND register_status = ?`
  70. pars = append(pars, req.RegisterStatus)
  71. }
  72. // 套餐筛选
  73. if req.ServiceType != 0 {
  74. registerIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
  75. if e != nil {
  76. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  77. return
  78. }
  79. if len(registerIds) > 0 {
  80. cond += ` AND contract_register_id IN ?`
  81. pars = append(pars, registerIds)
  82. } else {
  83. cond += ` AND 1 = 2`
  84. }
  85. }
  86. page := new(base.Page)
  87. page.SetPageSize(req.PageSize)
  88. page.SetCurrent(req.Current)
  89. page.AddOrderItem(base.OrderItem{Column: "create_time", Asc: false})
  90. total, list, e := fms.GetContractRegisterItemPageList(page, cond, pars)
  91. if e != nil {
  92. resp.FailMsg("获取失败", "获取合同登记列表失败, Err: "+e.Error(), c)
  93. return
  94. }
  95. registerIds := make([]int, 0)
  96. for i := range list {
  97. registerIds = append(registerIds, list[i].ContractRegisterId)
  98. }
  99. serviceMap := make(map[int]string, 0)
  100. invoiceMap := make(map[int][]*fms.ContractInvoiceItem, 0)
  101. paymentMap := make(map[int][]*fms.ContractInvoiceItem, 0)
  102. if len(registerIds) > 0 {
  103. // 获取服务套餐
  104. servicesNameList, e := fms.GetContractRegisterServicesNameByRegisterIds(registerIds)
  105. if e != nil {
  106. resp.FailMsg("获取失败", "获取套餐拼接字符串失败, Err: "+e.Error(), c)
  107. return
  108. }
  109. for i := range servicesNameList {
  110. serviceMap[servicesNameList[i].ContractRegisterId] = servicesNameList[i].ServicesName
  111. }
  112. // 获取开票/到款列表
  113. invoiceCond := `contract_register_id IN ?`
  114. invoicePars := make([]interface{}, 0)
  115. invoicePars = append(invoicePars, registerIds)
  116. invoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  117. if e != nil {
  118. resp.FailMsg("获取失败", "获取开票/到款列表失败, Err: "+e.Error(), c)
  119. return
  120. }
  121. for i := range invoiceList {
  122. if invoiceMap[invoiceList[i].ContractRegisterId] == nil {
  123. invoiceMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoiceItem, 0)
  124. }
  125. if paymentMap[invoiceList[i].ContractRegisterId] == nil {
  126. paymentMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoiceItem, 0)
  127. }
  128. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  129. invoiceMap[invoiceList[i].ContractRegisterId] = append(invoiceMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  130. }
  131. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  132. paymentMap[invoiceList[i].ContractRegisterId] = append(paymentMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  133. }
  134. }
  135. }
  136. respList := make([]*fms.ContractRegisterList, 0)
  137. for i := range list {
  138. v := new(fms.ContractRegisterList)
  139. v.ContractRegisterItem = list[i]
  140. v.ServicesName = serviceMap[list[i].ContractRegisterId]
  141. v.InvoiceList = invoiceMap[list[i].ContractRegisterId]
  142. v.PaymentList = paymentMap[list[i].ContractRegisterId]
  143. respList = append(respList, v)
  144. }
  145. page.SetTotal(total)
  146. baseData := new(base.BaseData)
  147. baseData.SetPage(page)
  148. baseData.SetList(respList)
  149. resp.OkData("获取成功", baseData, c)
  150. }
  151. // Add
  152. // @Title 新增合同登记
  153. // @Description 新增合同登记
  154. // @Param request body fms.ContractRegisterAddReq true "type json string"
  155. // @Success 200 string "操作成功"
  156. // @router /contract/register/add [post]
  157. func (rg *RegisterController) Add(c *gin.Context) {
  158. req := new(fms.ContractRegisterAddReq)
  159. err := c.ShouldBind(&req)
  160. if err != nil {
  161. errs, ok := err.(validator.ValidationErrors)
  162. if !ok {
  163. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  164. return
  165. }
  166. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  167. return
  168. }
  169. claims, _ := c.Get("adminInfo")
  170. adminInfo := claims.(*system.SysAdmin)
  171. // 日期校验
  172. startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  173. if e != nil {
  174. resp.FailMsg("合同开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
  175. return
  176. }
  177. endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  178. if e != nil {
  179. resp.FailMsg("合同结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
  180. return
  181. }
  182. signDate, _ := time.ParseInLocation(utils.FormatDate, "0000-00-00", time.Local)
  183. if req.SignDate != "" {
  184. signDateTime, e := time.ParseInLocation(utils.FormatDate, req.SignDate, time.Local)
  185. if e != nil {
  186. resp.FailMsg("合同签订日期格式有误", "合同签订日期格式有误, Err: "+e.Error(), c)
  187. return
  188. }
  189. signDate = signDateTime
  190. }
  191. // 是否存在相同合同编号的登记
  192. ob := new(fms.ContractRegister)
  193. existCond := `contract_code = ?`
  194. existPars := make([]interface{}, 0)
  195. existPars = append(existPars, req.ContractCode)
  196. exist, e := ob.FetchByCondition(existCond, existPars)
  197. if e != nil && e != utils.ErrNoRow {
  198. resp.FailMsg("操作失败", "获取相同登记号失败, Err: "+e.Error(), c)
  199. return
  200. }
  201. if exist != nil && exist.ContractRegisterId > 0 {
  202. resp.Fail("合同编号已存在", c)
  203. return
  204. }
  205. nowTime := time.Now().Local()
  206. ob.ContractCode = req.ContractCode
  207. ob.RelateContractCode = req.RelateContractCode
  208. ob.CrmContractId = req.CrmContractId
  209. ob.ContractSource = req.ContractSource
  210. ob.CompanyName = req.CompanyName
  211. ob.ActualCompanyName = req.ActualCompanyName
  212. ob.ProductId = req.ProductId
  213. ob.SellerId = req.SellerId
  214. ob.SellerName = req.SellerName
  215. ob.ContractType = req.ContractType
  216. ob.ContractAmount = req.ContractAmount
  217. ob.StartDate = startDate
  218. ob.EndDate = endDate
  219. ob.SignDate = signDate
  220. ob.AgreedPayTime = req.AgreedPayTime
  221. ob.ContractStatus = req.ContractStatus
  222. ob.RegisterStatus = fms.ContractRegisterStatusIng
  223. ob.Remark = req.Remark
  224. ob.ServiceRemark = req.ServiceRemark
  225. ob.HasPayment = req.HasPayment
  226. ob.Set()
  227. // 存在代付的直接完成登记, 且不允许进行开票/到款登记
  228. if req.HasPayment == 1 {
  229. ob.RegisterStatus = fms.ContractRegisterStatusComplete
  230. }
  231. // 套餐信息
  232. serviceList, e := fmsService.HandleContractServiceAndDetail(req.ProductId, req.Services, true)
  233. if e != nil {
  234. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  235. return
  236. }
  237. // 新增合同及套餐
  238. if e = fms.CreateContractRegisterAndServices(ob, serviceList); e != nil {
  239. resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c)
  240. return
  241. }
  242. // 操作日志
  243. go func() {
  244. opData := ""
  245. opDataByte, e := json.Marshal(req)
  246. if e != nil {
  247. return
  248. }
  249. opData = string(opDataByte)
  250. logItem := new(fms.ContractRegisterLog)
  251. logItem.ContractRegisterId = ob.ContractRegisterId
  252. logItem.AdminId = int(adminInfo.AdminId)
  253. logItem.AdminName = adminInfo.RealName
  254. logItem.OpData = opData
  255. logItem.OpType = fms.ContractRegisterOpTypeSave
  256. logItem.CreateTime = nowTime
  257. logItem.Remark = req.Remark
  258. if e = logItem.Create(); e != nil {
  259. return
  260. }
  261. }()
  262. resp.Ok("操作成功", c)
  263. }
  264. // Edit
  265. // @Title 编辑合同登记
  266. // @Description 编辑合同登记
  267. // @Param request body fms.ContractRegisterEditReq true "type json string"
  268. // @Success 200 string "操作成功"
  269. // @router /contract/register/edit [post]
  270. func (rg *RegisterController) Edit(c *gin.Context) {
  271. req := new(fms.ContractRegisterEditReq)
  272. err := c.ShouldBind(&req)
  273. if err != nil {
  274. errs, ok := err.(validator.ValidationErrors)
  275. if !ok {
  276. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  277. return
  278. }
  279. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  280. return
  281. }
  282. claims, _ := c.Get("adminInfo")
  283. adminInfo := claims.(*system.SysAdmin)
  284. // 日期校验
  285. startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  286. if e != nil {
  287. resp.FailMsg("合同开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
  288. return
  289. }
  290. endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  291. if e != nil {
  292. resp.FailMsg("合同结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
  293. return
  294. }
  295. signDate, _ := time.ParseInLocation(utils.FormatDate, "0000-00-00", time.Local)
  296. if req.SignDate != "" {
  297. signDateTime, e := time.ParseInLocation(utils.FormatDate, req.SignDate, time.Local)
  298. if e != nil {
  299. resp.FailMsg("合同签订日期格式有误", "合同签订日期格式有误, Err: "+e.Error(), c)
  300. return
  301. }
  302. signDate = signDateTime
  303. }
  304. ob := new(fms.ContractRegister)
  305. item, e := ob.Fetch(req.ContractRegisterId)
  306. if e != nil {
  307. if e == utils.ErrNoRow {
  308. resp.Fail("登记记录不存在或已被删除", c)
  309. return
  310. }
  311. resp.FailMsg("操作失败", "获取合同登记信息失败, Err:"+e.Error(), c)
  312. return
  313. }
  314. // 是否存在相同合同编号的登记
  315. existCond := `contract_code = ?`
  316. existPars := make([]interface{}, 0)
  317. existPars = append(existPars, req.ContractCode)
  318. exist, e := ob.FetchByCondition(existCond, existPars)
  319. if e != nil && e != utils.ErrNoRow {
  320. resp.FailMsg("操作失败", "获取相同登记号失败, Err: "+e.Error(), c)
  321. return
  322. }
  323. if exist != nil && exist.ContractRegisterId > 0 && exist.ContractRegisterId != item.ContractRegisterId {
  324. resp.Fail("合同编号已存在", c)
  325. return
  326. }
  327. originHasPayment := item.HasPayment
  328. updateCols := []string{
  329. "ContractCode", "RelateContractCode", "CrmContractId", "ContractSource", "CompanyName", "ActualCompanyName",
  330. "SellerId", "SellerName", "ContractType", "ContractAmount", "StartDate", "EndDate", "SignDate", "AgreedPayTime",
  331. "ContractStatus", "RegisterStatus", "Remark", "ServiceRemark", "HasPayment", "ModifyTime",
  332. }
  333. nowTime := time.Now().Local()
  334. item.ContractCode = req.ContractCode
  335. item.RelateContractCode = req.RelateContractCode
  336. item.CrmContractId = req.CrmContractId
  337. item.ContractSource = req.ContractSource
  338. item.CompanyName = req.CompanyName
  339. item.ActualCompanyName = req.ActualCompanyName
  340. item.SellerId = req.SellerId
  341. item.SellerName = req.SellerName
  342. item.ContractType = req.ContractType
  343. item.ContractAmount = req.ContractAmount
  344. item.StartDate = startDate
  345. item.EndDate = endDate
  346. item.SignDate = signDate
  347. item.AgreedPayTime = req.AgreedPayTime
  348. item.ContractStatus = req.ContractStatus
  349. item.RegisterStatus = fms.ContractRegisterStatusIng
  350. item.Remark = req.Remark
  351. item.ServiceRemark = req.ServiceRemark
  352. item.HasPayment = req.HasPayment
  353. item.ModifyTime = nowTime
  354. // 存在代付的直接完成登记, 且不允许进行开票/到款登记
  355. if req.HasPayment == 1 {
  356. item.RegisterStatus = fms.ContractRegisterStatusComplete
  357. updateCols = append(updateCols, "RegisterStatus")
  358. }
  359. // 套餐信息
  360. serviceList, e := fmsService.HandleContractServiceAndDetail(req.ProductId, req.Services, true)
  361. if e != nil {
  362. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  363. return
  364. }
  365. // 更新合同及套餐
  366. if e = fms.UpdateContractRegisterAndServices(item, updateCols, serviceList); e != nil {
  367. resp.FailMsg("操作失败", "更新合同及套餐失败, Err: "+e.Error(), c)
  368. return
  369. }
  370. // 若从无代付修改为有代付, 则删除无代付期间新增的所有开票/到款登记(此情况并不经常出现, 但是可能会存在这种操作)
  371. if originHasPayment == 0 && req.HasPayment == 1 {
  372. go func() {
  373. if e = fms.DeleteContractInvoicesByRegisterId(item.ContractRegisterId); e != nil {
  374. alarm_msg.SendAlarmMsg("无代付修改为有代付, 删除开票到款记录失败, ErrMsg: "+err.Error(), 3)
  375. }
  376. }()
  377. }
  378. // 校验金额-是否修改状态
  379. go fmsService.CheckContractRegisterAmount(item.ContractRegisterId)
  380. // 操作日志
  381. go func() {
  382. opData := ""
  383. opDataByte, e := json.Marshal(req)
  384. if e != nil {
  385. return
  386. }
  387. opData = string(opDataByte)
  388. logItem := new(fms.ContractRegisterLog)
  389. logItem.ContractRegisterId = item.ContractRegisterId
  390. logItem.AdminId = int(adminInfo.AdminId)
  391. logItem.AdminName = adminInfo.RealName
  392. logItem.OpData = opData
  393. logItem.OpType = fms.ContractRegisterOpTypeEdit
  394. logItem.CreateTime = nowTime
  395. logItem.Remark = req.Remark
  396. if e = logItem.Create(); e != nil {
  397. return
  398. }
  399. }()
  400. resp.Ok("操作成功", c)
  401. }
  402. // Del
  403. // @Title 删除合同登记
  404. // @Description 删除合同登记
  405. // @Param request body fms.ContractRegisterDelReq true "type json string"
  406. // @Success 200 string "操作成功"
  407. // @router /contract/register/del [post]
  408. func (rg *RegisterController) Del(c *gin.Context) {
  409. req := new(fms.ContractRegisterDelReq)
  410. err := c.ShouldBind(&req)
  411. if err != nil {
  412. errs, ok := err.(validator.ValidationErrors)
  413. if !ok {
  414. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  415. return
  416. }
  417. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  418. return
  419. }
  420. claims, _ := c.Get("adminInfo")
  421. adminInfo := claims.(*system.SysAdmin)
  422. ob := new(fms.ContractRegister)
  423. item, e := ob.Fetch(req.ContractRegisterId)
  424. if e != nil {
  425. if e == utils.ErrNoRow {
  426. resp.Fail("合同登记不存在或已被删除", c)
  427. return
  428. }
  429. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  430. return
  431. }
  432. nowTime := time.Now().Local()
  433. item.IsDeleted = 1
  434. item.ModifyTime = nowTime
  435. updateCols := []string{"IsDeleted", "ModifyTime"}
  436. if e = item.Update(updateCols); e != nil {
  437. resp.FailMsg("操作失败", "更新合同登记失败, Err:"+e.Error(), c)
  438. return
  439. }
  440. // 删除对应的开票到款登记
  441. go func() {
  442. if e = fms.DeleteContractInvoicesByRegisterId(item.ContractRegisterId); e != nil {
  443. alarm_msg.SendAlarmMsg("删除合同登记后, 删除开票到款记录失败, ErrMsg: "+err.Error(), 3)
  444. }
  445. }()
  446. // 操作日志
  447. go func() {
  448. opData := ""
  449. opDataByte, e := json.Marshal(req)
  450. if e != nil {
  451. return
  452. }
  453. opData = string(opDataByte)
  454. logItem := new(fms.ContractRegisterLog)
  455. logItem.ContractRegisterId = req.ContractRegisterId
  456. logItem.AdminId = int(adminInfo.AdminId)
  457. logItem.AdminName = adminInfo.RealName
  458. logItem.OpData = opData
  459. logItem.OpType = fms.ContractRegisterOpTypeDel
  460. logItem.CreateTime = nowTime
  461. if e = logItem.Create(); e != nil {
  462. return
  463. }
  464. }()
  465. resp.Ok("操作成功", c)
  466. }
  467. // Detail
  468. // @Title 合同登记详情
  469. // @Description 合同登记详情
  470. // @Param ContractRegisterId query int false "合同登记ID"
  471. // @Success 200 {object} fms.ContractRegisterDetail
  472. // @router /contract/register/detail [get]
  473. func (rg *RegisterController) Detail(c *gin.Context) {
  474. var req fms.ContractRegisterDetailReq
  475. if e := c.BindQuery(&req); e != nil {
  476. err, ok := e.(validator.ValidationErrors)
  477. if !ok {
  478. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  479. return
  480. }
  481. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  482. return
  483. }
  484. result := new(fms.ContractRegisterDetail)
  485. // 合同登记信息
  486. item, e := fms.GetContractRegisterItemById(req.ContractRegisterId)
  487. if e != nil {
  488. resp.FailData("获取失败", "获取合同登记详情失败, Err:"+e.Error(), c)
  489. return
  490. }
  491. result.ContractRegisterItem = item
  492. // 套餐信息
  493. serviceList, e := fmsService.GetContractServiceAndDetail(req.ContractRegisterId)
  494. if e != nil {
  495. resp.FailData("获取失败", "获取合同套餐信息失败, Err: "+e.Error(), c)
  496. return
  497. }
  498. result.ServiceList = serviceList
  499. // 开票/到款信息
  500. invoiceCond := `contract_register_id = ?`
  501. invoicePars := make([]interface{}, 0)
  502. invoicePars = append(invoicePars, req.ContractRegisterId)
  503. invoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  504. if e != nil {
  505. resp.FailData("获取失败", "获取合同开票/到款信息失败, Err: "+e.Error(), c)
  506. return
  507. }
  508. result.InvoiceList = make([]*fms.ContractInvoiceItem, 0)
  509. result.PaymentList = make([]*fms.ContractInvoiceItem, 0)
  510. for i := range invoiceList {
  511. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  512. result.InvoiceList = append(result.InvoiceList, invoiceList[i])
  513. continue
  514. }
  515. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  516. result.PaymentList = append(result.PaymentList, invoiceList[i])
  517. }
  518. }
  519. // 合同登记进度
  520. logCond := `contract_register_id = ?`
  521. logPars := make([]interface{}, 0)
  522. logPars = append(logPars, req.ContractRegisterId)
  523. logList, e := fms.GetContractRegisterLogItemList(logCond, logPars)
  524. if e != nil {
  525. resp.FailData("获取失败", "获取合同登记进度失败, Err: "+e.Error(), c)
  526. return
  527. }
  528. result.Logs = logList
  529. resp.OkData("获取成功", result, c)
  530. }
  531. // UpdateStatus
  532. // @Title 修改合同状态
  533. // @Description 修改合同状态
  534. // @Param request body fms.ContractRegisterUpdateStatusReq true "type json string"
  535. // @Success 200 string "操作成功"
  536. // @router /contract/register/update_status [post]
  537. func (rg *RegisterController) UpdateStatus(c *gin.Context) {
  538. req := new(fms.ContractRegisterUpdateStatusReq)
  539. err := c.ShouldBind(&req)
  540. if err != nil {
  541. errs, ok := err.(validator.ValidationErrors)
  542. if !ok {
  543. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  544. return
  545. }
  546. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  547. return
  548. }
  549. claims, _ := c.Get("adminInfo")
  550. adminInfo := claims.(*system.SysAdmin)
  551. ob := new(fms.ContractRegister)
  552. item, e := ob.Fetch(req.ContractRegisterId)
  553. if e != nil {
  554. if e == utils.ErrNoRow {
  555. resp.Fail("合同登记不存在或已被删除", c)
  556. return
  557. }
  558. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  559. return
  560. }
  561. nowTime := time.Now().Local()
  562. item.ContractStatus = req.ContractStatus
  563. item.ModifyTime = nowTime
  564. updateCols := []string{"ContractStatus", "ModifyTime"}
  565. if e = item.Update(updateCols); e != nil {
  566. resp.FailMsg("操作失败", "更新合同登记失败, Err:"+e.Error(), c)
  567. return
  568. }
  569. // 校验金额-是否修改状态
  570. go fmsService.CheckContractRegisterAmount(req.ContractRegisterId)
  571. // 操作日志
  572. go func() {
  573. opData := ""
  574. opDataByte, e := json.Marshal(req)
  575. if e != nil {
  576. return
  577. }
  578. opData = string(opDataByte)
  579. logItem := new(fms.ContractRegisterLog)
  580. logItem.ContractRegisterId = req.ContractRegisterId
  581. logItem.AdminId = int(adminInfo.AdminId)
  582. logItem.AdminName = adminInfo.RealName
  583. logItem.OpData = opData
  584. logItem.OpType = fms.ContractRegisterOpTypeStatus
  585. logItem.CreateTime = nowTime
  586. if e = logItem.Create(); e != nil {
  587. return
  588. }
  589. }()
  590. resp.Ok("操作成功", c)
  591. }
  592. // Invoice
  593. // @Title 开票/到款登记
  594. // @Description 开票/到款登记
  595. // @Param request body fms.ContractInvoiceSaveReq true "type json string"
  596. // @Success 200 string "操作成功"
  597. // @router /contract/register/invoice [post]
  598. func (rg *RegisterController) Invoice(c *gin.Context) {
  599. req := new(fms.ContractInvoiceSaveReq)
  600. err := c.ShouldBind(&req)
  601. if err != nil {
  602. errs, ok := err.(validator.ValidationErrors)
  603. if !ok {
  604. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  605. return
  606. }
  607. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  608. return
  609. }
  610. claims, _ := c.Get("adminInfo")
  611. adminInfo := claims.(*system.SysAdmin)
  612. registerOB := new(fms.ContractRegister)
  613. item, e := registerOB.Fetch(req.ContractRegisterId)
  614. if e != nil {
  615. if e == utils.ErrNoRow {
  616. resp.Fail("合同登记不存在或已被删除", c)
  617. return
  618. }
  619. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  620. return
  621. }
  622. // 合同存在代付不允许开票/到款登记
  623. if item.HasPayment == 1 {
  624. resp.Fail("合同存在代付不允许添加开票/到款登记", c)
  625. return
  626. }
  627. noChangeInvoiceIds := make([]int, 0)
  628. newInvoice := make([]*fms.ContractInvoice, 0)
  629. if len(req.AmountList) > 0 {
  630. for i := range req.AmountList {
  631. if req.AmountList[i].Amount <= 0 {
  632. resp.Fail("登记金额有误", c)
  633. return
  634. }
  635. if req.AmountList[i].InvoiceDate == "" {
  636. resp.Fail("请选择日期", c)
  637. return
  638. }
  639. t, e := time.ParseInLocation(utils.FormatDate, req.AmountList[i].InvoiceDate, time.Local)
  640. if e != nil {
  641. resp.FailData("日期格式有误", "Err:"+e.Error(), c)
  642. return
  643. }
  644. if req.AmountList[i].InvoiceId > 0 {
  645. noChangeInvoiceIds = append(noChangeInvoiceIds, req.AmountList[i].InvoiceId)
  646. } else {
  647. v := &fms.ContractInvoice{
  648. ContractRegisterId: req.ContractRegisterId,
  649. ContractCode: item.ContractCode,
  650. Amount: req.AmountList[i].Amount,
  651. InvoiceType: req.InvoiceType,
  652. InvoiceDate: t,
  653. AdminId: int(adminInfo.AdminId),
  654. AdminName: adminInfo.RealName,
  655. Remark: req.AmountList[i].Remark,
  656. }
  657. v.Set()
  658. newInvoice = append(newInvoice, v)
  659. }
  660. }
  661. }
  662. // 获取原有的登记信息
  663. invoiceCond := `contract_register_id = ? AND invoice_type = ?`
  664. invoicePars := make([]interface{}, 0)
  665. invoicePars = append(invoicePars, req.ContractRegisterId, req.InvoiceType)
  666. originInvoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  667. if e != nil {
  668. resp.FailMsg("获取失败", "获取开票/到款列表失败, Err: "+e.Error(), c)
  669. return
  670. }
  671. // 比对原有和现有的登记信息
  672. logList := make([]*fms.ContractRegisterLog, 0)
  673. opData := ""
  674. opDataByte, e := json.Marshal(req)
  675. if e != nil {
  676. return
  677. }
  678. opData = string(opDataByte)
  679. opType := fms.ContractRegisterOpTypeInvoice
  680. if req.InvoiceType == fms.ContractInvoiceTypePay {
  681. opType = fms.ContractRegisterOpTypePayment
  682. }
  683. nowTime := time.Now().Local()
  684. // 需要删除的记录
  685. deleteInvoiceIds := make([]int, 0)
  686. for i := range originInvoiceList {
  687. if !utils.InArrayByInt(noChangeInvoiceIds, originInvoiceList[i].ContractInvoiceId) {
  688. deleteInvoiceIds = append(deleteInvoiceIds, originInvoiceList[i].ContractInvoiceId)
  689. logList = append(logList, &fms.ContractRegisterLog{
  690. ContractRegisterId: req.ContractRegisterId,
  691. AdminId: int(adminInfo.AdminId),
  692. AdminName: adminInfo.RealName,
  693. OpData: opData,
  694. OpType: opType,
  695. CreateTime: nowTime,
  696. AmountRemark: fmt.Sprint("删除", fms.ContractInvoiceKeyNameMap[opType], "金额", originInvoiceList[i].Amount, "元"),
  697. })
  698. }
  699. }
  700. // 新增的记录
  701. if len(newInvoice) > 0 {
  702. newAmount := decimal.NewFromFloat(0).Round(2)
  703. for i := range newInvoice {
  704. a := decimal.NewFromFloat(newInvoice[i].Amount).Round(2)
  705. newAmount = newAmount.Add(a)
  706. }
  707. ia, _ := newAmount.Round(2).Float64()
  708. logList = append(logList, &fms.ContractRegisterLog{
  709. ContractRegisterId: req.ContractRegisterId,
  710. AdminId: int(adminInfo.AdminId),
  711. AdminName: adminInfo.RealName,
  712. OpData: opData,
  713. OpType: opType,
  714. CreateTime: nowTime,
  715. AmountRemark: fmt.Sprint("新增", fms.ContractInvoiceKeyNameMap[opType], "金额", ia, "元"),
  716. })
  717. }
  718. // 删除并新增登记
  719. ob := new(fms.ContractInvoice)
  720. if e := ob.DeleteAndCreateNewInvoice(req.ContractRegisterId, req.InvoiceType, deleteInvoiceIds, newInvoice); e != nil {
  721. resp.FailData("日期格式有误", "Err:"+e.Error(), c)
  722. return
  723. }
  724. // 校验金额-是否修改状态
  725. go fmsService.CheckContractRegisterAmount(req.ContractRegisterId)
  726. // 操作日志
  727. go func() {
  728. logOB := new(fms.ContractRegisterLog)
  729. if e := logOB.AddInBatches(logList); e != nil {
  730. return
  731. }
  732. }()
  733. resp.Ok("操作成功", c)
  734. }
  735. // Export
  736. // @Title 合同登记-导出
  737. // @Description 合同登记-导出
  738. // @Param Keyword query string false "关键词"
  739. // @Param StartDate query string false "合同开始日期"
  740. // @Param EndDate query string false "合同结束日期"
  741. // @Param ServiceType query int false "套餐类型"
  742. // @Param ContractType query int false "合同类型"
  743. // @Param RegisterStatus query int false "登记状态"
  744. // @Success 200 string "操作成功"
  745. // @router /contract/register/export [get]
  746. func (rg *RegisterController) Export(c *gin.Context) {
  747. var req fms.ContractRegisterListReq
  748. if e := c.BindQuery(&req); e != nil {
  749. err, ok := e.(validator.ValidationErrors)
  750. if !ok {
  751. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  752. return
  753. }
  754. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  755. return
  756. }
  757. cond := `1 = 1`
  758. pars := make([]interface{}, 0)
  759. // 合同编号/客户姓名/销售/实际使用方
  760. if req.Keyword != "" {
  761. kw := "%" + req.Keyword + "%"
  762. cond += ` AND (company_name LIKE ? OR contract_code LIKE ? OR seller_name LIKE ? OR actual_company_name LIKE ?)`
  763. pars = append(pars, kw, kw, kw, kw)
  764. }
  765. if req.StartDate != "" && req.EndDate != "" {
  766. st := fmt.Sprint(req.StartDate, " 00:00:00")
  767. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  768. cond += ` AND (create_time BETWEEN ? AND ?)`
  769. pars = append(pars, st, ed)
  770. }
  771. if req.ContractType != 0 {
  772. cond += ` AND contract_type = ?`
  773. pars = append(pars, req.ContractType)
  774. }
  775. if req.RegisterStatus != 0 {
  776. cond += ` AND register_status = ?`
  777. pars = append(pars, req.RegisterStatus)
  778. }
  779. if req.ServiceType != 0 {
  780. registerIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
  781. if e != nil {
  782. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  783. return
  784. }
  785. if len(registerIds) > 0 {
  786. cond += ` AND contract_register_id IN ?`
  787. pars = append(pars, registerIds)
  788. } else {
  789. cond += ` AND 1 = 2`
  790. }
  791. }
  792. // 获取列表数据
  793. cr := new(fms.ContractRegister)
  794. list, e := cr.List(cond, pars)
  795. if e != nil {
  796. resp.FailData("获取合同列表失败", "Err:"+e.Error(), c)
  797. return
  798. }
  799. if len(list) == 0 {
  800. resp.Fail("无有效数据可导出", c)
  801. return
  802. }
  803. registerIds := make([]int, 0)
  804. for i := range list {
  805. registerIds = append(registerIds, list[i].ContractRegisterId)
  806. }
  807. // 获取小套餐品种
  808. cpCond := `product_id = ? AND permission_name <> ?`
  809. cpPars := make([]interface{}, 0)
  810. cpPars = append(cpPars, crm.CompanyProductFicc, crm.ChartPermissionStrategyName)
  811. cp := new(crm.ChartPermission)
  812. permissionList, e := cp.List(cpCond, cpPars)
  813. if e != nil {
  814. resp.FailData("获取小套餐品种失败", "Err:"+e.Error(), c)
  815. return
  816. }
  817. permissionLen := len(permissionList)
  818. permissionNameIdMap := make(map[string]int)
  819. for i := range permissionList {
  820. permissionNameIdMap[permissionList[i].PermissionName] = permissionList[i].ChartPermissionId
  821. }
  822. // 获取套餐服务
  823. //serviceList, e := fms.GetContractServiceTemplateMapByProductId(crm.CompanyProductFicc)
  824. //if e != nil {
  825. // resp.FailData("获取套餐服务失败", "Err:"+e.Error(), c)
  826. // return
  827. //}
  828. // 套餐/开票/到款列表
  829. serviceMap := make(map[int][]*fms.ContractService)
  830. serviceChartPermissionsMap := make(map[int][]int)
  831. invoiceMap := make(map[int][]*fms.ContractInvoice)
  832. paymentMap := make(map[int][]*fms.ContractInvoice)
  833. maxInvoice := 0
  834. maxPayment := 0
  835. if len(registerIds) > 0 {
  836. // 获取套餐信息
  837. csCond := `contract_register_id IN ?`
  838. csPars := make([]interface{}, 0)
  839. csPars = append(csPars, registerIds)
  840. cs := new(fms.ContractService)
  841. serviceList, e := cs.List(csCond, csPars)
  842. if e != nil {
  843. resp.FailData("获取合同套餐列表失败", "Err:"+e.Error(), c)
  844. return
  845. }
  846. for i := range serviceList {
  847. cid := serviceList[i].ContractRegisterId
  848. if serviceMap[cid] == nil {
  849. serviceMap[cid] = make([]*fms.ContractService, 0)
  850. }
  851. serviceMap[cid] = append(serviceMap[cid], serviceList[i])
  852. // 小套餐权限
  853. if serviceChartPermissionsMap[cid] == nil {
  854. serviceChartPermissionsMap[cid] = make([]int, 0)
  855. }
  856. if serviceList[i].ChartPermissionIds != "" {
  857. ids := utils.JoinStr2IntArr(serviceList[i].ChartPermissionIds, ",")
  858. serviceChartPermissionsMap[cid] = append(serviceChartPermissionsMap[cid], ids...)
  859. }
  860. }
  861. // 获取开票/到款详情, 并取最大的开票/到款数(用于动态扩展第二列表头)
  862. ci := new(fms.ContractInvoice)
  863. invoiceList, e := ci.List(csCond, csPars, "")
  864. if e != nil {
  865. resp.FailData("获取开票/到款列表失败", "Err:"+e.Error(), c)
  866. return
  867. }
  868. for k := range invoiceList {
  869. cid := invoiceList[k].ContractRegisterId
  870. if invoiceMap[cid] == nil {
  871. invoiceMap[cid] = make([]*fms.ContractInvoice, 0)
  872. }
  873. if paymentMap[cid] == nil {
  874. paymentMap[cid] = make([]*fms.ContractInvoice, 0)
  875. }
  876. if invoiceList[k].InvoiceType == fms.ContractInvoiceTypeMake {
  877. invoiceMap[cid] = append(invoiceMap[cid], invoiceList[k])
  878. continue
  879. }
  880. if invoiceList[k].InvoiceType == fms.ContractInvoiceTypePay {
  881. paymentMap[cid] = append(paymentMap[cid], invoiceList[k])
  882. continue
  883. }
  884. }
  885. // 取最大开票/到款数
  886. for j := range invoiceMap {
  887. if len(invoiceMap[j]) > maxInvoice {
  888. maxInvoice = len(invoiceMap[j])
  889. }
  890. }
  891. for p := range paymentMap {
  892. if len(paymentMap[p]) > maxPayment {
  893. maxPayment = len(paymentMap[p])
  894. }
  895. }
  896. }
  897. // 生成Excel文件
  898. //dir, err := os.Executable()
  899. //exPath := filepath.Dir(dir)
  900. //filePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  901. xlsxFile := xlsx.NewFile()
  902. //if err != nil {
  903. // resp.FailData("生成文件失败", "Err:"+err.Error(), c)
  904. // return
  905. //}
  906. style := xlsx.NewStyle()
  907. alignment := xlsx.Alignment{
  908. Horizontal: "center",
  909. Vertical: "center",
  910. WrapText: true,
  911. }
  912. style.Alignment = alignment
  913. style.ApplyAlignment = true
  914. sheet, err := xlsxFile.AddSheet("合同登记")
  915. if err != nil {
  916. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  917. return
  918. }
  919. //_ = sheet.SetColWidth(0, 0, 60)
  920. //_ = sheet.SetColWidth(4, 10, 60)
  921. // 1行表头
  922. titleRow := sheet.AddRow()
  923. titleRow.SetHeight(40)
  924. // 1行1列-右合并两格
  925. cell1 := titleRow.AddCell()
  926. cell1.HMerge = 2
  927. cell1.SetValue("FICC客户签约表 2022")
  928. cell1.SetStyle(style)
  929. // 右增两列空白格用于第一列合并, 否则后续单元格合并会有问题
  930. titleRow.AddCell().SetValue("")
  931. titleRow.AddCell().SetValue("")
  932. // 1行2列
  933. cell2 := titleRow.AddCell()
  934. cell2.SetValue("FICC大套餐")
  935. cell2.SetStyle(style)
  936. // 1行3列-右合并小套餐数
  937. if permissionLen >= 1 {
  938. cell3 := titleRow.AddCell()
  939. cell3.HMerge = permissionLen - 1
  940. cell3.SetValue("FICC小套餐")
  941. cell3.SetStyle(style)
  942. // 同上右增单元格小套餐数-1的空白单元格用于合并
  943. for i := 0; i < permissionLen-1; i++ {
  944. titleRow.AddCell().SetValue("")
  945. }
  946. }
  947. cell4 := titleRow.AddCell()
  948. cell4.SetValue("市场策略")
  949. cell4.SetStyle(style)
  950. cell5 := titleRow.AddCell()
  951. cell5.SetValue("财富管理")
  952. cell5.SetStyle(style)
  953. // 第二行表头
  954. titleRow2 := sheet.AddRow()
  955. titleRow2.SetHeight(60)
  956. row2Title := make([]string, 0)
  957. row2Title = append(row2Title, "客户名称", "续约-0\n新增-1", "销售", "FICC大套餐")
  958. for i := range permissionList {
  959. row2Title = append(row2Title, permissionList[i].PermissionName)
  960. }
  961. row2Title = append(row2Title, "市场策略", "财富管理", "套餐备注", "开始时间", "到期时间", "2022年合同金额", "约定付款时间", "签订日",
  962. "签订月", "合同状态", "合同编号", "备注")
  963. for i := range row2Title {
  964. v := titleRow2.AddCell()
  965. v.SetValue(row2Title[i])
  966. v.SetStyle(style)
  967. }
  968. // 第二行表头-开票/收款(动态添加)
  969. for i := 0; i < maxInvoice; i++ {
  970. n := i + 1
  971. c1 := titleRow2.AddCell()
  972. t1 := fmt.Sprintf("%s%d", "开票日", n)
  973. c1.SetValue(t1)
  974. c1.SetStyle(style)
  975. c2 := titleRow2.AddCell()
  976. t2 := fmt.Sprintf("%s%d", "开票金额", n)
  977. c2.SetValue(t2)
  978. c2.SetStyle(style)
  979. row2Title = append(row2Title, t1, t2)
  980. }
  981. for i := 0; i < maxPayment; i++ {
  982. n := i + 1
  983. c1 := titleRow2.AddCell()
  984. t1 := fmt.Sprintf("%s%d", "收款日", n)
  985. c1.SetValue(t1)
  986. c1.SetStyle(style)
  987. c2 := titleRow2.AddCell()
  988. t2 := fmt.Sprintf("%s%d", "收款金额", n)
  989. c2.SetValue(t2)
  990. c2.SetStyle(style)
  991. row2Title = append(row2Title, t1, t2)
  992. }
  993. // 此处取第二行标题NameKeyMap, 后面的动态匹配
  994. row2NameKeyMap := make(map[string]int)
  995. for i := range row2Title {
  996. row2NameKeyMap[row2Title[i]] = i
  997. }
  998. contractTMap := map[int]int{
  999. fms.ContractTypeNew: 1,
  1000. fms.ContractTypeRenew: 0,
  1001. fms.ContractTypeAgentPay: 2, // 代付合同
  1002. fms.ContractTypePlus: 3, // 补充协议
  1003. }
  1004. for _, v := range list {
  1005. k := -1
  1006. dataRow := sheet.AddRow()
  1007. dataRow.SetHeight(20)
  1008. k += 3
  1009. dataRow.AddCell().SetString(v.CompanyName)
  1010. dataRow.AddCell().SetString(fmt.Sprint(contractTMap[v.ContractType]))
  1011. dataRow.AddCell().SetString(v.SellerName)
  1012. // 大套餐
  1013. k += 1
  1014. col4Name := row2Title[k]
  1015. svList := serviceMap[v.ContractRegisterId]
  1016. col4 := ""
  1017. if svList != nil && len(svList) > 0 {
  1018. for isv := range svList {
  1019. if svList[isv].Title == col4Name {
  1020. col4 = "是"
  1021. break
  1022. }
  1023. }
  1024. }
  1025. dataRow.AddCell().SetString(col4)
  1026. // 小套餐
  1027. serviceChartPermissionIds := serviceChartPermissionsMap[v.ContractRegisterId]
  1028. for i := 0; i < permissionLen; i++ {
  1029. k += 1
  1030. colName := row2Title[k]
  1031. chartPermissionId := permissionNameIdMap[colName]
  1032. if utils.InArray(chartPermissionId, serviceChartPermissionIds) {
  1033. dataRow.AddCell().SetString("是")
  1034. } else {
  1035. dataRow.AddCell().SetString("")
  1036. }
  1037. }
  1038. // 财富管理/市场策略(处理方式其实跟上面的大套餐一样, 只是中间隔了小套餐按照顺序要这么处理=_=!)
  1039. k += 1
  1040. col5Name := row2Title[k]
  1041. col5 := ""
  1042. if svList != nil && len(svList) > 0 {
  1043. for isv := range svList {
  1044. if svList[isv].Title == col5Name {
  1045. col5 = "是"
  1046. break
  1047. }
  1048. }
  1049. }
  1050. dataRow.AddCell().SetString(col5)
  1051. k += 1
  1052. col6Name := row2Title[k]
  1053. col6 := ""
  1054. if svList != nil && len(svList) > 0 {
  1055. for isv := range svList {
  1056. if svList[isv].Title == col6Name {
  1057. col6 = "是"
  1058. break
  1059. }
  1060. }
  1061. }
  1062. dataRow.AddCell().SetString(col6)
  1063. // 其他信息
  1064. dataRow.AddCell().SetString(v.ServiceRemark) // 套餐备注
  1065. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.StartDate)) // 开始时间
  1066. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.EndDate)) // 到期时间
  1067. dataRow.AddCell().SetString(fmt.Sprint("¥", v.ContractAmount)) // 2022年合同金额
  1068. dataRow.AddCell().SetString(v.AgreedPayTime) // 约定付款时间
  1069. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.SignDate)) // 签订日
  1070. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01", v.SignDate)) // 签订月
  1071. dataRow.AddCell().SetString(fms.ContractStatusKeyNameMap[v.ContractStatus]) // 合同状态
  1072. dataRow.AddCell().SetString(v.ContractCode) // 合同编号
  1073. dataRow.AddCell().SetString(v.Remark) // 备注
  1074. // 开票/到款信息
  1075. ivList := invoiceMap[v.ContractRegisterId]
  1076. ivListLen := len(ivList)
  1077. if ivList != nil && len(ivList) > 0 {
  1078. for ia := 0; ia < maxInvoice; ia++ {
  1079. if ia < ivListLen {
  1080. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", ivList[ia].InvoiceDate)) // 开票日
  1081. dataRow.AddCell().SetString(fmt.Sprint(ivList[ia].Amount)) // 开票金额
  1082. } else {
  1083. // 这里要把不够的填充为空
  1084. dataRow.AddCell().SetString("")
  1085. dataRow.AddCell().SetString("")
  1086. }
  1087. }
  1088. }
  1089. pyList := paymentMap[v.ContractRegisterId]
  1090. pyListLen := len(pyList)
  1091. if pyList != nil && pyListLen > 0 {
  1092. for ib := 0; ib < maxInvoice; ib++ {
  1093. if ib < pyListLen {
  1094. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", pyList[ib].InvoiceDate)) // 收款日
  1095. dataRow.AddCell().SetString(fmt.Sprint(pyList[ib].Amount)) // 收款金额
  1096. } else {
  1097. // 已经是最后的几列了其实可以不用填充空, 万一后面要加列此处还是填充上吧
  1098. dataRow.AddCell().SetString("")
  1099. dataRow.AddCell().SetString("")
  1100. }
  1101. }
  1102. }
  1103. }
  1104. // 输出文件
  1105. var buffer bytes.Buffer
  1106. _ = xlsxFile.Write(&buffer)
  1107. content := bytes.NewReader(buffer.Bytes())
  1108. //err = xlsxFile.Save(filePath)
  1109. //if err != nil {
  1110. // resp.FailData("保存文件失败", "Err:"+err.Error(), c)
  1111. // return
  1112. //}
  1113. //defer func() {
  1114. // _ = os.Remove(filePath)
  1115. //}()
  1116. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1117. fileName := "财务列表_" + randStr + ".xlsx"
  1118. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  1119. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1120. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  1121. }
  1122. // InvoiceList
  1123. // @Title 开票/到款列表
  1124. // @Description 开票/到款列表
  1125. // @Param InvoiceType query int false "类型: 1-开票登记; 2-到款登记"
  1126. // @Param ContractCode query string false "合同编号"
  1127. // @Param StartDate query string false "开始日期"
  1128. // @Param EndDate query string false "结束日期"
  1129. // @Param MinAmount query float64 false "开票金额区间-最小值"
  1130. // @Param MaxAmount query float64 false "开票金额区间-最大值"
  1131. // @Success 200 {object} fms.ContractInvoiceItem
  1132. // @router /contract/register/invoice_list [get]
  1133. func (rg *RegisterController) InvoiceList(c *gin.Context) {
  1134. var req fms.ContractInvoiceListReq
  1135. if e := c.BindQuery(&req); e != nil {
  1136. err, ok := e.(validator.ValidationErrors)
  1137. if !ok {
  1138. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1139. return
  1140. }
  1141. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1142. return
  1143. }
  1144. pageSize := req.PageSize
  1145. pageIndex := req.Current
  1146. if pageSize <= 0 {
  1147. pageSize = utils.PageSize20
  1148. }
  1149. if pageIndex <= 0 {
  1150. pageIndex = 1
  1151. }
  1152. cond := `invoice_type = ?`
  1153. pars := make([]interface{}, 0)
  1154. pars = append(pars, req.InvoiceType)
  1155. // 合同编号
  1156. if req.ContractCode != "" {
  1157. kw := fmt.Sprint("%", req.ContractCode, "%")
  1158. cond += ` AND contract_code LIKE ?`
  1159. pars = append(pars, kw)
  1160. }
  1161. if req.StartDate != "" && req.EndDate != "" {
  1162. st := fmt.Sprint(req.StartDate, " 00:00:00")
  1163. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1164. cond += ` AND (invoice_time BETWEEN ? AND ?)`
  1165. pars = append(pars, st, ed)
  1166. }
  1167. if req.MinAmount > 0 {
  1168. cond += ` AND amount >= ?`
  1169. pars = append(pars, req.MinAmount)
  1170. }
  1171. if req.MaxAmount > 0 {
  1172. cond += ` AND amount <= ?`
  1173. pars = append(pars, req.MaxAmount)
  1174. }
  1175. page := new(base.Page)
  1176. page.SetPageSize(pageSize)
  1177. page.SetCurrent(pageIndex)
  1178. page.AddOrderItem(base.OrderItem{Column: "invoice_time", Asc: false})
  1179. total, list, e := fms.GetContractInvoiceItemPageList(page, cond, pars)
  1180. if e != nil {
  1181. resp.FailMsg("获取失败", "获取合同开票/到款列表失败, Err: "+e.Error(), c)
  1182. return
  1183. }
  1184. page.SetTotal(total)
  1185. // 金额合计
  1186. ob := new(fms.ContractInvoice)
  1187. amountTotal, e := ob.Sum("amount", cond, pars)
  1188. if e != nil {
  1189. resp.FailMsg("获取失败", "获取合同开票/到款列表合计金额失败, Err: "+e.Error(), c)
  1190. return
  1191. }
  1192. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountTotal), 64)
  1193. type RespData struct {
  1194. Page *base.Page `json:"page"`
  1195. List interface{} `json:"list"`
  1196. AmountTotal float64 `json:"amount_total"`
  1197. }
  1198. respData := &RespData{
  1199. Page: page,
  1200. List: list,
  1201. AmountTotal: amountTotal,
  1202. }
  1203. resp.OkData("获取成功", respData, c)
  1204. }
  1205. // InvoiceExport
  1206. // @Title 开票/到款列表-导出
  1207. // @Description 合同登记-导出
  1208. // @Param InvoiceType query int false "类型: 1-开票登记; 2-到款登记"
  1209. // @Param ContractCode query string false "合同编号"
  1210. // @Param StartDate query string false "开始日期"
  1211. // @Param EndDate query string false "结束日期"
  1212. // @Param MinAmount query float64 false "开票金额区间-最小值"
  1213. // @Param MaxAmount query float64 false "开票金额区间-最大值"
  1214. // @Success 200 string "操作成功"
  1215. // @router /contract/register/invoice_export [get]
  1216. func (rg *RegisterController) InvoiceExport(c *gin.Context) {
  1217. var req fms.ContractInvoiceListReq
  1218. if e := c.BindQuery(&req); e != nil {
  1219. err, ok := e.(validator.ValidationErrors)
  1220. if !ok {
  1221. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1222. return
  1223. }
  1224. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1225. return
  1226. }
  1227. cond := `invoice_type = ?`
  1228. pars := make([]interface{}, 0)
  1229. pars = append(pars, req.InvoiceType)
  1230. // 合同编号
  1231. if req.ContractCode != "" {
  1232. kw := fmt.Sprint("%", req.ContractCode, "%")
  1233. cond += ` AND contract_code LIKE ?`
  1234. pars = append(pars, kw)
  1235. }
  1236. if req.StartDate != "" && req.EndDate != "" {
  1237. st := fmt.Sprint(req.StartDate, " 00:00:00")
  1238. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1239. cond += ` AND (invoice_time BETWEEN ? AND ?)`
  1240. pars = append(pars, st, ed)
  1241. }
  1242. if req.MinAmount > 0 {
  1243. cond += ` AND amount >= ?`
  1244. pars = append(pars, req.MinAmount)
  1245. }
  1246. if req.MaxAmount > 0 {
  1247. cond += ` AND amount <= ?`
  1248. pars = append(pars, req.MaxAmount)
  1249. }
  1250. // 获取列表数据
  1251. cr := new(fms.ContractInvoice)
  1252. orderRule := `invoice_time DESC`
  1253. list, e := cr.List(cond, pars, orderRule)
  1254. if e != nil {
  1255. resp.FailData("获取开票/到款列表失败", "Err:"+e.Error(), c)
  1256. return
  1257. }
  1258. if len(list) == 0 {
  1259. resp.Fail("无有效数据可导出", c)
  1260. return
  1261. }
  1262. // 生成Excel文件
  1263. xlsxFile := xlsx.NewFile()
  1264. style := xlsx.NewStyle()
  1265. alignment := xlsx.Alignment{
  1266. Horizontal: "center",
  1267. Vertical: "center",
  1268. WrapText: true,
  1269. }
  1270. style.Alignment = alignment
  1271. style.ApplyAlignment = true
  1272. sheet, err := xlsxFile.AddSheet("开票列表")
  1273. if err != nil {
  1274. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  1275. return
  1276. }
  1277. // 表头
  1278. titleRow := sheet.AddRow()
  1279. titleRow.SetHeight(40)
  1280. cell1 := titleRow.AddCell()
  1281. cell1.SetValue("合同编号")
  1282. cell1.SetStyle(style)
  1283. cell2 := titleRow.AddCell()
  1284. cell2.SetValue("开票金额")
  1285. cell2.SetStyle(style)
  1286. cell3 := titleRow.AddCell()
  1287. cell3.SetValue("开票日期")
  1288. cell3.SetStyle(style)
  1289. for _, v := range list {
  1290. dataRow := sheet.AddRow()
  1291. dataRow.SetHeight(20)
  1292. dataRow.AddCell().SetString(v.ContractCode)
  1293. dataRow.AddCell().SetString(fmt.Sprint(v.Amount))
  1294. dataRow.AddCell().SetString(utils.TimeTransferString("2006-01-02", v.InvoiceDate))
  1295. }
  1296. // 输出文件
  1297. var buffer bytes.Buffer
  1298. _ = xlsxFile.Write(&buffer)
  1299. content := bytes.NewReader(buffer.Bytes())
  1300. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1301. fileName := "开票列表_" + randStr + ".xlsx"
  1302. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  1303. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1304. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  1305. }
  1306. // Import
  1307. // @Title 合同登记-导入
  1308. // @Description 合同登记-导入
  1309. // @Success 200 string "操作成功"
  1310. // @router /contract/register/import [post]
  1311. func (rg *RegisterController) Import(c *gin.Context) {
  1312. h, err := c.FormFile("File")
  1313. if err != nil {
  1314. resp.FailData("获取文件失败", "Err:"+err.Error(), c)
  1315. return
  1316. }
  1317. ext := path.Ext(h.Filename)
  1318. if ext != ".xlsx" && ext != ".xls" {
  1319. resp.Fail("请上传Excel文件", c)
  1320. return
  1321. }
  1322. uploadDir := "static/xls"
  1323. err = os.MkdirAll(uploadDir, 766)
  1324. if err != nil {
  1325. resp.FailData("存储目录创建失败", "Err:"+err.Error(), c)
  1326. return
  1327. }
  1328. uploadPath := uploadDir + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + h.Filename
  1329. err = c.SaveUploadedFile(h, uploadPath)
  1330. if err != nil {
  1331. resp.FailData("保存本地文件失败", "Err:"+err.Error(), c)
  1332. return
  1333. }
  1334. defer func() {
  1335. _ = os.Remove(uploadPath)
  1336. }()
  1337. xlFile, err := xlsx.OpenFile(uploadPath)
  1338. if err != nil {
  1339. resp.FailData("打开文件失败", "Err:"+err.Error(), c)
  1340. return
  1341. }
  1342. // 获取所有已登记,根据合同编号去重
  1343. contractCodeArr := make([]string, 0)
  1344. registerOB := new(fms.ContractRegister)
  1345. registerCond := ``
  1346. registerPars := make([]interface{}, 0)
  1347. registerList, e := registerOB.List(registerCond, registerPars)
  1348. if e != nil {
  1349. resp.FailData("获取合同登记列表失败", "Err:"+e.Error(), c)
  1350. return
  1351. }
  1352. for i := range registerList {
  1353. contractCodeArr = append(contractCodeArr, registerList[i].ContractCode)
  1354. }
  1355. // 获取所有销售名称Map
  1356. sellerNameIdMap := make(map[string]int)
  1357. sellerCond := ``
  1358. sellerPars := make([]interface{}, 0)
  1359. sellerList, e := crm.GetCompanySellerByRoleCodes(sellerCond, sellerPars)
  1360. if e != nil {
  1361. resp.FailData("获取销售列表失败", "Err:"+e.Error(), c)
  1362. return
  1363. }
  1364. for i := range sellerList {
  1365. sellerNameIdMap[sellerList[i].RealName] = sellerList[i].AdminId
  1366. }
  1367. // 获取品种权限Map
  1368. chartPermissionNameIdMap := make(map[string]int)
  1369. cpCond := `product_id = ?`
  1370. cpPars := make([]interface{}, 0)
  1371. cpPars = append(cpPars, crm.CompanyProductFicc)
  1372. cp := new(crm.ChartPermission)
  1373. permissionList, e := cp.List(cpCond, cpPars)
  1374. if e != nil {
  1375. resp.FailData("获取权限列表失败", "Err:"+e.Error(), c)
  1376. return
  1377. }
  1378. for i := range permissionList {
  1379. chartPermissionNameIdMap[permissionList[i].PermissionName] = permissionList[i].ChartPermissionId
  1380. }
  1381. // 获取所有套餐模板
  1382. serviceTempCond := ``
  1383. serviceTempPars := make([]interface{}, 0)
  1384. serviceTempOB := new(fms.ContractServiceTemplate)
  1385. serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
  1386. if e != nil {
  1387. resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
  1388. return
  1389. }
  1390. serviceTempNameMap := make(map[string]*fms.ContractServiceTemplate)
  1391. for i := range serviceTempList {
  1392. serviceTempNameMap[serviceTempList[i].Title] = serviceTempList[i]
  1393. }
  1394. titleMap := make(map[int]string)
  1395. newIds := make([]int, 0)
  1396. contractTypeArr := []string{"0", "1", "2", "3"}
  1397. contractTypeMap := map[string]int{
  1398. "0": fms.ContractTypeRenew,
  1399. "1": fms.ContractTypeNew,
  1400. "2": fms.ContractTypeAgentPay,
  1401. "3": fms.ContractTypePlus,
  1402. }
  1403. checkDate := time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local)
  1404. for _, sheet := range xlFile.Sheets {
  1405. // 遍历行读取
  1406. maxRow := sheet.MaxRow
  1407. for i := 0; i < maxRow; i++ {
  1408. // 第二行开始读取表头
  1409. if i == 1 {
  1410. row := sheet.Row(i)
  1411. cells := row.Cells
  1412. for k, cell := range cells {
  1413. text := cell.String()
  1414. titleMap[k] = text
  1415. // 只检验前面部分表头
  1416. if k == 0 {
  1417. if text != "客户名称" {
  1418. resp.Fail("匹配失败, 请按模板导入", c)
  1419. return
  1420. }
  1421. }
  1422. if k == 1 {
  1423. if !strings.Contains(text, "续约") {
  1424. resp.Fail("匹配失败, 请按模板导入", c)
  1425. return
  1426. }
  1427. }
  1428. if k == 2 {
  1429. if text != "销售" {
  1430. resp.Fail("匹配失败, 请按模板导入", c)
  1431. return
  1432. }
  1433. }
  1434. if k == 3 {
  1435. if text != "FICC大套餐" {
  1436. resp.Fail("匹配失败, 请按模板导入", c)
  1437. return
  1438. }
  1439. }
  1440. }
  1441. }
  1442. // 数据行
  1443. if i >= 2 {
  1444. row := sheet.Row(i)
  1445. cells := row.Cells
  1446. // 登记信息
  1447. rowRegister := new(fms.ContractRegister)
  1448. // 套餐
  1449. rowServices := make([]*fms.ContractService, 0)
  1450. // 开票/到款
  1451. rowInvoices := make([]*fms.ContractInvoice, 0)
  1452. rowChartPermissionIdArr := make([]string, 0)
  1453. rowInvoice1 := new(fms.ContractInvoice)
  1454. rowInvoice2 := new(fms.ContractInvoice)
  1455. rowInvoice3 := new(fms.ContractInvoice)
  1456. rowInvoice4 := new(fms.ContractInvoice)
  1457. rowInvoice5 := new(fms.ContractInvoice)
  1458. rowInvoice6 := new(fms.ContractInvoice)
  1459. isSkip := false
  1460. for k, cell := range cells {
  1461. v := utils.TrimStr(cell.String())
  1462. // 客户名称
  1463. if k == 0 {
  1464. if v == "" {
  1465. resp.Fail(fmt.Sprintf("第%d行客户名称不可为空, 请按模板导入", i+1), c)
  1466. return
  1467. }
  1468. rowRegister.CompanyName = v
  1469. continue
  1470. }
  1471. // 续约/新增
  1472. if k == 1 {
  1473. if !utils.InArrayByStr(contractTypeArr, v) {
  1474. resp.Fail(fmt.Sprintf("第%d行合同类型有误, 请按模板导入", i+1), c)
  1475. return
  1476. }
  1477. rowRegister.ContractType = contractTypeMap[v]
  1478. if rowRegister.ContractType == 0 {
  1479. resp.Fail(fmt.Sprintf("第%d行合同类型匹配有误, 请按模板导入", i+1), c)
  1480. return
  1481. }
  1482. continue
  1483. }
  1484. // 销售
  1485. if k == 2 {
  1486. if v == "" {
  1487. resp.Fail(fmt.Sprintf("第%d行销售名称不可为空, 请按模板导入", i+1), c)
  1488. return
  1489. }
  1490. rowRegister.SellerName = v
  1491. rowRegister.SellerId = sellerNameIdMap[rowRegister.SellerName]
  1492. if rowRegister.SellerId == 0 {
  1493. resp.Fail(fmt.Sprintf("第%d行销售名称与系统销售不匹配, 请核对名称后导入", i+1), c)
  1494. return
  1495. }
  1496. continue
  1497. }
  1498. // FICC大套餐
  1499. if k == 3 {
  1500. if v == "是" {
  1501. tempItem := serviceTempNameMap[titleMap[k]]
  1502. if tempItem == nil {
  1503. resp.Fail(fmt.Sprintf("第%d行套餐名称不匹配, 请按模板导入", i+1), c)
  1504. return
  1505. }
  1506. cs := &fms.ContractService{
  1507. ProductId: crm.CompanyProductFicc,
  1508. ServiceTemplateId: tempItem.ServiceTemplateId,
  1509. Title: tempItem.Title,
  1510. Value: tempItem.Value,
  1511. TableValue: tempItem.TableValue,
  1512. ChartPermissionId: tempItem.ChartPermissionId,
  1513. }
  1514. cs.Set()
  1515. rowServices = append(rowServices, cs)
  1516. }
  1517. continue
  1518. }
  1519. // FICC小套餐
  1520. if k >= 4 && k <= 23 {
  1521. // 小套餐权限
  1522. if v == "是" {
  1523. rowChartPermissionIdArr = append(rowChartPermissionIdArr, strconv.Itoa(chartPermissionNameIdMap[titleMap[k]]))
  1524. }
  1525. continue
  1526. }
  1527. // 市场策略/财富管理
  1528. if k == 24 || k == 25 {
  1529. if v == "是" {
  1530. tempItem := serviceTempNameMap[titleMap[k]]
  1531. if tempItem == nil {
  1532. resp.Fail(fmt.Sprintf("第%d行市场策略/财富管理套餐名称不匹配, 请按模板导入", i+1), c)
  1533. return
  1534. }
  1535. cs := &fms.ContractService{
  1536. ProductId: crm.CompanyProductFicc,
  1537. ServiceTemplateId: tempItem.ServiceTemplateId,
  1538. Title: tempItem.Title,
  1539. Value: tempItem.Value,
  1540. TableValue: tempItem.TableValue,
  1541. ChartPermissionId: tempItem.ChartPermissionId,
  1542. }
  1543. cs.Set()
  1544. rowServices = append(rowServices, cs)
  1545. }
  1546. continue
  1547. }
  1548. // 套餐备注
  1549. if k == 26 {
  1550. rowRegister.ServiceRemark = v
  1551. continue
  1552. }
  1553. // 开始时间/到期时间
  1554. if k == 27 {
  1555. // 转换失败可能是因为格式为Excel日期格式, 读取出来会是一串数字, 将其转换成日期字符串再处理
  1556. va := cell.Value
  1557. if va == "" {
  1558. resp.Fail(fmt.Sprintf("第%d行开始时间不可为空, 请按模板导入", i+1), c)
  1559. return
  1560. }
  1561. startDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1562. if e != nil {
  1563. d := utils.ConvertToFormatDay(va, "2006/01/02")
  1564. startDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1565. if e != nil {
  1566. resp.Fail(fmt.Sprintf("第%d行开始时间格式转换有误, 请按模板导入", i+1), c)
  1567. return
  1568. }
  1569. }
  1570. // 转换后的日期小于1900-01-01表示当前生成的日期是有问题的
  1571. if startDate.Before(checkDate) {
  1572. resp.Fail(fmt.Sprintf("第%d行开始时间格式有误, 请按模板导入", i+1), c)
  1573. return
  1574. }
  1575. rowRegister.StartDate = startDate
  1576. continue
  1577. }
  1578. if k == 28 {
  1579. va := cell.Value
  1580. if va == "" {
  1581. resp.Fail(fmt.Sprintf("第%d行到期时间不可为空, 请按模板导入", i+1), c)
  1582. return
  1583. }
  1584. endDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1585. if e != nil {
  1586. d := utils.ConvertToFormatDay(va, "2006/01/02")
  1587. endDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1588. if e != nil {
  1589. resp.Fail(fmt.Sprintf("第%d行到期时间格式转换有误, 请按模板导入", i+1), c)
  1590. return
  1591. }
  1592. }
  1593. if endDate.Before(checkDate) {
  1594. resp.Fail(fmt.Sprintf("第%d行到期时间格式有误, 请按模板导入", i+1), c)
  1595. return
  1596. }
  1597. rowRegister.EndDate = endDate
  1598. continue
  1599. }
  1600. // 合同金额
  1601. if k == 29 {
  1602. amountStr := v
  1603. amount, e := strconv.ParseFloat(amountStr, 64)
  1604. if e != nil {
  1605. resp.Fail(fmt.Sprintf("第%d行合同金额有误, 请按模板导入", i+1), c)
  1606. return
  1607. }
  1608. rowRegister.ContractAmount = amount
  1609. continue
  1610. }
  1611. // 约定付款日期
  1612. if k == 30 {
  1613. rowRegister.AgreedPayTime = v
  1614. continue
  1615. }
  1616. // 签订日
  1617. // k == 32为签订月,可忽略
  1618. if k == 31 {
  1619. va := cell.Value
  1620. if va == "" {
  1621. continue
  1622. }
  1623. signDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1624. if e != nil {
  1625. d := utils.ConvertToFormatDay(va, "2006/01/02")
  1626. signDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1627. if e != nil {
  1628. resp.Fail(fmt.Sprintf("第%d行签订日格式转换有误, 请按模板导入", i+1), c)
  1629. return
  1630. }
  1631. }
  1632. if signDate.Before(checkDate) {
  1633. resp.Fail(fmt.Sprintf("第%d行签订日格式有误, 请按模板导入", i+1), c)
  1634. return
  1635. }
  1636. rowRegister.SignDate = signDate
  1637. continue
  1638. }
  1639. // 合同状态
  1640. if k == 33 {
  1641. rowRegister.ContractStatus = fms.ContractStatusNameKeyMap[v]
  1642. if rowRegister.ContractStatus == 0 {
  1643. resp.Fail(fmt.Sprintf("第%d行合同状态不匹配, 请按模板导入", i+1), c)
  1644. return
  1645. }
  1646. continue
  1647. }
  1648. // 合同编号
  1649. if k == 34 {
  1650. rowContractCode := v
  1651. if rowContractCode == "" {
  1652. resp.Fail(fmt.Sprintf("第%d行合同编号不可为空, 请按模板导入", i+1), c)
  1653. return
  1654. }
  1655. if utils.InArrayByStr(contractCodeArr, rowContractCode) {
  1656. // 此合同已登记, 跳过本行的读取
  1657. isSkip = true
  1658. break
  1659. }
  1660. rowRegister.ContractCode = rowContractCode
  1661. continue
  1662. }
  1663. // 备注
  1664. if k == 35 {
  1665. rowRegister.Remark = v
  1666. continue
  1667. }
  1668. // 开票日/开票金额
  1669. if k == 36 {
  1670. if v != "" {
  1671. va := cell.Value
  1672. invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1673. if e != nil {
  1674. d := utils.ConvertToFormatDay(va, "2006/01/02")
  1675. invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1676. if e != nil {
  1677. resp.Fail(fmt.Sprintf("第%d行开票时间1格式转换有误, 请按模板导入", i+1), c)
  1678. return
  1679. }
  1680. }
  1681. if invoiceDate.Before(checkDate) {
  1682. resp.Fail(fmt.Sprintf("第%d行开票时间1格式有误, 请按模板导入", i+1), c)
  1683. return
  1684. }
  1685. rowInvoice1.InvoiceDate = invoiceDate
  1686. rowInvoice1.ContractCode = rowRegister.ContractCode
  1687. rowInvoice1.InvoiceType = fms.ContractInvoiceTypeMake
  1688. }
  1689. continue
  1690. }
  1691. if k == 37 && v != "" {
  1692. amountStr := v
  1693. amount, e := strconv.ParseFloat(amountStr, 64)
  1694. if e != nil {
  1695. resp.Fail(fmt.Sprintf("第%d行开票金额1有误, 请按模板导入", i+1), c)
  1696. return
  1697. }
  1698. rowInvoice1.Amount = amount
  1699. rowInvoices = append(rowInvoices, rowInvoice1)
  1700. continue
  1701. }
  1702. if k == 38 {
  1703. if v != "" {
  1704. va := cell.Value
  1705. invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1706. if e != nil {
  1707. d := utils.ConvertToFormatDay(va, "2006/01/02") //录入日期
  1708. invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1709. if e != nil {
  1710. resp.Fail(fmt.Sprintf("第%d行开票时间2格式转换有误, 请按模板导入", i+1), c)
  1711. return
  1712. }
  1713. }
  1714. if invoiceDate.Before(checkDate) {
  1715. resp.Fail(fmt.Sprintf("第%d行开票时间2格式有误, 请按模板导入", i+1), c)
  1716. return
  1717. }
  1718. rowInvoice2.InvoiceDate = invoiceDate
  1719. rowInvoice2.ContractCode = rowRegister.ContractCode
  1720. rowInvoice2.InvoiceType = fms.ContractInvoiceTypeMake
  1721. }
  1722. continue
  1723. }
  1724. if k == 39 && v != "" {
  1725. amountStr := v
  1726. amount, e := strconv.ParseFloat(amountStr, 64)
  1727. if e != nil {
  1728. resp.Fail(fmt.Sprintf("第%d行开票金额2有误, 请按模板导入", i+1), c)
  1729. return
  1730. }
  1731. rowInvoice2.Amount = amount
  1732. rowInvoices = append(rowInvoices, rowInvoice2)
  1733. continue
  1734. }
  1735. if k == 40 {
  1736. if v != "" {
  1737. va := cell.Value
  1738. invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1739. if e != nil {
  1740. d := utils.ConvertToFormatDay(va, "2006/01/02") //录入日期
  1741. invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1742. if e != nil {
  1743. resp.Fail(fmt.Sprintf("第%d行开票时间3格式转换有误, 请按模板导入", i+1), c)
  1744. return
  1745. }
  1746. }
  1747. if invoiceDate.Before(checkDate) {
  1748. resp.Fail(fmt.Sprintf("第%d行开票时间3格式有误, 请按模板导入", i+1), c)
  1749. return
  1750. }
  1751. rowInvoice3.InvoiceDate = invoiceDate
  1752. rowInvoice3.ContractCode = rowRegister.ContractCode
  1753. rowInvoice3.InvoiceType = fms.ContractInvoiceTypeMake
  1754. }
  1755. continue
  1756. }
  1757. if k == 41 && v != "" {
  1758. amountStr := v
  1759. amount, e := strconv.ParseFloat(amountStr, 64)
  1760. if e != nil {
  1761. resp.Fail(fmt.Sprintf("第%d行开票金额3有误, 请按模板导入", i+1), c)
  1762. return
  1763. }
  1764. rowInvoice3.Amount = amount
  1765. rowInvoices = append(rowInvoices, rowInvoice3)
  1766. continue
  1767. }
  1768. // 收款日/收款金额
  1769. if k == 42 {
  1770. if v != "" {
  1771. va := cell.Value
  1772. invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1773. if e != nil {
  1774. d := utils.ConvertToFormatDay(va, "2006/01/02") //录入日期
  1775. invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1776. if e != nil {
  1777. resp.Fail(fmt.Sprintf("第%d行收款时间1格式转换有误, 请按模板导入", i+1), c)
  1778. return
  1779. }
  1780. }
  1781. if invoiceDate.Before(checkDate) {
  1782. resp.Fail(fmt.Sprintf("第%d行收款时间1格式有误, 请按模板导入", i+1), c)
  1783. return
  1784. }
  1785. rowInvoice4.InvoiceDate = invoiceDate
  1786. rowInvoice4.ContractCode = rowRegister.ContractCode
  1787. rowInvoice4.InvoiceType = fms.ContractInvoiceTypePay
  1788. }
  1789. continue
  1790. }
  1791. if k == 43 && v != "" {
  1792. amountStr := v
  1793. amount, e := strconv.ParseFloat(amountStr, 64)
  1794. if e != nil {
  1795. resp.Fail(fmt.Sprintf("第%d行收款金额1有误, 请按模板导入", i+1), c)
  1796. return
  1797. }
  1798. rowInvoice4.Amount = amount
  1799. rowInvoices = append(rowInvoices, rowInvoice4)
  1800. continue
  1801. }
  1802. if k == 44 {
  1803. if v != "" {
  1804. va := cell.Value
  1805. invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1806. if e != nil {
  1807. d := utils.ConvertToFormatDay(va, "2006/01/02") //录入日期
  1808. invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1809. if e != nil {
  1810. resp.Fail(fmt.Sprintf("第%d行收款时间2格式转换有误, 请按模板导入", i+1), c)
  1811. return
  1812. }
  1813. }
  1814. if invoiceDate.Before(checkDate) {
  1815. resp.Fail(fmt.Sprintf("第%d行收款时间2格式有误, 请按模板导入", i+1), c)
  1816. return
  1817. }
  1818. rowInvoice5.InvoiceDate = invoiceDate
  1819. rowInvoice5.ContractCode = rowRegister.ContractCode
  1820. rowInvoice5.InvoiceType = fms.ContractInvoiceTypePay
  1821. }
  1822. continue
  1823. }
  1824. if k == 45 && v != "" {
  1825. amountStr := v
  1826. amount, e := strconv.ParseFloat(amountStr, 64)
  1827. if e != nil {
  1828. resp.Fail(fmt.Sprintf("第%d行收款金额2有误, 请按模板导入", i+1), c)
  1829. return
  1830. }
  1831. rowInvoice5.Amount = amount
  1832. rowInvoices = append(rowInvoices, rowInvoice5)
  1833. continue
  1834. }
  1835. if k == 46 {
  1836. if v != "" {
  1837. va := cell.Value
  1838. invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1839. if e != nil {
  1840. d := utils.ConvertToFormatDay(va, "2006/01/02") //录入日期
  1841. invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1842. if e != nil {
  1843. resp.Fail(fmt.Sprintf("第%d行收款时间3格式转换有误, 请按模板导入", i+1), c)
  1844. return
  1845. }
  1846. }
  1847. if invoiceDate.Before(checkDate) {
  1848. resp.Fail(fmt.Sprintf("第%d行收款时间3格式有误, 请按模板导入", i+1), c)
  1849. return
  1850. }
  1851. rowInvoice6.InvoiceDate = invoiceDate
  1852. rowInvoice6.ContractCode = rowRegister.ContractCode
  1853. rowInvoice6.InvoiceType = fms.ContractInvoiceTypePay
  1854. }
  1855. continue
  1856. }
  1857. if k == 47 && v != "" {
  1858. amountStr := v
  1859. amount, e := strconv.ParseFloat(amountStr, 64)
  1860. if e != nil {
  1861. resp.Fail(fmt.Sprintf("第%d行收款金额3有误, 请按模板导入", i+1), c)
  1862. return
  1863. }
  1864. rowInvoice6.Amount = amount
  1865. rowInvoices = append(rowInvoices, rowInvoice6)
  1866. continue
  1867. }
  1868. }
  1869. if isSkip {
  1870. continue
  1871. }
  1872. // 小套餐
  1873. if len(rowChartPermissionIdArr) > 0 {
  1874. // 说明有小套餐
  1875. tempItem := serviceTempNameMap["FICC小套餐"]
  1876. if tempItem == nil {
  1877. resp.Fail(fmt.Sprintf("第%d行小套餐名称不匹配, 请按模板导入", i+1), c)
  1878. return
  1879. }
  1880. rowChartPermissionIds := strings.Join(rowChartPermissionIdArr, ",")
  1881. cs := &fms.ContractService{
  1882. ProductId: crm.CompanyProductFicc,
  1883. ServiceTemplateId: tempItem.ServiceTemplateId,
  1884. Title: tempItem.Title,
  1885. Value: tempItem.Value,
  1886. TableValue: tempItem.TableValue,
  1887. ChartPermissionId: tempItem.ChartPermissionId,
  1888. ChartPermissionIds: rowChartPermissionIds,
  1889. }
  1890. cs.Set()
  1891. rowServices = append(rowServices, cs)
  1892. }
  1893. // 如果导入的最后一条合同编号为空并且合同编号之后的字段均为空
  1894. // excel这个包读行的时候不会再往后面没数据的地方读取, 所以此处需要重新判断一次
  1895. if rowRegister.ContractCode == "" {
  1896. resp.Fail(fmt.Sprintf("第%d行合同编号为空", i+1), c)
  1897. return
  1898. }
  1899. rowRegister.RegisterStatus = fms.ContractRegisterStatusIng
  1900. // 新增登记、套餐、开票到款信息
  1901. newId, e := fms.CreateImportContractRegister(rowRegister, rowServices, rowInvoices)
  1902. if e != nil {
  1903. resp.FailData(fmt.Sprintf("第%d行导入失败", i+1), "新增导入登记失败, Err: "+e.Error(), c)
  1904. return
  1905. }
  1906. newIds = append(newIds, newId)
  1907. }
  1908. }
  1909. }
  1910. // 更新登记状态
  1911. go func() {
  1912. for i := range newIds {
  1913. fmsService.CheckContractRegisterAmount(newIds[i])
  1914. }
  1915. }()
  1916. resp.Ok("操作成功", c)
  1917. }