/*
 * @description: 合同支出业务相关
 * @Author: CP
 * @Date: 2020-12-21 15:35:03
 * @FilePath: \construction_management\services\contract_expenditure_service.go
 */

package services

import (
	"errors"
	"time"

	"go.mod/conf"
	"go.mod/models"
	"go.mod/web/viewmodels"
)

// 新增支出合同
func (s *contractService) AddExpenditure(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error {
	// 1. 项目节存在
	contracts := s.treeContractDao.Get(treeId, bidsectionId, projectId, 1)
	if contracts.Id == 0 {
		return errors.New("未找到项目节")
	}
	// k := int32(projectId)
	// 2.项目节是没有合同
	if contracts.ContractId != 0 {
		return errors.New("该项目节上已经存在合同")
	}

	// 3.新增合同 --合计标段上的金额
	contractsCm := &models.CmContracts{}
	contractsCm.Code = contractData.Code
	contractsCm.Name = contractData.Name
	contractsCm.ContractsType = 2
	contractsCm.Price = contractData.Price
	contractsCm.Returned = "0"
	contractsCm.Paid = "0"
	contractsCm.TreeId = treeId
	contractsCm.ProjectId = projectId
	contractsCm.BidsectionId = bidsectionId
	contractsCm.Status = 0
	contractsCm.CreateTime = time.Now()
	contractsCm.UpdateTime = time.Now()

	err := s.contractDao.Add(contractsCm)
	if err != nil {
		return err
	}

	// 3.获得该标段下合同总数 - 总收入金额
	contractTotal, priceTotal := s.getContractTotalAndPrice(bidsectionId, projectId, 1)
	// 更新标段目录上合同金额和总数
	err = s.treeDao.UpdateContractsAndPayPrice(projectId, bidsectionId, contractTotal, priceTotal)
	if err != nil {
		return err
	}

	return nil
}

// 更新支出合同
func (s *contractService) UpdateExpenditure(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error {
	// 1. 项目节存在
	contractsTree := s.treeContractDao.Get(treeId, bidsectionId, projectId, 1)
	if contractsTree.Id == 0 {
		return errors.New("未找到项目节")
	}
	// 2.项目节是没有合同
	if contractsTree.ContractId == 0 {
		return errors.New("该项目节上没有找到合同")
	}

	// 3.合同锁定 不能删除
	if contractsTree.ContractLocking == 1 {
		return errors.New("该合同已锁定")
	}

	contractsCm := &models.CmContracts{}
	contractsCm.Id = contractsTree.ContractId
	contractsCm.Content = contractData.Content
	contractsCm.Name = contractData.Name
	contractsCm.Price = contractData.Price
	contractsCm.PartyA = contractData.PartyA
	contractsCm.PartyASigner = contractData.PartyASigner
	contractsCm.PartyB = contractData.PartyB
	contractsCm.PartyBSigner = contractData.PartyBSigner

	loc, _ := time.LoadLocation("Local")
	SignerTime, err := time.ParseInLocation(conf.SysTimeform, contractData.SignerTime, loc)
	if err != nil {
		return errors.New("签约时间填写异常")
	}
	contractsCm.SignerTime = SignerTime
	contractsCm.Remarks = contractData.Remarks

	columns := []string{"Content", "Name", "Price", "PartyA", "PartyASigner", "PartyB", "PartyBSigner"}
	err = s.contractDao.Update(contractsCm, columns, projectId, bidsectionId, treeId)
	if err != nil {
		return err
	}
	// 3.获得该标段下合同总数 - 总收入金额
	contractTotal, priceTotal := s.getContractTotalAndPrice(bidsectionId, projectId, 1)
	// 更新标段目录上合同金额和总数
	err = s.treeDao.UpdateContractsAndPayPrice(projectId, bidsectionId, contractTotal, priceTotal)
	if err != nil {
		return err
	}
	return nil
}

// 删除支出合同
func (s *contractService) DeleteExpenditure(projectId int, bidsectionId int, treeId int, id int) error {
	// 1. 项目节存在
	contractsTree := s.treeContractDao.Get(treeId, bidsectionId, projectId, 1)
	if contractsTree.Id == 0 {
		return errors.New("未找到项目节")
	}
	// 2.项目节是没有合同
	if contractsTree.ContractId == 0 {
		return errors.New("该项目节上没有找到合同")
	}

	// 3.合同锁定 不能删除
	if contractsTree.ContractLocking == 1 {
		return errors.New("该合同已锁定")
	}

	// 删除支出合同
	err := s.contractDao.DeleteExpenditure(projectId, bidsectionId, treeId, id)
	if err != nil {
		return err
	}
	// 3.获得该标段下合同总数 - 总收入金额
	contractTotal, priceTotal := s.getContractTotalAndPrice(bidsectionId, projectId, 1)
	// 更新标段目录上合同金额和总数
	err = s.treeDao.UpdateContractsAndPayPrice(projectId, bidsectionId, contractTotal, priceTotal)
	if err != nil {
		return err
	}

	// 4.更新回款总金额
	err = s.contractPaidDao.UpdatePaidTotalPrice(projectId, bidsectionId, id)
	if err != nil {
		return err
	}

	return nil
}
