contract_dao.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * @description: 合同相关数据库操作
  3. * @Author: CP
  4. * @Date: 2020-11-17 10:41:05
  5. * @FilePath: \construction_management\dao\contract_dao.go
  6. */
  7. package dao
  8. import (
  9. "errors"
  10. "fmt"
  11. "log"
  12. "strconv"
  13. "github.com/go-xorm/xorm"
  14. "go.mod/models"
  15. )
  16. // 数据库操作引擎
  17. type ContractDao struct {
  18. engine *xorm.Engine
  19. }
  20. // 获得一个DAO对象
  21. func NewContractDao(engine *xorm.Engine) *ContractDao {
  22. return &ContractDao{
  23. engine: engine,
  24. }
  25. }
  26. // 获得本项目的合同项目节
  27. func (d *ContractDao) Get(id int) *models.CmContracts {
  28. data := &models.CmContracts{}
  29. _, err := d.engine.
  30. Where("id=? ", id).
  31. Get(data)
  32. if err != nil {
  33. data.Id = 0
  34. return data
  35. }
  36. return data
  37. }
  38. // 获得本项目的合同项目节
  39. func (d *ContractDao) GetByCode(projectId int, bidsectionId int, code string, contractsType int) []models.CmContracts {
  40. datalist := make([]models.CmContracts, 0)
  41. _ = d.engine.
  42. Where("project_id=? and bidsection_id=? and code=? and contracts_type=? ",
  43. projectId, bidsectionId, code, contractsType).
  44. Find(&datalist)
  45. return datalist
  46. }
  47. // 根据项目ID和表单ID数组获得数据
  48. func (d *ContractDao) GetByProjectIdInBidsectionId(bids string, projectId int) []models.CmContracts {
  49. datalist := make([]models.CmContracts, 0)
  50. d.engine.
  51. Asc("id").
  52. Where(" project_id=? and bidsection_id IN ("+bids+")", projectId).
  53. Find(&datalist)
  54. return datalist
  55. }
  56. // 获得本项目的合同项目节
  57. func (d *ContractDao) GetType(bidsectionId int, projectId int, contractsType int) []models.CmContracts {
  58. datalist := make([]models.CmContracts, 0)
  59. _ = d.engine.
  60. Where("project_id=? and bidsection_id=? and contracts_type=? ", projectId, bidsectionId, contractsType).
  61. Find(&datalist)
  62. return datalist
  63. }
  64. // 获得本项目的合同项目节
  65. func (d *ContractDao) GetTypeYear(bidsectionId int, projectId int, contractsType int, year int) []models.CmContracts {
  66. startYear := fmt.Sprintf("%d-01-01:00.00.00", year)
  67. endYear := fmt.Sprintf("%d-12-31:23.59.59", year)
  68. datalist := make([]models.CmContracts, 0)
  69. _ = d.engine.
  70. Where("project_id=? and bidsection_id=? and contracts_type=? and create_time>='"+startYear+"' and create_time<='"+endYear+"'",
  71. projectId, bidsectionId, contractsType).
  72. Find(&datalist)
  73. return datalist
  74. }
  75. // 合同
  76. func (d *ContractDao) GetInProjectAndBidsection(id int, projectId int, bidsectionId int) *models.CmContracts {
  77. data := &models.CmContracts{}
  78. _, err := d.engine.
  79. Where("id=? and project_id=? and bidsection_id=? ", id, projectId, bidsectionId).
  80. Get(data)
  81. if err != nil {
  82. data.Id = 0
  83. return data
  84. }
  85. return data
  86. }
  87. // 新增合同
  88. // contractData *models.CmContracts
  89. // contractTotal 合同总数
  90. // priceTotal 收入总金额
  91. func (d *ContractDao) Add(contractData *models.CmContracts) error {
  92. session := d.engine.NewSession()
  93. defer session.Close()
  94. err := session.Begin()
  95. if err != nil {
  96. return errors.New("新增合同出错-db")
  97. }
  98. // 1.写入合同表
  99. _, err = session.Insert(contractData)
  100. if err != nil {
  101. log.Println(err)
  102. session.Rollback()
  103. return errors.New("新增合同出错")
  104. }
  105. // 2.更新项目节表
  106. _, err = session.Exec("UPDATE cm_tree_contracts SET `contract_id` = ?,`contract_name` = ?,`contract_code` = ?,`contract_price` = ? "+
  107. "where tree_id = ? and project_id = ? and bidsection_id = ? ",
  108. contractData.Id, contractData.Name, contractData.Code, contractData.Price,
  109. contractData.TreeId, contractData.ProjectId, contractData.BidsectionId)
  110. if err != nil {
  111. session.Rollback()
  112. return errors.New("新增合同出错-项目节更新失败")
  113. }
  114. err = session.Commit()
  115. if err != nil {
  116. session.Rollback()
  117. return errors.New("新增合同出错-db")
  118. }
  119. return nil
  120. }
  121. // 更新收入合同
  122. func (d *ContractDao) Update(contractsCm *models.CmContracts, columns []string, projectId int, bidsectionId int, treeId int) error {
  123. session := d.engine.NewSession()
  124. defer session.Close()
  125. err := session.Begin()
  126. if err != nil {
  127. return errors.New("session出错-db")
  128. }
  129. // 1.更新合同表
  130. // successNum 是否有值更新的数量 为0,表示没有更新
  131. // successNum, err := session.Id(contractsCm.Id).MustCols(columns...).Update(contractsCm)
  132. _, err = session.Id(contractsCm.Id).MustCols(columns...).Update(contractsCm)
  133. if err != nil {
  134. session.Rollback()
  135. return errors.New("更新失败")
  136. }
  137. // 3.更新合同状态,合同金额和回款金额比对
  138. // 3-1获得回款总金额
  139. datalist := make([]models.CmContractsReturn, 0)
  140. err = d.engine.Where(" project_id =? and bidsection_id = ? and contracts_id =? ", projectId, bidsectionId, contractsCm.Id).Find(&datalist)
  141. if err != nil {
  142. session.Rollback()
  143. return errors.New("编辑合同出错-项目节更新失败")
  144. }
  145. contractsPrice := 0.00
  146. for _, item := range datalist {
  147. price, _ := strconv.ParseFloat(item.Price, 64)
  148. if item.ContractsId == contractsCm.Id {
  149. contractsPrice = contractsPrice + price
  150. }
  151. }
  152. contractsPrice, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", contractsPrice), 64)
  153. // 合同状态判定
  154. contractsDetailPrice, _ := strconv.ParseFloat(contractsCm.Price, 64)
  155. // 总回款大于等于合同金额 待关闭
  156. contractStatus := 0
  157. if contractsPrice >= contractsDetailPrice {
  158. contractStatus = 1
  159. }
  160. // 更新合同表状态
  161. _, err = session.Exec("UPDATE cm_contracts SET status = ? where id = ? ", contractStatus, contractsCm.Id)
  162. if err != nil {
  163. session.Rollback()
  164. return errors.New("合同状态更新失败")
  165. }
  166. // 2.更新项目节表
  167. _, err = session.Exec("UPDATE cm_tree_contracts SET `contract_name` = ?,`contract_price` = ? , contract_status = ? "+
  168. "where tree_id = ? and project_id = ? and bidsection_id = ? ",
  169. contractsCm.Name, contractsCm.Price, contractStatus,
  170. treeId, projectId, bidsectionId)
  171. if err != nil {
  172. session.Rollback()
  173. return errors.New("编辑合同出错-项目节更新失败")
  174. }
  175. err = session.Commit()
  176. if err != nil {
  177. session.Rollback()
  178. return errors.New("session出错-db")
  179. }
  180. return nil
  181. }
  182. // 更新收入合同
  183. func (d *ContractDao) UpdateExpenditure(contractsCm *models.CmContracts, columns []string, projectId int, bidsectionId int, treeId int) error {
  184. session := d.engine.NewSession()
  185. defer session.Close()
  186. err := session.Begin()
  187. if err != nil {
  188. return errors.New("session出错-db")
  189. }
  190. // 1.更新合同表
  191. // successNum 是否有值更新的数量 为0,表示没有更新
  192. // successNum, err := session.Id(contractsCm.Id).MustCols(columns...).Update(contractsCm)
  193. _, err = session.Id(contractsCm.Id).MustCols(columns...).Update(contractsCm)
  194. if err != nil {
  195. session.Rollback()
  196. return errors.New("更新失败")
  197. }
  198. // 3.更新合同状态,合同金额和回款金额比对
  199. // 3-1获得支出总金额
  200. datalist := make([]models.CmContractsPaid, 0)
  201. err = d.engine.Where(" project_id =? and bidsection_id = ? and contracts_id =? ", projectId, bidsectionId, contractsCm.Id).Find(&datalist)
  202. if err != nil {
  203. session.Rollback()
  204. return errors.New("编辑合同出错-项目节更新失败")
  205. }
  206. contractsPrice := 0.00
  207. for _, item := range datalist {
  208. price, _ := strconv.ParseFloat(item.Price, 64)
  209. if item.ContractsId == contractsCm.Id {
  210. contractsPrice = contractsPrice + price
  211. }
  212. }
  213. contractsPrice, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", contractsPrice), 64)
  214. // 合同状态判定
  215. contractsDetailPrice, _ := strconv.ParseFloat(contractsCm.Price, 64)
  216. // 总回款大于等于合同金额 待关闭
  217. contractStatus := 0
  218. if contractsPrice >= contractsDetailPrice {
  219. contractStatus = 1
  220. }
  221. // 更新合同表状态
  222. _, err = session.Exec("UPDATE cm_contracts SET status = ? where id = ? ", contractStatus, contractsCm.Id)
  223. if err != nil {
  224. session.Rollback()
  225. return errors.New("合同状态更新失败")
  226. }
  227. // 2.更新项目节表
  228. _, err = session.Exec("UPDATE cm_tree_contracts SET `contract_name` = ?,`contract_price` = ? , contract_status = ? "+
  229. "where tree_id = ? and project_id = ? and bidsection_id = ? ",
  230. contractsCm.Name, contractsCm.Price, contractStatus,
  231. treeId, projectId, bidsectionId)
  232. if err != nil {
  233. session.Rollback()
  234. return errors.New("编辑合同出错-项目节更新失败")
  235. }
  236. err = session.Commit()
  237. if err != nil {
  238. session.Rollback()
  239. return errors.New("session出错-db")
  240. }
  241. return nil
  242. }
  243. // 删除合同
  244. func (d *ContractDao) Delete(projectId int, bidsectionId int, treeId int, id int) error {
  245. contractsCm := models.CmContracts{}
  246. session := d.engine.NewSession()
  247. defer session.Close()
  248. err := session.Begin()
  249. if err != nil {
  250. return errors.New("session出错-db")
  251. }
  252. // 1.删除合同
  253. successNum, err := session.Where("id = ? and tree_id=? and bidsection_id= ? and project_id=? ", id, treeId, bidsectionId, projectId).Delete(contractsCm)
  254. if err != nil {
  255. session.Rollback()
  256. return errors.New("删除失败")
  257. }
  258. if successNum == 0 {
  259. session.Rollback()
  260. return errors.New("合同数据异常,删除失败")
  261. }
  262. // 2.删除项目节上合同信息-需求变更-删除项目节
  263. _, err = session.Exec("DELETE FROM `cm_tree_contracts` WHERE tree_id=? and project_id = ? and bidsection_id = ? ",
  264. treeId, projectId, bidsectionId)
  265. if err != nil {
  266. session.Rollback()
  267. return errors.New("编辑合同出错-项目节删除失败")
  268. }
  269. // _, err = session.Exec("UPDATE cm_tree_contracts SET `contract_name` = '',`contract_code` = '',`contract_price` = 0,`contract_id` = 0,`contract_returned` = 0,`contracts_paid` = 0,`contract_status` = 0 "+
  270. // ",contract_locking=0 where tree_id = ? and project_id = ? and bidsection_id = ? ",
  271. // treeId, projectId, bidsectionId)
  272. // if err != nil {
  273. // session.Rollback()
  274. // return errors.New("编辑合同出错-项目节更新失败")
  275. // }
  276. // 3.删除回款信息
  277. _, err = session.Exec("DELETE FROM `cm_contracts_return` WHERE contracts_id=? and project_id=? and bidsection_id=? ",
  278. id, projectId, bidsectionId)
  279. if err != nil {
  280. session.Rollback()
  281. return errors.New("编辑合同出错-删除回款信息失败")
  282. }
  283. // 4.删除附件-TODO
  284. err = session.Commit()
  285. if err != nil {
  286. session.Rollback()
  287. return errors.New("session出错-db")
  288. }
  289. return nil
  290. }
  291. // 删除支出合同
  292. func (d *ContractDao) DeleteExpenditure(projectId int, bidsectionId int, treeId int, id int) error {
  293. contractsCm := models.CmContracts{}
  294. session := d.engine.NewSession()
  295. defer session.Close()
  296. err := session.Begin()
  297. if err != nil {
  298. return errors.New("session出错-db")
  299. }
  300. // 1.删除合同
  301. successNum, err := session.Where("id = ? and tree_id=? and bidsection_id= ? and project_id=? ", id, treeId, bidsectionId, projectId).Delete(contractsCm)
  302. if err != nil {
  303. session.Rollback()
  304. return errors.New("删除失败")
  305. }
  306. if successNum == 0 {
  307. session.Rollback()
  308. return errors.New("合同数据异常,删除失败")
  309. }
  310. // 2.删除项目节上合同信息--移除项目节需求变更
  311. _, err = session.Exec("DELETE FROM `cm_tree_contracts` WHERE tree_id=? and project_id = ? and bidsection_id = ? ",
  312. treeId, projectId, bidsectionId)
  313. if err != nil {
  314. session.Rollback()
  315. return errors.New("编辑合同出错-删除项目节失败")
  316. }
  317. // _, err = session.Exec("UPDATE cm_tree_contracts SET `contract_name` = '',`contract_code` = '',`contract_price` = 0,`contract_id` = 0,`contract_returned` = 0,`contracts_paid` = 0,`contract_status` = 0 "+
  318. // ",contract_locking=0 where tree_id = ? and project_id = ? and bidsection_id = ? ",
  319. // treeId, projectId, bidsectionId)
  320. // if err != nil {
  321. // session.Rollback()
  322. // return errors.New("删除合同出错-项目节更新失败")
  323. // }
  324. // 3.删除已支付信息
  325. _, err = session.Exec("DELETE FROM `cm_contracts_paid` WHERE contracts_id=? and project_id=? and bidsection_id=? ",
  326. id, projectId, bidsectionId)
  327. if err != nil {
  328. session.Rollback()
  329. return errors.New("编辑合同出错-删除已支付失败")
  330. }
  331. // 4.删除附件-TODO
  332. err = session.Commit()
  333. if err != nil {
  334. session.Rollback()
  335. return errors.New("session出错-db")
  336. }
  337. return nil
  338. }
  339. // 关闭合同
  340. func (d *ContractDao) Close(projectId int, bidsectionId int, treeId int, id int, settlementCode string) error {
  341. session := d.engine.NewSession()
  342. defer session.Close()
  343. err := session.Begin()
  344. if err != nil {
  345. return errors.New("session出错-db")
  346. }
  347. // 1.关闭合同-关闭合同,默认锁定合同
  348. contractsCm := models.CmContracts{}
  349. contractsCm.Status = 2
  350. contractsCm.Locking = 1
  351. contractsCm.SettlementCode = settlementCode
  352. successNum, err := session.Where("id = ? and project_id = ? and bidsection_id = ? ", id, projectId, bidsectionId).Update(contractsCm)
  353. if err != nil {
  354. session.Rollback()
  355. return errors.New("关闭合同出错")
  356. }
  357. if successNum == 0 {
  358. session.Rollback()
  359. return errors.New("关闭合同异常")
  360. }
  361. // 2.更新项目节合同状态,锁定合同
  362. treeContractsCm := models.CmTreeContracts{}
  363. treeContractsCm.ContractStatus = 2
  364. treeContractsCm.ContractLocking = 1
  365. treeContractsCm.SettlementCode = settlementCode
  366. successNum, err = session.Where("tree_id = ? and project_id = ? and bidsection_id = ? ", treeId, projectId, bidsectionId).Update(treeContractsCm)
  367. if err != nil {
  368. session.Rollback()
  369. return errors.New("关闭合同出错-项目节")
  370. }
  371. if successNum == 0 {
  372. session.Rollback()
  373. return errors.New("关闭合同异常-项目节")
  374. }
  375. err = session.Commit()
  376. if err != nil {
  377. session.Rollback()
  378. return errors.New("session出错-db")
  379. }
  380. return nil
  381. }
  382. // 删除合同
  383. func (d *ContractDao) Unlock(projectId int, bidsectionId int, treeId int, id int) error {
  384. session := d.engine.NewSession()
  385. defer session.Close()
  386. err := session.Begin()
  387. if err != nil {
  388. return errors.New("session出错-db")
  389. }
  390. // 1.解锁合同
  391. contractsCm := models.CmContracts{}
  392. contractsCm.Locking = 0
  393. contractsCm.Status = 1
  394. _, err = session.Where("id = ? and project_id = ? and bidsection_id = ? ", id, projectId, bidsectionId).Cols("locking,Status").Update(contractsCm)
  395. if err != nil {
  396. session.Rollback()
  397. return errors.New("解锁合同出错")
  398. }
  399. // 2.更新项目节上合同锁状态
  400. treeContractsCm := models.CmTreeContracts{}
  401. treeContractsCm.ContractLocking = 0
  402. treeContractsCm.ContractStatus = 1
  403. _, err = session.Where("tree_id = ? and project_id = ? and bidsection_id = ? ", treeId, projectId, bidsectionId).Cols("contract_locking,contract_status").Update(treeContractsCm)
  404. if err != nil {
  405. session.Rollback()
  406. return errors.New("解锁合同出错-项目节")
  407. }
  408. err = session.Commit()
  409. if err != nil {
  410. session.Rollback()
  411. return errors.New("session出错-db")
  412. }
  413. return nil
  414. }
  415. // 筛选出应用了当前规则的条数
  416. func (d *ContractDao) CountRuleCode(bid int, contractType int) (int64, error) {
  417. data := &models.CmContracts{}
  418. total, err := d.engine.Where("`bidsection_id` = ? and `contracts_type` = ?", bid, contractType).Count(data)
  419. if err != nil {
  420. total = 0
  421. }
  422. return total, err
  423. }