Contract.go 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  1. package controllers
  2. import (
  3. "ERP_storage/Nats"
  4. "ERP_storage/Nats/NatsServer"
  5. "ERP_storage/conf"
  6. "ERP_storage/logs"
  7. "ERP_storage/models/Account"
  8. "ERP_storage/models/Basic"
  9. "ERP_storage/models/Contract"
  10. "ERP_storage/models/Stock"
  11. "fmt"
  12. "github.com/beego/beego/v2/adapter/orm"
  13. beego "github.com/beego/beego/v2/server/web"
  14. "github.com/robfig/cron/v3"
  15. "github.com/xuri/excelize/v2"
  16. natslibs "gogs.baozhida.cn/zoie/ERP_libs/Nats"
  17. userlibs "gogs.baozhida.cn/zoie/ERP_libs/User"
  18. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  19. "math"
  20. "net/url"
  21. "os"
  22. "strconv"
  23. "strings"
  24. "time"
  25. )
  26. type ContractController struct {
  27. beego.Controller
  28. User userlibs.User
  29. }
  30. func (c *ContractController) Prepare() {
  31. c.User = *Account.User_r
  32. }
  33. func (c *ContractController) Contract_GenT_number() {
  34. o := orm.NewOrm()
  35. ContractDao := Contract.NewContract(o)
  36. T_number := ""
  37. rand_x := 1
  38. for true {
  39. T_number = fmt.Sprintf("GZBZD-%s%03d", time.Now().Format("20060102"), rand_x)
  40. _, err := ContractDao.Read_Contract_ByT_number(T_number)
  41. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  42. break
  43. }
  44. rand_x += 1
  45. }
  46. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  47. c.ServeJSON()
  48. return
  49. }
  50. func (c *ContractController) Contract_List() {
  51. // 分页参数 初始化
  52. page, _ := c.GetInt("page")
  53. if page < 1 {
  54. page = 1
  55. }
  56. page_z, _ := c.GetInt("page_z")
  57. if page_z < 1 {
  58. page_z = conf.Page_size
  59. }
  60. // 查询
  61. T_name := c.GetString("T_name")
  62. T_state, _ := c.GetInt("T_state")
  63. userList, _ := NatsServer.Read_User_List_All()
  64. Account.Read_User_All_Map(userList)
  65. ContractDao := Contract.NewContract(orm.NewOrm())
  66. R_List, R_cnt := ContractDao.Read_Contract_List("", T_name, T_state, 0, page, page_z)
  67. var r_jsons lib.R_JSONS
  68. r_jsons.Num = R_cnt
  69. r_jsons.Data = R_List
  70. r_jsons.Page = page
  71. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  72. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  73. c.ServeJSON()
  74. return
  75. }
  76. func (c *ContractController) Contract_User_List() {
  77. // 分页参数 初始化
  78. page, _ := c.GetInt("page")
  79. if page < 1 {
  80. page = 1
  81. }
  82. page_z, _ := c.GetInt("page_z")
  83. if page_z < 1 {
  84. page_z = conf.Page_size
  85. }
  86. // 查询
  87. T_name := c.GetString("T_name")
  88. T_state, _ := c.GetInt("T_state")
  89. userList, _ := NatsServer.Read_User_List_All()
  90. Account.Read_User_All_Map(userList)
  91. ContractDao := Contract.NewContract(orm.NewOrm())
  92. R_List, R_cnt := ContractDao.Read_Contract_List(c.User.T_uuid, T_name, T_state, 0, page, page_z)
  93. var r_jsons lib.R_JSONS
  94. r_jsons.Num = R_cnt
  95. r_jsons.Data = R_List
  96. r_jsons.Page = page
  97. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  98. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  99. c.ServeJSON()
  100. return
  101. }
  102. func (c *ContractController) Contract_Get() {
  103. // 查询
  104. T_number := c.GetString("T_number")
  105. o := orm.NewOrm()
  106. ContractDao := Contract.NewContract(o)
  107. DeviceDao := Stock.NewDevice(o)
  108. ContractProductDao := Contract.NewContractProduct(o)
  109. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  110. if err != nil {
  111. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  112. c.ServeJSON()
  113. return
  114. }
  115. productList := ContractProductDao.Read_ContractProduct_List(contract.T_number)
  116. var pList []Contract.ContractProduct_R
  117. for _, v := range productList {
  118. p := Contract.ContractProductToContractProduct_R(v)
  119. p.T_device_list, _ = DeviceDao.Read_DeviceSn_List(T_number, v.T_product_id, "", "")
  120. pList = append(pList, p)
  121. }
  122. userList, _ := NatsServer.Read_User_List_All()
  123. Account.Read_User_All_Map(userList)
  124. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Contract.ContractToContract_Detail(contract, pList)}
  125. c.ServeJSON()
  126. return
  127. }
  128. func (c *ContractController) Contract_Add() {
  129. T_number := c.GetString("T_number")
  130. T_customer := c.GetString("T_customer")
  131. T_money, _ := c.GetFloat("T_money") // 总价
  132. T_discount, _ := c.GetFloat("T_discount") // 优惠价
  133. T_date := c.GetString("T_date")
  134. T_product := c.GetString("T_product")
  135. T_remark := c.GetString("T_remark")
  136. T_pdf := c.GetString("T_pdf")
  137. T_project := c.GetString("T_project")
  138. T_recoveries := c.GetString("T_recoveries")
  139. T_invoice := c.GetString("T_invoice")
  140. T_submit := c.GetString("T_submit")
  141. if len(T_submit) == 0 {
  142. T_submit = c.User.T_uuid
  143. }
  144. var_ := Contract.Contract{
  145. T_number: T_number,
  146. T_customer: T_customer,
  147. T_money: float32(T_money),
  148. T_discount: float32(T_discount),
  149. T_project: T_project,
  150. T_type: 1, //合同类型 1-销售合同 2-验证合同
  151. T_date: T_date,
  152. T_out: 1,
  153. T_State: 3,
  154. T_remark: T_remark,
  155. T_pdf: T_pdf,
  156. T_submit: T_submit,
  157. T_recoveries: T_recoveries,
  158. T_invoice: T_invoice,
  159. }
  160. _, var_.T_recoveries_money = Contract.ContractToContractFinance_R(T_recoveries)
  161. _, var_.T_invoice_money = Contract.ContractToContractFinance_R(T_invoice)
  162. o := orm.NewOrm()
  163. o.Begin()
  164. ContractDao := Contract.NewContract(o)
  165. ContractProductDao := Contract.NewContractProduct(o)
  166. _, err := ContractDao.Read_Contract_ByT_number(T_number)
  167. if err == nil {
  168. o.Rollback()
  169. c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同编号重复!"}
  170. c.ServeJSON()
  171. return
  172. }
  173. _, err = ContractDao.Add_Contract(var_)
  174. if err != nil {
  175. o.Rollback()
  176. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  177. c.ServeJSON()
  178. return
  179. }
  180. if len(T_product) == 0 {
  181. o.Rollback()
  182. c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同产品明细不能为空"}
  183. c.ServeJSON()
  184. return
  185. }
  186. productList := lib.SplitString(T_product, "|")
  187. for _, v := range productList {
  188. product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
  189. num, _ := strconv.Atoi(strings.Split(v, ",")[1])
  190. contractProduct := Contract.ContractProduct{
  191. T_contract_number: T_number,
  192. T_product_id: product_id,
  193. T_product_total: num,
  194. T_product_out: 0, // 已出库数量
  195. T_State: 1, // 1-未出库 2-已部分出库 3-已全部出库
  196. }
  197. _, err = ContractProductDao.Add_ContractProduct(contractProduct)
  198. if err != nil {
  199. o.Rollback()
  200. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  201. c.ServeJSON()
  202. return
  203. }
  204. }
  205. o.Commit()
  206. NatsServer.AddUserLogs(c.User.T_uuid, "合同", "添加", var_)
  207. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  208. c.ServeJSON()
  209. return
  210. }
  211. func (c *ContractController) Contract_Approval() {
  212. T_number := c.GetString("T_number")
  213. T_state, _ := c.GetInt("T_state")
  214. o := orm.NewOrm()
  215. ContractDao := Contract.NewContract(o)
  216. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  217. if err != nil {
  218. o.Rollback()
  219. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  220. c.ServeJSON()
  221. return
  222. }
  223. // 1-已通过 2-未通过
  224. contract.T_State = T_state
  225. contract.T_approver = c.User.T_uuid
  226. err = ContractDao.Update_Contract(contract, "T_State", "T_approver")
  227. if err != nil {
  228. o.Rollback()
  229. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  230. c.ServeJSON()
  231. return
  232. }
  233. if T_state == 1 {
  234. NatsServer.AddNews(contract.T_submit, fmt.Sprintf("【合同审核】您提交的合同(%s)审核已通过", T_number), conf.ContractApprovalUrl)
  235. }
  236. if T_state == 2 {
  237. NatsServer.AddNews(contract.T_submit, fmt.Sprintf("【合同审核】您提交的合同(%s)审核未通过", T_number), conf.ContractApprovalUrl)
  238. }
  239. NatsServer.AddUserLogs(c.User.T_uuid, "合同", "审核", contract)
  240. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  241. c.ServeJSON()
  242. return
  243. }
  244. func (c *ContractController) Contract_Edit() {
  245. T_number := c.GetString("T_number")
  246. T_customer := c.GetString("T_customer")
  247. T_money, _ := c.GetFloat("T_money")
  248. T_discount, _ := c.GetFloat("T_discount") // 优惠价
  249. T_date := c.GetString("T_date")
  250. T_product := c.GetString("T_product")
  251. T_remark := c.GetString("T_remark")
  252. T_pdf := c.GetString("T_pdf")
  253. T_project := c.GetString("T_project")
  254. T_recoveries := c.GetString("T_recoveries")
  255. T_invoice := c.GetString("T_invoice")
  256. T_submit := c.GetString("T_submit")
  257. o := orm.NewOrm()
  258. o.Begin()
  259. ContractDao := Contract.NewContract(o)
  260. ContractProductDao := Contract.NewContractProduct(o)
  261. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  262. if err != nil {
  263. o.Rollback()
  264. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
  265. c.ServeJSON()
  266. return
  267. }
  268. if len(T_customer) > 0 {
  269. contract.T_customer = T_customer
  270. }
  271. if T_money > 0 {
  272. contract.T_money = float32(T_money)
  273. }
  274. if T_discount > 0 {
  275. contract.T_discount = float32(T_discount)
  276. }
  277. if len(T_date) > 0 {
  278. contract.T_date = T_date
  279. }
  280. if len(T_remark) > 0 {
  281. contract.T_remark = T_remark
  282. }
  283. if len(T_pdf) > 0 {
  284. contract.T_pdf = T_pdf
  285. }
  286. if len(T_project) > 0 {
  287. contract.T_project = T_project
  288. }
  289. if len(T_recoveries) > 0 {
  290. contract.T_recoveries = T_recoveries
  291. _, contract.T_recoveries_money = Contract.ContractToContractFinance_R(T_recoveries)
  292. }
  293. if len(T_invoice) > 0 {
  294. contract.T_invoice = T_invoice
  295. _, contract.T_invoice_money = Contract.ContractToContractFinance_R(T_invoice)
  296. }
  297. if len(T_submit) > 0 {
  298. contract.T_submit = T_submit
  299. }
  300. //1-已通过 2-未通过 3-待审核 合同状态为未通过,修改之后将状态更改为待审核
  301. if contract.T_State == 2 {
  302. contract.T_State = 3
  303. }
  304. err = ContractDao.Update_Contract(contract, "T_customer", "T_money", "T_discount", "T_date", "T_remark", "T_pdf", "T_State",
  305. "T_project", "T_recoveries", "T_recoveries_money", "T_invoice", "T_invoice_money", "T_submit")
  306. if err != nil {
  307. o.Rollback()
  308. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  309. c.ServeJSON()
  310. return
  311. }
  312. if len(T_product) == 0 {
  313. o.Rollback()
  314. c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同产品明细不能为空"}
  315. c.ServeJSON()
  316. return
  317. }
  318. productList := lib.SplitString(T_product, "|")
  319. cpl, _ := ContractProductDao.Read_ContractProductList_ByT_number(T_number)
  320. needToDeleteList, allList := Check_NeedToDelete_Product_List(productList, cpl)
  321. // 检查编辑时需要修改的产品
  322. for k, v := range needToDeleteList {
  323. id, _ := strconv.Atoi(strings.Split(k, "-")[0])
  324. productId, _ := strconv.Atoi(strings.Split(k, "-")[1])
  325. if v > 0 {
  326. o.Rollback()
  327. product, _ := Basic.Read_Product_ById(productId)
  328. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s已(部分)出库,禁止删除!", product.T_name)}
  329. c.ServeJSON()
  330. return
  331. }
  332. cp := Contract.ContractProduct{Id: id}
  333. err = ContractProductDao.Delete_ContractProduct(cp)
  334. if err != nil {
  335. o.Rollback()
  336. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  337. c.ServeJSON()
  338. return
  339. }
  340. }
  341. for _, v := range productList {
  342. product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
  343. num, _ := strconv.Atoi(strings.Split(v, ",")[1])
  344. oldContractProduct, ok := allList[product_id]
  345. if !ok {
  346. contractProduct := Contract.ContractProduct{
  347. T_contract_number: T_number,
  348. T_product_id: product_id,
  349. T_product_total: num,
  350. T_product_out: 0, // 已出库数量
  351. T_State: 1, // 1-未出库 2-已部分出库 3-已全部出库
  352. }
  353. _, err = ContractProductDao.Add_ContractProduct(contractProduct)
  354. if err != nil {
  355. o.Rollback()
  356. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  357. c.ServeJSON()
  358. return
  359. } else {
  360. continue
  361. }
  362. }
  363. // 修改产品总数量
  364. contractProductId, _ := strconv.Atoi(strings.Split(oldContractProduct, "-")[0])
  365. oldNum, _ := strconv.Atoi(strings.Split(oldContractProduct, "-")[1])
  366. if oldNum == num {
  367. continue
  368. }
  369. cp := Contract.ContractProduct{Id: contractProductId, T_product_total: num}
  370. err = ContractProductDao.Update_ContractProduct(cp, "T_product_total")
  371. if err != nil {
  372. o.Rollback()
  373. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  374. c.ServeJSON()
  375. return
  376. }
  377. }
  378. o.Commit()
  379. NatsServer.AddUserLogs(c.User.T_uuid, "合同", "修改", contract)
  380. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: contract.Id}
  381. c.ServeJSON()
  382. return
  383. }
  384. func (c *ContractController) Contract_Del() {
  385. T_number := c.GetString("T_number")
  386. o := orm.NewOrm()
  387. o.Begin()
  388. ContractDao := Contract.NewContract(o)
  389. ContractProductDao := Contract.NewContractProduct(o)
  390. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  391. if err != nil {
  392. o.Rollback()
  393. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
  394. c.ServeJSON()
  395. return
  396. }
  397. //1-已通过 2-未通过 3-待审核 审核状态
  398. if contract.T_State == 1 {
  399. o.Rollback()
  400. c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同已审核通过,禁止删除!"}
  401. c.ServeJSON()
  402. return
  403. }
  404. err = ContractDao.Delete_Contract(contract)
  405. if err != nil {
  406. o.Rollback()
  407. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  408. c.ServeJSON()
  409. return
  410. }
  411. //合同类型 1-销售合同 2-验证合同
  412. cpList := ContractProductDao.Read_ContractProduct_List(T_number)
  413. for _, v := range cpList {
  414. err = ContractProductDao.Delete_ContractProduct(v)
  415. if err != nil {
  416. o.Rollback()
  417. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  418. c.ServeJSON()
  419. return
  420. }
  421. }
  422. o.Commit()
  423. NatsServer.AddUserLogs(c.User.T_uuid, "合同", "删除", T_number)
  424. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  425. c.ServeJSON()
  426. return
  427. }
  428. func (c *ContractController) Contract_List_For_Out() {
  429. // 分页参数 初始化
  430. page, _ := c.GetInt("page")
  431. if page < 1 {
  432. page = 1
  433. }
  434. page_z, _ := c.GetInt("page_z")
  435. if page_z < 1 {
  436. page_z = conf.Page_size
  437. }
  438. // 查询
  439. T_name := c.GetString("T_name")
  440. userList, _ := NatsServer.Read_User_List_All()
  441. Account.Read_User_All_Map(userList)
  442. ContractDao := Contract.NewContract(orm.NewOrm())
  443. R_List, R_cnt := ContractDao.Read_Contract_List("", T_name, 1, 1, page, page_z)
  444. var r_jsons lib.R_JSONS
  445. r_jsons.Num = R_cnt
  446. r_jsons.Data = R_List
  447. r_jsons.Page = page
  448. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  449. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  450. c.ServeJSON()
  451. return
  452. }
  453. // 合同下的产品列表 出库时使用
  454. func (c *ContractController) Contract_Product_List() {
  455. // 查询
  456. T_number := c.GetString("T_number")
  457. T_depot_id, _ := c.GetInt("T_depot_id")
  458. o := orm.NewOrm()
  459. ContractDao := Contract.NewContract(o)
  460. ContractProductDao := Contract.NewContractProduct(o)
  461. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  462. if err != nil {
  463. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  464. c.ServeJSON()
  465. return
  466. }
  467. productList := ContractProductDao.Read_ContractProduct_OutList(contract.T_number)
  468. Stock.Read_Stock_All_Map(T_depot_id)
  469. var pList []Contract.ContractProduct_Out
  470. for _, v := range productList {
  471. p := Contract.ContractProductToContractProduct_Out(v)
  472. p.T_stock_total = Stock.Read_Stock_Get(T_depot_id, p.T_product_id)
  473. pList = append(pList, p)
  474. }
  475. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: pList}
  476. c.ServeJSON()
  477. return
  478. }
  479. // 检查需要删除的合同产品列表
  480. func Check_NeedToDelete_Product_List(list []string, newList []Contract.ContractProduct) (needToDelete map[string]int, all map[int]string) {
  481. var productMap = map[int]int{}
  482. needToDelete = make(map[string]int)
  483. all = make(map[int]string)
  484. for _, v := range list {
  485. product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
  486. num, _ := strconv.Atoi(strings.Split(v, ",")[1])
  487. productMap[product_id] = num
  488. }
  489. for _, product := range newList {
  490. all[product.T_product_id] = fmt.Sprintf("%d-%d-%f", product.Id, product.T_product_total, product.T_price)
  491. _, ok := productMap[product.T_product_id]
  492. if !ok {
  493. id := fmt.Sprintf("%d-%d-%f", product.Id, product.T_product_id, product.T_price)
  494. needToDelete[id] = product.T_product_out
  495. }
  496. }
  497. return
  498. }
  499. // 验证合同
  500. func (c *ContractController) VerifyContract_List() {
  501. // 分页参数 初始化
  502. page, _ := c.GetInt("page")
  503. if page < 1 {
  504. page = 1
  505. }
  506. page_z, _ := c.GetInt("page_z")
  507. if page_z < 1 {
  508. page_z = conf.Page_size
  509. }
  510. T_customer_id := c.GetString("T_customer_id")
  511. // 查询
  512. T_name := c.GetString("T_name")
  513. userList, _ := NatsServer.Read_User_List_All()
  514. Account.Read_User_All_Map(userList)
  515. ContractDao := Contract.NewContract(orm.NewOrm())
  516. R_List, R_cnt := ContractDao.Read_VerifyContract_List(T_name, T_customer_id, page, page_z)
  517. var r_jsons lib.R_JSONS
  518. r_jsons.Num = R_cnt
  519. r_jsons.Data = R_List
  520. r_jsons.Page = page
  521. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  522. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  523. c.ServeJSON()
  524. return
  525. }
  526. // 验证合同呢添加
  527. func (c *ContractController) VerifyContract_Add() {
  528. T_number := c.GetString("T_number")
  529. T_customer_id := c.GetString("T_customer_id")
  530. T_money, _ := c.GetFloat("T_money")
  531. T_discount, _ := c.GetFloat("T_discount")
  532. T_date := c.GetString("T_date")
  533. T_product := c.GetString("T_product")
  534. T_submit := c.GetString("T_submit")
  535. T_remark := c.GetString("T_remark")
  536. T_pdf := c.GetString("T_pdf")
  537. T_project := c.GetString("T_project")
  538. T_recoveries := c.GetString("T_recoveries")
  539. T_invoice := c.GetString("T_invoice")
  540. T_start_date := c.GetString("T_start_date")
  541. T_end_date := c.GetString("T_end_date")
  542. T_stamping_date := c.GetString("T_stamping_date")
  543. T_payment_method := c.GetString("T_payment_method")
  544. var_ := Contract.Contract{
  545. T_number: T_number,
  546. T_customer_id: T_customer_id,
  547. T_money: float32(T_money),
  548. T_discount: float32(T_discount),
  549. T_project: T_project,
  550. T_type: 2, //合同类型 1-销售合同 2-验证合同
  551. T_date: T_date,
  552. T_State: 1,
  553. T_remark: T_remark,
  554. T_pdf: T_pdf,
  555. T_submit: T_submit,
  556. T_recoveries: T_recoveries,
  557. T_invoice: T_invoice,
  558. T_start_date: T_start_date,
  559. T_end_date: T_end_date,
  560. T_stamping_date: T_stamping_date,
  561. T_payment_method: T_payment_method,
  562. }
  563. _, var_.T_recoveries_money = Contract.ContractToContractFinance_R(T_recoveries)
  564. _, var_.T_invoice_money = Contract.ContractToContractFinance_R(T_invoice)
  565. o := orm.NewOrm()
  566. o.Begin()
  567. ContractDao := Contract.NewContract(o)
  568. ContractProductDao := Contract.NewContractProduct(o)
  569. _, err := ContractDao.Read_Contract_ByT_date(T_customer_id, T_start_date, T_end_date)
  570. if err == nil {
  571. o.Rollback()
  572. c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同签约时间重复!"}
  573. c.ServeJSON()
  574. return
  575. }
  576. _, err = ContractDao.Read_Contract_ByT_number(T_number)
  577. if err == nil {
  578. o.Rollback()
  579. c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同编号重复!"}
  580. c.ServeJSON()
  581. return
  582. }
  583. ContractID, err := ContractDao.Add_Contract(var_)
  584. if err != nil {
  585. o.Rollback()
  586. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  587. c.ServeJSON()
  588. return
  589. }
  590. if len(T_product) == 0 {
  591. o.Rollback()
  592. c.Data["json"] = lib.JSONS{Code: 202, Msg: "验证合同明细不能为空"}
  593. c.ServeJSON()
  594. return
  595. }
  596. productList := lib.SplitString(T_product, "|")
  597. ContractOut := 0
  598. for _, v := range productList {
  599. product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
  600. num, _ := strconv.Atoi(strings.Split(v, ",")[1])
  601. price := lib.StringToFloat64TwoDecimal(strings.Split(v, ",")[2])
  602. product, _ := Basic.Read_Product_ById(product_id)
  603. contractProduct := Contract.ContractProduct{
  604. T_contract_number: T_number,
  605. T_product_id: product_id,
  606. T_product_total: num,
  607. T_product_out: 0, // 已出库数量
  608. T_price: float32(price), // 价格
  609. T_State: 1, // 1-未出库 2-已部分出库 3-已全部出库
  610. }
  611. if product.T_class < 0 {
  612. contractProduct.T_State = 3
  613. }
  614. if product.T_class == 0 {
  615. ContractOut = 1
  616. }
  617. _, err = ContractProductDao.Add_ContractProduct(contractProduct)
  618. if err != nil {
  619. o.Rollback()
  620. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  621. c.ServeJSON()
  622. return
  623. }
  624. }
  625. o.Commit()
  626. o2 := orm.NewOrm()
  627. ContractDao2 := Contract.NewContract(o2)
  628. if ContractOut == 1 {
  629. err = ContractDao2.Update_Contract(Contract.Contract{Id: int(ContractID), T_out: ContractOut}, "T_out")
  630. if err != nil {
  631. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同出库状态失败"}
  632. c.ServeJSON()
  633. return
  634. }
  635. }
  636. err = Update_VerifyContract_State(T_customer_id, "ADD")
  637. if err != nil {
  638. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新状态失败"}
  639. c.ServeJSON()
  640. return
  641. }
  642. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "添加", var_)
  643. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  644. c.ServeJSON()
  645. return
  646. }
  647. // 验证合同呢修改
  648. func (c *ContractController) VerifyContract_Edit() {
  649. Id, _ := c.GetInt("Id")
  650. T_number := c.GetString("T_number")
  651. T_money, _ := c.GetFloat("T_money")
  652. T_discount, _ := c.GetFloat("T_discount")
  653. T_date := c.GetString("T_date")
  654. T_product := c.GetString("T_product")
  655. T_remark := c.GetString("T_remark")
  656. T_pdf := c.GetString("T_pdf")
  657. T_project := c.GetString("T_project")
  658. T_recoveries := c.GetString("T_recoveries")
  659. T_invoice := c.GetString("T_invoice")
  660. T_start_date := c.GetString("T_start_date")
  661. T_end_date := c.GetString("T_end_date")
  662. T_submit := c.GetString("T_submit")
  663. T_stamping_date := c.GetString("T_stamping_date")
  664. T_payment_method := c.GetString("T_payment_method")
  665. o := orm.NewOrm()
  666. o.Begin()
  667. ContractDao := Contract.NewContract(o)
  668. ContractProductDao := Contract.NewContractProduct(o)
  669. contract, err := ContractDao.Read_Contract_ById(Id)
  670. if err != nil || contract.T_type == 1 {
  671. o.Rollback()
  672. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
  673. c.ServeJSON()
  674. return
  675. }
  676. oldT_number := contract.T_number
  677. if T_number != oldT_number {
  678. _, err1 := ContractDao.Read_Contract_ByT_number(T_number)
  679. if err1 == nil {
  680. o.Rollback()
  681. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("合同编号%s已存在!", T_number)}
  682. c.ServeJSON()
  683. return
  684. }
  685. contract.T_number = T_number
  686. }
  687. if T_money > 0 {
  688. contract.T_money = float32(T_money)
  689. }
  690. if T_discount > 0 {
  691. contract.T_discount = float32(T_discount)
  692. }
  693. if len(T_date) > 0 {
  694. contract.T_date = T_date
  695. }
  696. if len(T_remark) > 0 {
  697. contract.T_remark = T_remark
  698. }
  699. if len(T_pdf) > 0 {
  700. contract.T_pdf = T_pdf
  701. }
  702. if len(T_project) > 0 {
  703. contract.T_project = T_project
  704. }
  705. if len(T_recoveries) > 0 {
  706. contract.T_recoveries = T_recoveries
  707. _, contract.T_recoveries_money = Contract.ContractToContractFinance_R(T_recoveries)
  708. }
  709. if len(T_invoice) > 0 {
  710. contract.T_invoice = T_invoice
  711. _, contract.T_invoice_money = Contract.ContractToContractFinance_R(T_invoice)
  712. }
  713. if len(T_start_date) > 0 {
  714. contract.T_start_date = T_start_date
  715. }
  716. if len(T_end_date) > 0 {
  717. contract.T_end_date = T_end_date
  718. }
  719. if len(T_submit) > 0 {
  720. contract.T_submit = T_submit
  721. }
  722. if len(T_stamping_date) > 0 {
  723. contract.T_stamping_date = T_stamping_date
  724. }
  725. if len(T_payment_method) > 0 {
  726. contract.T_payment_method = T_payment_method
  727. }
  728. err = ContractDao.Update_Contract(contract, "T_number", "T_money", "T_discount", "T_date", "T_remark", "T_pdf", "T_State", "T_project",
  729. "T_recoveries", "T_recoveries_money", "T_invoice", "T_invoice_money", "T_start_date", "T_end_date", "T_submit", "T_stamping_date", "T_payment_method")
  730. if err != nil {
  731. o.Rollback()
  732. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  733. c.ServeJSON()
  734. return
  735. }
  736. if len(T_product) == 0 {
  737. o.Rollback()
  738. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  739. c.ServeJSON()
  740. return
  741. }
  742. productList := lib.SplitString(T_product, "|")
  743. cpl, _ := ContractProductDao.Read_ContractProductList_ByT_number(oldT_number)
  744. needToDeleteList, allList := Check_NeedToDelete_Product_List(productList, cpl)
  745. // 检查编辑时需要修改的产品
  746. for k, v := range needToDeleteList {
  747. id, _ := strconv.Atoi(strings.Split(k, "-")[0])
  748. productId, _ := strconv.Atoi(strings.Split(k, "-")[1])
  749. if v > 0 {
  750. o.Rollback()
  751. product, _ := Basic.Read_Product_ById(productId)
  752. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s已(部分)出库,禁止删除!", product.T_name)}
  753. c.ServeJSON()
  754. return
  755. }
  756. cp := Contract.ContractProduct{Id: id}
  757. err = ContractProductDao.Delete_ContractProduct(cp)
  758. if err != nil {
  759. o.Rollback()
  760. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  761. c.ServeJSON()
  762. return
  763. }
  764. }
  765. for _, v := range productList {
  766. product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
  767. num, _ := strconv.Atoi(strings.Split(v, ",")[1])
  768. price := lib.StringToFloat64TwoDecimal(strings.Split(v, ",")[2])
  769. oldContractProduct, ok := allList[product_id]
  770. if !ok {
  771. contractProduct := Contract.ContractProduct{
  772. T_contract_number: T_number,
  773. T_product_id: product_id,
  774. T_product_total: num,
  775. T_product_out: 0, // 已出库数量
  776. T_price: float32(price), // 已出库数量
  777. T_State: 1, // 1-未出库 2-已部分出库 3-已全部出库
  778. }
  779. _, err = ContractProductDao.Add_ContractProduct(contractProduct)
  780. if err != nil {
  781. o.Rollback()
  782. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  783. c.ServeJSON()
  784. return
  785. } else {
  786. continue
  787. }
  788. }
  789. // 修改产品总数量
  790. contractProductId, _ := strconv.Atoi(strings.Split(oldContractProduct, "-")[0])
  791. oldNum, _ := strconv.Atoi(strings.Split(oldContractProduct, "-")[1])
  792. oldPrice := lib.StringToFloat64TwoDecimal(strings.Split(oldContractProduct, "-")[2])
  793. if oldNum == num && T_number == oldT_number && oldPrice == price {
  794. continue
  795. }
  796. cp := Contract.ContractProduct{
  797. Id: contractProductId,
  798. T_contract_number: T_number,
  799. T_product_total: num,
  800. T_price: float32(price), // 价格
  801. }
  802. err = ContractProductDao.Update_ContractProduct(cp, "T_product_total", "T_contract_number", "T_price")
  803. if err != nil {
  804. o.Rollback()
  805. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  806. c.ServeJSON()
  807. return
  808. }
  809. }
  810. o.Commit()
  811. err = Update_VerifyContract_State(contract.T_customer_id, "UPDATE")
  812. if err != nil {
  813. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新状态失败"}
  814. c.ServeJSON()
  815. return
  816. }
  817. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "修改", contract)
  818. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: contract.Id}
  819. c.ServeJSON()
  820. return
  821. }
  822. func (c *ContractController) VerifyContract_Del() {
  823. T_number := c.GetString("T_number")
  824. o := orm.NewOrm()
  825. o.Begin()
  826. ContractDao := Contract.NewContract(o)
  827. ContractProductDao := Contract.NewContractProduct(o)
  828. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  829. if err != nil {
  830. o.Rollback()
  831. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
  832. c.ServeJSON()
  833. return
  834. }
  835. err = ContractDao.Delete_Contract(contract)
  836. if err != nil {
  837. o.Rollback()
  838. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  839. c.ServeJSON()
  840. return
  841. }
  842. cpList := ContractProductDao.Read_ContractProduct_List(T_number)
  843. for _, v := range cpList {
  844. err = ContractProductDao.Delete_ContractProduct(v)
  845. if err != nil {
  846. o.Rollback()
  847. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  848. c.ServeJSON()
  849. return
  850. }
  851. }
  852. o.Commit()
  853. err = Update_VerifyContract_State(contract.T_customer_id, "DELETE")
  854. if err != nil {
  855. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改状态失败"}
  856. c.ServeJSON()
  857. return
  858. }
  859. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "删除", T_number)
  860. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  861. c.ServeJSON()
  862. return
  863. }
  864. // 导出验证合同
  865. func (c *ContractController) VerifyContract_Excel() {
  866. // 分页参数 初始化
  867. page, _ := c.GetInt("page")
  868. if page < 1 {
  869. page = 1
  870. }
  871. page_z, _ := c.GetInt("page_z")
  872. if page_z < 1 {
  873. page_z = conf.Page_size
  874. }
  875. // 查询
  876. T_name := c.GetString("T_name") // 客户名称
  877. T_state, _ := c.GetInt("T_state") // 签约状态
  878. T_start_date := c.GetString("T_start_date") // 盖章开始时间
  879. T_end_date := c.GetString("T_end_date") // 盖章结束时间
  880. data := ""
  881. if len(T_start_date) > 0 || len(T_end_date) > 0 {
  882. data = fmt.Sprintf("%s - %s", T_start_date, T_end_date)
  883. }
  884. userList, _ := NatsServer.Read_User_List_All()
  885. Account.Read_User_All_Map(userList)
  886. Contract.Read_VerifyContract_All_Map()
  887. ContractDao := Contract.NewContract(orm.NewOrm())
  888. R_List := ContractDao.Read_VerifyContract_Excel(T_state, T_name, T_start_date, T_end_date)
  889. f := excelize.NewFile() //设置单元格值
  890. // 这里设置表头ÒÒ
  891. Style1, _ := f.NewStyle(
  892. &excelize.Style{
  893. Font: &excelize.Font{Size: 20, Family: "宋体"},
  894. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  895. })
  896. Style2, _ := f.NewStyle(
  897. &excelize.Style{
  898. Font: &excelize.Font{Size: 15, Family: "宋体"},
  899. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  900. Border: []excelize.Border{
  901. {Type: "left", Color: "000000", Style: 1},
  902. {Type: "top", Color: "000000", Style: 1},
  903. {Type: "bottom", Color: "000000", Style: 1},
  904. {Type: "right", Color: "000000", Style: 1},
  905. },
  906. Fill: excelize.Fill{Type: "pattern", Color: []string{"D9D9D9"}, Pattern: 1},
  907. })
  908. Style3, _ := f.NewStyle(
  909. &excelize.Style{
  910. Font: &excelize.Font{Size: 15, Family: "宋体"},
  911. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  912. Border: []excelize.Border{
  913. {Type: "left", Color: "000000", Style: 1},
  914. {Type: "top", Color: "000000", Style: 1},
  915. {Type: "bottom", Color: "000000", Style: 1},
  916. {Type: "right", Color: "000000", Style: 1},
  917. },
  918. })
  919. f.MergeCell("Sheet1", "A1", "T1")
  920. f.SetCellValue("Sheet1", "A1", data+"合同明细")
  921. f.SetCellStyle("Sheet1", "A1", "T1", Style1)
  922. // 写入表头
  923. headers := []string{"合同编号", "客户名称", "销售人员", "签订时间", "起始时间", "终止时间", "盖章时间", "合同名称", "验证明细",
  924. "合同金额", "优惠金额", "付款方式/预付款", "回款时间", "以往回款", "回款金额", "未回款金额", "开票时间", "已开票金额", "未开票金额",
  925. "状态"}
  926. for col, header := range headers {
  927. cell := fmt.Sprintf("%c%d", rune('A'+col), 2)
  928. f.SetCellValue("Sheet1", cell, header)
  929. }
  930. // 这里设置表头
  931. f.SetCellStyle("Sheet1", "A2", "T2", Style2)
  932. f.SetRowHeight("Sheet1", 1, 50)
  933. f.SetRowHeight("Sheet1", 2, 50)
  934. colsWidth := []float64{15, 15, 12, 12, 12, 12, 12, 15, 50, 12, 12, 12, 12, 25, 12, 10, 25, 10, 10, 10}
  935. // 冻结1-2行
  936. f.SetPanes("Sheet1", &excelize.Panes{
  937. Freeze: true,
  938. Split: false,
  939. XSplit: 0,
  940. YSplit: 2,
  941. TopLeftCell: "A1",
  942. ActivePane: "topRight",
  943. })
  944. for col, width := range colsWidth {
  945. cell := fmt.Sprintf("%c", rune('A'+col))
  946. f.SetColWidth("Sheet1", cell, cell, width)
  947. }
  948. line := 2
  949. for _, v := range R_List {
  950. valueList := []interface{}{
  951. v.T_number, v.T_customer, v.T_submit_name, v.T_date, v.T_start_date, v.T_end_date, v.T_stamping_date, "冷链验证合同",
  952. v.T_Product, v.T_money, v.T_discount, Contract.Get_Contract_payment_method(v.T_payment_method), v.T_recoveries_time, v.T_recoveries, v.T_discount - v.T_no_recoveries_money,
  953. v.T_no_recoveries_money, v.T_invoice, v.T_discount - v.T_no_invoice_money, v.T_no_invoice_money, v.T_verify_state,
  954. }
  955. line++
  956. for col, value := range valueList {
  957. cell := fmt.Sprintf("%c", rune('A'+col))
  958. if cell == "N" || cell == "Q" {
  959. ll := value.([]Contract.ContractFinance_R)
  960. var sv []string
  961. for _, info := range ll {
  962. sv = append(sv, fmt.Sprintf("%s %s", info.T_date, FormatFloat(float64(info.T_money))))
  963. }
  964. f.SetCellValue("Sheet1", fmt.Sprintf("%s%d", cell, line), strings.Join(sv, "\n"))
  965. continue
  966. }
  967. if cell == "I" {
  968. ll := value.([]Contract.ContractProduct_R)
  969. var sv []string
  970. for _, info := range ll {
  971. sv = append(sv, fmt.Sprintf("%s %s*%d=%s", info.T_product_name, FormatFloat(float64(info.T_price)), info.T_product_total, FormatFloat(float64(info.T_price)*float64(info.T_product_total))))
  972. }
  973. f.SetCellValue("Sheet1", fmt.Sprintf("%s%d", cell, line), strings.Join(sv, "\n"))
  974. continue
  975. }
  976. f.SetCellValue("Sheet1", fmt.Sprintf("%s%d", cell, line), value)
  977. }
  978. }
  979. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("T%d", line), Style3)
  980. timeStr := time.Now().Format("20060102150405")
  981. fileName := fmt.Sprintf("%s合同明细%v.xlsx", data, timeStr)
  982. filePath := "ofile/" + fileName
  983. // 保存文件
  984. if err := f.SaveAs(filePath); err != nil {
  985. logs.Error("保存文件失败:", err)
  986. }
  987. defer func() {
  988. os.Remove(filePath)
  989. }()
  990. c.Ctx.Output.Header("Content-Type", "application/vnd.ms-excel;charset=utf8")
  991. c.Ctx.Output.Header("Content-Disposition", "attachment; filename="+url.PathEscape(fileName))
  992. c.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
  993. c.Ctx.Output.Download(filePath, fileName)
  994. }
  995. func Update_VerifyContract_State(T_customer_id, flag string) error {
  996. o := orm.NewOrm()
  997. ContractDao := Contract.NewContract(o)
  998. VerifyContractDao := Contract.NewVerifyContract(o)
  999. vcn := ContractDao.Read_VerifyContract_Newest(T_customer_id)
  1000. vc, err := VerifyContractDao.Read_VerifyContract_ByT_customer_id(T_customer_id)
  1001. if err != nil {
  1002. return err
  1003. }
  1004. if vcn.Id == 0 {
  1005. vc.T_sign_times = 0
  1006. vc.T_start_date = ""
  1007. vc.T_end_date = ""
  1008. vc.T_State = 1
  1009. err = VerifyContractDao.Update_VerifyContract(vc, "T_start_date", "T_end_date", "T_sign_times", "T_State")
  1010. return err
  1011. }
  1012. if flag == "ADD" {
  1013. vc.T_sign_times += 1
  1014. } else if flag == "DELETE" {
  1015. vc.T_sign_times -= 1
  1016. }
  1017. if len(vc.T_start_date) == 0 {
  1018. vc.T_start_date = vcn.T_start_date
  1019. }
  1020. if vc.T_end_date != vcn.T_end_date {
  1021. vc.T_end_date = vcn.T_end_date
  1022. }
  1023. nowTime := time.Now()
  1024. now := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, time.Local)
  1025. end, _ := lib.DateStrToTime(vcn.T_end_date)
  1026. now2Month := now.AddDate(0, 2, 0)
  1027. // 1-未签约 2-已作废 3-已签约 4-即将到期
  1028. state := 1
  1029. // 结束时间<今天 已过期
  1030. if end.Before(now) {
  1031. state = 2
  1032. } else {
  1033. // 结束时间<今天+2月 即将到期 && 结束时间>今天
  1034. if end.Before(now2Month) || end.Equal(now) {
  1035. state = 4
  1036. NatsServer.AddNews(vcn.T_approver, fmt.Sprintf("【合同即将过期提醒】%s的验证合同将于%s过期!", vc.T_customer, vc.T_end_date), conf.VerifyContractUrl)
  1037. }
  1038. // 结束时间>今天+2月 已签约
  1039. if end.After(now2Month) || end.Equal(now2Month) {
  1040. state = 3
  1041. }
  1042. }
  1043. vc.T_State = state
  1044. err = VerifyContractDao.Update_VerifyContract(vc, "T_start_date", "T_end_date", "T_sign_times", "T_State")
  1045. return err
  1046. }
  1047. // 验证合同-添加客户
  1048. func (c *ContractController) VerifyContract_Add_Customer() {
  1049. T_customer := c.GetString("T_customer")
  1050. o := orm.NewOrm()
  1051. VerifyContractDao := Contract.NewVerifyContract(o)
  1052. T_number := ""
  1053. for true {
  1054. rand_x := 1
  1055. T_number = fmt.Sprintf("YZKH-%s", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x)))
  1056. _, err := VerifyContractDao.Read_VerifyContract_ByT_customer_id(T_number)
  1057. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  1058. break
  1059. }
  1060. rand_x += 1
  1061. }
  1062. var_ := Contract.VerifyContract{
  1063. T_customer: T_customer,
  1064. T_customer_id: T_number,
  1065. T_State: 1,
  1066. }
  1067. _, err := VerifyContractDao.Add_VerifyContract(var_)
  1068. if err != nil {
  1069. o.Rollback()
  1070. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  1071. c.ServeJSON()
  1072. return
  1073. }
  1074. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "添加客户", var_)
  1075. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1076. c.ServeJSON()
  1077. return
  1078. }
  1079. // 验证合同-编辑客户
  1080. func (c *ContractController) VerifyContract_Update_Customer() {
  1081. T_customer_id := c.GetString("T_customer_id")
  1082. T_customer := c.GetString("T_customer")
  1083. o := orm.NewOrm()
  1084. VerifyContractDao := Contract.NewVerifyContract(o)
  1085. vc, err := VerifyContractDao.Read_VerifyContract_ByT_customer_id(T_customer_id)
  1086. vc.T_customer = T_customer
  1087. err = VerifyContractDao.Update_VerifyContract(vc)
  1088. if err != nil {
  1089. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  1090. c.ServeJSON()
  1091. return
  1092. }
  1093. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "编辑客户", vc)
  1094. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_customer_id}
  1095. c.ServeJSON()
  1096. return
  1097. }
  1098. // 验证合同-客户列表
  1099. func (c *ContractController) VerifyContract_Customer_List() {
  1100. // 分页参数 初始化
  1101. page, _ := c.GetInt("page")
  1102. if page < 1 {
  1103. page = 1
  1104. }
  1105. page_z, _ := c.GetInt("page_z")
  1106. if page_z < 1 {
  1107. page_z = conf.Page_size
  1108. }
  1109. // 查询
  1110. T_name := c.GetString("T_name")
  1111. T_state, _ := c.GetInt("T_state")
  1112. VerifyContractDao := Contract.NewVerifyContract(orm.NewOrm())
  1113. R_List, R_cnt := VerifyContractDao.Read_VerifyContract_List(T_name, T_state, page, page_z)
  1114. var r_jsons lib.R_JSONS
  1115. r_jsons.Num = R_cnt
  1116. r_jsons.Data = R_List
  1117. r_jsons.Page = page
  1118. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1119. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1120. c.ServeJSON()
  1121. return
  1122. }
  1123. // 合同回款明细列表
  1124. func (c *ContractController) Contract_List_RecoveriesMoney() {
  1125. // 分页参数 初始化
  1126. page, _ := c.GetInt("page")
  1127. if page < 1 {
  1128. page = 1
  1129. }
  1130. page_z, _ := c.GetInt("page_z")
  1131. if page_z < 1 {
  1132. page_z = conf.Page_size
  1133. }
  1134. // 查询
  1135. T_submit := c.GetString("T_submit")
  1136. T_name := c.GetString("T_name")
  1137. T_start_date := c.GetString("T_start_date") // 回款开始日期
  1138. T_end_date := c.GetString("T_end_date") // 回款结束日期
  1139. T_recoveries_state, _ := c.GetInt("T_recoveries_state") // 回款状态 1 未回款 2 部分回款 3 全部回款
  1140. T_invoice_state, _ := c.GetInt("T_invoice_state") // 开票状态 1 未开票 2 部分开票 3 全部开票
  1141. T_percentage_state, _ := c.GetInt("T_percentage_state") // 提成状态 1-未打款 2-已打款
  1142. T_mine, _ := c.GetInt("T_mine") // 我的 用于合同提成显示
  1143. userList, _ := NatsServer.Read_User_List_All()
  1144. Account.Read_User_All_Map(userList)
  1145. Contract.Read_VerifyContract_All_Map()
  1146. ContractDao := Contract.NewContract(orm.NewOrm())
  1147. R_List, R_cnt := ContractDao.Read_Contract_List_For_RecoveriesAndInvoice(c.User.T_uuid, T_submit, T_mine, T_name, T_start_date, T_end_date, T_recoveries_state, T_invoice_state, T_percentage_state, page, page_z)
  1148. var r_jsons lib.R_JSONS
  1149. r_jsons.Num = R_cnt
  1150. r_jsons.Data = R_List
  1151. r_jsons.Page = page
  1152. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1153. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1154. c.ServeJSON()
  1155. return
  1156. }
  1157. // 导出合同回款明细列表
  1158. func (c *ContractController) Contract_List_RecoveriesMoney_Export() {
  1159. // 查询
  1160. T_submit := c.GetString("T_submit")
  1161. T_name := c.GetString("T_name")
  1162. T_start_date := c.GetString("T_start_date") // 回款开始日期
  1163. T_end_date := c.GetString("T_end_date") // 回款结束日期
  1164. T_recoveries_state, _ := c.GetInt("T_recoveries_state") // 回款状态 1 未回款 2 部分回款 3 全部回款
  1165. T_invoice_state, _ := c.GetInt("T_invoice_state") // 开票状态 1 未开票 2 部分开票 3 全部开票
  1166. T_percentage_state, _ := c.GetInt("T_percentage_state") // 提成状态 1-未打款 2-已打款
  1167. T_mine, _ := c.GetInt("T_mine") // 我的 用于合同提成显示
  1168. userList, _ := NatsServer.Read_User_List_All()
  1169. Account.Read_User_All_Map(userList)
  1170. Contract.Read_VerifyContract_All_Map()
  1171. ContractDao := Contract.NewContract(orm.NewOrm())
  1172. R_List, _ := ContractDao.Read_Contract_List_For_RecoveriesAndInvoice(c.User.T_uuid, T_submit, T_mine, T_name, T_start_date, T_end_date, T_recoveries_state, T_invoice_state, T_percentage_state, 0, 9999)
  1173. title := "销售提成明细表"
  1174. if T_mine == 1 {
  1175. title = fmt.Sprintf("%s-销售提成明细表", c.User.T_name)
  1176. }
  1177. filename := fmt.Sprintf("%s(%s)", title, lib.GetRandstring(6, "0123456789", 0))
  1178. f := excelize.NewFile() // 设置单元格的值
  1179. Style1, _ := f.NewStyle(
  1180. &excelize.Style{
  1181. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1182. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1183. })
  1184. Style2, _ := f.NewStyle(
  1185. &excelize.Style{
  1186. Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
  1187. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1188. Border: []excelize.Border{
  1189. {Type: "left", Color: "000000", Style: 1},
  1190. {Type: "top", Color: "000000", Style: 1},
  1191. {Type: "bottom", Color: "000000", Style: 1},
  1192. {Type: "right", Color: "000000", Style: 1},
  1193. },
  1194. })
  1195. Style3, _ := f.NewStyle(
  1196. &excelize.Style{
  1197. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1198. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1199. Border: []excelize.Border{
  1200. {Type: "left", Color: "000000", Style: 1},
  1201. {Type: "top", Color: "000000", Style: 1},
  1202. {Type: "bottom", Color: "000000", Style: 1},
  1203. {Type: "right", Color: "000000", Style: 1},
  1204. },
  1205. })
  1206. f.MergeCell("Sheet1", "A1", "J1")
  1207. f.SetRowStyle("Sheet1", 1, 1, Style1)
  1208. f.SetCellValue("Sheet1", "A1", title)
  1209. f.SetRowHeight("Sheet1", 1, 30)
  1210. // 这里设置表头
  1211. f.SetCellStyle("Sheet1", "A2", "J2", Style2)
  1212. f.SetRowHeight("Sheet1", 2, 20)
  1213. f.SetCellValue("Sheet1", "A2", "公司名称")
  1214. f.SetCellValue("Sheet1", "B2", "合同编号")
  1215. f.SetCellValue("Sheet1", "C2", "签约时间")
  1216. f.SetCellValue("Sheet1", "D2", "合同金额")
  1217. f.SetCellValue("Sheet1", "E2", "回款金额")
  1218. f.SetCellValue("Sheet1", "F2", "回款比例")
  1219. f.SetCellValue("Sheet1", "G2", "销售类型")
  1220. f.SetCellValue("Sheet1", "H2", "销售提成5%")
  1221. f.SetCellValue("Sheet1", "I2", "提成金额")
  1222. f.SetCellValue("Sheet1", "J2", "提成状态")
  1223. // 设置列宽
  1224. f.SetColWidth("Sheet1", "A", "A", 25)
  1225. f.SetColWidth("Sheet1", "B", "I", 15)
  1226. line := 2
  1227. for _, contract := range R_List {
  1228. line += 1
  1229. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-2)
  1230. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), contract.T_customer)
  1231. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), contract.T_number)
  1232. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), contract.T_date)
  1233. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), contract.T_discount)
  1234. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), contract.T_recoveries_money)
  1235. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), fmt.Sprintf("%.02f%%", contract.T_recoveries_money/contract.T_discount*100))
  1236. T_type_str := "验证合同"
  1237. if contract.T_type == 1 {
  1238. T_type_str = "销售合同"
  1239. }
  1240. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), T_type_str)
  1241. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), fmt.Sprintf("%.02f", contract.T_discount*0.05))
  1242. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), fmt.Sprintf("%.02f", contract.T_discount*0.05))
  1243. T_percentage_str := "未打款"
  1244. if contract.T_percentage_state == 2 {
  1245. T_percentage_str = "已打款"
  1246. }
  1247. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), T_percentage_str)
  1248. }
  1249. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("J%d", line), Style3)
  1250. // 保存文件
  1251. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1252. fmt.Println(err)
  1253. }
  1254. var url string
  1255. //// 上传 OSS
  1256. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1257. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1258. if !is {
  1259. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1260. c.ServeJSON()
  1261. return
  1262. }
  1263. //删除目录
  1264. err := os.Remove("ofile/" + filename + ".xlsx")
  1265. if err != nil {
  1266. logs.Error(lib.FuncName(), err)
  1267. }
  1268. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1269. c.ServeJSON()
  1270. return
  1271. }
  1272. func (c *ContractController) Contract_Stat() {
  1273. T_type, _ := c.GetInt("T_type")
  1274. T_year, _ := c.GetInt("T_year")
  1275. type R_JSONS struct {
  1276. ContractMoneyByYear interface{}
  1277. TotalMoneyByYear float32
  1278. RecoveriesMoneyByYear float32
  1279. ContractNumByYear interface{}
  1280. TotalMumByYear int64
  1281. ContractMoneyByMonth interface{}
  1282. TotalMoneyByMonth float32
  1283. RecoveriesMoneyByMonth float32
  1284. ContractNumByMonth interface{}
  1285. TotalMumByMonth int64
  1286. TotalMoneyByThisYear float32 // 今年合同总金额
  1287. RecoveriesMoneyByThisYear float32 // 今年回款总金额
  1288. TotalMumByThisYear int64 // 今年合同总数
  1289. TotalMoneyByLastYear float32 // 去年合同总金额
  1290. RecoveriesMoneyByLastYear float32 // 去年回款总金额
  1291. TotalMumByLastYear int64 // 去年合同总数
  1292. }
  1293. var r_jsons R_JSONS
  1294. thisYear := time.Now().Year()
  1295. if T_year == 0 {
  1296. T_year = thisYear
  1297. }
  1298. ContractDao := Contract.NewContract(orm.NewOrm())
  1299. monryStat, moneyTotal, moneyRecoveries := ContractDao.Stat_ContractMoney_ByYear(T_type)
  1300. numStat, numTotal := ContractDao.Stat_ContractNum_ByYear(T_type)
  1301. monryMonthStat, moneyMonthTotal, moneyMonthRecoveries := ContractDao.Stat_ContractMoney_ByMonth(T_type, T_year)
  1302. numMonthStat, numMonthTotal := ContractDao.Stat_ContractNum_ByMonth(T_type, T_year)
  1303. // 去年
  1304. _, r_jsons.TotalMoneyByLastYear, r_jsons.RecoveriesMoneyByLastYear = ContractDao.Stat_ContractMoney_ByMonth(T_type, T_year-1)
  1305. _, r_jsons.TotalMumByLastYear = ContractDao.Stat_ContractNum_ByMonth(T_type, T_year-1)
  1306. r_jsons.ContractMoneyByYear = monryStat
  1307. r_jsons.TotalMoneyByYear = moneyTotal
  1308. r_jsons.RecoveriesMoneyByYear = moneyRecoveries
  1309. r_jsons.ContractNumByYear = numStat
  1310. r_jsons.TotalMumByYear = numTotal
  1311. r_jsons.ContractMoneyByMonth = monryMonthStat
  1312. r_jsons.TotalMoneyByMonth = moneyMonthTotal
  1313. r_jsons.RecoveriesMoneyByMonth = moneyMonthRecoveries
  1314. r_jsons.ContractNumByMonth = numMonthStat
  1315. r_jsons.TotalMumByMonth = numMonthTotal
  1316. r_jsons.TotalMoneyByThisYear = moneyMonthTotal
  1317. r_jsons.RecoveriesMoneyByThisYear = moneyMonthRecoveries
  1318. r_jsons.TotalMumByThisYear = numMonthTotal
  1319. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1320. c.ServeJSON()
  1321. return
  1322. }
  1323. func Cron_VerifyContract() {
  1324. //创建一个定时任务对象
  1325. c := cron.New(cron.WithSeconds())
  1326. //给对象增加定时任务
  1327. //c.AddFunc("0 */1 * * * ?", Cron_VerifyContract_ChangeState)
  1328. c.AddFunc("@daily", Cron_VerifyContract_ChangeState)
  1329. //启动定时任务
  1330. c.Start()
  1331. defer c.Stop()
  1332. //查询语句,阻塞,让main函数不退出,保持程序运行
  1333. select {}
  1334. }
  1335. // 修改合同状态
  1336. func Cron_VerifyContract_ChangeState() {
  1337. T_date := time.Now().Format("2006-01-02")
  1338. logs.Info("开始处理" + T_date + "验证合同状态")
  1339. VerifyContractDao := Contract.NewVerifyContract(orm.NewOrm())
  1340. R_List, _ := VerifyContractDao.Read_VerifyContract_List("", 0, 0, 999)
  1341. for _, v := range R_List {
  1342. err := Update_VerifyContract_State(v.T_customer_id, "UPDATE")
  1343. if err != nil {
  1344. logs.Error("修改合同状态失败")
  1345. }
  1346. }
  1347. }
  1348. // 修改回款金额及开票金额
  1349. func UpdateVerifyContract_RecoveriesInvoice_Money() {
  1350. ContractDao := Contract.NewContract(orm.NewOrm())
  1351. R_List, _ := ContractDao.Read_VerifyContract_List("", "", 0, 9999)
  1352. for _, verify := range R_List {
  1353. R := Contract.Contract{Id: verify.Id}
  1354. _, R.T_recoveries_money = Contract.ContractToContractFinance_R(verify.T_recoveries)
  1355. _, R.T_invoice_money = Contract.ContractToContractFinance_R(verify.T_invoice)
  1356. ContractDao.Update_Contract(R, "T_recoveries_money", "T_invoice_money")
  1357. }
  1358. }
  1359. // 修改回款时间
  1360. func UpdateVerifyContract_RecoveriesInvoice_Time() {
  1361. ContractDao := Contract.NewContract(orm.NewOrm())
  1362. R_List, _ := ContractDao.Read_VerifyContract_List("", "", 0, 9999)
  1363. for _, verify := range R_List {
  1364. R := Contract.Contract{Id: verify.Id}
  1365. if verify.T_discount == verify.T_recoveries_money {
  1366. recoveriesList, _ := Contract.ContractToContractFinance_R(verify.T_recoveries)
  1367. if len(recoveriesList) > 0 {
  1368. R.T_recoveries_time = recoveriesList[len(recoveriesList)-1].T_date
  1369. ContractDao.Update_Contract(R, "T_recoveries_time")
  1370. }
  1371. }
  1372. }
  1373. }
  1374. // 合同提成打款
  1375. func (c *ContractController) Contract_Percentage_Remit() {
  1376. T_number := c.GetString("T_number")
  1377. o := orm.NewOrm()
  1378. ContractDao := Contract.NewContract(o)
  1379. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  1380. if err != nil {
  1381. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
  1382. c.ServeJSON()
  1383. return
  1384. }
  1385. if contract.T_recoveries_money != contract.T_discount {
  1386. c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同未回款,不可打款!"}
  1387. c.ServeJSON()
  1388. return
  1389. }
  1390. contract.T_percentage_state = 2 // 已打款
  1391. err = ContractDao.Update_Contract(contract, "T_percentage_state")
  1392. if err != nil {
  1393. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  1394. c.ServeJSON()
  1395. return
  1396. }
  1397. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "打款", contract)
  1398. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: contract.Id}
  1399. c.ServeJSON()
  1400. return
  1401. }
  1402. func (c *ContractController) User_List() {
  1403. var r_jsons lib.R_JSONS
  1404. // 分页参数 初始化
  1405. page, _ := c.GetInt("page")
  1406. if page < 1 {
  1407. page = 1
  1408. }
  1409. page_z, _ := c.GetInt("page_z")
  1410. if page_z < 1 {
  1411. page_z = conf.Page_size
  1412. }
  1413. T_name := c.GetString("T_name")
  1414. ContractDao := Contract.NewContract(orm.NewOrm())
  1415. uuidList := ContractDao.Read_T_submit_List()
  1416. if len(uuidList) == 0 {
  1417. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1418. c.ServeJSON()
  1419. return
  1420. }
  1421. R_List, R_cnt, err := NatsServer.Read_User_List_T_uuid(T_name, uuidList, page, page_z)
  1422. if err != nil {
  1423. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败"}
  1424. c.ServeJSON()
  1425. return
  1426. }
  1427. var U_List []userlibs.User
  1428. for _, user := range R_List {
  1429. U_List = append(U_List, user)
  1430. }
  1431. r_jsons.Num = R_cnt
  1432. r_jsons.Data = U_List
  1433. r_jsons.Page = page
  1434. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1435. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1436. c.ServeJSON()
  1437. return
  1438. }
  1439. func FormatFloat(f float64) string {
  1440. // 先判断是否为整数
  1441. if f == math.Trunc(f) {
  1442. return strconv.FormatFloat(f, 'f', 0, 64)
  1443. }
  1444. // 否则保留2位小数
  1445. return strconv.FormatFloat(f, 'f', 2, 64)
  1446. }