/*
 * @description: 合同回款 相关业务操作
 * @Author: CP
 * @Date: 2020-12-01 10:21:30
 * @FilePath: \construction_management\services\contract_return_service.go
 */

package services

import (
	"errors"
	"log"
	"strconv"
	"time"

	"github.com/kataras/iris/v12"
	"go.mod/comm"
	"go.mod/conf"
	"go.mod/models"
	"go.mod/web/viewmodels"
)

func (s *contractService) makeContractRetrunVM(data *models.CmContractsReturn) *viewmodels.ContractsReturn {
	viewContractsReturn := &viewmodels.ContractsReturn{}
	id, _ := comm.AesEncrypt(strconv.Itoa(data.Id), conf.SignSecret)
	contractsId, _ := comm.AesEncrypt(strconv.Itoa(data.ContractsId), conf.SignSecret)
	projectId, _ := comm.AesEncrypt(strconv.Itoa(data.ProjectId), conf.SignSecret)
	bidsectionId, _ := comm.AesEncrypt(strconv.Itoa(data.BidsectionId), conf.SignSecret)

	viewContractsReturn.Id = id
	viewContractsReturn.ContractsId = contractsId
	viewContractsReturn.ProjectId = projectId
	viewContractsReturn.BidsectionId = bidsectionId
	viewContractsReturn.Time = data.Time.Format(conf.SysTimeform)
	viewContractsReturn.Price = data.Price

	viewContractsReturn.Way = data.Way
	viewContractsReturn.CreateUser = data.CreateUser
	viewContractsReturn.Remarks = data.Remarks
	viewContractsReturn.CreateTime = data.CreateTime.Format(conf.SysTimeform)

	counts, _ := s.annexDao.GetCount(1, data.Id)
	viewContractsReturn.FileCounts = counts

	return viewContractsReturn
}

// 校验回款参数
func (s *contractService) ValidRuleContractRetrunAdd(ctx iris.Context) (*viewmodels.ContractsReturn, error) {

	// 创建一个存放前端传过来参数
	contractsVaild := &viewmodels.ContractsReturn{}
	// 存放raw的值，放入到contractsVaild
	err := ctx.ReadJSON(contractsVaild)
	if err != nil {
		log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
		return contractsVaild, err
	}
	// 验证合同传参
	err = contractsVaild.ValidateAdd()
	if err != nil {
		log.Println("参数验证错误, error=", err)
		return contractsVaild, err
	}

	return contractsVaild, nil
}

func (s *contractService) ValidRuleContractRetrun(ctx iris.Context) (*viewmodels.ContractsReturn, error) {
	// 创建一个存放前端传过来参数
	contractsVaild := &viewmodels.ContractsReturn{}
	// 存放raw的值，放入到contractsVaild
	err := ctx.ReadForm(contractsVaild)
	if err != nil {
		log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
		return contractsVaild, err
	}
	// 验证合同传参
	err = contractsVaild.Validate()
	if err != nil {
		log.Println("参数验证错误, error=", err)
		return contractsVaild, err
	}

	return contractsVaild, nil
}

//
func (s *contractService) ValidRuleContractRetrunDel(ctx iris.Context) (*viewmodels.ContractsReturn, error) {
	// 创建一个存放前端传过来参数
	contractsVaild := &viewmodels.ContractsReturn{}
	// 存放raw的值，放入到contractsVaild
	err := ctx.ReadForm(contractsVaild)
	if err != nil {
		log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
		return contractsVaild, err
	}
	// 验证合同传参
	err = contractsVaild.ValidateDel()
	if err != nil {
		log.Println("参数验证错误, error=", err)
		return contractsVaild, err
	}

	return contractsVaild, nil
}

// 获得合同下回款列表
func (s *contractService) ReturnAll(projectId int, bidsectionId int, contractsId int, page int) []*viewmodels.ContractsReturn {
	// 1.获得合同回款列表
	// data := s.contractReturnDao.GetPage(projectId, bidsectionId, contractsId, page)
	data := s.contractReturnDao.GetAll(projectId, bidsectionId, contractsId)

	contractsReturnVM := make([]*viewmodels.ContractsReturn, 0)
	for _, item := range data {
		cr := s.makeContractRetrunVM(&item)
		contractsReturnVM = append(contractsReturnVM, cr)
	}

	return contractsReturnVM
}

// 创建回款信息
func (s *contractService) ReturnCreate(returnData *viewmodels.ContractsReturn, projectId int, bidsectionId int, contractsId int, projectAccountId int) error {
	// 1.合同存在
	contract := s.contractDao.GetInProjectAndBidsection(contractsId, projectId, bidsectionId)
	if contract.Id == 0 {
		return errors.New("未找到合同")
	}

	// 2.回款信息
	contractsReturnCm := &models.CmContractsReturn{}
	contractsReturnCm.ContractsId = contractsId
	contractsReturnCm.ProjectId = projectId
	contractsReturnCm.BidsectionId = bidsectionId
	contractsReturnCm.Way = returnData.Way
	contractsReturnCm.Remarks = returnData.Remarks
	contractsReturnCm.CreateTime = time.Now()
	contractsReturnCm.UpdateTime = time.Now()

	loc, _ := time.LoadLocation("Local")
	time, err := time.ParseInLocation(conf.SysTimeform, returnData.Time, loc)
	if err != nil {
		return errors.New("签约时间填写异常")
	}
	contractsReturnCm.Time = time
	contractsReturnCm.CreateUser = returnData.CreateUser
	contractsReturnCm.AccountId = projectAccountId

	_, err = strconv.ParseFloat(returnData.Price, 64)
	if err != nil {
		return errors.New("金额填写有误")
	}
	contractsReturnCm.Price = returnData.Price

	// 3.新增回款
	err = s.contractReturnDao.Add(contractsReturnCm)
	if err != nil {
		return err
	}

	// 4. 更新回款总金额 -项目节树 标段树 合同表
	err = s.contractReturnDao.UpdateTotalPrice(projectId, bidsectionId, contractsId)
	if err != nil {
		return err
	}
	return nil
}

// 编辑回款信息
func (s *contractService) ReturnUpdate(returnData *viewmodels.ContractsReturn, projectId int, bidsectionId int, contractsId int, id int) error {

	// 1.合同存在
	contract := s.contractDao.GetInProjectAndBidsection(contractsId, projectId, bidsectionId)
	if contract.Id == 0 {
		return errors.New("未找到合同")
	}
	// 1-1 回款是否存在
	contractReturn := s.contractReturnDao.Get(id, contractsId, bidsectionId)
	if contractReturn.Id == 0 {
		return errors.New("未找到回款")
	}
	// 2.回款信息
	contractsReturnCm := &models.CmContractsReturn{}
	contractsReturnCm.Id = id
	contractsReturnCm.Way = returnData.Way
	contractsReturnCm.Remarks = returnData.Remarks

	loc, _ := time.LoadLocation("Local")
	time, err := time.ParseInLocation("2006-01-02", returnData.Time, loc)
	if err != nil {
		return errors.New("回款时间填写异常")
	}
	contractsReturnCm.Time = time

	_, err = strconv.ParseFloat(returnData.Price, 64)
	if err != nil {
		return errors.New("金额填写有误")
	}
	contractsReturnCm.Price = returnData.Price

	// 3.更新回款信息
	err = s.contractReturnDao.Update(contractsReturnCm, contractsId, bidsectionId)
	if err != nil {
		return err
	}

	// 4. 更新回款总金额 -项目节树 标段树 合同表
	err = s.contractReturnDao.UpdateTotalPrice(projectId, bidsectionId, contractsId)
	if err != nil {
		return err
	}

	return nil
}

// 删除回款
func (s *contractService) ReturnDelete(projectId int, bidsectionId int, contractsId int, id int) error {
	// 1.合同存在
	contract := s.contractDao.GetInProjectAndBidsection(contractsId, projectId, bidsectionId)
	if contract.Id == 0 {
		return errors.New("未找到合同")
	}
	// 1-1 回款是否存在
	contractReturn := s.contractReturnDao.Get(id, contractsId, bidsectionId)
	if contractReturn.Id == 0 {
		return errors.New("未找到回款")
	}

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

	// 1-2. 删除回款
	err := s.contractReturnDao.Delete(id, contractsId, bidsectionId, projectId)
	if err != nil {
		return err
	}

	// 2. 更新回款总金额 -项目节树 标段树 合同表
	err = s.contractReturnDao.UpdateTotalPrice(projectId, bidsectionId, contractsId)
	if err != nil {
		return err
	}

	return nil
}
