register.go 56 KB

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