/*
 * @description: 回款相关数据操作
 * @Author: CP
 * @Date: 2020-11-30 11:19:14
 * @FilePath: \construction_management\dao\contract_return_dao.go
 */
package dao

import (
	"errors"
	"fmt"
	"strconv"

	"github.com/go-xorm/xorm"
	"go.mod/models"
)

//数据库操作引擎
type ContractReturnDao struct {
	engine *xorm.Engine
}

//获得一个DAO对象
func NewContractReturnDao(engine *xorm.Engine) *ContractReturnDao {
	return &ContractReturnDao{
		engine: engine,
	}
}

// 获得回款
func (d *ContractReturnDao) Get(id int, contractsId int, bidsectionId int) *models.CmContractsReturn {
	data := &models.CmContractsReturn{Id: id, ContractsId: contractsId, BidsectionId: bidsectionId}
	ok, err := d.engine.Get(data)
	if ok && err == nil {
		return data
	} else {
		data.Id = 0
		return data
	}

}

func (d *ContractReturnDao) GetAll(projectId int, bidsectionId int, contractsId int) []models.CmContractsReturn {
	datalist := make([]models.CmContractsReturn, 0)

	err := d.engine.
		Where("project_id = ? and bidsection_id =? and contracts_id=?", projectId, bidsectionId, contractsId).
		Desc("id").
		Find(&datalist)
	if err != nil {
		return datalist
	} else {
		return datalist
	}
}

//回款分页
func (d *ContractReturnDao) GetPage(projectId int, bidsectionId int, contractsId int, page int) []models.CmContractsReturn {
	datalist := make([]models.CmContractsReturn, 0)
	size := 8
	if page == 1 {
		page = 0
	} else {
		page--
		page = page * size
	}

	_ = d.engine.Where("project_id = ? and bidsection_id =? and contracts_id=?", projectId, bidsectionId, contractsId).
		Limit(size, page).
		Find(&datalist)
	return datalist
}

// 创建回款
func (d *ContractReturnDao) Add(contractsReturn *models.CmContractsReturn) error {
	_, err := d.engine.Insert(contractsReturn)
	return err
}

// 更新回款
func (d *ContractReturnDao) Update(contractsReturn *models.CmContractsReturn, contracts_id int, bidsectionId int) error {

	is, err := d.engine.Where("id = ? and contracts_id = ? and bidsection_id=? ", contractsReturn.Id, contracts_id, bidsectionId).Update(contractsReturn)
	if is == 0 {
		return errors.New("未找到回款信息")
	}
	return err
}

// 删除回款
func (d *ContractReturnDao) Delete(id int, contractsId int, bidsectionId int, projectId int) error {
	data := &models.CmContractsReturn{}
	is, err := d.engine.Where("id = ? and contracts_id = ? and project_id =? and bidsection_id = ? ", id, contractsId, projectId, bidsectionId).Delete(data)
	if is == 0 {
		return errors.New("未找到回款")
	}
	return err
}

// 更新回款总金额
func (d *ContractReturnDao) UpdateTotalPrice(projectId int, bidsectionId int, contractsId int) error {
	session := d.engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return errors.New("session出错-db")
	}

	// 0.获得合同金额
	contractsDetail := &models.CmContracts{}
	_, err = d.engine.Where(" id = ? ", contractsId).Get(contractsDetail)
	if err != nil {
		return errors.New("未找到合同")
	}

	// 1.获得合同回款总金额
	datalist := make([]models.CmContractsReturn, 0)
	err = d.engine.Where(" project_id =? and bidsection_id = ? ", projectId, bidsectionId).Find(&datalist)
	if err != nil {
		session.Rollback()
		return errors.New("编辑合同出错-项目节更新失败")
	}
	priceTotal := 0.00
	contractsPrice := 0.00
	for _, item := range datalist {
		price, _ := strconv.ParseFloat(item.Price, 64)
		priceTotal = priceTotal + price
		if item.ContractsId == contractsId {
			contractsPrice = contractsPrice + price
		}
	}
	priceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", priceTotal), 64)
	contractsPrice, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", contractsPrice), 64)

	// 1-1合同状态的判定
	contractsDetailPrice, _ := strconv.ParseFloat(contractsDetail.Price, 64)
	// 总回款大于等于合同金额 待关闭
	contractStatus := 0
	if contractsPrice >= contractsDetailPrice {
		contractStatus = 1
	}

	// 2.更新合同表 合同下回款总金额
	_, err = session.Exec("UPDATE  cm_contracts SET `returned` = ? , status = ? where id = ? ", contractsPrice, contractStatus, contractsId)
	if err != nil {
		session.Rollback()
		return errors.New("金额更新失败")
	}
	// 3.更新项目节 合同下回款总金额
	_, err = session.Exec("UPDATE  cm_tree_contracts SET `contract_returned` = ? , contract_status = ? where project_id = ? and bidsection_id=? and contract_id=? ",
		contractsPrice, contractStatus, projectId, bidsectionId, contractsId)
	if err != nil {
		session.Rollback()
		return errors.New("金额更新失败")
	}
	// 4.更新标段树 整个标段下回款总金额
	_, err = session.Exec("UPDATE  cm_tree SET `contracts_returned` = ? where project_id = ? and bidsection_id=? ", priceTotal, 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
}
