contract.go 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081
  1. package contract
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "github.com/shopspring/decimal"
  7. "github.com/tealeg/xlsx"
  8. "hongze/hz_crm_api/controllers"
  9. "hongze/hz_crm_api/models"
  10. "hongze/hz_crm_api/models/company"
  11. "hongze/hz_crm_api/models/contract"
  12. "hongze/hz_crm_api/models/contract/request"
  13. "hongze/hz_crm_api/models/contract/response"
  14. "hongze/hz_crm_api/services"
  15. contractService "hongze/hz_crm_api/services/contract"
  16. "hongze/hz_crm_api/services/seal"
  17. "hongze/hz_crm_api/utils"
  18. "os"
  19. "path"
  20. "path/filepath"
  21. "strconv"
  22. "strings"
  23. "time"
  24. )
  25. // 合同管理
  26. type ContractController struct {
  27. controllers.BaseAuthController
  28. }
  29. // GetServiceTemplateList
  30. // @Title 获取服务套餐列表
  31. // @Description 获取服务套餐列表
  32. // @Param ProductId query int false "客户类型:传0或者不传为当前账号权限,1 代表是:ficc;2 代表是:权益"
  33. // @Param ContractType query string false "合同类型:枚举值:'新签合同','续约合同','补充协议'"
  34. // @Success 200 {object} []contract.ContractServiceTemplateMapItems
  35. // @router /service/template/list [get]
  36. func (this *ContractController) GetServiceTemplateList() {
  37. br := new(models.BaseResponse).Init()
  38. defer func() {
  39. this.Data["json"] = br
  40. this.ServeJSON()
  41. }()
  42. productId, _ := this.GetInt("ProductId")
  43. contractType := this.GetString("ContractType")
  44. //没有传参时,默认是从账号中获取
  45. if productId == 0 {
  46. productId = services.GetProductId(this.SysUser.RoleTypeCode)
  47. }
  48. if productId == 0 {
  49. br.Msg = "参数异常"
  50. br.ErrMsg = "获取参数异常,获取合同类型异常,当前RoleTypeCode:" + this.SysUser.RoleTypeCode
  51. return
  52. }
  53. list, err := contract.GetContractServiceTemplateMapByProductId(productId)
  54. if err != nil {
  55. br.Msg = "获取失败"
  56. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  57. return
  58. }
  59. for i := 0; i < len(list); i++ {
  60. item := list[i]
  61. tmpList, err := contract.GetContractServiceTemplateMapByParentId(item.ServiceTemplateId)
  62. if err != nil {
  63. br.Msg = "获取失败"
  64. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  65. return
  66. }
  67. secondLen := len(tmpList)
  68. for j := 0; j < secondLen; j++ {
  69. detail, err := contract.GetContractServiceDetailByTemplateId(tmpList[j].ServiceTemplateId)
  70. if err != nil {
  71. br.Msg = "获取失败"
  72. br.ErrMsg = "获取详情模板失败,Err:" + err.Error()
  73. return
  74. }
  75. tmpList[j].Detail = detail
  76. // 权益存在第三级主客观套餐-CRM8.8
  77. thirdList, e := contract.GetContractServiceTemplateMapByParentId(tmpList[j].ServiceTemplateId)
  78. if e != nil {
  79. br.Msg = "获取失败"
  80. br.ErrMsg = "获取三级套餐失败, Err: " + e.Error()
  81. return
  82. }
  83. tmpList[j].Children = thirdList
  84. //if len(thirdList) > 0 {
  85. // if utils.InArrayByInt([]int{19, 20, 21, 22}, thirdList[0].ChartPermissionId) {
  86. // }
  87. //}
  88. }
  89. // 权益获取升级套餐-CRM13.2
  90. if productId == 2 {
  91. newTempList := make([]*contract.ContractServiceTemplateMapItems, 0)
  92. upgradeMap := make(map[int]*contract.ContractServiceTemplateMapItems)
  93. upgradeList, err := contract.GetContractServiceTemplateMapByChartPermissionId()
  94. if err != nil {
  95. br.Msg = "获取失败"
  96. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  97. return
  98. }
  99. for _, v := range upgradeList {
  100. upgradeMap[v.ChartPermissionId-100000] = v
  101. fmt.Println("v.ChartPermissionId-10000:", v.ChartPermissionId-100000)
  102. }
  103. for _, v := range tmpList {
  104. newTempList = append(newTempList, v)
  105. if len(v.Children) > 0 {
  106. if utils.InArrayByInt([]int{19, 20, 21, 22}, v.Children[0].ChartPermissionId) {
  107. newTempList = append(newTempList, upgradeMap[v.Children[0].ChartPermissionId])
  108. }
  109. }
  110. }
  111. tmpList = newTempList
  112. }
  113. detail, err := contract.GetContractServiceDetailByTemplateId(list[i].ServiceTemplateId)
  114. if err != nil {
  115. br.Msg = "获取失败"
  116. br.ErrMsg = "获取详情模板失败,Err:" + err.Error()
  117. return
  118. }
  119. if productId == 1 && list[i].ServiceTemplateId == 2 {
  120. // 特殊处理Ficc周报、商品双周报、数据点评的套餐显示
  121. // 把detail转成go struct
  122. // 查询默认的公有权限
  123. publicPermissionList, tmpE := models.GetFiccPermissionSecondPublic()
  124. if tmpE != nil {
  125. br.Msg = "获取失败"
  126. br.ErrMsg = "获取默认的公有权限失败,Err:" + tmpE.Error()
  127. return
  128. }
  129. publicPermissionIds := make([]int, 0)
  130. publicPermissionNames := make([]string, 0)
  131. for _, v := range publicPermissionList {
  132. publicPermissionIds = append(publicPermissionIds, v.ChartPermissionId)
  133. publicPermissionNames = append(publicPermissionNames, v.PermissionName)
  134. }
  135. for k, v := range detail {
  136. if v.Id == 4 || v.Id == 5 || v.Id == 6 {
  137. detailStruct := new(contract.ServiceTemplateDetailCol2)
  138. e := json.Unmarshal([]byte(v.Col2), detailStruct)
  139. if e != nil {
  140. br.Msg = "获取失败"
  141. br.ErrMsg = "获取套餐信息失败, json.Unmarshal Err: " + e.Error()
  142. return
  143. }
  144. //替换默认品种
  145. detailStruct.ValueId = publicPermissionIds
  146. detailStruct.Value = strings.Join(publicPermissionNames, ",")
  147. newCol2, e := json.Marshal(detailStruct)
  148. if e != nil {
  149. br.Msg = "获取失败"
  150. br.ErrMsg = "获取套餐信息失败, json.Marshal Err: " + e.Error()
  151. return
  152. }
  153. detail[k].Col2 = string(newCol2)
  154. }
  155. }
  156. }
  157. if productId == 1 && contractType == "补充协议" {
  158. for _, v := range detail {
  159. //ficc 的合同模板的 补充协议需要将默认的宏观经济给移除掉
  160. if utils.InArrayByInt([]int{4, 5, 6}, v.Id) {
  161. var tableCel request.AddContractServiceDetailReq
  162. tmpErr := json.Unmarshal([]byte(v.Col2), &tableCel)
  163. if tmpErr != nil {
  164. br.Msg = "获取失败"
  165. br.ErrMsg = "配置解析失败,Err:" + tmpErr.Error()
  166. return
  167. }
  168. tableCel.ValueId = make([]int, 0)
  169. tableCel.Value = ``
  170. //contract.
  171. tmpStr, tmpErr := json.Marshal(tableCel)
  172. if tmpErr != nil {
  173. br.Msg = "获取失败"
  174. br.ErrMsg = "修改json配置后,转字符串失败,Err:" + tmpErr.Error()
  175. return
  176. }
  177. v.Col2 = string(tmpStr)
  178. }
  179. //v.Col1
  180. }
  181. }
  182. list[i].Detail = detail
  183. list[i].Children = tmpList
  184. }
  185. br.Ret = 200
  186. br.Success = true
  187. br.Msg = "获取成功"
  188. br.Data = list
  189. }
  190. // GetContractTemplateList
  191. // @Title 获取合同模板列表
  192. // @Description 获取合同模板列表
  193. // @Success 200 {object} []contract.ContractTemplate
  194. // @router /template/list [get]
  195. func (this *ContractController) GetContractTemplateList() {
  196. br := new(models.BaseResponse).Init()
  197. defer func() {
  198. this.Data["json"] = br
  199. this.ServeJSON()
  200. }()
  201. productId := services.GetProductId(this.SysUser.RoleTypeCode)
  202. list, err := contract.GetContractTemplateMapByProductId(productId)
  203. if err != nil {
  204. br.Msg = "获取失败"
  205. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  206. return
  207. }
  208. br.Ret = 200
  209. br.Success = true
  210. br.Msg = "获取成功"
  211. br.Data = list
  212. }
  213. // GetPayChannel
  214. // @Title 获取合同模板列表
  215. // @Description 获取合同模板列表
  216. // @Param Keyword query string true "关键字"
  217. // @Success 200 {object} []contract.PayChannel
  218. // @router /pay_channel/list [get]
  219. func (this *ContractController) GetPayChannel() {
  220. br := new(models.BaseResponse).Init()
  221. defer func() {
  222. this.Data["json"] = br
  223. this.ServeJSON()
  224. }()
  225. keyword := this.GetString("Keyword", "")
  226. list, err := contract.GetPayChannelList(keyword)
  227. if err != nil {
  228. br.Msg = "获取失败"
  229. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  230. return
  231. }
  232. br.Ret = 200
  233. br.Success = true
  234. br.Msg = "获取成功"
  235. br.Data = list
  236. }
  237. // AddContract
  238. // @Title 新增合同
  239. // @Description 新增合同接口
  240. // @Param request body request.AddContractReq true "type json string"
  241. // @Success 200 {object} response.AddContractResp
  242. // @router /add [post]
  243. func (this *ContractController) AddContract() {
  244. br := new(models.BaseResponse).Init()
  245. defer func() {
  246. this.Data["json"] = br
  247. this.ServeJSON()
  248. }()
  249. sysUser := this.SysUser
  250. if sysUser == nil {
  251. br.Msg = "请登录"
  252. br.ErrMsg = "请登录,SysUser Is Empty"
  253. br.Ret = 408
  254. return
  255. }
  256. productId := services.GetProductId(sysUser.RoleTypeCode)
  257. if productId == 0 {
  258. br.Msg = "账户异常"
  259. br.ErrMsg = "账户异常,当前账户类型为:" + sysUser.RoleTypeCode
  260. br.Ret = 408
  261. return
  262. }
  263. var req request.AddContractReq
  264. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  265. if err != nil {
  266. br.Msg = "参数解析异常!"
  267. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  268. return
  269. }
  270. // 限制一下合同金额, 目前只支持人民币转大写只支持到千亿, 虽然生产使用中不可能到这么大, 但还是加上吧 =_=!
  271. maxPrice := float64(1000000000000)
  272. if req.OriginalPrice >= maxPrice || req.Price >= maxPrice {
  273. br.Msg = "合同金额过大, 请核验金额"
  274. return
  275. }
  276. contractInfo, err, errMsg := contractService.AddContract(req, sysUser.AdminId, productId, sysUser.RealName)
  277. if err != nil {
  278. br.Msg = "保存合同失败!"
  279. if errMsg != "" {
  280. br.Msg = errMsg
  281. }
  282. br.ErrMsg = "保存合同失败,Err:" + err.Error()
  283. return
  284. }
  285. msg := "添加成功"
  286. //如果是直接发起审批,那么
  287. if req.IsAudit {
  288. tmpErr, errMsg := contractService.Apply(contractInfo.ContractId)
  289. if tmpErr != nil {
  290. br.Msg = "提交审批失败!"
  291. if errMsg != "" {
  292. br.Msg = errMsg
  293. }
  294. br.ErrMsg = "提交审批失败,Err:" + tmpErr.Error()
  295. return
  296. }
  297. msg = "提交审批成功"
  298. }
  299. br.Ret = 200
  300. br.Success = true
  301. br.Msg = msg
  302. br.Data = response.AddContractResp{
  303. ContractId: contractInfo.ContractId,
  304. }
  305. }
  306. // List
  307. // @Title 合同列表
  308. // @Description 合同列表接口
  309. // @Param ContractBusinessType query string false "合同业务类型,枚举值:'业务合同','代付合同'"
  310. // @Param ContractType query string false "合同类型,枚举值:'新签合同','续约合同','补充协议'"
  311. // @Param ContractStatus query string false "合同状态,枚举值:'待提交','待审批','已撤回','已审批','已驳回','已作废'"
  312. // @Param ProductId query int false "客户类型:传0或者不传为当前账号权限,1 代表是:ficc;2 代表是:权益"
  313. // @Param ModifyStartTime query string false "服务更新时间的选择开始时间,格式:2021-05-23 00:00:00"
  314. // @Param ModifyEndTime query string false "服务更新时间的选择结束时间,格式:2021-05-26 23:59:59"
  315. // @Param SellerId query string false "选择的销售id"
  316. // @Param KeywordType query string false "搜索关键字类型,枚举值:'使用方','代付方'"
  317. // @Param Keyword query string false "搜索关键字"
  318. // @Param IsExport query bool false "是否导出excel,默认是false"
  319. // @Success 200 {object} response.ContractListResp
  320. // @router /list [get]
  321. func (this *ContractController) List() {
  322. br := new(models.BaseResponse).Init()
  323. defer func() {
  324. this.Data["json"] = br
  325. this.ServeJSON()
  326. }()
  327. sysUser := this.SysUser
  328. if sysUser == nil {
  329. br.Msg = "请登录"
  330. br.ErrMsg = "请登录,SysUser Is Empty"
  331. br.Ret = 408
  332. return
  333. }
  334. //合同类型、产品类型、合同状态、更新时间、所选销售
  335. //关键字:合同编号、客户名称,社会信用码
  336. contractType := this.GetString("ContractType")
  337. contractBusinessType := this.GetString("ContractBusinessType")
  338. contractStatus := this.GetString("ContractStatus")
  339. productId, _ := this.GetInt("ProductId")
  340. modifyStartTime := this.GetString("ModifyStartTime")
  341. modifyEndTime := this.GetString("ModifyEndTime")
  342. sellerIds := this.GetString("SellerId")
  343. keyword := this.GetString("Keyword")
  344. keywordType := this.GetString("KeywordType")
  345. condition := ""
  346. pars := make([]interface{}, 0)
  347. joinStr := "" //表连接
  348. productSlice := make([]int, 0) //需要下载的 产品 切片
  349. //如果不是超管或者合规,那么只能查看自己的合同
  350. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  351. condition += ` AND a.product_id = 1 AND a.status not in ("待提交","已撤回") `
  352. } else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  353. condition += ` AND a.product_id = 2 AND a.status not in ("待提交","已撤回") `
  354. } else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FINANCE { //超管可查看所有状态的合同(除过已撤回、待提交状态合同),可下载所有状态的合同
  355. condition += ` AND a.status not in ("待提交","已撤回") `
  356. } else {
  357. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_FINANCE {
  358. condition += ` AND a.seller_id = ? `
  359. pars = append(pars, sysUser.AdminId)
  360. }
  361. }
  362. //合同类型、、更新时间、所选销售
  363. //关键字:合同编号、客户名称,社会信用码
  364. if contractBusinessType != "" { //合同业务类型
  365. condition += ` AND a.contract_business_type = ? `
  366. pars = append(pars, contractBusinessType)
  367. }
  368. if contractType != "" {
  369. condition += ` AND a.contract_type = ? `
  370. pars = append(pars, contractType)
  371. }
  372. //合同状态
  373. if contractStatus != "" {
  374. condition += ` AND a.status = ? `
  375. pars = append(pars, contractStatus)
  376. }
  377. //产品类型
  378. if productId > 0 {
  379. condition += ` AND a.product_id = ? `
  380. pars = append(pars, productId)
  381. productSlice = append(productSlice, productId)
  382. } else {
  383. nowProductId := services.GetProductId(sysUser.RoleTypeCode)
  384. if nowProductId > 0 {
  385. productSlice = append(productSlice, nowProductId)
  386. } else {
  387. productSlice = append(productSlice, 1)
  388. productSlice = append(productSlice, 2)
  389. }
  390. }
  391. //所选销售
  392. if sellerIds != "" {
  393. condition += ` AND a.seller_id IN (` + sellerIds + `) `
  394. }
  395. //更新开始时间
  396. if modifyStartTime != "" {
  397. condition += ` AND a.modify_time >= ? `
  398. pars = append(pars, modifyStartTime)
  399. }
  400. //更新结束时间
  401. if modifyEndTime != "" {
  402. condition += ` AND a.modify_time <= ? `
  403. pars = append(pars, modifyEndTime)
  404. }
  405. //关键字
  406. //if keyword != "" {
  407. // condition += ` AND (contract_code LIKE '%` + keyword + `%' OR company_name LIKE '%` + keyword + `%' OR credit_code LIKE '%` + keyword + `%' ) `
  408. //}
  409. //搜索关键字和搜索类型同时不为空
  410. if keywordType != "" && keyword != "" {
  411. switch contractBusinessType {
  412. case "业务合同":
  413. if keywordType == "使用方" {
  414. condition += ` AND (a.contract_code LIKE '%` + keyword + `%' OR a.company_name LIKE '%` + keyword + `%' OR a.credit_code LIKE '%` + keyword + `%' ) `
  415. } else {
  416. joinStr = " join contract_relation b on a.contract_id=b.contract_id join contract c on b.payment_on_behalf_contract_id=c.contract_id "
  417. condition += ` AND (c.contract_code LIKE '%` + keyword + `%' OR c.company_name LIKE '%` + keyword + `%' OR c.credit_code LIKE '%` + keyword + `%' ) `
  418. //关联合同的查询条件
  419. condition += ` AND c.status in ("已签回","已审批") AND c.is_delete = 0 `
  420. }
  421. case "代付合同":
  422. if keywordType == "代付方" {
  423. condition += ` AND (a.contract_code LIKE '%` + keyword + `%' OR a.company_name LIKE '%` + keyword + `%' OR a.credit_code LIKE '%` + keyword + `%' ) `
  424. } else {
  425. joinStr = " join contract_relation b on a.contract_id=b.payment_on_behalf_contract_id join contract c on b.contract_id=c.contract_id "
  426. condition += ` AND (c.contract_code LIKE '%` + keyword + `%' OR c.company_name LIKE '%` + keyword + `%' OR c.credit_code LIKE '%` + keyword + `%' ) `
  427. //关联合同的查询条件
  428. condition += ` AND c.status in ("已签回","已审批") AND c.is_delete = 0 `
  429. }
  430. }
  431. }
  432. pageSize, _ := this.GetInt("PageSize")
  433. currentIndex, _ := this.GetInt("CurrentIndex")
  434. var startSize int
  435. if pageSize <= 0 {
  436. pageSize = utils.PageSize20
  437. }
  438. if currentIndex <= 0 {
  439. currentIndex = 1
  440. }
  441. startSize = paging.StartIndex(currentIndex, pageSize)
  442. //是否导出报表
  443. isExport, _ := this.GetBool("IsExport")
  444. if isExport {
  445. pageSize = 10000
  446. currentIndex = 1
  447. }
  448. total, err := contract.GetContractListCount(condition, joinStr, pars)
  449. if err != nil {
  450. br.Msg = "获取失败"
  451. br.ErrMsg = "获取数据总数失败,Err:" + err.Error()
  452. return
  453. }
  454. list, err := contract.GetContractList(condition, joinStr, pars, startSize, pageSize)
  455. if err != nil {
  456. br.Msg = "获取合同列表失败!"
  457. br.ErrMsg = "获取合同列表失败,Err:" + err.Error()
  458. return
  459. }
  460. //获取合同套餐服务列表
  461. serviceTemplateList, err := contract.GetAllContractServiceTemplateList()
  462. if err != nil {
  463. br.Msg = "获取合同套餐服务列表失败!"
  464. br.ErrMsg = "获取合同套餐服务列表失败,Err:" + err.Error()
  465. return
  466. }
  467. templateTitleMap := make(map[int]string)
  468. for _, serviceTemplate := range serviceTemplateList {
  469. templateTitleMap[serviceTemplate.ServiceTemplateId] = serviceTemplate.Title
  470. }
  471. //业务合同的关联合同id集合
  472. businessContractIdList := make([]string, 0)
  473. //代付合同的关联合同id集合
  474. paymentOnBehalfContractIdList := make([]string, 0)
  475. //所有合同id集合(用于查询服务内容)
  476. allServiceContractIdList := make([]string, 0)
  477. //基础合同的关联合同数据
  478. contractRelationMap := make(map[int][]*contract.RelationContractList)
  479. // 权益合同map
  480. equityContractMap := make(map[int]bool)
  481. for _, v := range list {
  482. //合同id
  483. allServiceContractIdList = append(allServiceContractIdList, fmt.Sprint(v.ContractId))
  484. switch v.ContractBusinessType {
  485. case "业务合同":
  486. businessContractIdList = append(businessContractIdList, fmt.Sprint(v.ContractId))
  487. case "代付合同":
  488. paymentOnBehalfContractIdList = append(paymentOnBehalfContractIdList, fmt.Sprint(v.ContractId))
  489. }
  490. // 权益合同
  491. if v.ProductId == 2 {
  492. equityContractMap[v.ContractId] = true
  493. }
  494. }
  495. //业务合同的关联合同
  496. if len(businessContractIdList) > 0 {
  497. contractIdStr := strings.Join(businessContractIdList, ",")
  498. businessContractList, err := contract.GetContractRelationListByRelationContractIds(contractIdStr)
  499. if err != nil {
  500. br.Msg = "获取失败"
  501. br.ErrMsg = "获取业务关联合同失败,Err:" + err.Error()
  502. return
  503. }
  504. for _, v := range businessContractList {
  505. //代付合同id
  506. allServiceContractIdList = append(allServiceContractIdList, fmt.Sprint(v.PaymentOnBehalfContractId))
  507. if _, ok := contractRelationMap[v.ContractId]; ok {
  508. contractRelationMap[v.ContractId] = append(contractRelationMap[v.ContractId], v)
  509. } else {
  510. tmpList := make([]*contract.RelationContractList, 0)
  511. contractRelationMap[v.ContractId] = append(tmpList, v)
  512. }
  513. }
  514. }
  515. //代付合同的关联合同
  516. if len(paymentOnBehalfContractIdList) > 0 {
  517. paymentOnBehalfContractIdStr := strings.Join(paymentOnBehalfContractIdList, ",")
  518. paymentOnBehalfContractList, err := contract.GetContractRelationListByPaymentOnBehalfContractIds(paymentOnBehalfContractIdStr)
  519. if err != nil {
  520. br.Msg = "获取失败"
  521. br.ErrMsg = "获取代付关联合同失败,Err:" + err.Error()
  522. return
  523. }
  524. for _, v := range paymentOnBehalfContractList {
  525. //业务合同id
  526. allServiceContractIdList = append(allServiceContractIdList, fmt.Sprint(v.ContractId))
  527. if _, ok := contractRelationMap[v.PaymentOnBehalfContractId]; ok {
  528. contractRelationMap[v.PaymentOnBehalfContractId] = append(contractRelationMap[v.PaymentOnBehalfContractId], v)
  529. } else {
  530. tmpList := make([]*contract.RelationContractList, 0)
  531. contractRelationMap[v.PaymentOnBehalfContractId] = append(tmpList, v)
  532. }
  533. }
  534. }
  535. //合同套餐服务列表
  536. serviceListMap := make(map[int][]*contract.ContractServiceAndDetail, 0)
  537. // 权益合同
  538. if len(allServiceContractIdList) > 0 {
  539. allRelationIdStr := strings.Join(allServiceContractIdList, ",")
  540. tmpServiceList, err := contract.GetContractServiceAndDetailListByIds(allRelationIdStr)
  541. if err != nil {
  542. br.Msg = "获取失败"
  543. br.ErrMsg = "获取关联合同服务详情失败,Err:" + err.Error()
  544. return
  545. }
  546. allContractServiceIdList := make([]string, 0)
  547. serviceDetailListMap := make(map[int][]*contract.ContractServiceDetail, 0)
  548. for _, service := range tmpServiceList {
  549. if service.HasDetail == "是" {
  550. allContractServiceIdList = append(allContractServiceIdList, strconv.Itoa(service.ContractServiceId))
  551. }
  552. }
  553. if len(allContractServiceIdList) > 0 {
  554. allContractServiceIdStr := strings.Join(allContractServiceIdList, ",")
  555. list, detailErr := contract.GetContractServiceDetailListByServiceIds(allContractServiceIdStr)
  556. if detailErr != nil {
  557. br.Msg = "获取失败"
  558. br.ErrMsg = "查找合同服务详情异常,Err:" + detailErr.Error()
  559. return
  560. }
  561. for _, v := range list {
  562. if _, ok := serviceDetailListMap[v.ContractServiceId]; ok {
  563. serviceDetailListMap[v.ContractServiceId] = append(serviceDetailListMap[v.ContractServiceId], v)
  564. } else {
  565. tmpList := make([]*contract.ContractServiceDetail, 0)
  566. serviceDetailListMap[v.ContractServiceId] = append(tmpList, v)
  567. }
  568. }
  569. }
  570. for _, service := range tmpServiceList {
  571. if service.HasDetail == "是" {
  572. if serviceDetailList, ok := serviceDetailListMap[service.ContractServiceId]; ok {
  573. service.DetailList = serviceDetailList
  574. }
  575. //套餐名称
  576. if title, ok := templateTitleMap[service.ServiceTemplateId]; ok {
  577. service.Title = title
  578. }
  579. }
  580. //服务列表
  581. if _, ok := serviceListMap[service.ContractId]; ok {
  582. serviceListMap[service.ContractId] = append(serviceListMap[service.ContractId], service)
  583. } else {
  584. tmpList := make([]*contract.ContractServiceAndDetail, 0)
  585. serviceListMap[service.ContractId] = append(tmpList, service)
  586. }
  587. }
  588. // CRM8.8-权益合同主客观合并
  589. for k, v := range serviceListMap {
  590. if equityContractMap[k] {
  591. mergeList := contractService.EquityMergeSubjectAndObject(v)
  592. serviceListMap[k] = mergeList
  593. }
  594. }
  595. }
  596. //关联合同
  597. for _, contractRelationList := range contractRelationMap {
  598. for _, contractRelation := range contractRelationList {
  599. //服务权限列表
  600. // 关联合同同样合并
  601. if serviceList, ok := serviceListMap[contractRelation.ContractId]; ok {
  602. relationMergeList := contractService.EquityMergeSubjectAndObject(serviceList)
  603. contractRelation.Service = relationMergeList
  604. }
  605. }
  606. }
  607. for i := 0; i < len(list); i++ {
  608. //合同操作权限
  609. var opButton contract.ContractOpButton
  610. item := list[i]
  611. list[i].StartDateStr = item.StartDate.Format(utils.FormatDate)
  612. list[i].EndDateStr = item.EndDate.Format(utils.FormatDate)
  613. list[i].CreateTimeStr = item.CreateTime.Format(utils.FormatDateTime)
  614. list[i].ModifyTimeStr = item.ModifyTime.Format(utils.FormatDateTime)
  615. list[i].ApproveTimeStr = item.ApproveTime.Format(utils.FormatDateTime)
  616. list[i].InvalidTimeStr = item.InvalidTime.Format(utils.FormatDateTime)
  617. list[i].CheckBackFileTimeStr = item.CheckBackFileTime.Format(utils.FormatDateTime)
  618. list[i].RescindTimeStr = item.RescindTime.Format(utils.FormatDateTime)
  619. //服务列表
  620. if serviceList, ok := serviceListMap[item.ContractId]; ok {
  621. item.Service = serviceList
  622. }
  623. //serviceList, err := contract.GetContractServiceAndDetailList(item.ContractId)
  624. //if err != nil {
  625. // br.Msg = "获取失败"
  626. // br.ErrMsg = "查找合同服务异常,Err:" + err.Error()
  627. // return
  628. //}
  629. //for i := 0; len(serviceList) > i; i++ {
  630. // if serviceList[i].HasDetail == "是" {
  631. // list, detailErr := contract.GetContractServiceDetailListByServiceId(serviceList[i].ContractServiceId)
  632. // if detailErr != nil {
  633. // br.Msg = "获取失败"
  634. // br.ErrMsg = "查找合同服务详情异常,Err:" + err.Error()
  635. // return
  636. // }
  637. //
  638. // //套餐名称
  639. // serviceList[i].DetailList = list
  640. // if title, ok := templateTitleMap[serviceList[i].ServiceTemplateId]; ok {
  641. // serviceList[i].Title = title
  642. // }
  643. // }
  644. //}
  645. //list[i].Service = serviceList
  646. if item.Status == "待审批" {
  647. approvalInfo, tmpErr := contract.GetLastContractApprovalByContractId(item.ContractId, "contract")
  648. if tmpErr != nil {
  649. //err= tmpErr
  650. //return
  651. continue
  652. }
  653. //如果合同归属是当前账号,且处于第一级审批
  654. if item.SellerId == sysUser.AdminId && approvalInfo.CurrNodeId == approvalInfo.StartNodeId {
  655. opButton.Cancel = true
  656. }
  657. }
  658. //更新附件权限
  659. if item.SellerId == sysUser.AdminId && item.Status == "已审批" {
  660. opButton.UpdateFile = true
  661. }
  662. list[i].OpButton = opButton
  663. //关联合同
  664. if relationContractList, ok := contractRelationMap[item.ContractId]; ok {
  665. item.RelationContractList = relationContractList
  666. //list[i].RelationContractList = relationContractList
  667. }
  668. }
  669. page := paging.GetPaging(currentIndex, pageSize, total)
  670. //导出excel
  671. if isExport {
  672. contractListExport(this, list, productSlice, contractBusinessType, br)
  673. return
  674. }
  675. br.Ret = 200
  676. br.Success = true
  677. br.Msg = "获取成功"
  678. br.Data = response.ContractListResp{
  679. List: list,
  680. Paging: page,
  681. }
  682. }
  683. // contractListExport 合同列表数据导出
  684. func contractListExport(this *ContractController, list []*contract.ContractList, productSlice []int, contractBusinessType string, br *models.BaseResponse) {
  685. //产品类型
  686. //获取合同套餐服务列表
  687. serviceTemplateList, err := contract.GetAllContractServiceTemplateList()
  688. if err != nil {
  689. br.Msg = "获取合同套餐服务列表失败!"
  690. br.ErrMsg = "获取合同套餐服务列表失败,Err:" + err.Error()
  691. return
  692. }
  693. templateTitleMap := make(map[int]string)
  694. for _, serviceTemplate := range serviceTemplateList {
  695. templateTitleMap[serviceTemplate.ServiceTemplateId] = serviceTemplate.Title
  696. }
  697. //生成excel文件
  698. dir, err := os.Executable()
  699. exPath := filepath.Dir(dir)
  700. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  701. xlsxFile := xlsx.NewFile()
  702. if err != nil {
  703. br.Msg = "生成文件失败"
  704. br.ErrMsg = "生成文件失败"
  705. return
  706. }
  707. //设置默认字体和文字大小
  708. xlsx.SetDefaultFont(12, "宋体")
  709. //定义边框样式
  710. border := xlsx.NewBorder("thin", "thin", "thin", "thin")
  711. //定义文字排版样式
  712. alignment := xlsx.Alignment{
  713. Horizontal: "center",
  714. Vertical: "center",
  715. WrapText: true,
  716. }
  717. //普通样式
  718. style := xlsx.NewStyle()
  719. style.Alignment = alignment
  720. style.ApplyAlignment = true
  721. style.Border = *border
  722. //标红文字,便于查看哪些产品被勾选
  723. redStyle := xlsx.NewStyle()
  724. redStyle.Alignment = alignment
  725. redStyle.ApplyAlignment = true
  726. redStyle.Font.Color = "ff0000"
  727. //定义底色需要标黄的 单元格颜色
  728. redFill := xlsx.Fill{"solid", "ffff00", "ffff00"}
  729. redStyle.Fill = redFill
  730. redStyle.Border = *border
  731. //标题样式
  732. titleStyle := xlsx.NewStyle()
  733. titleFont := xlsx.NewFont(14, "宋体")
  734. titleFont.Bold = true
  735. titleStyle.Font = *titleFont
  736. titleStyle.Alignment = alignment
  737. titleStyle.Border = *border
  738. //titleStyle.ApplyAlignment = true
  739. //表头
  740. headerStyle := xlsx.NewStyle()
  741. headerFont := xlsx.NewFont(12, "宋体")
  742. headerFont.Bold = true
  743. headerStyle.Font = *headerFont
  744. headerStyle.Alignment = alignment
  745. headerStyle.ApplyAlignment = true
  746. headerStyle.Border = *border
  747. defer func() {
  748. os.Remove(downLoadnFilePath)
  749. }()
  750. //遍历所拥有的的产品权限,然后插入到对应的sheet表单中
  751. for pidIndex := 0; pidIndex < len(productSlice); pidIndex++ {
  752. productId := productSlice[pidIndex]
  753. //表单名称
  754. sheetName := "合同数据"
  755. switch productId {
  756. case 1:
  757. sheetName = "ficc合同信息"
  758. case 2:
  759. sheetName = "权益合同信息"
  760. }
  761. //新增一个sheet表单
  762. sheel, err := xlsxFile.AddSheet(sheetName)
  763. if err != nil {
  764. br.Msg = "新增Sheet失败"
  765. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  766. return
  767. }
  768. //设置列宽
  769. sheel.SetColWidth(0, 0, 28)
  770. sheel.SetColWidth(1, 1, 30)
  771. sheel.SetColWidth(2, 2, 18)
  772. sheel.SetColWidth(3, 3, 18)
  773. sheel.SetColWidth(4, 4, 35)
  774. sheel.SetColWidth(5, 6, 20)
  775. sheel.SetColWidth(7, 7, 40)
  776. //合同信息列
  777. endContractInfoColIndex := 8
  778. sheel.SetColWidth(8, 8, 40)
  779. //权限列
  780. sheel.SetColWidth(endContractInfoColIndex, endContractInfoColIndex+18, 30)
  781. //标题行
  782. titleRow := sheel.AddRow()
  783. titleRow.SetHeight(30)
  784. //合同信息列
  785. titleCell := titleRow.AddCell()
  786. titleCell.HMerge = endContractInfoColIndex //向右合并列数,不包括自身列
  787. //遍历去添加需要合并的列
  788. for i := 0; i < endContractInfoColIndex; i++ {
  789. titleRow.AddCell()
  790. }
  791. //报表标题名称
  792. titleCell.SetValue("合同信息")
  793. titleCell.SetStyle(titleStyle)
  794. startI := endContractInfoColIndex + 1
  795. customStartCellI := endContractInfoColIndex + 1
  796. serviceSlice := make([]string, 0)
  797. serviceCellMap := make(map[string]int)
  798. //获取所有套餐列表
  799. {
  800. serviceList, err := getServiceTemplateList(productId)
  801. if err != nil {
  802. br.Msg = "查询套餐类型失败"
  803. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  804. return
  805. }
  806. for i := 0; i < len(serviceList); i++ {
  807. //需要向右合并添加的列数
  808. addCellNum := 0
  809. item := serviceList[i]
  810. //套餐服务列
  811. servicesCell := titleRow.AddCell()
  812. //套餐名称
  813. servicesCell.SetValue(item.Title)
  814. servicesCell.SetStyle(titleStyle)
  815. //ficc小套餐
  816. if item.ServiceTemplateId == 2 {
  817. productPermission := make(map[int]string)
  818. productPermission[1] = "每日宏观商品复盘"
  819. productPermission[2] = "能化专栏《化里化外》"
  820. productPermission[3] = "股债日评"
  821. productPermission[4] = "黑色专栏《知白守黑》"
  822. productPermission[5] = "有色专栏《有声有色》"
  823. for id, name := range productPermission {
  824. key := fmt.Sprint(item.ServiceTemplateId, "_product_permission_", id)
  825. serviceCellMap[key] = startI
  826. serviceSlice = append(serviceSlice, name)
  827. startI++
  828. addCellNum++
  829. }
  830. permissionList := make([]*company.PermissionSetItem, 0)
  831. allPermissions, permissionMap, e := services.GetBaseFiccPermissionSetItem()
  832. if e != nil {
  833. br.Msg = "查询基础权限失败"
  834. br.ErrMsg = "查询基础权限失败,Err:" + e.Error()
  835. return
  836. }
  837. //遍历获取
  838. for _, v := range allPermissions {
  839. if v.ParentId == 0 {
  840. items, ok := permissionMap[v.ChartPermissionId]
  841. if ok {
  842. permissionList = append(permissionList, items...)
  843. }
  844. }
  845. }
  846. //权限列表
  847. for j := 0; j < len(permissionList); j++ {
  848. key := fmt.Sprint(item.ServiceTemplateId, "_permission_", permissionList[j].ChartPermissionId)
  849. serviceCellMap[key] = startI
  850. serviceSlice = append(serviceSlice, permissionList[j].PermissionName)
  851. startI++
  852. addCellNum++
  853. }
  854. //因为自己本身有一列了,所以在最后增加列的时候,需要减去自身列
  855. addCellNum = addCellNum - 1
  856. if addCellNum > 0 {
  857. for tmpAddCellNum := 0; tmpAddCellNum < addCellNum; tmpAddCellNum++ {
  858. titleRow.AddCell()
  859. }
  860. servicesCell.HMerge = addCellNum //向右合并列数,不包括自身列
  861. }
  862. continue
  863. } else {
  864. if item.Children != nil && len(item.Children) > 0 {
  865. for j := 0; j < len(item.Children); j++ {
  866. key := fmt.Sprint("service_template_", item.Children[j].ServiceTemplateId)
  867. serviceCellMap[key] = startI
  868. serviceSlice = append(serviceSlice, item.Children[j].Title)
  869. startI++
  870. addCellNum++
  871. }
  872. //因为自己本身有一列了,所以在最后增加列的时候,需要减去自身列
  873. addCellNum = addCellNum - 1
  874. if addCellNum > 0 {
  875. for tmpAddCellNum := 0; tmpAddCellNum < addCellNum; tmpAddCellNum++ {
  876. titleRow.AddCell()
  877. }
  878. servicesCell.HMerge = addCellNum //向右合并列数,不包括自身列
  879. }
  880. } else {
  881. key := fmt.Sprint("service_template_", item.ServiceTemplateId)
  882. serviceCellMap[key] = startI
  883. serviceSlice = append(serviceSlice, item.Title)
  884. startI++
  885. }
  886. }
  887. }
  888. }
  889. //表头
  890. headerRow := sheel.AddRow()
  891. headerRow.SetHeight(24)
  892. //合同基础信息
  893. {
  894. codeHeaderCell := headerRow.AddCell()
  895. codeHeaderCell.SetValue("合同编号")
  896. codeHeaderCell.SetStyle(headerStyle)
  897. companyHeaderCell := headerRow.AddCell()
  898. companyHeaderStr := "客户名称"
  899. if contractBusinessType == "代付合同" {
  900. companyHeaderStr = "代付方"
  901. }
  902. companyHeaderCell.SetValue(companyHeaderStr)
  903. companyHeaderCell.SetStyle(headerStyle)
  904. typeHeaderCell := headerRow.AddCell()
  905. typeHeaderCell.SetValue("合同类型")
  906. typeHeaderCell.SetStyle(headerStyle)
  907. priceHeaderCell := headerRow.AddCell()
  908. priceHeaderCell.SetValue("合同金额")
  909. priceHeaderCell.SetStyle(headerStyle)
  910. dateHeaderCell := headerRow.AddCell()
  911. dateHeaderCell.SetValue("合同有效期")
  912. dateHeaderCell.SetStyle(headerStyle)
  913. sellerHeaderCell := headerRow.AddCell()
  914. sellerHeaderCell.SetValue("销售")
  915. sellerHeaderCell.SetStyle(headerStyle)
  916. statusHeaderCell := headerRow.AddCell()
  917. statusHeaderCell.SetValue("合同状态")
  918. statusHeaderCell.SetStyle(headerStyle)
  919. //关联合同信息
  920. payHeaderCell := headerRow.AddCell()
  921. payHeaderStr := "付款方"
  922. if contractBusinessType == "代付合同" {
  923. payHeaderStr = "客户名称"
  924. }
  925. payHeaderCell.SetValue(payHeaderStr)
  926. payHeaderCell.SetStyle(headerStyle)
  927. remarkHeaderCell := headerRow.AddCell()
  928. remarkHeaderCell.SetValue("备注")
  929. remarkHeaderCell.SetStyle(headerStyle)
  930. }
  931. //添加服务套餐名称
  932. for _, service := range serviceSlice {
  933. cell := headerRow.AddCell()
  934. cell.SetValue(service)
  935. cell.SetStyle(headerStyle)
  936. }
  937. for _, v := range list {
  938. //如果当前合同不是该产品
  939. if v.ProductId != productId {
  940. continue
  941. }
  942. dataRow := sheel.AddRow()
  943. dataRow.SetHeight(24)
  944. //合同基础信息
  945. {
  946. codeCell := dataRow.AddCell()
  947. codeCell.SetValue(v.ContractCode)
  948. codeCell.SetStyle(style)
  949. companyCell := dataRow.AddCell()
  950. companyCell.SetValue(v.CompanyName)
  951. companyCell.SetStyle(style)
  952. typeCell := dataRow.AddCell()
  953. typeCell.SetValue(v.ContractType)
  954. typeCell.SetStyle(style)
  955. priceCell := dataRow.AddCell()
  956. priceCell.SetValue(v.Price)
  957. priceCell.SetStyle(style)
  958. dateCell := dataRow.AddCell()
  959. dateCell.SetValue(fmt.Sprint(v.StartDate.Format(utils.FormatDate), " 至 ", v.EndDate.Format(utils.FormatDate)))
  960. dateCell.SetStyle(style)
  961. sellerCell := dataRow.AddCell()
  962. sellerCell.SetValue(v.SellerName)
  963. sellerCell.SetStyle(style)
  964. //合同状态
  965. statusCell := dataRow.AddCell()
  966. statusCell.SetValue(v.Status)
  967. statusCell.SetStyle(style)
  968. //关联合同客户信息
  969. relationCompanyName := ``
  970. if v.RelationContractList != nil && len(v.RelationContractList) > 0 {
  971. payCompanyNameList := make([]string, 0)
  972. for _, relationContract := range v.RelationContractList {
  973. payCompanyNameList = append(payCompanyNameList, relationContract.CompanyName)
  974. }
  975. relationCompanyName = strings.Join(payCompanyNameList, ",")
  976. } else {
  977. relationCompanyName = v.PayChannel
  978. }
  979. payCell := dataRow.AddCell()
  980. payCell.SetValue(relationCompanyName)
  981. payCell.SetStyle(style)
  982. //备注:该字段由三部分拼接:服务内容中的补充内容+赠送时间+优惠金额
  983. //补充内容直接取该字段内容;
  984. //赠送时间以年为单位,超过整年的月份作为赠送月份,如14个月即为赠送2个月;
  985. //优惠金额等于原价-优惠后价格=优惠价格
  986. //补充内容
  987. var remark string
  988. if v.Remark != "" {
  989. remark += "补充内容:" + v.Remark
  990. }
  991. //赠送时间
  992. //合同结束日期与合同开始日期的时间差(小时差)
  993. newDecimal := decimal.NewFromFloat(v.EndDate.Sub(v.StartDate).Hours())
  994. //分母为365天 * 24 小时
  995. newDecimal2 := decimal.NewFromInt(24 * 365)
  996. //计算出来相差多少年,保留一位小数(四舍五入)
  997. numYearDecimal := newDecimal.Div(newDecimal2).Round(1).Mul(decimal.NewFromInt(10))
  998. //如果大于1年,那么才去判断是否赠送
  999. numYear, _ := numYearDecimal.Float64()
  1000. if numYear > 10 {
  1001. //获取多出来的整数年数据
  1002. numYearDecimal = numYearDecimal.Mod(decimal.NewFromInt(10)).Div(decimal.NewFromInt(10))
  1003. //计算赠送出来的月份
  1004. numMonthDecimal := numYearDecimal.Mul(decimal.NewFromInt(12)).Round(0)
  1005. numMonth, _ := numMonthDecimal.Float64()
  1006. if numMonth > 0 {
  1007. if remark != "" {
  1008. remark += ";"
  1009. }
  1010. remark += "赠送时间为:" + numMonthDecimal.String() + "个月"
  1011. }
  1012. }
  1013. //优惠金额
  1014. if v.OriginalPrice > v.Price {
  1015. differencePrice := v.OriginalPrice - v.Price
  1016. differencePriceDecimal := decimal.NewFromFloat(differencePrice)
  1017. if remark != "" {
  1018. remark += ";"
  1019. }
  1020. remark += "优惠金额:" + differencePriceDecimal.String() + "元。"
  1021. }
  1022. remarkCell := dataRow.AddCell()
  1023. remarkCell.SetValue(remark)
  1024. remarkCell.SetStyle(style)
  1025. }
  1026. //服务套餐
  1027. {
  1028. tmpServiceList := v.Service
  1029. if contractBusinessType == "代付合同" {
  1030. if v.RelationContractList != nil && len(v.RelationContractList) > 0 {
  1031. tmpServiceList = v.RelationContractList[0].Service
  1032. }
  1033. }
  1034. cellServiceMap := make(map[int]string)
  1035. for _, cellI := range serviceCellMap {
  1036. cellServiceMap[cellI] = "否"
  1037. }
  1038. if tmpServiceList != nil && len(tmpServiceList) > 0 {
  1039. permissionValues := []string{
  1040. "FICC周报", "商品双周报+线上电话会讨论会<br/>(由弘则的研究员主持线上讨论)", "数据点评", "深度月报:包括宏观经济月报和下游草根调研月报+电话会讨论会<br/>(由弘则的研究员主持线上讨论)",
  1041. }
  1042. for _, item := range tmpServiceList {
  1043. //ficc小套餐
  1044. if item.ServiceTemplateId == 2 {
  1045. for _, detail := range item.DetailList {
  1046. var tableCel request.AddContractServiceDetailReq
  1047. tmpErr := json.Unmarshal([]byte(detail.Col1), &tableCel)
  1048. if tmpErr != nil {
  1049. err = tmpErr
  1050. return
  1051. }
  1052. if tableCel.Value == "商品复盘" {
  1053. var tableCel2 request.AddContractServiceDetailReq
  1054. //获取品种列数据
  1055. tmpErr := contractService.GetProductCelData(*detail, &tableCel2)
  1056. if tmpErr != nil {
  1057. err = tmpErr
  1058. return
  1059. }
  1060. //tableCel2.Tag
  1061. for _, serviceId := range tableCel2.ValueId {
  1062. key := fmt.Sprint(item.ServiceTemplateId, "_product_permission_", serviceId)
  1063. if cellI, ok := serviceCellMap[key]; ok {
  1064. cellServiceMap[cellI] = "是"
  1065. }
  1066. }
  1067. } else if strings.Contains(strings.Join(permissionValues, ","), tableCel.Value) {
  1068. //获取品种列数据
  1069. var tableCel2 request.AddContractServiceDetailReq
  1070. tmpErr := contractService.GetProductCelData(*detail, &tableCel2)
  1071. if tmpErr != nil {
  1072. err = tmpErr
  1073. return
  1074. }
  1075. for _, serviceId := range tableCel2.ValueId {
  1076. key := fmt.Sprint(item.ServiceTemplateId, "_permission_", serviceId)
  1077. if cellI, ok := serviceCellMap[key]; ok {
  1078. cellServiceMap[cellI] = "是"
  1079. }
  1080. }
  1081. }
  1082. }
  1083. } else {
  1084. key := fmt.Sprint("service_template_", item.ServiceTemplateId)
  1085. if cellI, ok := serviceCellMap[key]; ok {
  1086. cellServiceMap[cellI] = "是"
  1087. }
  1088. }
  1089. }
  1090. }
  1091. //遍历所有套餐
  1092. for tmpStartCellI := customStartCellI; tmpStartCellI < startI; tmpStartCellI++ {
  1093. servicesCell := dataRow.AddCell()
  1094. cellValue, ok := cellServiceMap[tmpStartCellI]
  1095. if ok == false {
  1096. cellValue = "否"
  1097. }
  1098. servicesCell.SetValue(cellValue)
  1099. if cellValue == "是" { //勾选了话,标红处理
  1100. servicesCell.SetStyle(redStyle)
  1101. } else {
  1102. servicesCell.SetStyle(style)
  1103. }
  1104. }
  1105. }
  1106. }
  1107. }
  1108. err = xlsxFile.Save(downLoadnFilePath)
  1109. if err != nil {
  1110. br.Msg = "保存文件失败"
  1111. br.ErrMsg = "保存文件失败"
  1112. return
  1113. }
  1114. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1115. downloadFileName := "合同列表" + randStr + ".xlsx"
  1116. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  1117. br.Ret = 200
  1118. br.Success = true
  1119. br.Msg = "导出成功"
  1120. }
  1121. // OperationList
  1122. // @Title 获取合同操作记录
  1123. // @Description 合同操作记录接口
  1124. // @Param ContractId query int true "合同id编号"
  1125. // @Success 200 {object} response.ContractOperationListResp
  1126. // @router /operation/list [get]
  1127. func (this *ContractController) OperationList() {
  1128. br := new(models.BaseResponse).Init()
  1129. defer func() {
  1130. this.Data["json"] = br
  1131. this.ServeJSON()
  1132. }()
  1133. sysUser := this.SysUser
  1134. if sysUser == nil {
  1135. br.Msg = "请登录"
  1136. br.ErrMsg = "请登录,SysUser Is Empty"
  1137. br.Ret = 408
  1138. return
  1139. }
  1140. //合同类型、产品类型、合同状态、更新时间、所选销售
  1141. //关键字:合同编号、客户名称,社会信用码
  1142. contractId, err := this.GetInt("ContractId")
  1143. if err != nil {
  1144. br.Msg = "获取合同编号失败"
  1145. br.ErrMsg = "获取合同编号失败,err:" + err.Error()
  1146. return
  1147. }
  1148. if contractId <= 0 {
  1149. br.Msg = "请传入合同编号"
  1150. br.ErrMsg = "请传入合同编号"
  1151. return
  1152. }
  1153. list, err := contract.GetContractOperationRListByContractId(contractId)
  1154. if err != nil {
  1155. br.Msg = "获取合同操作记录列表失败!"
  1156. br.ErrMsg = "获取合同操作记录列表失败,Err:" + err.Error()
  1157. return
  1158. }
  1159. contractOperationRecordList := make([]response.ContractOperationRecordListResp, 0)
  1160. for i := 0; i < len(list); i++ {
  1161. contractOperationRecord := response.ContractOperationRecordListResp{
  1162. Id: list[i].Id,
  1163. ContractId: list[i].ContractId,
  1164. Operation: list[i].Operation,
  1165. OpUserId: list[i].OpUserId,
  1166. OpUserName: list[i].OpUserName,
  1167. Remark: list[i].Remark,
  1168. ApprovalRecordId: list[i].ApprovalRecordId,
  1169. CreateTime: list[i].CreateTime,
  1170. CreateTimeStr: list[i].CreateTime.Format(utils.FormatDateTime),
  1171. }
  1172. contractOperationRecordList = append(contractOperationRecordList, contractOperationRecord)
  1173. }
  1174. br.Ret = 200
  1175. br.Success = true
  1176. br.Msg = "获取成功"
  1177. br.Data = response.ContractOperationListResp{
  1178. List: contractOperationRecordList,
  1179. }
  1180. }
  1181. // EditContract
  1182. // @Title 编辑合同
  1183. // @Description 编辑合同接口
  1184. // @Param request body request.EditContractReq true "type json string"
  1185. // @Success 200 {object} response.AddContractResp
  1186. // @router /edit [post]
  1187. func (this *ContractController) EditContract() {
  1188. br := new(models.BaseResponse).Init()
  1189. defer func() {
  1190. this.Data["json"] = br
  1191. this.ServeJSON()
  1192. }()
  1193. sysUser := this.SysUser
  1194. if sysUser == nil {
  1195. br.Msg = "请登录"
  1196. br.ErrMsg = "请登录,SysUser Is Empty"
  1197. br.Ret = 408
  1198. return
  1199. }
  1200. productId := services.GetProductId(sysUser.RoleTypeCode)
  1201. if productId == 0 {
  1202. br.Msg = "账户异常"
  1203. br.ErrMsg = "账户异常,当前账户类型为:" + sysUser.RoleTypeCode
  1204. br.Ret = 408
  1205. return
  1206. }
  1207. var req request.EditContractReq
  1208. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1209. if err != nil {
  1210. br.Msg = "参数解析异常!"
  1211. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1212. return
  1213. }
  1214. if req.ContractId <= 0 {
  1215. br.Msg = "合同编号必传!"
  1216. br.ErrMsg = "合同编号必传!"
  1217. return
  1218. }
  1219. // 限制一下合同金额, 目前只支持人民币转大写只支持到千亿, 虽然生产使用中不可能到这么大, 但还是加上吧 =_=!
  1220. maxPrice := float64(1000000000000)
  1221. if req.OriginalPrice >= maxPrice || req.Price >= maxPrice {
  1222. br.Msg = "合同金额过大, 请核验金额"
  1223. return
  1224. }
  1225. contractInfo, err, errMsg := contractService.EditContract(req, sysUser)
  1226. if err != nil {
  1227. br.Msg = "修改合同失败!"
  1228. if errMsg != "" {
  1229. br.Msg = errMsg
  1230. }
  1231. br.ErrMsg = "修改合同失败,Err:" + err.Error()
  1232. return
  1233. }
  1234. msg := "修改成功"
  1235. //如果发起重新审核,那么
  1236. if req.ReAudit {
  1237. tmpErr, errMsg := contractService.Apply(contractInfo.ContractId)
  1238. if tmpErr != nil {
  1239. br.Msg = "发起重申失败!"
  1240. if errMsg != "" {
  1241. br.Msg = errMsg
  1242. }
  1243. br.ErrMsg = "发起重申失败,Err:" + tmpErr.Error()
  1244. return
  1245. }
  1246. msg = "发起重申成功"
  1247. }
  1248. br.Ret = 200
  1249. br.Success = true
  1250. br.Msg = msg
  1251. br.Data = response.AddContractResp{
  1252. ContractId: contractInfo.ContractId,
  1253. }
  1254. }
  1255. // Delete
  1256. // @Title 删除合同
  1257. // @Description 删除合同接口
  1258. // @Param ContractId query int true "合同id"
  1259. // @Success Ret=200 删除成功
  1260. // @router /delete [get]
  1261. func (this *ContractController) Delete() {
  1262. br := new(models.BaseResponse).Init()
  1263. defer func() {
  1264. this.Data["json"] = br
  1265. this.ServeJSON()
  1266. }()
  1267. sysUser := this.SysUser
  1268. if sysUser == nil {
  1269. br.Msg = "请登录"
  1270. br.ErrMsg = "请登录,SysUser Is Empty"
  1271. br.Ret = 408
  1272. return
  1273. }
  1274. contractId, _ := this.GetInt("ContractId")
  1275. //合同id
  1276. if contractId <= 0 {
  1277. br.Msg = "合同id必传!"
  1278. br.ErrMsg = "合同id必传"
  1279. return
  1280. }
  1281. err := contractService.DeleteContract(contractId, sysUser)
  1282. if err != nil {
  1283. br.Msg = "修改合同失败!"
  1284. br.ErrMsg = "修改合同失败,Err:" + err.Error()
  1285. return
  1286. }
  1287. br.Ret = 200
  1288. br.Success = true
  1289. br.Msg = "删除成功"
  1290. return
  1291. }
  1292. // Invalid
  1293. // @Title 作废合同
  1294. // @Description 作废合同接口
  1295. // @Param ContractId query int true "合同id"
  1296. // @Success Ret=200 作废成功
  1297. // @router /invalid [get]
  1298. func (this *ContractController) Invalid() {
  1299. br := new(models.BaseResponse).Init()
  1300. defer func() {
  1301. this.Data["json"] = br
  1302. this.ServeJSON()
  1303. }()
  1304. sysUser := this.SysUser
  1305. if sysUser == nil {
  1306. br.Msg = "请登录"
  1307. br.ErrMsg = "请登录,SysUser Is Empty"
  1308. br.Ret = 408
  1309. return
  1310. }
  1311. contractId, _ := this.GetInt("ContractId")
  1312. //合同id
  1313. if contractId <= 0 {
  1314. br.Msg = "合同id必传!"
  1315. br.ErrMsg = "合同id必传"
  1316. return
  1317. }
  1318. err := contractService.InvalidContract(contractId, sysUser)
  1319. if err != nil {
  1320. br.Msg = "修改合同失败!"
  1321. br.ErrMsg = "修改合同失败,Err:" + err.Error()
  1322. return
  1323. }
  1324. br.Ret = 200
  1325. br.Success = true
  1326. br.Msg = "作废成功"
  1327. return
  1328. }
  1329. // UploadCheckBackFile
  1330. // @Title 上传签回附件
  1331. // @Description 上传签回附件接口
  1332. // @Param request body request.UploadCheckBackFileReq true "type json string"
  1333. // @Success Ret=200 上传成功
  1334. // @router /upload_check_back_file [post]
  1335. func (this *ContractController) UploadCheckBackFile() {
  1336. br := new(models.BaseResponse).Init()
  1337. defer func() {
  1338. this.Data["json"] = br
  1339. this.ServeJSON()
  1340. }()
  1341. sysUser := this.SysUser
  1342. if sysUser == nil {
  1343. br.Msg = "请登录"
  1344. br.ErrMsg = "请登录,SysUser Is Empty"
  1345. br.Ret = 408
  1346. return
  1347. }
  1348. //var req request.UploadCheckBackFileReq
  1349. //err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1350. //if err != nil {
  1351. // br.Msg = "参数解析异常!"
  1352. // br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1353. // return
  1354. //}
  1355. ////合同编号
  1356. //if req.ContractId <= 0 {
  1357. // br.Msg = "请传入合同编号!"
  1358. // br.ErrMsg = "请传入合同编号"
  1359. // return
  1360. //}
  1361. //
  1362. //if req.FileUrl == "" {
  1363. // br.Msg = "请先上传附件!"
  1364. // br.ErrMsg = "请先上传附件"
  1365. // return
  1366. //}
  1367. contractIdStr := this.Ctx.Request.Form.Get("ContractId")
  1368. if contractIdStr == "" {
  1369. br.Msg = "合同ID必传!"
  1370. br.ErrMsg = "合同ID必传,Err"
  1371. return
  1372. }
  1373. contractId, err := strconv.Atoi(contractIdStr)
  1374. if err != nil {
  1375. br.Msg = "合同ID异常!"
  1376. br.ErrMsg = "合同ID异常,Err:" + err.Error()
  1377. return
  1378. }
  1379. fileMulti, h, err := this.GetFile("file")
  1380. if err != nil {
  1381. br.Msg = "获取资源信息失败"
  1382. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  1383. return
  1384. }
  1385. //合同编号
  1386. if contractId <= 0 {
  1387. br.Msg = "请传入合同编号!"
  1388. br.ErrMsg = "请传入合同编号"
  1389. return
  1390. }
  1391. ext := path.Ext(h.Filename)
  1392. resourceUrl, err := contractService.UploadCheckBackFile(contractId, ext, fileMulti, this.SysUser)
  1393. if err != nil {
  1394. br.Msg = "更新失败!"
  1395. br.ErrMsg = "更新失败" + err.Error()
  1396. return
  1397. }
  1398. //更新用印中的签回合同
  1399. _, _ = seal.UploadCheckBackFile(contractId, resourceUrl, this.SysUser)
  1400. br.Ret = 200
  1401. br.Success = true
  1402. br.Msg = "上传成功"
  1403. }
  1404. // UploadRescindFile
  1405. // @Title 上传解约附件
  1406. // @Description 上传解约附件接口
  1407. // @Param request body request.UploadRescindFileReq true "type json string"
  1408. // @Success Ret=200 上传成功
  1409. // @router /upload_rescind_file [post]
  1410. func (this *ContractController) UploadRescindFile() {
  1411. br := new(models.BaseResponse).Init()
  1412. defer func() {
  1413. this.Data["json"] = br
  1414. this.ServeJSON()
  1415. }()
  1416. sysUser := this.SysUser
  1417. if sysUser == nil {
  1418. br.Msg = "请登录"
  1419. br.ErrMsg = "请登录,SysUser Is Empty"
  1420. br.Ret = 408
  1421. return
  1422. }
  1423. //var req request.UploadRescindFileReq
  1424. //err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1425. //if err != nil {
  1426. // br.Msg = "参数解析异常!"
  1427. // br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1428. // return
  1429. //}
  1430. ////合同编号
  1431. //if req.ContractId <= 0 {
  1432. // br.Msg = "请传入合同编号!"
  1433. // br.ErrMsg = "请传入合同编号"
  1434. // return
  1435. //}
  1436. //
  1437. //if req.FileUrl == "" {
  1438. // br.Msg = "请先上传附件!"
  1439. // br.ErrMsg = "请先上传附件"
  1440. // return
  1441. //}
  1442. contractIdStr := this.Ctx.Request.Form.Get("ContractId")
  1443. if contractIdStr == "" {
  1444. br.Msg = "合同ID必传!"
  1445. br.ErrMsg = "合同ID必传,Err"
  1446. return
  1447. }
  1448. contractId, err := strconv.Atoi(contractIdStr)
  1449. if err != nil {
  1450. br.Msg = "合同ID异常!"
  1451. br.ErrMsg = "合同ID异常,Err:" + err.Error()
  1452. return
  1453. }
  1454. fileMulti, h, err := this.GetFile("file")
  1455. if err != nil {
  1456. br.Msg = "获取资源信息失败"
  1457. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  1458. return
  1459. }
  1460. //合同编号
  1461. if contractId <= 0 {
  1462. br.Msg = "请传入合同编号!"
  1463. br.ErrMsg = "请传入合同编号"
  1464. return
  1465. }
  1466. ext := path.Ext(h.Filename)
  1467. err = contractService.UploadRescindFile(contractId, ext, fileMulti, this.SysUser)
  1468. br.Ret = 200
  1469. br.Success = true
  1470. br.Msg = "上传成功"
  1471. }
  1472. // Detail
  1473. // @Title 获取合同详情
  1474. // @Description 获取合同详情接口
  1475. // @Param ContractId query int true "合同id"
  1476. // @Success 200 {object} contract.ContractDetail
  1477. // @router /detail [get]
  1478. func (this *ContractController) Detail() {
  1479. br := new(models.BaseResponse).Init()
  1480. defer func() {
  1481. this.Data["json"] = br
  1482. this.ServeJSON()
  1483. }()
  1484. sysUser := this.SysUser
  1485. if sysUser == nil {
  1486. br.Msg = "请登录"
  1487. br.ErrMsg = "请登录,SysUser Is Empty"
  1488. br.Ret = 408
  1489. return
  1490. }
  1491. //合同类型、产品类型、合同状态、更新时间、所选销售
  1492. //关键字:合同编号、客户名称,社会信用码
  1493. contractId, _ := this.GetInt("ContractId")
  1494. //合同id
  1495. if contractId <= 0 {
  1496. br.Msg = "合同id必传!"
  1497. br.ErrMsg = "合同id必传"
  1498. return
  1499. }
  1500. contractInfo, err := contractService.GetContractDetail(contractId)
  1501. if err != nil {
  1502. br.Msg = "获取合同详情失败!"
  1503. br.ErrMsg = "获取合同详情失败,ERR:" + err.Error()
  1504. return
  1505. }
  1506. // CRM8.8-权益主客观合并
  1507. if contractInfo.ProductId == 2 {
  1508. mergeList := contractService.EquityMergeSubjectAndObject(contractInfo.Service)
  1509. contractInfo.Service = mergeList
  1510. // 关联合同同样合并
  1511. for i, v := range contractInfo.RelationContractDetailList {
  1512. relationMergeList := contractService.EquityMergeSubjectAndObject(v.Service)
  1513. contractInfo.RelationContractDetailList[i].Service = relationMergeList
  1514. }
  1515. }
  1516. contractInfo.StartDateStr = contractInfo.StartDate.Format(utils.FormatDate)
  1517. contractInfo.EndDateStr = contractInfo.EndDate.Format(utils.FormatDate)
  1518. contractInfo.ModifyTimeStr = contractInfo.ModifyTime.Format(utils.FormatDateTime)
  1519. contractInfo.CreateTimeStr = contractInfo.CreateTime.Format(utils.FormatDateTime)
  1520. //合同中包含的产品权限
  1521. isNeedPermission := false //是否需要列出来套餐中的详细权限
  1522. for _, service := range contractInfo.Service {
  1523. if service.ServiceTemplateId == 2 { //小套餐才需要列出来详细的权限
  1524. isNeedPermission = true
  1525. }
  1526. }
  1527. var permissionLookListResp = make([]*contract.PermissionLookList, 0)
  1528. if isNeedPermission {
  1529. checkPermissionMap, tErr := contractService.GetServicePermissionMap(contractInfo.Service)
  1530. if tErr == nil {
  1531. permissionLookListMap := make(map[int][]*contract.PermissionLookItem)
  1532. var permissionIds []string
  1533. for _, v := range checkPermissionMap {
  1534. permissionIds = append(permissionIds, strconv.Itoa(v))
  1535. }
  1536. permissionIdStr := strings.Join(permissionIds, ",")
  1537. charPermissions, tErr1 := company.GetPermissionLookItemsSandO(permissionIdStr)
  1538. if tErr1 == nil {
  1539. for _, v := range charPermissions {
  1540. tmp := new(contract.PermissionLookItem)
  1541. tmp.ClassifyName = v.ClassifyName
  1542. tmp.PermissionName = v.PermissionName
  1543. tmp.ChartPermissionId = v.ChartPermissionId
  1544. permissionLookListMap[v.ParentId] = append(permissionLookListMap[v.ParentId], tmp)
  1545. }
  1546. classifyNameList, tErr2 := company.GetChartPermissionFirst()
  1547. if tErr2 == nil {
  1548. for _, v := range classifyNameList {
  1549. if perList, ok := permissionLookListMap[v.ChartPermissionId]; ok {
  1550. permissionLookListRespItem := &contract.PermissionLookList{
  1551. ClassifyName: v.ClassifyName,
  1552. Items: perList,
  1553. }
  1554. permissionLookListResp = append(permissionLookListResp, permissionLookListRespItem)
  1555. }
  1556. }
  1557. }
  1558. }
  1559. }
  1560. }
  1561. contractInfo.PermissionLookList = permissionLookListResp
  1562. br.Ret = 200
  1563. br.Success = true
  1564. br.Msg = "获取成功"
  1565. br.Data = contractInfo
  1566. }
  1567. //Export
  1568. //@Title 合同列表数据导出
  1569. //@Description 合同列表数据导出接口
  1570. //@Description 合同列表接口
  1571. //@Param ContractType query string false "合同类型,枚举值:'新签合同','续约合同','补充协议'"
  1572. //@Param ContractStatus query string false "合同状态,枚举值:'待提交','待审批','已撤回','已审批','已驳回','已作废'"
  1573. //@Param ProductId query int false "客户类型:传0或者不传为当前账号权限,1 代表是:ficc;2 代表是:权益"
  1574. //@Param ModifyStartTime query string false "服务更新时间的选择开始时间,格式:2021-05-23 00:00:00"
  1575. //@Param ModifyEndTime query string false "服务更新时间的选择结束时间,格式:2021-05-26 23:59:59"
  1576. //@Param SellerId query string false "选择的销售id"
  1577. //@Param Keyword query string false "搜索关键字"
  1578. //@Success 200 {object} response.ContractListResp
  1579. //@router /export [get]
  1580. //func (this *ContractController) Export() {
  1581. // br := new(models.BaseResponse).Init()
  1582. // defer func() {
  1583. // this.Data["json"] = br
  1584. // this.ServeJSON()
  1585. // }()
  1586. // sysUser := this.SysUser
  1587. // if sysUser == nil {
  1588. // br.Msg = "请登录"
  1589. // br.ErrMsg = "请登录,SysUser Is Empty"
  1590. // br.Ret = 408
  1591. // return
  1592. // }
  1593. //
  1594. // contractType := this.GetString("ContractType")
  1595. // contractStatus := this.GetString("ContractStatus")
  1596. // productId, _ := this.GetInt("ProductId")
  1597. // modifyStartTime := this.GetString("ModifyStartTime")
  1598. // modifyEndTime := this.GetString("ModifyEndTime")
  1599. // sellerIds := this.GetString("SellerId")
  1600. // keyword := this.GetString("Keyword")
  1601. //
  1602. // condition := ""
  1603. // pars := make([]interface{}, 0)
  1604. //
  1605. // //如果不是超管或者合规,那么只能查看自己的合同
  1606. // if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  1607. // condition += ` AND product_id = 1 AND status not in ("待提交","已撤回") `
  1608. // } else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  1609. // condition += ` AND product_id = 2 AND status not in ("待提交","已撤回") `
  1610. // } else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN { //超管可查看所有状态的合同(除过已撤回、待提交状态合同),可下载所有状态的合同
  1611. // condition += ` AND status not in ("待提交","已撤回") `
  1612. // } else {
  1613. // if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  1614. // condition += ` AND seller_id = ? `
  1615. // pars = append(pars, sysUser.AdminId)
  1616. // }
  1617. // }
  1618. //
  1619. // //需要下载的 产品 切片
  1620. // productSlice := make([]int, 0)
  1621. //
  1622. // //合同类型、、更新时间、所选销售
  1623. // //关键字:合同编号、客户名称,社会信用码
  1624. // if contractType != "" {
  1625. // condition += ` AND contract_type = ? `
  1626. // pars = append(pars, contractType)
  1627. // }
  1628. // //合同状态
  1629. // if contractStatus != "" {
  1630. // condition += ` AND status = ? `
  1631. // pars = append(pars, contractStatus)
  1632. // }
  1633. //
  1634. // //产品类型
  1635. // if productId > 0 {
  1636. // condition += ` AND product_id = ? `
  1637. // pars = append(pars, productId)
  1638. //
  1639. // productSlice = append(productSlice, productId)
  1640. // } else {
  1641. // nowProductId := services.GetProductId(sysUser.RoleTypeCode)
  1642. // if nowProductId > 0 {
  1643. // productSlice = append(productSlice, nowProductId)
  1644. // } else {
  1645. // productSlice = append(productSlice, 1)
  1646. // productSlice = append(productSlice, 2)
  1647. // }
  1648. // }
  1649. // //所选销售
  1650. // if sellerIds != "" {
  1651. // condition += ` AND seller_id IN (` + sellerIds + `) `
  1652. // }
  1653. // //更新开始时间
  1654. // if modifyStartTime != "" {
  1655. // condition += ` AND modify_time >= ? `
  1656. // pars = append(pars, modifyStartTime)
  1657. // }
  1658. // //更新结束时间
  1659. // if modifyEndTime != "" {
  1660. // condition += ` AND modify_time <= ? `
  1661. // pars = append(pars, modifyEndTime)
  1662. // }
  1663. // //关键字
  1664. // if keyword != "" {
  1665. // condition += ` AND (contract_code LIKE '%` + keyword + `%' OR company_name LIKE '%` + keyword + `%' OR credit_code LIKE '%` + keyword + `%' ) `
  1666. // }
  1667. //
  1668. // //取 一万 条合同数据
  1669. // list, err := contract.GetContractList(condition, pars, 0, 10000)
  1670. // if err != nil {
  1671. // br.Msg = "获取合同列表失败!"
  1672. // br.ErrMsg = "获取合同列表失败,Err:" + err.Error()
  1673. // return
  1674. // }
  1675. // //获取合同套餐服务列表
  1676. // serviceTemplateList, err := contract.GetAllContractServiceTemplateList()
  1677. // if err != nil {
  1678. // br.Msg = "获取合同套餐服务列表失败!"
  1679. // br.ErrMsg = "获取合同套餐服务列表失败,Err:" + err.Error()
  1680. // return
  1681. // }
  1682. // templateTitleMap := make(map[int]string)
  1683. // for _, serviceTemplate := range serviceTemplateList {
  1684. // templateTitleMap[serviceTemplate.ServiceTemplateId] = serviceTemplate.Title
  1685. // }
  1686. //
  1687. // for i := 0; i < len(list); i++ {
  1688. // item := list[i]
  1689. // list[i].StartDateStr = item.StartDate.Format(utils.FormatDate)
  1690. // list[i].EndDateStr = item.EndDate.Format(utils.FormatDate)
  1691. //
  1692. // list[i].CreateTimeStr = item.CreateTime.Format(utils.FormatDateTime)
  1693. // list[i].ModifyTimeStr = item.ModifyTime.Format(utils.FormatDateTime)
  1694. //
  1695. // //服务列表
  1696. // serviceList, err := contract.GetContractServiceAndDetailList(item.ContractId)
  1697. // if err != nil {
  1698. // br.Msg = "获取失败"
  1699. // br.ErrMsg = "查找合同服务异常,Err:" + err.Error()
  1700. // return
  1701. // }
  1702. // for i := 0; len(serviceList) > i; i++ {
  1703. // if serviceList[i].HasDetail == "是" {
  1704. // list, detailErr := contract.GetContractServiceDetailListByServiceId(serviceList[i].ContractServiceId)
  1705. // if detailErr != nil {
  1706. // br.Msg = "获取失败"
  1707. // br.ErrMsg = "查找合同服务详情异常,Err:" + err.Error()
  1708. // return
  1709. // }
  1710. //
  1711. // //套餐名称
  1712. // serviceList[i].DetailList = list
  1713. // if title, ok := templateTitleMap[serviceList[i].ServiceTemplateId]; ok {
  1714. // serviceList[i].Title = title
  1715. // }
  1716. // }
  1717. // }
  1718. // list[i].Service = serviceList
  1719. // }
  1720. //
  1721. // //生成excel文件
  1722. // dir, err := os.Executable()
  1723. // exPath := filepath.Dir(dir)
  1724. // downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1725. // xlsxFile := xlsx.NewFile()
  1726. // if err != nil {
  1727. // br.Msg = "生成文件失败"
  1728. // br.ErrMsg = "生成文件失败"
  1729. // return
  1730. // }
  1731. //
  1732. // //设置默认字体和文字大小
  1733. // xlsx.SetDefaultFont(12, "宋体")
  1734. //
  1735. // //定义边框样式
  1736. // border := xlsx.NewBorder("thin", "thin", "thin", "thin")
  1737. // //定义文字排版样式
  1738. // alignment := xlsx.Alignment{
  1739. // Horizontal: "center",
  1740. // Vertical: "center",
  1741. // WrapText: true,
  1742. // }
  1743. //
  1744. // //普通样式
  1745. // style := xlsx.NewStyle()
  1746. // style.Alignment = alignment
  1747. // style.ApplyAlignment = true
  1748. // style.Border = *border
  1749. //
  1750. // //标红文字,便于查看哪些产品被勾选
  1751. // redStyle := xlsx.NewStyle()
  1752. // redStyle.Alignment = alignment
  1753. // redStyle.ApplyAlignment = true
  1754. // redStyle.Font.Color = "ff0000"
  1755. // //定义底色需要标黄的 单元格颜色
  1756. // redFill := xlsx.Fill{"solid", "ffff00", "ffff00"}
  1757. // redStyle.Fill = redFill
  1758. // redStyle.Border = *border
  1759. //
  1760. // //标题样式
  1761. // titleStyle := xlsx.NewStyle()
  1762. // titleFont := xlsx.NewFont(14, "宋体")
  1763. // titleFont.Bold = true
  1764. // titleStyle.Font = *titleFont
  1765. // titleStyle.Alignment = alignment
  1766. // titleStyle.Border = *border
  1767. //
  1768. // //titleStyle.ApplyAlignment = true
  1769. //
  1770. // //表头
  1771. // headerStyle := xlsx.NewStyle()
  1772. // headerFont := xlsx.NewFont(12, "宋体")
  1773. // headerFont.Bold = true
  1774. // headerStyle.Font = *headerFont
  1775. // headerStyle.Alignment = alignment
  1776. // headerStyle.ApplyAlignment = true
  1777. // headerStyle.Border = *border
  1778. //
  1779. // defer func() {
  1780. // os.Remove(downLoadnFilePath)
  1781. // }()
  1782. //
  1783. // //遍历所拥有的的产品权限,然后插入到对应的sheet表单中
  1784. // for pidIndex := 0; pidIndex < len(productSlice); pidIndex++ {
  1785. // productId = productSlice[pidIndex]
  1786. // //表单名称
  1787. // sheetName := "合同数据"
  1788. // switch productId {
  1789. // case 1:
  1790. // sheetName = "ficc合同信息"
  1791. // case 2:
  1792. // sheetName = "权益合同信息"
  1793. // }
  1794. // //新增一个sheet表单
  1795. // sheel, err := xlsxFile.AddSheet(sheetName)
  1796. // if err != nil {
  1797. // br.Msg = "新增Sheet失败"
  1798. // br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  1799. // return
  1800. // }
  1801. // //设置列宽
  1802. // sheel.SetColWidth(0, 0, 28)
  1803. // sheel.SetColWidth(1, 1, 30)
  1804. // sheel.SetColWidth(2, 2, 18)
  1805. // sheel.SetColWidth(3, 3, 18)
  1806. // sheel.SetColWidth(4, 4, 35)
  1807. // sheel.SetColWidth(5, 6, 20)
  1808. // sheel.SetColWidth(7, 7, 40)
  1809. // sheel.SetColWidth(7, 35, 30)
  1810. //
  1811. // //标题行
  1812. // titleRow := sheel.AddRow()
  1813. // titleRow.SetHeight(30)
  1814. //
  1815. // //合同信息列
  1816. // titleCell := titleRow.AddCell()
  1817. // titleCell.HMerge = 7 //向右合并列数,不包括自身列
  1818. // //遍历去添加需要合并的列
  1819. // for i := 0; i < 7; i++ {
  1820. // titleRow.AddCell()
  1821. // }
  1822. //
  1823. // //报表标题名称
  1824. // titleCell.SetValue("合同信息")
  1825. // titleCell.SetStyle(titleStyle)
  1826. //
  1827. // startI := 8
  1828. // customStartCellI := 8
  1829. //
  1830. // serviceSlice := make([]string, 0)
  1831. // serviceCellMap := make(map[string]int)
  1832. //
  1833. // //获取所有套餐列表
  1834. // {
  1835. // serviceList, err := getServiceTemplateList(productId)
  1836. // if err != nil {
  1837. // br.Msg = "查询套餐类型失败"
  1838. // br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  1839. // return
  1840. // }
  1841. // for i := 0; i < len(serviceList); i++ {
  1842. // //需要向右合并添加的列数
  1843. // addCellNum := 0
  1844. //
  1845. // item := serviceList[i]
  1846. // //套餐服务列
  1847. // servicesCell := titleRow.AddCell()
  1848. // //套餐名称
  1849. // servicesCell.SetValue(item.Title)
  1850. // servicesCell.SetStyle(titleStyle)
  1851. //
  1852. // //ficc小套餐
  1853. // if item.ServiceTemplateId == 2 {
  1854. // productPermission := make(map[int]string)
  1855. // productPermission[1] = "每日宏观商品复盘"
  1856. // productPermission[2] = "能化专栏《化里化外》"
  1857. // productPermission[3] = "股债日评"
  1858. // productPermission[4] = "黑色专栏《知白守黑》"
  1859. // productPermission[5] = "有色专栏《有声有色》"
  1860. // for id, name := range productPermission {
  1861. // key := fmt.Sprint(item.ServiceTemplateId, "_product_permission_", id)
  1862. // serviceCellMap[key] = startI
  1863. // serviceSlice = append(serviceSlice, name)
  1864. // startI++
  1865. // addCellNum++
  1866. // }
  1867. //
  1868. // permissionList := make([]*company.PermissionSetItem, 0)
  1869. // for _, v := range utils.PermissionFiccClassifyArr {
  1870. // items, err := company.GetPermissionSetItems(1, v)
  1871. // if err != nil {
  1872. // br.Msg = "获取失败"
  1873. // br.ErrMsg = "获取权限信息失败,Err:" + err.Error()
  1874. // return
  1875. // }
  1876. // permissionList = append(permissionList, items...)
  1877. // }
  1878. //
  1879. // //权限列表
  1880. // for j := 0; j < len(permissionList); j++ {
  1881. // key := fmt.Sprint(item.ServiceTemplateId, "_permission_", permissionList[j].ChartPermissionId)
  1882. // serviceCellMap[key] = startI
  1883. // serviceSlice = append(serviceSlice, permissionList[j].PermissionName)
  1884. // startI++
  1885. // addCellNum++
  1886. // }
  1887. // //因为自己本身有一列了,所以在最后增加列的时候,需要减去自身列
  1888. // addCellNum = addCellNum - 1
  1889. // if addCellNum > 0 {
  1890. // for tmpAddCellNum := 0; tmpAddCellNum < addCellNum; tmpAddCellNum++ {
  1891. // titleRow.AddCell()
  1892. // }
  1893. // servicesCell.HMerge = addCellNum //向右合并列数,不包括自身列
  1894. // }
  1895. // continue
  1896. // } else {
  1897. // if item.Children != nil && len(item.Children) > 0 {
  1898. // for j := 0; j < len(item.Children); j++ {
  1899. // key := fmt.Sprint("service_template_", item.Children[j].ServiceTemplateId)
  1900. // serviceCellMap[key] = startI
  1901. // serviceSlice = append(serviceSlice, item.Children[j].Title)
  1902. // startI++
  1903. // addCellNum++
  1904. // }
  1905. // //因为自己本身有一列了,所以在最后增加列的时候,需要减去自身列
  1906. // addCellNum = addCellNum - 1
  1907. // if addCellNum > 0 {
  1908. // for tmpAddCellNum := 0; tmpAddCellNum < addCellNum; tmpAddCellNum++ {
  1909. // titleRow.AddCell()
  1910. // }
  1911. // servicesCell.HMerge = addCellNum //向右合并列数,不包括自身列
  1912. // }
  1913. // } else {
  1914. // key := fmt.Sprint("service_template_", item.ServiceTemplateId)
  1915. // serviceCellMap[key] = startI
  1916. // serviceSlice = append(serviceSlice, item.Title)
  1917. // startI++
  1918. // }
  1919. // }
  1920. // }
  1921. // }
  1922. //
  1923. // //表头
  1924. // headerRow := sheel.AddRow()
  1925. // headerRow.SetHeight(24)
  1926. //
  1927. // //合同基础信息
  1928. // {
  1929. // codeHeaderCell := headerRow.AddCell()
  1930. // codeHeaderCell.SetValue("合同编号")
  1931. // codeHeaderCell.SetStyle(headerStyle)
  1932. //
  1933. // companyHeaderCell := headerRow.AddCell()
  1934. // companyHeaderCell.SetValue("客户名称")
  1935. // companyHeaderCell.SetStyle(headerStyle)
  1936. //
  1937. // typeHeaderCell := headerRow.AddCell()
  1938. // typeHeaderCell.SetValue("合同类型")
  1939. // typeHeaderCell.SetStyle(headerStyle)
  1940. //
  1941. // priceHeaderCell := headerRow.AddCell()
  1942. // priceHeaderCell.SetValue("合同金额")
  1943. // priceHeaderCell.SetStyle(headerStyle)
  1944. //
  1945. // dateHeaderCell := headerRow.AddCell()
  1946. // dateHeaderCell.SetValue("合同有效期")
  1947. // dateHeaderCell.SetStyle(headerStyle)
  1948. //
  1949. // sellerHeaderCell := headerRow.AddCell()
  1950. // sellerHeaderCell.SetValue("销售")
  1951. // sellerHeaderCell.SetStyle(headerStyle)
  1952. //
  1953. // statusHeaderCell := headerRow.AddCell()
  1954. // statusHeaderCell.SetValue("合同状态")
  1955. // statusHeaderCell.SetStyle(headerStyle)
  1956. //
  1957. // remarkHeaderCell := headerRow.AddCell()
  1958. // remarkHeaderCell.SetValue("备注")
  1959. // remarkHeaderCell.SetStyle(headerStyle)
  1960. // }
  1961. //
  1962. // //添加服务套餐名称
  1963. // for _, service := range serviceSlice {
  1964. // cell := headerRow.AddCell()
  1965. // cell.SetValue(service)
  1966. // cell.SetStyle(headerStyle)
  1967. // }
  1968. //
  1969. // for _, v := range list {
  1970. // //如果当前合同不是该产品
  1971. // if v.ProductId != productId {
  1972. // continue
  1973. // }
  1974. // dataRow := sheel.AddRow()
  1975. // dataRow.SetHeight(24)
  1976. //
  1977. // //合同基础信息
  1978. // {
  1979. // codeCell := dataRow.AddCell()
  1980. // codeCell.SetValue(v.ContractCode)
  1981. // codeCell.SetStyle(style)
  1982. //
  1983. // companyCell := dataRow.AddCell()
  1984. // companyCell.SetValue(v.CompanyName)
  1985. // companyCell.SetStyle(style)
  1986. //
  1987. // typeCell := dataRow.AddCell()
  1988. // typeCell.SetValue(v.ContractType)
  1989. // typeCell.SetStyle(style)
  1990. //
  1991. // priceCell := dataRow.AddCell()
  1992. // priceCell.SetValue(v.Price)
  1993. // priceCell.SetStyle(style)
  1994. //
  1995. // dateCell := dataRow.AddCell()
  1996. // dateCell.SetValue(fmt.Sprint(v.StartDate.Format(utils.FormatDate), " 至 ", v.EndDate.Format(utils.FormatDate)))
  1997. // dateCell.SetStyle(style)
  1998. //
  1999. // sellerCell := dataRow.AddCell()
  2000. // sellerCell.SetValue(v.SellerName)
  2001. // sellerCell.SetStyle(style)
  2002. //
  2003. // statusCell := dataRow.AddCell()
  2004. // statusCell.SetValue(v.Status)
  2005. // statusCell.SetStyle(style)
  2006. //
  2007. // //备注:该字段由三部分拼接:服务内容中的补充内容+赠送时间+优惠金额
  2008. // //补充内容直接取该字段内容;
  2009. // //赠送时间以年为单位,超过整年的月份作为赠送月份,如14个月即为赠送2个月;
  2010. // //优惠金额等于原价-优惠后价格=优惠价格
  2011. //
  2012. // //补充内容
  2013. // var remark string
  2014. // if v.Remark != "" {
  2015. // remark += "补充内容:" + v.Remark
  2016. // }
  2017. //
  2018. // //赠送时间
  2019. // //合同结束日期与合同开始日期的时间差(小时差)
  2020. // newDecimal := decimal.NewFromFloat(v.EndDate.Sub(v.StartDate).Hours())
  2021. // //分母为365天 * 24 小时
  2022. // newDecimal2 := decimal.NewFromInt(24 * 365)
  2023. // //计算出来相差多少年,保留一位小数(四舍五入)
  2024. // numYearDecimal := newDecimal.Div(newDecimal2).Round(1).Mul(decimal.NewFromInt(10))
  2025. // //如果大于1年,那么才去判断是否赠送
  2026. // numYear, _ := numYearDecimal.Float64()
  2027. // if numYear > 10 {
  2028. // //获取多出来的整数年数据
  2029. // numYearDecimal = numYearDecimal.Mod(decimal.NewFromInt(10)).Div(decimal.NewFromInt(10))
  2030. // //计算赠送出来的月份
  2031. // numMonthDecimal := numYearDecimal.Mul(decimal.NewFromInt(12)).Round(0)
  2032. // numMonth, _ := numMonthDecimal.Float64()
  2033. // if numMonth > 0 {
  2034. // if remark != "" {
  2035. // remark += ";"
  2036. // }
  2037. // remark += "赠送时间为:" + numMonthDecimal.String() + "个月"
  2038. // }
  2039. // }
  2040. // //优惠金额
  2041. // if v.OriginalPrice > v.Price {
  2042. // differencePrice := v.OriginalPrice - v.Price
  2043. // differencePriceDecimal := decimal.NewFromFloat(differencePrice)
  2044. //
  2045. // if remark != "" {
  2046. // remark += ";"
  2047. // }
  2048. // remark += "优惠金额:" + differencePriceDecimal.String() + "元。"
  2049. // }
  2050. // remarkCell := dataRow.AddCell()
  2051. // remarkCell.SetValue(remark)
  2052. // remarkCell.SetStyle(style)
  2053. // }
  2054. //
  2055. // //服务套餐
  2056. // {
  2057. // cellServiceMap := make(map[int]string)
  2058. // for _, cellI := range serviceCellMap {
  2059. // cellServiceMap[cellI] = "否"
  2060. // }
  2061. //
  2062. // if v.Service != nil && len(v.Service) > 0 {
  2063. // permissionValues := []string{
  2064. // "FICC周报", "商品双周报+线上电话会讨论会<br/>(由弘则的研究员主持线上讨论)", "数据点评", "深度月报:包括宏观经济月报和下游草根调研月报+电话会讨论会<br/>(由弘则的研究员主持线上讨论)",
  2065. // }
  2066. // for _, item := range v.Service {
  2067. // //ficc小套餐
  2068. // if item.ServiceTemplateId == 2 {
  2069. // for _, detail := range item.DetailList {
  2070. // var tableCel request.AddContractServiceDetailReq
  2071. // tmpErr := json.Unmarshal([]byte(detail.Col1), &tableCel)
  2072. // if tmpErr != nil {
  2073. // err = tmpErr
  2074. // return
  2075. // }
  2076. // if tableCel.Value == "商品复盘" {
  2077. // var tableCel2 request.AddContractServiceDetailReq
  2078. // //获取品种列数据
  2079. // tmpErr := contractService.GetProductCelData(*detail, &tableCel2)
  2080. // if tmpErr != nil {
  2081. // err = tmpErr
  2082. // return
  2083. // }
  2084. // //tableCel2.Tag
  2085. // for _, serviceId := range tableCel2.ValueId {
  2086. // key := fmt.Sprint(item.ServiceTemplateId, "_product_permission_", serviceId)
  2087. // if cellI, ok := serviceCellMap[key]; ok {
  2088. // cellServiceMap[cellI] = "是"
  2089. // }
  2090. // }
  2091. //
  2092. // } else if strings.Contains(strings.Join(permissionValues, ","), tableCel.Value) {
  2093. // //获取品种列数据
  2094. // var tableCel2 request.AddContractServiceDetailReq
  2095. // tmpErr := contractService.GetProductCelData(*detail, &tableCel2)
  2096. // if tmpErr != nil {
  2097. // err = tmpErr
  2098. // return
  2099. // }
  2100. // for _, serviceId := range tableCel2.ValueId {
  2101. // key := fmt.Sprint(item.ServiceTemplateId, "_permission_", serviceId)
  2102. // if cellI, ok := serviceCellMap[key]; ok {
  2103. // cellServiceMap[cellI] = "是"
  2104. // }
  2105. // }
  2106. // }
  2107. // }
  2108. // } else {
  2109. // key := fmt.Sprint("service_template_", item.ServiceTemplateId)
  2110. // if cellI, ok := serviceCellMap[key]; ok {
  2111. // cellServiceMap[cellI] = "是"
  2112. // }
  2113. // }
  2114. // }
  2115. // }
  2116. //
  2117. // //遍历所有套餐
  2118. // for tmpStartCellI := customStartCellI; tmpStartCellI < startI; tmpStartCellI++ {
  2119. // servicesCell := dataRow.AddCell()
  2120. // cellValue, ok := cellServiceMap[tmpStartCellI]
  2121. // if ok == false {
  2122. // cellValue = "否"
  2123. // }
  2124. // servicesCell.SetValue(cellValue)
  2125. // if cellValue == "是" { //勾选了话,标红处理
  2126. // servicesCell.SetStyle(redStyle)
  2127. // } else {
  2128. // servicesCell.SetStyle(style)
  2129. // }
  2130. // }
  2131. // }
  2132. //
  2133. // }
  2134. // }
  2135. // err = xlsxFile.Save(downLoadnFilePath)
  2136. // if err != nil {
  2137. // br.Msg = "保存文件失败"
  2138. // br.ErrMsg = "保存文件失败"
  2139. // return
  2140. // }
  2141. // randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  2142. // downloadFileName := "合同列表" + randStr + ".xlsx"
  2143. // this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  2144. // br.Ret = 200
  2145. // br.Success = true
  2146. // br.Msg = "导出成功"
  2147. //}
  2148. // Preview
  2149. // @Title 预览合同
  2150. // @Description 预览合同接口v
  2151. // @Param request body request.AddContractReq true "type json string"
  2152. // @Success 200 {object} response.ContractPreviewResp
  2153. // @router /preview [post]
  2154. func (this *ContractController) Preview() {
  2155. br := new(models.BaseResponse).Init()
  2156. defer func() {
  2157. this.Data["json"] = br
  2158. this.ServeJSON()
  2159. }()
  2160. sysUser := this.SysUser
  2161. if sysUser == nil {
  2162. br.Msg = "请登录"
  2163. br.ErrMsg = "请登录,SysUser Is Empty"
  2164. br.Ret = 408
  2165. return
  2166. }
  2167. productId := services.GetProductId(sysUser.RoleTypeCode)
  2168. if productId == 0 {
  2169. br.Msg = "账户异常"
  2170. br.ErrMsg = "账户异常,当前账户类型为:" + sysUser.RoleTypeCode
  2171. br.Ret = 408
  2172. return
  2173. }
  2174. var req request.AddContractReq
  2175. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2176. if err != nil {
  2177. br.Msg = "参数解析异常!"
  2178. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2179. return
  2180. }
  2181. // 限制一下合同金额, 目前只支持人民币转大写只支持到千亿, 虽然生产使用中不可能到这么大, 但还是加上吧 =_=!
  2182. maxPrice := float64(1000000000000)
  2183. if req.OriginalPrice >= maxPrice || req.Price >= maxPrice {
  2184. br.Msg = "合同金额过大, 请核验金额"
  2185. return
  2186. }
  2187. contractInfo, err := contractService.GetContractDetailByAddContractReq(req, sysUser.AdminId, productId, sysUser.RealName)
  2188. if err != nil {
  2189. br.Msg = "获取合同数据失败!"
  2190. br.ErrMsg = "获取合同数据失败,Err:" + err.Error()
  2191. return
  2192. }
  2193. // CMR8.8-合并权益主客观
  2194. if contractInfo.ProductId == 2 {
  2195. mergeList := contractService.EquityMergeSubjectAndObject(contractInfo.Service)
  2196. contractInfo.Service = mergeList
  2197. }
  2198. contractHtml, err := services.GetHtmlByContractDetail(contractInfo, "html")
  2199. if err != nil {
  2200. br.Msg = "生成预览合同失败!"
  2201. br.ErrMsg = "生成预览合同失败,Err:" + err.Error()
  2202. return
  2203. }
  2204. br.Ret = 200
  2205. br.Success = true
  2206. br.Msg = "获取成功"
  2207. br.Data = response.ContractPreviewResp{
  2208. Html: contractHtml,
  2209. }
  2210. }
  2211. // PreviewByContractId
  2212. // @Title 预览合同
  2213. // @Description 预览合同接口
  2214. // @Param ContractId query int true "合同id"
  2215. // @Success 200 {object} response.ContractPreviewResp
  2216. // @router /previewByContractId [get]
  2217. func (this *ContractController) PreviewByContractId() {
  2218. br := new(models.BaseResponse).Init()
  2219. defer func() {
  2220. this.Data["json"] = br
  2221. this.ServeJSON()
  2222. }()
  2223. sysUser := this.SysUser
  2224. if sysUser == nil {
  2225. br.Msg = "请登录"
  2226. br.ErrMsg = "请登录,SysUser Is Empty"
  2227. br.Ret = 408
  2228. return
  2229. }
  2230. contractId, _ := this.GetInt("ContractId")
  2231. //合同id
  2232. if contractId <= 0 {
  2233. br.Msg = "合同id必传!"
  2234. br.ErrMsg = "合同id必传"
  2235. return
  2236. }
  2237. contractInfo, err := contractService.GetContractDetail(contractId)
  2238. if err != nil {
  2239. br.Msg = "获取合同详情失败!"
  2240. br.ErrMsg = "获取合同详情失败,ERR:" + err.Error()
  2241. return
  2242. }
  2243. // 限制一下合同金额, 目前只支持人民币转大写只支持到千亿, 虽然生产使用中不可能到这么大, 但还是加上吧 =_=!
  2244. maxPrice := float64(1000000000000)
  2245. if contractInfo.OriginalPrice >= maxPrice || contractInfo.Price >= maxPrice {
  2246. br.Msg = "合同金额过大, 请核验金额"
  2247. return
  2248. }
  2249. // CMR8.8-合并权益主客观
  2250. if contractInfo.ProductId == 2 {
  2251. mergeList := contractService.EquityMergeSubjectAndObject(contractInfo.Service)
  2252. contractInfo.Service = mergeList
  2253. }
  2254. contractHtml, err := services.GetHtmlByContractDetail(contractInfo, "html")
  2255. if err != nil {
  2256. br.Msg = "生成预览合同失败!"
  2257. br.ErrMsg = "生成预览合同失败,Err:" + err.Error()
  2258. return
  2259. }
  2260. br.Ret = 200
  2261. br.Success = true
  2262. br.Msg = "获取成功"
  2263. br.Data = response.ContractPreviewResp{
  2264. Html: contractHtml,
  2265. }
  2266. }
  2267. // DownLoadTmpContract
  2268. // @Title 下载临时合同
  2269. // @Description 下载临时合同接口
  2270. // @Param ContractId query int true "合同id"
  2271. // @router /downLoad/tmpContract [get]
  2272. func (this *ContractController) DownLoadTmpContract() {
  2273. br := new(models.BaseResponse).Init()
  2274. defer func() {
  2275. this.Data["json"] = br
  2276. this.ServeJSON()
  2277. }()
  2278. sysUser := this.SysUser
  2279. if sysUser == nil {
  2280. br.Msg = "请登录"
  2281. br.ErrMsg = "请登录,SysUser Is Empty"
  2282. br.Ret = 408
  2283. return
  2284. }
  2285. //合同类型、产品类型、合同状态、更新时间、所选销售
  2286. //关键字:合同编号、客户名称,社会信用码
  2287. contractId, _ := this.GetInt("ContractId")
  2288. //合同id
  2289. if contractId <= 0 {
  2290. br.Msg = "合同id必传!"
  2291. br.ErrMsg = "合同id必传"
  2292. return
  2293. }
  2294. contractInfo, err := contractService.GetContractDetail(contractId)
  2295. if err != nil {
  2296. br.Msg = "获取合同详情失败!"
  2297. br.ErrMsg = "获取合同详情失败,ERR:" + err.Error()
  2298. return
  2299. }
  2300. // 限制一下合同金额, 目前只支持人民币转大写只支持到千亿, 虽然生产使用中不可能到这么大, 但还是加上吧 =_=!
  2301. maxPrice := float64(1000000000000)
  2302. if contractInfo.OriginalPrice >= maxPrice || contractInfo.Price >= maxPrice {
  2303. br.Msg = "合同金额过大, 请核验金额"
  2304. return
  2305. }
  2306. // CMR8.8-合并权益主客观
  2307. if contractInfo.ProductId == 2 {
  2308. mergeList := contractService.EquityMergeSubjectAndObject(contractInfo.Service)
  2309. contractInfo.Service = mergeList
  2310. }
  2311. //合同下载权限校验
  2312. if (sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_SELLER || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER) && contractInfo.SellerId != sysUser.AdminId {
  2313. br.Msg = "合同异常!"
  2314. br.ErrMsg = "合同异常"
  2315. return
  2316. }
  2317. //contractHtml, tmpErr := services.GetHtmlByContractDetail(contractInfo, "pdf")
  2318. //if tmpErr != nil {
  2319. // err = tmpErr
  2320. // return
  2321. //}
  2322. //生成pdf
  2323. //pdfPath := fmt.Sprint("./static/word/系统生成合同_", contractId, ".pdf")
  2324. //tmpErr = services.Html2Pdf(contractHtml, pdfPath)
  2325. //if tmpErr != nil {
  2326. // err = tmpErr
  2327. // return
  2328. //}
  2329. //defer func() {
  2330. // os.Remove(pdfPath)
  2331. //}()
  2332. //downloadFileName := "合同草稿_" + contractInfo.ContractCode + ".pdf"
  2333. wordPath := fmt.Sprint("./static/word/系统生成合同_", contractId, ".docx")
  2334. tmpErr := services.GenerateWordV2(contractInfo, wordPath)
  2335. if tmpErr != nil {
  2336. err = tmpErr
  2337. return
  2338. }
  2339. defer func() {
  2340. os.Remove(wordPath)
  2341. }()
  2342. downloadFileName := contractInfo.CompanyName + "_" + contractInfo.ContractCode + "(草稿).docx"
  2343. this.Ctx.Output.Download(wordPath, downloadFileName)
  2344. br.Ret = 200
  2345. br.Success = true
  2346. br.Msg = "下载成功"
  2347. }
  2348. // getServiceTemplateList 获取系统服务列表
  2349. func getServiceTemplateList(productId int) (list []*contract.ContractServiceTemplateMapItems, err error) {
  2350. list, err = contract.GetContractServiceTemplateMapByProductId(productId)
  2351. if err != nil {
  2352. return
  2353. }
  2354. for i := 0; i < len(list); i++ {
  2355. item := list[i]
  2356. tmpList, tmpErr := contract.GetContractServiceTemplateMapByParentId(item.ServiceTemplateId)
  2357. if tmpErr != nil {
  2358. err = tmpErr
  2359. return
  2360. }
  2361. for j := 0; j < len(tmpList); j++ {
  2362. detail, tmpErr := contract.GetContractServiceDetailByTemplateId(tmpList[j].ServiceTemplateId)
  2363. if tmpErr != nil {
  2364. err = tmpErr
  2365. return
  2366. }
  2367. tmpList[j].Detail = detail
  2368. }
  2369. detail, tmpErr := contract.GetContractServiceDetailByTemplateId(list[i].ServiceTemplateId)
  2370. if tmpErr != nil {
  2371. err = tmpErr
  2372. return
  2373. }
  2374. list[i].Detail = detail
  2375. list[i].Children = tmpList
  2376. }
  2377. return
  2378. }
  2379. // BusinessContractCompanyList
  2380. // @Title 业务合同客户名称列表
  2381. // @Description 业务合同客户名称列表接口
  2382. // @Param Keyword query string true "搜索关键字"
  2383. // @Success 200 {object} []string
  2384. // @router /business_contract/company_name/list/ [get]
  2385. func (this *ContractController) BusinessContractCompanyList() {
  2386. br := new(models.BaseResponse).Init()
  2387. defer func() {
  2388. this.Data["json"] = br
  2389. this.ServeJSON()
  2390. }()
  2391. sysUser := this.SysUser
  2392. if sysUser == nil {
  2393. br.Msg = "请登录"
  2394. br.ErrMsg = "请登录,SysUser Is Empty"
  2395. br.Ret = 408
  2396. return
  2397. }
  2398. condition := ""
  2399. pars := make([]interface{}, 0)
  2400. condition += ` AND a.contract_business_type ="业务合同" AND a.status ="已签回" `
  2401. //如果不是超管或者合规,那么只能查看自己的合同
  2402. //if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  2403. // condition += ` AND a.product_id = 1 `
  2404. //} else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  2405. // condition += ` AND product_id = 2 `
  2406. //} else {
  2407. // if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  2408. // condition += ` AND a.seller_id = ? `
  2409. // pars = append(pars, sysUser.AdminId)
  2410. // }
  2411. //}
  2412. condition += ` AND a.seller_id = ? `
  2413. pars = append(pars, sysUser.AdminId)
  2414. //关键字
  2415. keyword := this.GetString("Keyword")
  2416. keyword = strings.TrimSpace(keyword)
  2417. if keyword == "" {
  2418. br.Msg = "请输入客户名称"
  2419. br.ErrMsg = "请输入客户名称"
  2420. return
  2421. }
  2422. kw := fmt.Sprint("%", keyword, "%")
  2423. condition += ` AND (contract_code LIKE ? OR company_name LIKE ? OR credit_code LIKE ? ) `
  2424. pars = append(pars, kw, kw, kw)
  2425. list, err := contract.GetContractCompanyNameList(condition, pars)
  2426. if err != nil {
  2427. br.Msg = "获取合同列表失败!"
  2428. br.ErrMsg = "获取合同列表失败,Err:" + err.Error()
  2429. return
  2430. }
  2431. companyNameList := make([]string, 0)
  2432. for _, v := range list {
  2433. companyNameList = append(companyNameList, v.CompanyName)
  2434. }
  2435. // 13.6正式共享客户的处理
  2436. {
  2437. // 通过关键词获取分配给当前销售的正式共享客户
  2438. cond := ` AND share_seller_id = ? AND is_share = 1 AND company_name LIKE ?`
  2439. pars := make([]interface{}, 0)
  2440. pars = append(pars, sysUser.AdminId, kw)
  2441. shares, e := company.GetCompanyProductList(cond, pars)
  2442. if e != nil {
  2443. br.Msg = "获取失败"
  2444. br.ErrMsg = "获取共享客户信息失败, Err: " + e.Error()
  2445. return
  2446. }
  2447. for _, v := range shares {
  2448. if !utils.InArrayByStr(companyNameList, v.CompanyName) {
  2449. companyNameList = append(companyNameList, v.CompanyName)
  2450. }
  2451. }
  2452. }
  2453. br.Ret = 200
  2454. br.Success = true
  2455. br.Msg = "获取成功"
  2456. br.Data = companyNameList
  2457. }
  2458. // BusinessContractListByCompanyName
  2459. // @Title 根据客户名称获取所有已签回的业务合同列表
  2460. // @Description 根据客户名称获取所有已签回的业务合同列表接口
  2461. // @Param CompanyName query string false "客户名称"
  2462. // @Success 200 {object} response.ContractListResp
  2463. // @router /business_contract/list/ [get]
  2464. func (this *ContractController) BusinessContractListByCompanyName() {
  2465. br := new(models.BaseResponse).Init()
  2466. defer func() {
  2467. this.Data["json"] = br
  2468. this.ServeJSON()
  2469. }()
  2470. sysUser := this.SysUser
  2471. if sysUser == nil {
  2472. br.Msg = "请登录"
  2473. br.ErrMsg = "请登录,SysUser Is Empty"
  2474. br.Ret = 408
  2475. return
  2476. }
  2477. condition := ""
  2478. pars := make([]interface{}, 0)
  2479. condition += ` AND a.contract_business_type ="业务合同" AND a.status ="已签回" and a.price>a.paid_price `
  2480. //如果不是超管或者合规,那么只能查看自己的合同
  2481. //if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  2482. // condition += ` AND a.product_id = 1 `
  2483. //} else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  2484. // condition += ` AND product_id = 2 `
  2485. //} else {
  2486. // if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  2487. // condition += ` AND a.seller_id = ? `
  2488. // pars = append(pars, sysUser.AdminId)
  2489. // }
  2490. //}
  2491. //condition += ` AND a.seller_id = ? `
  2492. //pars = append(pars, sysUser.AdminId)
  2493. // 13.6正式共享客户-可以选择分配给自己的共享客户下的非自己创建的合同
  2494. joinStr := ` LEFT JOIN company_product AS b ON a.company_name = b.company_name `
  2495. condition += ` AND (a.seller_id = ? OR (b.is_share = 1 AND b.share_seller_id = ?))`
  2496. pars = append(pars, sysUser.AdminId, sysUser.AdminId)
  2497. //关键字
  2498. companyName := this.GetString("CompanyName")
  2499. if companyName == "" {
  2500. br.Msg = "请输入客户名称"
  2501. br.ErrMsg = "请输入客户名称"
  2502. return
  2503. }
  2504. condition += ` AND a.company_name = ? `
  2505. pars = append(pars, companyName)
  2506. pageSize, _ := this.GetInt("PageSize")
  2507. currentIndex, _ := this.GetInt("CurrentIndex")
  2508. var startSize int
  2509. if pageSize <= 0 {
  2510. pageSize = utils.PageSize20
  2511. }
  2512. if currentIndex <= 0 {
  2513. currentIndex = 1
  2514. }
  2515. startSize = paging.StartIndex(currentIndex, pageSize)
  2516. total, err := contract.GetContractListCount(condition, joinStr, pars)
  2517. if err != nil {
  2518. br.Msg = "获取失败"
  2519. br.ErrMsg = "获取数据总数失败,Err:" + err.Error()
  2520. return
  2521. }
  2522. list, err := contract.GetContractList(condition, joinStr, pars, startSize, pageSize)
  2523. if err != nil {
  2524. br.Msg = "获取合同列表失败!"
  2525. br.ErrMsg = "获取合同列表失败,Err:" + err.Error()
  2526. return
  2527. }
  2528. //获取合同套餐服务列表
  2529. serviceTemplateList, err := contract.GetAllContractServiceTemplateList()
  2530. if err != nil {
  2531. br.Msg = "获取合同套餐服务列表失败!"
  2532. br.ErrMsg = "获取合同套餐服务列表失败,Err:" + err.Error()
  2533. return
  2534. }
  2535. templateTitleMap := make(map[int]string)
  2536. for _, serviceTemplate := range serviceTemplateList {
  2537. templateTitleMap[serviceTemplate.ServiceTemplateId] = serviceTemplate.Title
  2538. }
  2539. //业务合同的关联合同id集合
  2540. businessContractIdList := make([]string, 0)
  2541. //代付合同的关联合同id集合
  2542. paymentOnBehalfContractIdList := make([]string, 0)
  2543. //基础合同的关联合同数据
  2544. contractRelationMap := make(map[int][]*contract.RelationContractList)
  2545. for _, v := range list {
  2546. switch v.ContractBusinessType {
  2547. case "业务合同":
  2548. businessContractIdList = append(businessContractIdList, fmt.Sprint(v.ContractId))
  2549. case "代付合同":
  2550. paymentOnBehalfContractIdList = append(paymentOnBehalfContractIdList, fmt.Sprint(v.ContractId))
  2551. }
  2552. }
  2553. //业务合同的关联合同
  2554. if len(businessContractIdList) > 0 {
  2555. contractIdStr := strings.Join(businessContractIdList, ",")
  2556. businessContractList, err := contract.GetContractRelationListByRelationContractIds(contractIdStr)
  2557. if err != nil {
  2558. br.Msg = "获取失败"
  2559. br.ErrMsg = "获取业务关联合同失败,Err:" + err.Error()
  2560. return
  2561. }
  2562. for _, v := range businessContractList {
  2563. if _, ok := contractRelationMap[v.ContractId]; ok {
  2564. contractRelationMap[v.ContractId] = append(contractRelationMap[v.ContractId], v)
  2565. } else {
  2566. tmpList := make([]*contract.RelationContractList, 0)
  2567. contractRelationMap[v.ContractId] = append(tmpList, v)
  2568. }
  2569. }
  2570. }
  2571. //代付合同的关联合同
  2572. if len(paymentOnBehalfContractIdList) > 0 {
  2573. paymentOnBehalfContractIdStr := strings.Join(paymentOnBehalfContractIdList, ",")
  2574. paymentOnBehalfContractList, err := contract.GetContractRelationListByPaymentOnBehalfContractIds(paymentOnBehalfContractIdStr)
  2575. if err != nil {
  2576. br.Msg = "获取失败"
  2577. br.ErrMsg = "获取代付关联合同失败,Err:" + err.Error()
  2578. return
  2579. }
  2580. for _, v := range paymentOnBehalfContractList {
  2581. if _, ok := contractRelationMap[v.PaymentOnBehalfContractId]; ok {
  2582. contractRelationMap[v.PaymentOnBehalfContractId] = append(contractRelationMap[v.PaymentOnBehalfContractId], v)
  2583. } else {
  2584. tmpList := make([]*contract.RelationContractList, 0)
  2585. contractRelationMap[v.PaymentOnBehalfContractId] = append(tmpList, v)
  2586. }
  2587. }
  2588. }
  2589. for i := 0; i < len(list); i++ {
  2590. //合同操作权限
  2591. var opButton contract.ContractOpButton
  2592. item := list[i]
  2593. list[i].StartDateStr = item.StartDate.Format(utils.FormatDate)
  2594. list[i].EndDateStr = item.EndDate.Format(utils.FormatDate)
  2595. list[i].CreateTimeStr = item.CreateTime.Format(utils.FormatDateTime)
  2596. list[i].ModifyTimeStr = item.ModifyTime.Format(utils.FormatDateTime)
  2597. list[i].ApproveTimeStr = item.ApproveTime.Format(utils.FormatDateTime)
  2598. list[i].InvalidTimeStr = item.InvalidTime.Format(utils.FormatDateTime)
  2599. list[i].CheckBackFileTimeStr = item.CheckBackFileTime.Format(utils.FormatDateTime)
  2600. list[i].RescindTimeStr = item.RescindTime.Format(utils.FormatDateTime)
  2601. //服务列表
  2602. serviceList, err := contract.GetContractServiceAndDetailList(item.ContractId)
  2603. if err != nil {
  2604. br.Msg = "获取失败"
  2605. br.ErrMsg = "查找合同服务异常,Err:" + err.Error()
  2606. return
  2607. }
  2608. for i := 0; len(serviceList) > i; i++ {
  2609. if serviceList[i].HasDetail == "是" {
  2610. list, detailErr := contract.GetContractServiceDetailListByServiceId(serviceList[i].ContractServiceId)
  2611. if detailErr != nil {
  2612. br.Msg = "获取失败"
  2613. br.ErrMsg = "查找合同服务详情异常,Err:" + err.Error()
  2614. return
  2615. }
  2616. //套餐名称
  2617. serviceList[i].DetailList = list
  2618. if title, ok := templateTitleMap[serviceList[i].ServiceTemplateId]; ok {
  2619. serviceList[i].Title = title
  2620. }
  2621. }
  2622. }
  2623. list[i].Service = serviceList
  2624. if item.Status == "待审批" {
  2625. approvalInfo, tmpErr := contract.GetLastContractApprovalByContractId(item.ContractId, "contract")
  2626. if tmpErr != nil {
  2627. //err= tmpErr
  2628. //return
  2629. continue
  2630. }
  2631. //如果合同归属是当前账号,且处于第一级审批
  2632. if item.SellerId == sysUser.AdminId && approvalInfo.CurrNodeId == approvalInfo.StartNodeId {
  2633. opButton.Cancel = true
  2634. }
  2635. }
  2636. list[i].OpButton = opButton
  2637. if relationContractList, ok := contractRelationMap[item.ContractId]; ok {
  2638. list[i].RelationContractList = relationContractList
  2639. }
  2640. }
  2641. page := paging.GetPaging(currentIndex, pageSize, total)
  2642. br.Ret = 200
  2643. br.Success = true
  2644. br.Msg = "获取成功"
  2645. br.Data = response.ContractListResp{
  2646. List: list,
  2647. Paging: page,
  2648. }
  2649. return
  2650. }
  2651. // UpdateFile
  2652. // @Title 更新合同附件(用于非标)
  2653. // @Description 更新合同附件接口
  2654. // @Param request body request.UploadFileReq true "type json string"
  2655. // @Success Ret=200 上传成功
  2656. // @router /update_file [post]
  2657. func (this *ContractController) UpdateFile() {
  2658. br := new(models.BaseResponse).Init()
  2659. defer func() {
  2660. this.Data["json"] = br
  2661. this.ServeJSON()
  2662. }()
  2663. sysUser := this.SysUser
  2664. if sysUser == nil {
  2665. br.Msg = "请登录"
  2666. br.ErrMsg = "请登录,SysUser Is Empty"
  2667. br.Ret = 408
  2668. return
  2669. }
  2670. //var req request.UploadFileReq
  2671. //err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2672. //if err != nil {
  2673. // br.Msg = "参数解析异常!"
  2674. // br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2675. // return
  2676. //}
  2677. contractIdStr := this.Ctx.Request.Form.Get("ContractId")
  2678. if contractIdStr == "" {
  2679. br.Msg = "合同ID必传!"
  2680. br.ErrMsg = "合同ID必传,Err"
  2681. return
  2682. }
  2683. contractId, err := strconv.Atoi(contractIdStr)
  2684. if err != nil {
  2685. br.Msg = "合同ID异常!"
  2686. br.ErrMsg = "合同ID异常,Err:" + err.Error()
  2687. return
  2688. }
  2689. fileMulti, h, err := this.GetFile("file")
  2690. if err != nil {
  2691. br.Msg = "获取资源信息失败"
  2692. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  2693. return
  2694. }
  2695. //合同编号
  2696. if contractId <= 0 {
  2697. br.Msg = "请传入合同编号!"
  2698. br.ErrMsg = "请传入合同编号"
  2699. return
  2700. }
  2701. ext := path.Ext(h.Filename)
  2702. err = contractService.UploadFile(contractId, ext, fileMulti, this.SysUser)
  2703. br.Ret = 200
  2704. br.Success = true
  2705. br.Msg = "上传成功"
  2706. }
  2707. // GetListBySeal
  2708. // @Title 获取用印所需合同列表
  2709. // @Description 获取用印所需合同列表
  2710. // @Param Keyword query string false "搜索关键字"
  2711. // @Success 200 {object} response.ContractListBySealResp
  2712. // @router /getListBySeal [get]
  2713. func (this *ContractController) GetListBySeal() {
  2714. br := new(models.BaseResponse).Init()
  2715. defer func() {
  2716. this.Data["json"] = br
  2717. this.ServeJSON()
  2718. }()
  2719. sysUser := this.SysUser
  2720. if sysUser == nil {
  2721. br.Msg = "请登录"
  2722. br.ErrMsg = "请登录,SysUser Is Empty"
  2723. br.Ret = 408
  2724. return
  2725. }
  2726. keyword := this.GetString("Keyword")
  2727. condition := ` AND a.status = "已审批"`
  2728. pars := make([]interface{}, 0)
  2729. if keyword != "" {
  2730. kw := fmt.Sprint("%", keyword, "%")
  2731. condition += ` AND (a.company_name LIKE ?) `
  2732. pars = append(pars, kw)
  2733. }
  2734. // 13.6正式共享客户
  2735. joinStr := ` LEFT JOIN company_product AS b ON a.company_name = b.company_name `
  2736. // 非合规角色可查看自己的合同或是自己的共享客户下的非自己创建的合同
  2737. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE {
  2738. condition += ` AND (a.seller_id = ? OR (b.is_share = 1 AND b.share_seller_id = ?))`
  2739. pars = append(pars, sysUser.AdminId, sysUser.AdminId)
  2740. }
  2741. list, e := contract.GetContractList(condition, joinStr, pars, 0, 100)
  2742. if e != nil {
  2743. br.Msg = "获取失败"
  2744. br.ErrMsg = "获取合同列表失败, Err:" + e.Error()
  2745. return
  2746. }
  2747. //list, err := contract.GetSearchListBySeal(condition, pars)
  2748. //if err != nil {
  2749. // br.Msg = "获取失败"
  2750. // br.ErrMsg = "获取数据失败,Err:" + err.Error()
  2751. // return
  2752. //}
  2753. newList := make([]response.ContractListBySealResp, 0)
  2754. if len(list) > 0 {
  2755. // 代付合同查询代付用户名称
  2756. contractIdSlice := make([]string, 0)
  2757. for i := 0; i < len(list); i++ {
  2758. if list[i].ContractBusinessType == "代付合同" {
  2759. contractIdSlice = append(contractIdSlice, strconv.Itoa(list[i].ContractId))
  2760. }
  2761. }
  2762. userCompanyNameMap := make(map[int]string, 0)
  2763. if len(contractIdSlice) > 0 {
  2764. contractIdStr := strings.Join(contractIdSlice, ",")
  2765. contractList, err := contract.GetContractRelationListByPaymentOnBehalfContractIds(contractIdStr)
  2766. if err != nil {
  2767. br.Msg = "获取失败"
  2768. br.ErrMsg = "获取代付合同对应的合同数据失败,Err:" + err.Error()
  2769. return
  2770. }
  2771. for i := 0; i < len(contractList); i++ {
  2772. userCompanyNameMap[contractList[i].PaymentOnBehalfContractId] = contractList[i].CompanyName
  2773. }
  2774. }
  2775. for _, item := range list {
  2776. var tempItem response.ContractListBySealResp
  2777. tempItem.ContractId = item.ContractId
  2778. tempItem.ContractCode = item.ContractCode
  2779. tempItem.CompanyName = item.CompanyName
  2780. tempItem.FileUrl = item.FileUrl
  2781. tempItem.Price = item.Price
  2782. tempItem.CreditCode = item.CreditCode
  2783. tempItem.ContractBusinessType = item.ContractBusinessType
  2784. if item.ContractBusinessType == "代付合同" {
  2785. tempItem.ContractType = "代付合同"
  2786. tempItem.UserCompanyName = userCompanyNameMap[item.ContractId]
  2787. } else {
  2788. tempItem.ContractType = item.ContractType
  2789. }
  2790. newList = append(newList, tempItem)
  2791. }
  2792. }
  2793. br.Ret = 200
  2794. br.Success = true
  2795. br.Msg = "获取成功"
  2796. br.Data = newList
  2797. }
  2798. // DetailMerge
  2799. // @Title 获取合同详情-有套餐合并,用于审批单回显等
  2800. // @Description 获取合同详情接口
  2801. // @Param ContractId query int true "合同id"
  2802. // @Success 200 {object} contract.ContractDetail
  2803. // @router /detail/merge [get]
  2804. func (this *ContractController) DetailMerge() {
  2805. br := new(models.BaseResponse).Init()
  2806. defer func() {
  2807. this.Data["json"] = br
  2808. this.ServeJSON()
  2809. }()
  2810. sysUser := this.SysUser
  2811. if sysUser == nil {
  2812. br.Msg = "请登录"
  2813. br.ErrMsg = "请登录,SysUser Is Empty"
  2814. br.Ret = 408
  2815. return
  2816. }
  2817. //合同类型、产品类型、合同状态、更新时间、所选销售
  2818. //关键字:合同编号、客户名称,社会信用码
  2819. contractId, _ := this.GetInt("ContractId")
  2820. //合同id
  2821. if contractId <= 0 {
  2822. br.Msg = "合同id必传!"
  2823. br.ErrMsg = "合同id必传"
  2824. return
  2825. }
  2826. contractInfo, err := contractService.GetContractDetail(contractId)
  2827. if err != nil {
  2828. br.Msg = "获取合同详情失败!"
  2829. br.ErrMsg = "获取合同详情失败,ERR:" + err.Error()
  2830. return
  2831. }
  2832. // CRM8.8-权益主客观合并
  2833. if contractInfo.ProductId == 2 {
  2834. mergeList := contractService.EquityMergeSubjectAndObject(contractInfo.Service)
  2835. contractInfo.Service = mergeList
  2836. // 关联合同同样合并
  2837. for i, v := range contractInfo.RelationContractDetailList {
  2838. relationMergeList := contractService.EquityMergeSubjectAndObject(v.Service)
  2839. contractInfo.RelationContractDetailList[i].Service = relationMergeList
  2840. }
  2841. }
  2842. contractInfo.StartDateStr = contractInfo.StartDate.Format(utils.FormatDate)
  2843. contractInfo.EndDateStr = contractInfo.EndDate.Format(utils.FormatDate)
  2844. contractInfo.ModifyTimeStr = contractInfo.ModifyTime.Format(utils.FormatDateTime)
  2845. contractInfo.CreateTimeStr = contractInfo.CreateTime.Format(utils.FormatDateTime)
  2846. //合同中包含的产品权限
  2847. isNeedPermission := false //是否需要列出来套餐中的详细权限
  2848. for _, service := range contractInfo.Service {
  2849. if service.ServiceTemplateId == 2 { //小套餐才需要列出来详细的权限
  2850. isNeedPermission = true
  2851. }
  2852. }
  2853. var permissionLookListResp = make([]*contract.PermissionLookList, 0)
  2854. if isNeedPermission {
  2855. checkPermissionMap, tErr := contractService.GetServicePermissionMap(contractInfo.Service)
  2856. if tErr == nil {
  2857. permissionLookListMap := make(map[int][]*contract.PermissionLookItem)
  2858. var permissionIds []string
  2859. for _, v := range checkPermissionMap {
  2860. permissionIds = append(permissionIds, strconv.Itoa(v))
  2861. }
  2862. permissionIdStr := strings.Join(permissionIds, ",")
  2863. charPermissions, tErr1 := company.GetPermissionLookItemsSandO(permissionIdStr)
  2864. if tErr1 == nil {
  2865. for _, v := range charPermissions {
  2866. tmp := new(contract.PermissionLookItem)
  2867. tmp.ClassifyName = v.ClassifyName
  2868. tmp.PermissionName = v.PermissionName
  2869. tmp.ChartPermissionId = v.ChartPermissionId
  2870. permissionLookListMap[v.ParentId] = append(permissionLookListMap[v.ParentId], tmp)
  2871. }
  2872. classifyNameList, tErr2 := company.GetChartPermissionFirst()
  2873. if tErr2 == nil {
  2874. for _, v := range classifyNameList {
  2875. if perList, ok := permissionLookListMap[v.ChartPermissionId]; ok {
  2876. permissionLookListRespItem := &contract.PermissionLookList{
  2877. ClassifyName: v.ClassifyName,
  2878. Items: perList,
  2879. }
  2880. permissionLookListResp = append(permissionLookListResp, permissionLookListRespItem)
  2881. }
  2882. }
  2883. }
  2884. }
  2885. }
  2886. }
  2887. contractInfo.PermissionLookList = permissionLookListResp
  2888. br.Ret = 200
  2889. br.Success = true
  2890. br.Msg = "获取成功"
  2891. br.Data = contractInfo
  2892. }