123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- package service
- import (
- "context"
- "fmt"
- modernPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/xdmy"
- "gitee.com/xuyiping_admin/pkg/xerr"
- "github.com/pkg/errors"
- "gorm.io/gorm"
- "kpt.xdmy/apiserver/model"
- "strconv"
- )
- // CreateSpecialtyPurchase 创建特殊申购信息
- func CreateSpecialtyPurchase(ctx context.Context, req *modernPb.SparePartsRequisitionsRequest) error {
- var bigPartPurchaseId int64 = 0
- if req.PurchaseType == 1 {
- sql := `SELECT
- count(1) count
- FROM
- bigpartpurchase bp
- JOIN pasture p
- on p.id = bp.pastureId
- WHERE
- bp.pastureId = ?
- AND bp.statue < 3 and bp.purchase_type = 1 and createTime >'2024-03-18'
- and ( select name from department where id = bp.departmentId) != '挤奶处' order by bp.id desc limit 1 `
- var total int64
- s.d.DB.Raw(sql, req.PastureId).Count(&total)
- if total > 0 {
- return errors.New("单号已生成,请返回开始页面重新申购!")
- }
- s.d.DB.Exec(` delete from partpurchase p where (select pastureId from department where id = p.departmentId ) = ? and reject = 1 and employeId = ?
- and (select purchase_type from bigpartpurchase where id = p.bigId ) = 1 `, req.DepartmentId, req.EmployeId)
- }
- // 赠品
- defer func() {
- if req.PurchaseType == modernPb.PurchaseType_Complimentary_Item {
- if err := SpecialtyToProcure(bigPartPurchaseId); err != nil {
- fmt.Println("SpecialtyToProcure===>", bigPartPurchaseId)
- }
- }
- }()
- return s.d.DB.Transaction(func(tx *gorm.DB) error {
- newBigPartPurchase := model.NewBigPartPurchase(
- int64(req.PastureId), int64(req.DepartmentId), int64(req.EmployeId),
- int64(req.ProviderId), int64(req.FunderId),
- req.PurchaseType, req.OrderNumber, req.CreateTime, req.ProviderName,
- )
- if err := tx.Model(new(model.BigPartPurchase)).Create(newBigPartPurchase).Error; err != nil {
- return xerr.WithStack(err)
- }
- partPurchaseList := make([]*model.PartPurchase, 0)
- //for _, v := range req.SpotList {
- // amount, _ := strconv.Atoi(v.Amount)
- // partPurchaseList = append(partPurchaseList, model.NewPartPurchase(
- // newBigPartPurchase.Id, int64(req.PastureId), v.PartId, int64(v.BrandId), int64(amount),
- // v.ContractId, v.PartCode, v.PartName, v.Specification, v.Unit, v.Purpose, v.Price,
- // float64(v.StorageAmount)))
- //}
- for _, v := range req.SpotList {
- amount, _ := strconv.Atoi(v.Amount)
- emp, _ := strconv.ParseInt(v.EmployeId, 10, 64)
- partPurchaseList = append(partPurchaseList, model.NewPartPurchase(
- newBigPartPurchase.Id, int64(req.PastureId), v.PartId, int64(v.BrandId), int64(amount),
- v.ContractId, v.PartCode, v.PartName, v.Specification, v.Unit, v.Purpose, v.Price,
- float64(v.StorageAmount), v.DepartmentId, v.Date, emp))
- }
- if len(partPurchaseList) > 0 {
- if err := tx.Model(new(model.PartPurchase)).Create(partPurchaseList).Error; err != nil {
- return xerr.WithStack(err)
- }
- }
- bigPartPurchaseId = newBigPartPurchase.Id
- return nil
- })
- }
- // SpecialtyToProcure 特殊申购-自动流转采购单
- func SpecialtyToProcure(id int64) error {
- // 获取特殊申购单信息
- newBigPartPurchase := &model.BigPartPurchase{Id: id}
- if err := s.d.DB.Model(new(model.BigPartPurchase)).First(newBigPartPurchase).Error; err != nil {
- return xerr.WithStack(err)
- }
- // 过滤掉非特殊申购的数据
- if newBigPartPurchase.PurchaseType <= 0 {
- return nil
- }
- newPartPurchase := make([]*model.PartPurchase, 0)
- if err := s.d.DB.Model(new(model.PartPurchase)).Where("bigId = ?", newBigPartPurchase.Id).Find(&newPartPurchase).Error; err != nil {
- return xerr.WithStack(err)
- }
- return s.d.DB.Transaction(func(tx *gorm.DB) error {
- // 获取申购单号
- var orderCode string
- if err := tx.Raw(fmt.Sprintf("CALL createCodeN(%d,'%s')", newBigPartPurchase.PastureId, "CG")).Scan(&orderCode).Error; err != nil {
- return xerr.WithStack(err)
- }
- historyBigBuyDetail := &model.BigBuyDetail{}
- if err := tx.Model(new(model.BigBuyDetail)).Where("buyeCode = ?", orderCode).First(historyBigBuyDetail).Error; err != nil {
- if !errors.Is(err, gorm.ErrRecordNotFound) {
- return xerr.WithStack(err)
- }
- }
- // 避免重复提交
- if historyBigBuyDetail.BuyeCode == orderCode {
- return xerr.Customf("该申购单号已经存在:%s", orderCode)
- }
- newBigBuyDetail := model.NewBigBuyDetail(
- orderCode, newBigPartPurchase.OrderNumber, newBigPartPurchase.ProviderName, newBigPartPurchase.PastureId,
- newBigPartPurchase.ProviderId, newBigPartPurchase.EmployeId, newBigPartPurchase.PurchaseType)
- if err := tx.Model(new(model.BigBuyDetail)).Create(newBigBuyDetail).Error; err != nil {
- return xerr.WithStack(err)
- }
- buyDetailList := make([]*model.BuyDetail, 0)
- var allAmount int64 = 0
- for _, v := range newPartPurchase {
- contractId, _ := strconv.Atoi(v.ContractId)
- //newContract := &model.Contract{}
- //if err := tx.Model(new(model.Contract)).Where("id = ?", contractId).First(newContract).Error; err != nil {
- // fmt.Println("newContract", err)
- //}
- //
- //bigContract := &model.BigContract{}
- //if err := tx.Model(new(model.BigContract)).Where("id = ?", newContract.BigID).First(bigContract).Error; err != nil {
- // fmt.Println("CreateSpecialtyPurchase", err)
- //}
- department := &model.Department{}
- if err := tx.Model(new(model.Department)).Where("pastureId = ?", newBigPartPurchase.PastureId).
- Where("id = ?", newBigPartPurchase.DepartmentId).First(department).Error; err != nil {
- return xerr.WithStack(err)
- }
- price, _ := strconv.ParseFloat(v.Price, 64)
- buyDetailList = append(buyDetailList, model.NewBuyDetail(int64(newBigBuyDetail.ID), int64(newBigBuyDetail.PastureID),
- v.Amount, int64(contractId), v.BrandId, v.PartCode, "", v.Purpose, department.Name, price))
- allAmount += v.Amount
- }
- if err := s.d.DB.Model(new(model.BigBuyDetail)).Where("id = ?", newBigBuyDetail.ID).Updates(map[string]interface{}{
- "zeroCou": allAmount,
- }).Error; err != nil {
- return xerr.WithStack(err)
- }
- if len(buyDetailList) > 0 {
- if err := tx.Model(new(model.BuyDetail)).Create(buyDetailList).Error; err != nil {
- return xerr.WithStack(err)
- }
- }
- return nil
- })
- }
|