/*
 * @description: 合同已支付 相关业务操作
 * @Author: CP
 * @Date: 2020-12-22 14:33:43
 * @FilePath: \construction_management\services\contract_paid_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) PaidAll(projectId int, bidsectionId int, contractsId int, page int) []*viewmodels.ContractsPaid {
	// 1.获得合同回款列表
	// data := s.contractReturnDao.GetPage(projectId, bidsectionId, contractsId, page)
	data := s.contractPaidDao.GetAll(projectId, bidsectionId, contractsId)

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

	return contractsReturnVM
}

// 创建回款信息
func (s *contractService) PaidCreate(returnData *viewmodels.ContractsPaid, 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.CmContractsPaid{}
	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.contractPaidDao.Add(contractsReturnCm)
	if err != nil {
		return err
	}

	// 4. 更新已支付总金额 -项目节树 标段树 合同表
	err = s.contractPaidDao.UpdatePaidTotalPrice(projectId, bidsectionId, contractsId)
	if err != nil {
		return err
	}
	return nil
}

// 编辑已支付信息
func (s *contractService) PaidUpdate(returnData *viewmodels.ContractsPaid, 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 已支付是否存在
	contractPaid := s.contractPaidDao.Get(id, contractsId, bidsectionId)
	if contractPaid.Id == 0 {
		return errors.New("未找到已支付")
	}
	// 2.已支付信息
	contractsReturnCm := &models.CmContractsPaid{}
	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.contractPaidDao.Update(contractsReturnCm, contractsId, bidsectionId)
	if err != nil {
		return err
	}

	// 4. 更新已支付总金额 -项目节树 标段树 合同表
	err = s.contractPaidDao.UpdatePaidTotalPrice(projectId, bidsectionId, contractsId)
	if err != nil {
		return err
	}

	return nil
}

// 删除已支付
func (s *contractService) PaidDelete(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 已支付是否存在
	contractPaid := s.contractPaidDao.Get(id, contractsId, bidsectionId)
	if contractPaid.Id == 0 {
		return errors.New("未找到已支付")
	}

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

	// 1-2. 删除已支付
	err := s.contractPaidDao.Delete(id, contractsId, bidsectionId, projectId)
	if err != nil {
		return err
	}

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

	return nil
}

func (s *contractService) makeContractPaidVM(data *models.CmContractsPaid) *viewmodels.ContractsPaid {
	viewContractsReturn := &viewmodels.ContractsPaid{}
	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) ValidRuleContractPaidAdd(ctx iris.Context) (*viewmodels.ContractsPaid, error) {

	// 创建一个存放前端传过来参数
	contractsVaild := &viewmodels.ContractsPaid{}
	// 存放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) ValidRuleContractPaid(ctx iris.Context) (*viewmodels.ContractsPaid, error) {
	// 创建一个存放前端传过来参数
	contractsVaild := &viewmodels.ContractsPaid{}
	// 存放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) ValidRuleContractPaidDel(ctx iris.Context) (*viewmodels.ContractsPaid, error) {
	// 创建一个存放前端传过来参数
	contractsVaild := &viewmodels.ContractsPaid{}
	// 存放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
}
