contract_dao.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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) GetInProjectAndBidsection(id int, projectId int, bidsectionId int) *models.CmContracts {
  40. data := &models.CmContracts{}
  41. _, err := d.engine.
  42. Where("id=? and project_id=? and bidsection_id=? ", id, projectId, bidsectionId).
  43. Get(data)
  44. if err != nil {
  45. data.Id = 0
  46. return data
  47. }
  48. return data
  49. }
  50. // 新增合同
  51. // contractData *models.CmContracts
  52. // contractTotal 合同总数
  53. // priceTotal 收入总金额
  54. func (d *ContractDao) Add(contractData *models.CmContracts) error {
  55. session := d.engine.NewSession()
  56. defer session.Close()
  57. err := session.Begin()
  58. if err != nil {
  59. return errors.New("新增合同出错-db")
  60. }
  61. // 1.写入合同表
  62. _, err = session.Insert(contractData)
  63. if err != nil {
  64. log.Println(err)
  65. session.Rollback()
  66. return errors.New("新增合同出错")
  67. }
  68. // 2.更新项目节表
  69. _, err = session.Exec("UPDATE cm_tree_contracts SET `contract_id` = ?,`contract_name` = ?,`contract_code` = ?,`contract_price` = ? "+
  70. "where tree_id = ? and project_id = ? and bidsection_id = ? ",
  71. contractData.Id, contractData.Name, contractData.Code, contractData.Price,
  72. contractData.TreeId, contractData.ProjectId, contractData.BidsectionId)
  73. if err != nil {
  74. session.Rollback()
  75. return errors.New("新增合同出错-项目节更新失败")
  76. }
  77. err = session.Commit()
  78. if err != nil {
  79. session.Rollback()
  80. return errors.New("新增合同出错-db")
  81. }
  82. return nil
  83. }
  84. // 更新合同
  85. func (d *ContractDao) Update(contractsCm *models.CmContracts, columns []string, projectId int, bidsectionId int, treeId int) error {
  86. session := d.engine.NewSession()
  87. defer session.Close()
  88. err := session.Begin()
  89. if err != nil {
  90. return errors.New("session出错-db")
  91. }
  92. // 1.更新合同表
  93. successNum, err := session.Id(contractsCm.Id).MustCols(columns...).Update(contractsCm)
  94. if err != nil {
  95. session.Rollback()
  96. return errors.New("更新失败")
  97. }
  98. if successNum == 0 {
  99. session.Rollback()
  100. return errors.New("合同数据异常,更新失败")
  101. }
  102. // 3.更新合同状态,合同金额和回款金额比对
  103. // 3-1获得回款总金额
  104. datalist := make([]models.CmContractsReturn, 0)
  105. err = d.engine.Where(" project_id =? and bidsection_id = ? and contracts_id =? ", projectId, bidsectionId, contractsCm.Id).Find(&datalist)
  106. if err != nil {
  107. session.Rollback()
  108. return errors.New("编辑合同出错-项目节更新失败")
  109. }
  110. contractsPrice := 0.00
  111. for _, item := range datalist {
  112. price, _ := strconv.ParseFloat(item.Price, 64)
  113. if item.ContractsId == contractsCm.Id {
  114. contractsPrice = contractsPrice + price
  115. }
  116. }
  117. contractsPrice, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", contractsPrice), 64)
  118. // 合同状态判定
  119. contractsDetailPrice, _ := strconv.ParseFloat(contractsCm.Price, 64)
  120. // 总回款大于等于合同金额 待关闭
  121. contractStatus := 0
  122. if contractsPrice >= contractsDetailPrice {
  123. contractStatus = 1
  124. }
  125. // 更新合同表状态
  126. _, err = session.Exec("UPDATE cm_contracts SET status = ? where id = ? ", contractStatus, contractsCm.Id)
  127. if err != nil {
  128. session.Rollback()
  129. return errors.New("合同状态更新失败")
  130. }
  131. // 2.更新项目节表
  132. _, err = session.Exec("UPDATE cm_tree_contracts SET `contract_name` = ?,`contract_price` = ? , contract_status = ? "+
  133. "where tree_id = ? and project_id = ? and bidsection_id = ? ",
  134. contractsCm.Name, contractsCm.Price, contractStatus,
  135. treeId, projectId, bidsectionId)
  136. if err != nil {
  137. session.Rollback()
  138. return errors.New("编辑合同出错-项目节更新失败")
  139. }
  140. err = session.Commit()
  141. if err != nil {
  142. session.Rollback()
  143. return errors.New("session出错-db")
  144. }
  145. return nil
  146. }
  147. // 删除合同
  148. func (d *ContractDao) Delete(projectId int, bidsectionId int, treeId int, id int) error {
  149. contractsCm := models.CmContracts{}
  150. session := d.engine.NewSession()
  151. defer session.Close()
  152. err := session.Begin()
  153. if err != nil {
  154. return errors.New("session出错-db")
  155. }
  156. // 1.删除合同
  157. successNum, err := session.Where("id = ? and tree_id=? and bidsection_id= ? and project_id=? ", id, treeId, bidsectionId, projectId).Delete(contractsCm)
  158. if err != nil {
  159. session.Rollback()
  160. return errors.New("删除失败")
  161. }
  162. if successNum == 0 {
  163. session.Rollback()
  164. return errors.New("合同数据异常,删除失败")
  165. }
  166. // 2.删除项目节上合同信息
  167. _, 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 "+
  168. ",contract_locking=0 where tree_id = ? and project_id = ? and bidsection_id = ? ",
  169. treeId, projectId, bidsectionId)
  170. if err != nil {
  171. session.Rollback()
  172. return errors.New("编辑合同出错-项目节更新失败")
  173. }
  174. // 3.删除回款信息
  175. _, err = session.Exec("DELETE FROM `cm_contracts_return` WHERE contracts_id=? and project_id=? and bidsection_id=? ",
  176. id, projectId, bidsectionId)
  177. if err != nil {
  178. session.Rollback()
  179. return errors.New("编辑合同出错-项目节更新失败")
  180. }
  181. // 4.删除附件-TODO
  182. err = session.Commit()
  183. if err != nil {
  184. session.Rollback()
  185. return errors.New("session出错-db")
  186. }
  187. return nil
  188. }
  189. // 删除支出合同
  190. func (d *ContractDao) DeleteExpenditure(projectId int, bidsectionId int, treeId int, id int) error {
  191. contractsCm := models.CmContracts{}
  192. session := d.engine.NewSession()
  193. defer session.Close()
  194. err := session.Begin()
  195. if err != nil {
  196. return errors.New("session出错-db")
  197. }
  198. // 1.删除合同
  199. successNum, err := session.Where("id = ? and tree_id=? and bidsection_id= ? and project_id=? ", id, treeId, bidsectionId, projectId).Delete(contractsCm)
  200. if err != nil {
  201. session.Rollback()
  202. return errors.New("删除失败")
  203. }
  204. if successNum == 0 {
  205. session.Rollback()
  206. return errors.New("合同数据异常,删除失败")
  207. }
  208. // 2.删除项目节上合同信息
  209. _, 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 "+
  210. ",contract_locking=0 where tree_id = ? and project_id = ? and bidsection_id = ? ",
  211. treeId, projectId, bidsectionId)
  212. if err != nil {
  213. session.Rollback()
  214. return errors.New("删除合同出错-项目节更新失败")
  215. }
  216. // 3.删除已支付信息
  217. _, err = session.Exec("DELETE FROM `cm_contracts_paid` WHERE contracts_id=? and project_id=? and bidsection_id=? ",
  218. id, projectId, bidsectionId)
  219. if err != nil {
  220. session.Rollback()
  221. return errors.New("编辑合同出错-项目节更新失败")
  222. }
  223. // 4.删除附件-TODO
  224. err = session.Commit()
  225. if err != nil {
  226. session.Rollback()
  227. return errors.New("session出错-db")
  228. }
  229. return nil
  230. }
  231. // 关闭合同
  232. func (d *ContractDao) Close(projectId int, bidsectionId int, treeId int, id int) error {
  233. session := d.engine.NewSession()
  234. defer session.Close()
  235. err := session.Begin()
  236. if err != nil {
  237. return errors.New("session出错-db")
  238. }
  239. // 1.关闭合同-关闭合同,默认锁定合同
  240. contractsCm := models.CmContracts{}
  241. contractsCm.Status = 2
  242. contractsCm.Locking = 1
  243. successNum, err := session.Where("id = ? and project_id = ? and bidsection_id = ? ", id, projectId, bidsectionId).Update(contractsCm)
  244. if err != nil {
  245. session.Rollback()
  246. return errors.New("关闭合同出错")
  247. }
  248. if successNum == 0 {
  249. session.Rollback()
  250. return errors.New("关闭合同异常")
  251. }
  252. // 2.更新项目节合同状态,锁定合同
  253. treeContractsCm := models.CmTreeContracts{}
  254. treeContractsCm.ContractStatus = 2
  255. treeContractsCm.ContractLocking = 1
  256. successNum, err = session.Where("tree_id = ? and project_id = ? and bidsection_id = ? ", treeId, projectId, bidsectionId).Update(treeContractsCm)
  257. if err != nil {
  258. session.Rollback()
  259. return errors.New("关闭合同出错-项目节")
  260. }
  261. if successNum == 0 {
  262. session.Rollback()
  263. return errors.New("关闭合同异常-项目节")
  264. }
  265. err = session.Commit()
  266. if err != nil {
  267. session.Rollback()
  268. return errors.New("session出错-db")
  269. }
  270. return nil
  271. }
  272. // 删除合同
  273. func (d *ContractDao) Unlock(projectId int, bidsectionId int, treeId int, id int) error {
  274. session := d.engine.NewSession()
  275. defer session.Close()
  276. err := session.Begin()
  277. if err != nil {
  278. return errors.New("session出错-db")
  279. }
  280. // 1.解锁合同
  281. contractsCm := models.CmContracts{}
  282. contractsCm.Locking = 0
  283. successNum, err := session.Where("id = ? and project_id = ? and bidsection_id = ? ", id, projectId, bidsectionId).Cols("locking").Update(contractsCm)
  284. if err != nil {
  285. session.Rollback()
  286. return errors.New("解锁合同出错")
  287. }
  288. if successNum == 0 {
  289. session.Rollback()
  290. return errors.New("解锁合同异常")
  291. }
  292. // 2.更新项目节上合同锁状态
  293. treeContractsCm := models.CmTreeContracts{}
  294. treeContractsCm.ContractLocking = 0
  295. successNum, err = session.Where("tree_id = ? and project_id = ? and bidsection_id = ? ", treeId, projectId, bidsectionId).Cols("contract_locking").Update(treeContractsCm)
  296. if err != nil {
  297. session.Rollback()
  298. return errors.New("解锁合同出错-项目节")
  299. }
  300. if successNum == 0 {
  301. session.Rollback()
  302. return errors.New("解锁合同异常-项目节")
  303. }
  304. err = session.Commit()
  305. if err != nil {
  306. session.Rollback()
  307. return errors.New("session出错-db")
  308. }
  309. return nil
  310. }
  311. // 筛选出应用了当前规则的条数
  312. func (d *ContractDao) CountRuleCode(bid int) (int64, error) {
  313. data := &models.CmContracts{}
  314. total, err := d.engine.Where("`bidsection_id` = ?", bid).Count(data)
  315. if err != nil {
  316. total = 0
  317. }
  318. return total, err
  319. }