register.go 56 KB

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