Contract.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. package Contract
  2. import (
  3. "ERP_storage/logs"
  4. "ERP_storage/models/Account"
  5. "fmt"
  6. "git.baozhida.cn/ERP_libs/lib"
  7. orm2 "github.com/beego/beego/v2/client/orm"
  8. "strconv"
  9. "strings"
  10. "sync"
  11. "time"
  12. _ "github.com/astaxie/beego/cache/redis"
  13. "github.com/beego/beego/v2/adapter/orm"
  14. _ "github.com/go-sql-driver/mysql"
  15. )
  16. // 合同
  17. type Contract struct {
  18. Id int `orm:"column(ID);size(11);auto;pk"`
  19. T_number string `orm:"size(256);null"` // 合同编号
  20. T_customer string `orm:"size(256);null"` // 客户名称
  21. T_customer_id string `orm:"size(256);null"` // 客户id
  22. T_money float32 `orm:"digits(12);decimals(2)"` // 合同金额
  23. T_discount float32 `orm:"digits(12);decimals(2)"` // 合同优惠价
  24. T_project string `orm:"size(256);null"` // 项目
  25. T_type int `orm:"size(2);default(1)"` // 合同类型 1-销售合同 2-验证合同
  26. T_date string `orm:"size(256);null"` // 签订时间
  27. T_out int `orm:"size(2);default(1)"` // 出库状态 1-未出库 2-部分出库 3-已出库
  28. T_State int `orm:"size(2);default(1)"` // 0 删除(伪删除) 1-已通过 2-未通过 3-待审核 审核状态
  29. T_pdf string `orm:"size(256);null"` // 附件
  30. T_remark string `orm:"type(text);null"` // 备注
  31. T_recoveries string `orm:"type(text);null"` // 回款信息 回款时间,回款金额|回款时间,回款金额|
  32. T_recoveries_money float32 `orm:"digits(12);decimals(2)"` // 回款总金额
  33. T_invoice string `orm:"type(text);null"` // 开票信息 开票时间,开票金额|开票时间,开票金额|
  34. T_invoice_money float32 `orm:"digits(12);decimals(2)"` // 开票总金额
  35. T_start_date string `orm:"size(256);null"` // 起始时间
  36. T_end_date string `orm:"size(256);null"` // 终止时间
  37. T_submit string `orm:"size(256);null"` // 合同提交人员
  38. T_approver string `orm:"size(256);null"` // 审批人
  39. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  40. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  41. }
  42. func (t *Contract) TableName() string {
  43. return "contract" // 数据库名称 // ************** 替换 FormulaList **************
  44. }
  45. type ContractDaoImpl struct {
  46. orm orm.Ormer
  47. }
  48. func NewContract(orm orm.Ormer) *ContractDaoImpl {
  49. return &ContractDaoImpl{orm: orm}
  50. }
  51. var Contract_list *sync.Map
  52. func init() {
  53. //注册模型
  54. orm.RegisterModel(new(Contract))
  55. Contract_list = new(sync.Map)
  56. }
  57. type Contract_R struct {
  58. Id int
  59. T_number string // 合同编号
  60. T_customer string // 客户名称
  61. T_money float32 // 合同金额
  62. T_discount float32 // 合同优惠价
  63. T_type int // 合同类型 1-销售合同 2-验证合同
  64. T_date string // 签订时间
  65. T_out int // 出库状态 1-未出库 2-已部分出库 3-已全部出库
  66. T_submit string // 合同提交人员uuid
  67. T_submit_name string // 合同提交人员名称
  68. T_approver string // 合同审批人员uuid
  69. T_approver_name string // 合同审批人员名称
  70. T_pdf string // 附件
  71. T_State int // 1-已通过 2-未通过 3-待审核 审核状态
  72. T_recoveries_money float32 // 回款总金额
  73. T_invoice_money float32 // 开票总金额
  74. }
  75. type Contract_Detail struct {
  76. Id int
  77. T_number string
  78. T_customer string // 客户名称
  79. T_money float32 // 合同金额
  80. T_discount float32 // 合同优惠价
  81. T_project string // 项目
  82. T_type int // 合同类型 1-销售合同 2-验证合同
  83. T_date string // 签订时间
  84. T_submit string // 合同提交人员uuid
  85. T_submit_name string // 合同提交人员名称
  86. T_approver string // 审批人uuid
  87. T_approver_name string // 审批人名称
  88. T_out int // 出库状态 1-未出库 2-已部分出库 3-已全部出库
  89. T_State int // 1-已通过 2-未通过 3-待审核 审核状态
  90. T_remark string // 备注
  91. T_pdf string // 附件
  92. T_start_date string // 起始时间
  93. T_end_date string // 终止时间
  94. T_Product []ContractProduct_R
  95. T_recoveries []ContractFinance_R // 回款信息 回款时间,回款金额|回款时间,回款金额|
  96. T_no_recoveries_money float32 // 未回款金额
  97. T_invoice []ContractFinance_R // 开票信息 开票时间,开票金额|开票时间,开票金额|
  98. T_no_invoice_money float32 // 未开票金额
  99. }
  100. type ContractFinance_R struct {
  101. T_date string // 时间
  102. T_money float32 // 金额)
  103. }
  104. type Contract_Verify struct {
  105. Id int
  106. T_number string
  107. T_money float32 // 合同金额
  108. T_discount float32 // 合同优惠价
  109. T_date string // 签订时间
  110. T_start_date string // 起始时间
  111. T_end_date string // 终止时间
  112. T_verify_state int // 1-未签约 2-已作废 3-已签约 4-即将到期
  113. T_recoveries string // 回款信息
  114. T_invoice string // 开票信息
  115. }
  116. type ContractMoney_Stat struct {
  117. T_date int
  118. T_discount float32
  119. T_recoveries_money float32 // 回款信息
  120. }
  121. type ContractNum_Stat struct {
  122. T_date int
  123. T_num int
  124. }
  125. func ContractToContractFinance_R(T_detail string) (r []ContractFinance_R, money float32) {
  126. if len(T_detail) == 0 {
  127. return
  128. }
  129. detailList := strings.Split(strings.Trim(T_detail, "|"), "|")
  130. for _, detail := range detailList {
  131. T_date := strings.Split(detail, ",")[0]
  132. T_money := strings.Split(detail, ",")[1]
  133. temp := float32(lib.StringToFloat64TwoDecimal(T_money))
  134. money += temp
  135. r = append(r, ContractFinance_R{
  136. T_date: T_date,
  137. T_money: temp,
  138. })
  139. }
  140. return r, money
  141. }
  142. func ContractToContract_R(t Contract) (r Contract_R) {
  143. // 1-未出库 2-已部分出库 3-已全部出库
  144. r.T_out = t.T_out
  145. r.Id = t.Id
  146. r.T_number = t.T_number
  147. r.T_customer = t.T_customer
  148. r.T_money = t.T_money
  149. r.T_discount = t.T_discount
  150. r.T_type = t.T_type
  151. r.T_date = t.T_date
  152. r.T_State = t.T_State
  153. r.T_pdf = t.T_pdf
  154. r.T_submit = t.T_submit
  155. r.T_submit_name = Account.Read_User_T_name_Get(t.T_submit)
  156. r.T_approver = t.T_approver
  157. r.T_approver_name = Account.Read_User_T_name_Get(t.T_approver)
  158. r.T_recoveries_money = t.T_recoveries_money
  159. r.T_invoice_money = t.T_invoice_money
  160. return r
  161. }
  162. func ContractToContract_Detail(t Contract, productList []ContractProduct_R) (r Contract_Detail) {
  163. // 1-未出库 2-已部分出库 3-已全部出库
  164. r.T_out = t.T_out
  165. r.Id = t.Id
  166. r.T_number = t.T_number
  167. r.T_customer = t.T_customer
  168. r.T_money = t.T_money
  169. r.T_discount = t.T_discount
  170. r.T_project = t.T_project
  171. r.T_type = t.T_type
  172. r.T_date = t.T_date
  173. r.T_State = t.T_State
  174. r.T_remark = t.T_remark
  175. r.T_pdf = t.T_pdf
  176. r.T_submit = t.T_submit
  177. r.T_submit_name = Account.Read_User_T_name_Get(t.T_submit)
  178. r.T_approver = t.T_approver
  179. r.T_approver_name = Account.Read_User_T_name_Get(t.T_approver)
  180. r.T_Product = productList
  181. r.T_start_date = t.T_start_date
  182. r.T_end_date = t.T_end_date
  183. recoveriesList, recoveries := ContractToContractFinance_R(t.T_recoveries)
  184. r.T_recoveries = recoveriesList
  185. r.T_no_recoveries_money = t.T_money - recoveries
  186. invoiceList, invoice := ContractToContractFinance_R(t.T_invoice)
  187. r.T_invoice = invoiceList
  188. r.T_no_invoice_money = t.T_money - invoice
  189. return r
  190. }
  191. func ContractToContract_Verify(t Contract) (r Contract_Verify) {
  192. r.Id = t.Id
  193. r.T_number = t.T_number
  194. r.T_money = t.T_money
  195. r.T_discount = t.T_discount
  196. r.T_date = t.T_date
  197. r.T_start_date = t.T_start_date
  198. r.T_end_date = t.T_end_date
  199. nowTime := time.Now()
  200. now := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, time.Local)
  201. end, _ := lib.DateStrToTime(t.T_end_date)
  202. now2Month := now.AddDate(0, 2, 0)
  203. // 1-未签约 2-已作废 3-已签约 4-即将到期
  204. state := 0
  205. if len(t.T_end_date) == 0 {
  206. state = 1
  207. }
  208. // 结束时间<今天 已过期
  209. if end.Before(now) {
  210. state = 2
  211. } else {
  212. // 结束时间<今天+2月 即将到期 && 结束时间>今天
  213. if end.Before(now2Month) || end.Equal(now) {
  214. state = 4
  215. }
  216. // 结束时间>今天+2月 已签约
  217. if end.After(now2Month) || end.Equal(now2Month) {
  218. state = 3
  219. }
  220. }
  221. r.T_verify_state = state
  222. r.T_recoveries = t.T_recoveries
  223. r.T_invoice = t.T_invoice
  224. return r
  225. }
  226. // 添加
  227. func (dao *ContractDaoImpl) Add_Contract(r Contract) (id int64, err error) {
  228. id, err = dao.orm.Insert(&r)
  229. if err != nil {
  230. logs.Error(lib.FuncName(), err)
  231. }
  232. return id, err
  233. }
  234. // 获取 ById
  235. func (dao *ContractDaoImpl) Read_Contract_ByT_number(T_number string) (r Contract, err error) {
  236. qs := dao.orm.QueryTable(new(Contract))
  237. err = qs.Filter("T_number", T_number).Filter("T_State__gt", 0).One(&r)
  238. if err != nil {
  239. logs.Error(lib.FuncName(), err)
  240. }
  241. return
  242. }
  243. func (dao *ContractDaoImpl) Read_Contract_ById(Id int) (r Contract, err error) {
  244. qs := dao.orm.QueryTable(new(Contract))
  245. err = qs.Filter("Id", Id).One(&r)
  246. if err != nil {
  247. logs.Error(lib.FuncName(), err)
  248. }
  249. return
  250. }
  251. // 修改
  252. func (dao *ContractDaoImpl) Update_Contract(m Contract, cols ...string) error {
  253. _, err := dao.orm.Update(&m, cols...)
  254. if err != nil {
  255. logs.Error(lib.FuncName(), err)
  256. return err
  257. }
  258. return nil
  259. }
  260. // 删除
  261. func (dao *ContractDaoImpl) Delete_Contract(v Contract) error {
  262. v.T_State = 0
  263. _, err := dao.orm.Update(&v, "T_State")
  264. if err != nil {
  265. logs.Error(lib.FuncName(), err)
  266. }
  267. return err
  268. }
  269. // 获取列表
  270. func (dao *ContractDaoImpl) Read_Contract_List(T_submit, T_name string, T_state, T_out, page, page_z int) (r_ []Contract_R, cnt int64) {
  271. // 也可以直接使用 Model 结构体作为表名
  272. qs := dao.orm.QueryTable(new(Contract))
  273. var offset int64
  274. if page <= 1 {
  275. offset = 0
  276. } else {
  277. offset = int64((page - 1) * page_z)
  278. }
  279. // 过滤
  280. cond := orm.NewCondition()
  281. cond1 := cond.And("T_State__gt", 0).And("T_type", 1)
  282. if T_state > 0 {
  283. cond1 = cond1.And("T_State", T_state)
  284. }
  285. if len(T_name) > 0 {
  286. cond1 = cond1.AndCond(cond.Or("T_number__icontains", T_name).Or("T_customer__icontains", T_name))
  287. }
  288. if len(T_submit) > 0 {
  289. cond1 = cond1.And("T_submit", T_submit)
  290. }
  291. // T_out == 1 查询未出库或部分出库数据
  292. if T_out == 1 {
  293. cond1 = cond1.And("T_out__in", []int{1, 2})
  294. }
  295. // 查询
  296. var r []Contract
  297. _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  298. if err != nil {
  299. logs.Error(lib.FuncName(), err)
  300. return
  301. }
  302. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  303. if err != nil {
  304. logs.Error(lib.FuncName(), err)
  305. return
  306. }
  307. for _, v := range r {
  308. r_ = append(r_, ContractToContract_R(v))
  309. }
  310. return r_, cnt
  311. }
  312. // 获取列表
  313. func (dao *ContractDaoImpl) Read_VerifyContract_List(T_name, T_customer_id string, page, page_z int) (r_ []Contract_Verify, cnt int64) {
  314. // 也可以直接使用 Model 结构体作为表名
  315. qs := dao.orm.QueryTable(new(Contract))
  316. var offset int64
  317. if page <= 1 {
  318. offset = 0
  319. } else {
  320. offset = int64((page - 1) * page_z)
  321. }
  322. // 过滤
  323. cond := orm.NewCondition()
  324. cond1 := cond.And("T_State__gt", 0).And("T_type", 2)
  325. if len(T_customer_id) > 0 {
  326. cond1 = cond1.And("T_customer_id", T_customer_id)
  327. }
  328. if len(T_name) > 0 {
  329. cond1 = cond1.AndCond(cond.Or("T_number__icontains", T_name).Or("T_customer__icontains", T_name))
  330. }
  331. // 查询
  332. var r []Contract
  333. var err error
  334. if page_z == 9999 || len(T_customer_id) == 0 {
  335. _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-T_end_date").All(&r)
  336. } else {
  337. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-T_end_date").All(&r)
  338. }
  339. if err != nil {
  340. logs.Error(lib.FuncName(), err)
  341. return
  342. }
  343. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  344. if err != nil {
  345. logs.Error(lib.FuncName(), err)
  346. return
  347. }
  348. for _, v := range r {
  349. r_ = append(r_, ContractToContract_Verify(v))
  350. }
  351. return r_, cnt
  352. }
  353. func (dao *ContractDaoImpl) Read_Contract_List_For_RecoveriesAndInvoice(T_name string, T_recoveries_state, T_invoice_state, page, page_z int) (r_ []Contract_R, cnt int64) {
  354. // 也可以直接使用 Model 结构体作为表名
  355. qs := dao.orm.QueryTable(new(Contract))
  356. var offset int64
  357. if page <= 1 {
  358. offset = 0
  359. } else {
  360. offset = int64((page - 1) * page_z)
  361. }
  362. // 过滤
  363. cond := orm2.NewCondition()
  364. cond = cond.And("T_State", 1)
  365. // 回款状态 1 未回款 2 部分回款 3 全部回款
  366. // 开票状态 1 未开票 2 部分开票 3 全部开票
  367. if len(T_name) > 0 {
  368. cond = cond.AndCond(cond.Or("T_number__icontains", T_name).Or("T_customer__icontains", T_name))
  369. }
  370. qs = qs.SetCond(cond)
  371. if T_recoveries_state == 1 {
  372. qs = qs.FilterRaw("t_recoveries_money", " = 0")
  373. }
  374. if T_recoveries_state == 2 {
  375. qs = qs.FilterRaw("t_recoveries_money", " < t_discount").FilterRaw("t_recoveries_money", " > 0")
  376. }
  377. if T_recoveries_state == 3 {
  378. qs = qs.FilterRaw("t_recoveries_money", " = t_discount")
  379. }
  380. if T_invoice_state == 1 {
  381. qs = qs.FilterRaw("t_invoice_money", " = 0")
  382. }
  383. if T_invoice_state == 2 {
  384. qs = qs.FilterRaw("t_invoice_money", " < t_discount").FilterRaw("t_invoice_money", " > 0")
  385. }
  386. if T_invoice_state == 3 {
  387. qs = qs.FilterRaw("t_invoice_money", " = t_discount")
  388. }
  389. cond = qs.GetCond()
  390. // 查询
  391. var r []Contract
  392. _, err := qs.Limit(page_z, offset).SetCond(cond).OrderBy("-Id").All(&r)
  393. if err != nil {
  394. logs.Error(lib.FuncName(), err)
  395. return
  396. }
  397. cnt, err = qs.SetCond(cond).Count()
  398. if err != nil {
  399. logs.Error(lib.FuncName(), err)
  400. return
  401. }
  402. for _, v := range r {
  403. r_ = append(r_, ContractToContract_R(v))
  404. }
  405. return r_, cnt
  406. }
  407. // 获取最新的验证合同
  408. func (dao *ContractDaoImpl) Read_VerifyContract_Newest(T_customer_id string) (r_ Contract) {
  409. // 也可以直接使用 Model 结构体作为表名
  410. qs := dao.orm.QueryTable(new(Contract))
  411. // 过滤
  412. cond := orm.NewCondition()
  413. cond1 := cond.And("T_State__gt", 0).And("T_type", 2).And("T_customer_id", T_customer_id)
  414. // 查询
  415. var r []Contract
  416. _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-T_end_date").All(&r)
  417. if err != nil {
  418. logs.Error(lib.FuncName(), err)
  419. return
  420. }
  421. if len(r) == 0 {
  422. return
  423. }
  424. return r[0]
  425. }
  426. // 获取验证合同
  427. func (dao *ContractDaoImpl) Read_Contract_ByT_date(T_customer_id, T_start_date, T_end_date string) (r Contract, err error) {
  428. qs := dao.orm.QueryTable(new(Contract))
  429. err = qs.Filter("T_customer_id", T_customer_id).Filter("T_start_date", T_start_date).Filter("T_end_date", T_end_date).
  430. Filter("T_type", 2).Filter("T_State__gt", 0).One(&r)
  431. if err != nil {
  432. logs.Error(lib.FuncName(), err)
  433. }
  434. return
  435. }
  436. // 按年统计合同金额,汇款金额
  437. func (dao *ContractDaoImpl) Stat_ContractMoney_ByYear(T_type int) ([]ContractMoney_Stat, float32, float32) {
  438. var stat []ContractMoney_Stat
  439. var discount, recoveries float32
  440. sql := "SELECT YEAR(t_date) AS t_date, SUM(t_discount) AS t_discount, SUM(t_recoveries_money) AS t_recoveries_money FROM contract WHERE t__state = 1"
  441. if T_type > 0 {
  442. sql += " AND t_type = " + strconv.Itoa(T_type)
  443. }
  444. sql += " GROUP BY YEAR(t_date)"
  445. _, err := dao.orm.Raw(sql).QueryRows(&stat)
  446. if err != nil {
  447. logs.Error(lib.FuncName(), err)
  448. return stat, 0, 0
  449. }
  450. sqlSum := "SELECT SUM(t_discount),SUM(t_recoveries_money) FROM contract WHERE t__state = 1"
  451. if T_type > 0 {
  452. sqlSum += " AND t_type = " + strconv.Itoa(T_type)
  453. }
  454. err = dao.orm.Raw(sqlSum).QueryRow(&discount, &recoveries)
  455. if err != nil {
  456. logs.Error(lib.FuncName(), err)
  457. return stat, 0, 0
  458. }
  459. return stat, discount, recoveries
  460. }
  461. func (dao *ContractDaoImpl) Stat_ContractNum_ByYear(T_type int) ([]ContractNum_Stat, int64) {
  462. var stat []ContractNum_Stat
  463. var count int64
  464. sql := "SELECT YEAR(t_date) AS t_date, COUNT(ID) AS t_num FROM contract WHERE t__state = 1"
  465. if T_type > 0 {
  466. sql += " AND t_type = " + strconv.Itoa(T_type)
  467. }
  468. sql += " GROUP BY YEAR(t_date)"
  469. _, err := dao.orm.Raw(sql).QueryRows(&stat)
  470. if err != nil {
  471. logs.Error(lib.FuncName(), err)
  472. return stat, 0
  473. }
  474. sqlCount := "SELECT COUNT(ID) FROM contract WHERE t__state = 1"
  475. if T_type > 0 {
  476. sqlCount += " AND t_type = " + strconv.Itoa(T_type)
  477. }
  478. err = dao.orm.Raw(sqlCount).QueryRow(&count)
  479. if err != nil {
  480. logs.Error(lib.FuncName(), err)
  481. return stat, 0
  482. }
  483. return stat, count
  484. }
  485. func (dao *ContractDaoImpl) Stat_ContractMoney_ByMonth(T_type, T_year int) ([]ContractMoney_Stat, float32, float32) {
  486. //var stat []ContractMoney_Stat
  487. var stat []ContractMoney_Stat
  488. var discount, recoveries float32
  489. sql := "SELECT MONTH(t_date) AS t_date, SUM(t_discount) AS t_discount, SUM(t_recoveries_money) AS t_recoveries_money FROM contract WHERE t__state = 1" +
  490. " AND t_date BETWEEN '" + strconv.Itoa(T_year) + "-01-01' AND '" + strconv.Itoa(T_year) + "-12-31'"
  491. if T_type > 0 {
  492. sql += " AND t_type = " + strconv.Itoa(T_type)
  493. }
  494. sql += " GROUP BY MONTH(t_date)"
  495. fmt.Println(sql)
  496. _, err := dao.orm.Raw(sql).QueryRows(&stat)
  497. if err != nil {
  498. logs.Error(lib.FuncName(), err)
  499. return stat, 0, 0
  500. }
  501. sqlSum := "SELECT SUM(t_discount),SUM(t_recoveries_money) FROM contract WHERE t__state = 1" +
  502. " AND t_date BETWEEN '" + strconv.Itoa(T_year) + "-01-01' AND '" + strconv.Itoa(T_year) + "-12-31'"
  503. if T_type > 0 {
  504. sqlSum += " AND t_type = " + strconv.Itoa(T_type)
  505. }
  506. err = dao.orm.Raw(sqlSum).QueryRow(&discount, &recoveries)
  507. if err != nil {
  508. logs.Error(lib.FuncName(), err)
  509. return stat, 0, 0
  510. }
  511. return stat, discount, recoveries
  512. }
  513. func (dao *ContractDaoImpl) Stat_ContractNum_ByMonth(T_type, T_year int) ([]ContractNum_Stat, int64) {
  514. var stat []ContractNum_Stat
  515. var count int64
  516. sql := "SELECT MONTH(t_date) AS t_date, COUNT(ID) AS t_num FROM contract WHERE t__state = 1" +
  517. " AND t_date BETWEEN '" + strconv.Itoa(T_year) + "-01-01' AND '" + strconv.Itoa(T_year) + "-12-31'"
  518. if T_type > 0 {
  519. sql += " AND t_type = " + strconv.Itoa(T_type)
  520. }
  521. sql += " GROUP BY MONTH(t_date)"
  522. _, err := dao.orm.Raw(sql).QueryRows(&stat)
  523. if err != nil {
  524. logs.Error(lib.FuncName(), err)
  525. return stat, 0
  526. }
  527. sqlCount := "SELECT COUNT(ID) FROM contract WHERE t__state = 1" +
  528. " AND t_date BETWEEN '" + strconv.Itoa(T_year) + "-01-01' AND '" + strconv.Itoa(T_year) + "-12-31'"
  529. if T_type > 0 {
  530. sqlCount += " AND t_type = " + strconv.Itoa(T_type)
  531. }
  532. err = dao.orm.Raw(sqlCount).QueryRow(&count)
  533. if err != nil {
  534. logs.Error(lib.FuncName(), err)
  535. return stat, 0
  536. }
  537. return stat, count
  538. }
  539. // 获取全部
  540. func Read_Contract_All_Map() {
  541. o := orm.NewOrm()
  542. var r []Contract
  543. qs := o.QueryTable(new(Contract))
  544. _, err := qs.All(&r)
  545. if err != nil {
  546. logs.Error(lib.FuncName(), err)
  547. }
  548. for _, v := range r {
  549. Contract_list.Store(v.T_number, v)
  550. }
  551. }
  552. func Read_Contract_Get(T_number string) Contract {
  553. // 有先加入 给全部人发消息
  554. v, ok := Contract_list.Load(T_number) /*如果确定是真实的,则存在,否则不存在 */
  555. if ok {
  556. return v.(Contract)
  557. }
  558. return Contract{}
  559. }