contract_dao.go 10 KB

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