register.go 56 KB

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