contract_dao.go 11 KB

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