123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474 |
- /*
- * @description: 合同相关数据库操作
- * @Author: CP
- * @Date: 2020-11-17 10:41:05
- * @FilePath: \construction_management\dao\contract_dao.go
- */
- package dao
- import (
- "errors"
- "fmt"
- "log"
- "strconv"
- "github.com/go-xorm/xorm"
- "go.mod/models"
- )
- // 数据库操作引擎
- type ContractDao struct {
- engine *xorm.Engine
- }
- // 获得一个DAO对象
- func NewContractDao(engine *xorm.Engine) *ContractDao {
- return &ContractDao{
- engine: engine,
- }
- }
- // 获得本项目的合同项目节
- func (d *ContractDao) Get(id int) *models.CmContracts {
- data := &models.CmContracts{}
- _, err := d.engine.
- Where("id=? ", id).
- Get(data)
- if err != nil {
- data.Id = 0
- return data
- }
- return data
- }
- // 获得本项目的合同项目节
- func (d *ContractDao) GetByCode(projectId int, bidsectionId int, code string, contractsType int) []models.CmContracts {
- datalist := make([]models.CmContracts, 0)
- _ = d.engine.
- Where("project_id=? and bidsection_id=? and code=? and contracts_type=? ",
- projectId, bidsectionId, code, contractsType).
- Find(&datalist)
- return datalist
- }
- // 根据项目ID和表单ID数组获得数据
- func (d *ContractDao) GetByProjectIdInBidsectionId(bids string, projectId int) []models.CmContracts {
- datalist := make([]models.CmContracts, 0)
- d.engine.
- Asc("id").
- Where(" project_id=? and bidsection_id IN ("+bids+")", projectId).
- Find(&datalist)
- return datalist
- }
- // 获得本项目的合同项目节
- func (d *ContractDao) GetType(bidsectionId int, projectId int, contractsType int) []models.CmContracts {
- datalist := make([]models.CmContracts, 0)
- _ = d.engine.
- Where("project_id=? and bidsection_id=? and contracts_type=? ", projectId, bidsectionId, contractsType).
- Find(&datalist)
- return datalist
- }
- // 获得本项目的合同项目节
- func (d *ContractDao) GetTypeYear(bidsectionId int, projectId int, contractsType int, year int) []models.CmContracts {
- startYear := fmt.Sprintf("%d-01-01:00.00.00", year)
- endYear := fmt.Sprintf("%d-12-31:23.59.59", year)
- datalist := make([]models.CmContracts, 0)
- _ = d.engine.
- Where("project_id=? and bidsection_id=? and contracts_type=? and create_time>='"+startYear+"' and create_time<='"+endYear+"'",
- projectId, bidsectionId, contractsType).
- Find(&datalist)
- return datalist
- }
- // 合同
- func (d *ContractDao) GetInProjectAndBidsection(id int, projectId int, bidsectionId int) *models.CmContracts {
- data := &models.CmContracts{}
- _, err := d.engine.
- Where("id=? and project_id=? and bidsection_id=? ", id, projectId, bidsectionId).
- Get(data)
- if err != nil {
- data.Id = 0
- return data
- }
- return data
- }
- // 新增合同
- // contractData *models.CmContracts
- // contractTotal 合同总数
- // priceTotal 收入总金额
- func (d *ContractDao) Add(contractData *models.CmContracts) error {
- session := d.engine.NewSession()
- defer session.Close()
- err := session.Begin()
- if err != nil {
- return errors.New("新增合同出错-db")
- }
- // 1.写入合同表
- _, err = session.Insert(contractData)
- if err != nil {
- log.Println(err)
- session.Rollback()
- return errors.New("新增合同出错")
- }
- // 2.更新项目节表
- _, err = session.Exec("UPDATE cm_tree_contracts SET `contract_id` = ?,`contract_name` = ?,`contract_code` = ?,`contract_price` = ? "+
- "where tree_id = ? and project_id = ? and bidsection_id = ? ",
- contractData.Id, contractData.Name, contractData.Code, contractData.Price,
- contractData.TreeId, contractData.ProjectId, contractData.BidsectionId)
- if err != nil {
- session.Rollback()
- return errors.New("新增合同出错-项目节更新失败")
- }
- err = session.Commit()
- if err != nil {
- session.Rollback()
- return errors.New("新增合同出错-db")
- }
- return nil
- }
- // 更新收入合同
- func (d *ContractDao) Update(contractsCm *models.CmContracts, columns []string, projectId int, bidsectionId int, treeId int) error {
- session := d.engine.NewSession()
- defer session.Close()
- err := session.Begin()
- if err != nil {
- return errors.New("session出错-db")
- }
- // 1.更新合同表
- // successNum 是否有值更新的数量 为0,表示没有更新
- // successNum, err := session.Id(contractsCm.Id).MustCols(columns...).Update(contractsCm)
- _, err = session.Id(contractsCm.Id).MustCols(columns...).Update(contractsCm)
- if err != nil {
- session.Rollback()
- return errors.New("更新失败")
- }
- // 3.更新合同状态,合同金额和回款金额比对
- // 3-1获得回款总金额
- datalist := make([]models.CmContractsReturn, 0)
- err = d.engine.Where(" project_id =? and bidsection_id = ? and contracts_id =? ", projectId, bidsectionId, contractsCm.Id).Find(&datalist)
- if err != nil {
- session.Rollback()
- return errors.New("编辑合同出错-项目节更新失败")
- }
- contractsPrice := 0.00
- for _, item := range datalist {
- price, _ := strconv.ParseFloat(item.Price, 64)
- if item.ContractsId == contractsCm.Id {
- contractsPrice = contractsPrice + price
- }
- }
- contractsPrice, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", contractsPrice), 64)
- // 合同状态判定
- contractsDetailPrice, _ := strconv.ParseFloat(contractsCm.Price, 64)
- // 总回款大于等于合同金额 待关闭
- contractStatus := 0
- if contractsPrice >= contractsDetailPrice {
- contractStatus = 1
- }
- // 更新合同表状态
- _, err = session.Exec("UPDATE cm_contracts SET status = ? where id = ? ", contractStatus, contractsCm.Id)
- if err != nil {
- session.Rollback()
- return errors.New("合同状态更新失败")
- }
- // 2.更新项目节表
- _, err = session.Exec("UPDATE cm_tree_contracts SET `contract_name` = ?,`contract_price` = ? , contract_status = ? "+
- "where tree_id = ? and project_id = ? and bidsection_id = ? ",
- contractsCm.Name, contractsCm.Price, contractStatus,
- treeId, projectId, bidsectionId)
- if err != nil {
- session.Rollback()
- return errors.New("编辑合同出错-项目节更新失败")
- }
- err = session.Commit()
- if err != nil {
- session.Rollback()
- return errors.New("session出错-db")
- }
- return nil
- }
- // 更新收入合同
- func (d *ContractDao) UpdateExpenditure(contractsCm *models.CmContracts, columns []string, projectId int, bidsectionId int, treeId int) error {
- session := d.engine.NewSession()
- defer session.Close()
- err := session.Begin()
- if err != nil {
- return errors.New("session出错-db")
- }
- // 1.更新合同表
- // successNum 是否有值更新的数量 为0,表示没有更新
- // successNum, err := session.Id(contractsCm.Id).MustCols(columns...).Update(contractsCm)
- _, err = session.Id(contractsCm.Id).MustCols(columns...).Update(contractsCm)
- if err != nil {
- session.Rollback()
- return errors.New("更新失败")
- }
- // 3.更新合同状态,合同金额和回款金额比对
- // 3-1获得支出总金额
- datalist := make([]models.CmContractsPaid, 0)
- err = d.engine.Where(" project_id =? and bidsection_id = ? and contracts_id =? ", projectId, bidsectionId, contractsCm.Id).Find(&datalist)
- if err != nil {
- session.Rollback()
- return errors.New("编辑合同出错-项目节更新失败")
- }
- contractsPrice := 0.00
- for _, item := range datalist {
- price, _ := strconv.ParseFloat(item.Price, 64)
- if item.ContractsId == contractsCm.Id {
- contractsPrice = contractsPrice + price
- }
- }
- contractsPrice, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", contractsPrice), 64)
- // 合同状态判定
- contractsDetailPrice, _ := strconv.ParseFloat(contractsCm.Price, 64)
- // 总回款大于等于合同金额 待关闭
- contractStatus := 0
- if contractsPrice >= contractsDetailPrice {
- contractStatus = 1
- }
- // 更新合同表状态
- _, err = session.Exec("UPDATE cm_contracts SET status = ? where id = ? ", contractStatus, contractsCm.Id)
- if err != nil {
- session.Rollback()
- return errors.New("合同状态更新失败")
- }
- // 2.更新项目节表
- _, err = session.Exec("UPDATE cm_tree_contracts SET `contract_name` = ?,`contract_price` = ? , contract_status = ? "+
- "where tree_id = ? and project_id = ? and bidsection_id = ? ",
- contractsCm.Name, contractsCm.Price, contractStatus,
- treeId, projectId, bidsectionId)
- if err != nil {
- session.Rollback()
- return errors.New("编辑合同出错-项目节更新失败")
- }
- err = session.Commit()
- if err != nil {
- session.Rollback()
- return errors.New("session出错-db")
- }
- return nil
- }
- // 删除合同
- func (d *ContractDao) Delete(projectId int, bidsectionId int, treeId int, id int) error {
- contractsCm := models.CmContracts{}
- session := d.engine.NewSession()
- defer session.Close()
- err := session.Begin()
- if err != nil {
- return errors.New("session出错-db")
- }
- // 1.删除合同
- successNum, err := session.Where("id = ? and tree_id=? and bidsection_id= ? and project_id=? ", id, treeId, bidsectionId, projectId).Delete(contractsCm)
- if err != nil {
- session.Rollback()
- return errors.New("删除失败")
- }
- if successNum == 0 {
- session.Rollback()
- return errors.New("合同数据异常,删除失败")
- }
- // 2.删除项目节上合同信息-需求变更-删除项目节
- _, err = session.Exec("DELETE FROM `cm_tree_contracts` WHERE tree_id=? and project_id = ? and bidsection_id = ? ",
- treeId, projectId, bidsectionId)
- if err != nil {
- session.Rollback()
- return errors.New("编辑合同出错-项目节删除失败")
- }
- // _, 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 "+
- // ",contract_locking=0 where tree_id = ? and project_id = ? and bidsection_id = ? ",
- // treeId, projectId, bidsectionId)
- // if err != nil {
- // session.Rollback()
- // return errors.New("编辑合同出错-项目节更新失败")
- // }
- // 3.删除回款信息
- _, err = session.Exec("DELETE FROM `cm_contracts_return` WHERE contracts_id=? and project_id=? and bidsection_id=? ",
- id, projectId, bidsectionId)
- if err != nil {
- session.Rollback()
- return errors.New("编辑合同出错-删除回款信息失败")
- }
- // 4.删除附件-TODO
- err = session.Commit()
- if err != nil {
- session.Rollback()
- return errors.New("session出错-db")
- }
- return nil
- }
- // 删除支出合同
- func (d *ContractDao) DeleteExpenditure(projectId int, bidsectionId int, treeId int, id int) error {
- contractsCm := models.CmContracts{}
- session := d.engine.NewSession()
- defer session.Close()
- err := session.Begin()
- if err != nil {
- return errors.New("session出错-db")
- }
- // 1.删除合同
- successNum, err := session.Where("id = ? and tree_id=? and bidsection_id= ? and project_id=? ", id, treeId, bidsectionId, projectId).Delete(contractsCm)
- if err != nil {
- session.Rollback()
- return errors.New("删除失败")
- }
- if successNum == 0 {
- session.Rollback()
- return errors.New("合同数据异常,删除失败")
- }
- // 2.删除项目节上合同信息--移除项目节需求变更
- _, err = session.Exec("DELETE FROM `cm_tree_contracts` WHERE tree_id=? and project_id = ? and bidsection_id = ? ",
- treeId, projectId, bidsectionId)
- if err != nil {
- session.Rollback()
- return errors.New("编辑合同出错-删除项目节失败")
- }
- // _, 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 "+
- // ",contract_locking=0 where tree_id = ? and project_id = ? and bidsection_id = ? ",
- // treeId, projectId, bidsectionId)
- // if err != nil {
- // session.Rollback()
- // return errors.New("删除合同出错-项目节更新失败")
- // }
- // 3.删除已支付信息
- _, err = session.Exec("DELETE FROM `cm_contracts_paid` WHERE contracts_id=? and project_id=? and bidsection_id=? ",
- id, projectId, bidsectionId)
- if err != nil {
- session.Rollback()
- return errors.New("编辑合同出错-删除已支付失败")
- }
- // 4.删除附件-TODO
- err = session.Commit()
- if err != nil {
- session.Rollback()
- return errors.New("session出错-db")
- }
- return nil
- }
- // 关闭合同
- func (d *ContractDao) Close(projectId int, bidsectionId int, treeId int, id int, settlementCode string) error {
- session := d.engine.NewSession()
- defer session.Close()
- err := session.Begin()
- if err != nil {
- return errors.New("session出错-db")
- }
- // 1.关闭合同-关闭合同,默认锁定合同
- contractsCm := models.CmContracts{}
- contractsCm.Status = 2
- contractsCm.Locking = 1
- contractsCm.SettlementCode = settlementCode
- successNum, err := session.Where("id = ? and project_id = ? and bidsection_id = ? ", id, projectId, bidsectionId).Update(contractsCm)
- if err != nil {
- session.Rollback()
- return errors.New("关闭合同出错")
- }
- if successNum == 0 {
- session.Rollback()
- return errors.New("关闭合同异常")
- }
- // 2.更新项目节合同状态,锁定合同
- treeContractsCm := models.CmTreeContracts{}
- treeContractsCm.ContractStatus = 2
- treeContractsCm.ContractLocking = 1
- treeContractsCm.SettlementCode = settlementCode
- successNum, err = session.Where("tree_id = ? and project_id = ? and bidsection_id = ? ", treeId, projectId, bidsectionId).Update(treeContractsCm)
- if err != nil {
- session.Rollback()
- return errors.New("关闭合同出错-项目节")
- }
- if successNum == 0 {
- session.Rollback()
- return errors.New("关闭合同异常-项目节")
- }
- err = session.Commit()
- if err != nil {
- session.Rollback()
- return errors.New("session出错-db")
- }
- return nil
- }
- // 删除合同
- func (d *ContractDao) Unlock(projectId int, bidsectionId int, treeId int, id int) error {
- session := d.engine.NewSession()
- defer session.Close()
- err := session.Begin()
- if err != nil {
- return errors.New("session出错-db")
- }
- // 1.解锁合同
- contractsCm := models.CmContracts{}
- contractsCm.Locking = 0
- contractsCm.Status = 1
- _, err = session.Where("id = ? and project_id = ? and bidsection_id = ? ", id, projectId, bidsectionId).Cols("locking,Status").Update(contractsCm)
- if err != nil {
- session.Rollback()
- return errors.New("解锁合同出错")
- }
- // 2.更新项目节上合同锁状态
- treeContractsCm := models.CmTreeContracts{}
- treeContractsCm.ContractLocking = 0
- treeContractsCm.ContractStatus = 1
- _, err = session.Where("tree_id = ? and project_id = ? and bidsection_id = ? ", treeId, projectId, bidsectionId).Cols("contract_locking,contract_status").Update(treeContractsCm)
- if err != nil {
- session.Rollback()
- return errors.New("解锁合同出错-项目节")
- }
- err = session.Commit()
- if err != nil {
- session.Rollback()
- return errors.New("session出错-db")
- }
- return nil
- }
- // 筛选出应用了当前规则的条数
- func (d *ContractDao) CountRuleCode(bid int, contractType int) (int64, error) {
- data := &models.CmContracts{}
- total, err := d.engine.Where("`bidsection_id` = ? and `contracts_type` = ?", bid, contractType).Count(data)
- if err != nil {
- total = 0
- }
- return total, err
- }
|