contract_dao.go 14 KB

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