contract_dao.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. "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) Close(projectId int, bidsectionId int, treeId int, id int) error {
  191. session := d.engine.NewSession()
  192. defer session.Close()
  193. err := session.Begin()
  194. if err != nil {
  195. return errors.New("session出错-db")
  196. }
  197. // 1.关闭合同-关闭合同,默认锁定合同
  198. contractsCm := models.CmContracts{}
  199. contractsCm.Status = 2
  200. contractsCm.Locking = 1
  201. successNum, err := session.Where("id = ? and project_id = ? and bidsection_id = ? ", id, projectId, bidsectionId).Update(contractsCm)
  202. if err != nil {
  203. session.Rollback()
  204. return errors.New("关闭合同出错")
  205. }
  206. if successNum == 0 {
  207. session.Rollback()
  208. return errors.New("关闭合同异常")
  209. }
  210. // 2.更新项目节合同状态,锁定合同
  211. treeContractsCm := models.CmTreeContracts{}
  212. treeContractsCm.ContractStatus = 2
  213. treeContractsCm.ContractLocking = 1
  214. successNum, err = session.Where("tree_id = ? and project_id = ? and bidsection_id = ? ", treeId, projectId, bidsectionId).Update(treeContractsCm)
  215. if err != nil {
  216. session.Rollback()
  217. return errors.New("关闭合同出错-项目节")
  218. }
  219. if successNum == 0 {
  220. session.Rollback()
  221. return errors.New("关闭合同异常-项目节")
  222. }
  223. err = session.Commit()
  224. if err != nil {
  225. session.Rollback()
  226. return errors.New("session出错-db")
  227. }
  228. return nil
  229. }
  230. // 删除合同
  231. func (d *ContractDao) Unlock(projectId int, bidsectionId int, treeId int, id int) error {
  232. session := d.engine.NewSession()
  233. defer session.Close()
  234. err := session.Begin()
  235. if err != nil {
  236. return errors.New("session出错-db")
  237. }
  238. // 1.解锁合同
  239. contractsCm := models.CmContracts{}
  240. contractsCm.Locking = 0
  241. successNum, err := session.Where("id = ? and project_id = ? and bidsection_id = ? ", id, projectId, bidsectionId).Cols("locking").Update(contractsCm)
  242. if err != nil {
  243. session.Rollback()
  244. return errors.New("解锁合同出错")
  245. }
  246. if successNum == 0 {
  247. session.Rollback()
  248. return errors.New("解锁合同异常")
  249. }
  250. // 2.更新项目节上合同锁状态
  251. treeContractsCm := models.CmTreeContracts{}
  252. treeContractsCm.ContractLocking = 0
  253. successNum, err = session.Where("tree_id = ? and project_id = ? and bidsection_id = ? ", treeId, projectId, bidsectionId).Cols("contract_locking").Update(treeContractsCm)
  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. err = session.Commit()
  263. if err != nil {
  264. session.Rollback()
  265. return errors.New("session出错-db")
  266. }
  267. return nil
  268. }
  269. // 筛选出应用了当前规则的条数
  270. func (d *ContractDao) CountRuleCode(bid int) (int64, error) {
  271. data := &models.CmContracts{}
  272. total, err := d.engine.Where("`bidsection_id` = ?", bid).Count(data)
  273. if err != nil {
  274. total = 0
  275. }
  276. return total, err
  277. }