初始
This commit is contained in:
		| @ -0,0 +1,173 @@ | ||||
| package logic | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"github.com/gogf/gf/v2/frame/g" | ||||
| 	"github.com/gogf/gf/v2/util/gconv" | ||||
| 	"github.com/tiger1103/gfast/v3/api/v1/system" | ||||
| 	"github.com/tiger1103/gfast/v3/internal/app/system/dao" | ||||
| 	"github.com/tiger1103/gfast/v3/internal/app/system/model" | ||||
| 	"github.com/tiger1103/gfast/v3/internal/app/system/model/do" | ||||
| 	"github.com/tiger1103/gfast/v3/internal/app/system/service" | ||||
| 	"github.com/tiger1103/gfast/v3/library/liberr" | ||||
| 	"math" | ||||
| ) | ||||
|  | ||||
| func init() { | ||||
| 	service.RegisterBusEquipmentMaterialsExcel(New()) | ||||
| } | ||||
|  | ||||
| func New() *sBusEquipmentMaterialsExcel { | ||||
| 	return &sBusEquipmentMaterialsExcel{} | ||||
| } | ||||
|  | ||||
| type sBusEquipmentMaterialsExcel struct{} | ||||
|  | ||||
| // 首页材料展示 | ||||
| func (s *sBusEquipmentMaterialsExcel) Index(ctx context.Context, req *system.BusEquipmentMaterialsExcelIndexReq) (listRes *system.BusEquipmentMaterialsExcelIndexRes, err error) { | ||||
| 	listRes = new(system.BusEquipmentMaterialsExcelIndexRes) | ||||
| 	var res []*model.BusEquimentIndexRes | ||||
| 	err = g.Try(ctx, func(ctx context.Context) { | ||||
| 		m := dao.BusEquipmentMaterialsInventory.Ctx(ctx) | ||||
| 		sql := `SELECT  | ||||
|     equipmentMaterialsName, | ||||
|     weightId, | ||||
|     SUM(EstimatedMaterialQuantity) AS quantityCount, | ||||
|     SUM(OutputQuantity) AS outboundNumber, | ||||
|     SUM(InputQuantity) AS inventoryNumber | ||||
| FROM ( | ||||
|     SELECT  | ||||
|         bem.equipment_materials_name AS equipmentMaterialsName, | ||||
|         bem.weight_id AS weightId, | ||||
|         bem.quantity_count AS EstimatedMaterialQuantity, | ||||
|         SUM(CASE WHEN bemi.out_put = '1' THEN bemi.number ELSE 0 END) AS OutputQuantity, | ||||
|         SUM(CASE WHEN bemi.out_put = '2' THEN bemi.number ELSE 0 END) AS InputQuantity | ||||
|     FROM  | ||||
|         bus_equipment_materials bem | ||||
|     JOIN  | ||||
|         bus_equipment_materials_inventory bemi ON bem.equipment_materials_id = bemi.equipment_materials_id | ||||
|     JOIN  | ||||
|         sys_project sp on    bem.project_id = sp.id | ||||
|     WHERE  | ||||
|        bem.project_id = ? AND bem.deleted_at IS NULL AND bemi.deleted_at IS NULL AND  sp.show_hidden = 1 | ||||
|     GROUP BY  | ||||
|         bem.equipment_materials_id, bem.equipment_materials_name, bem.weight_id | ||||
| ) AS SubQuery | ||||
| GROUP BY  | ||||
|     equipmentMaterialsName, weightId;` | ||||
| 		m.Raw(sql, req.ProjectId).Scan(&res) | ||||
| 		listRes.List = res | ||||
| 	}) | ||||
|  | ||||
| 	return | ||||
| } | ||||
|  | ||||
| func (s *sBusEquipmentMaterialsExcel) List(ctx context.Context, req *system.BusEquipmentMaterialsExcelSearchReq) (listRes *system.BusEquipmentMaterialsExcelSearchRes, err error) { | ||||
| 	listRes = new(system.BusEquipmentMaterialsExcelSearchRes) | ||||
| 	err = g.Try(ctx, func(ctx context.Context) { | ||||
| 		// 先去查询所有的设备ID | ||||
| 		var ids []int | ||||
| 		m := dao.BusEquipmentMaterials.Ctx(ctx) | ||||
| 		if req.ProjectId != 0 { | ||||
| 			m = m.Where(dao.BusEquipmentMaterials.Columns().ProjectId, req.ProjectId) | ||||
| 		} | ||||
| 		if req.EquipmentMaterialsName != "" { | ||||
| 			m = m.Where("equipment_materials_name like ?", "%"+req.EquipmentMaterialsName+"%") | ||||
| 		} | ||||
| 		Allids, err := m.Fields("equipment_materials_id As id").Array() | ||||
| 		ids = gconv.Ints(Allids) | ||||
| 		liberr.ErrIsNil(ctx, err, "获取设备材料ID失败") | ||||
| 		var list []*model.BusEquimentRes | ||||
| 		// 根据id 查询材料设备表 和材料设备入库表的的对应字段 根据名字 更新时间排序 | ||||
| 		dao.BusEquipmentMaterialsInventory.Ctx(ctx).As("bmi"). | ||||
| 			RightJoin("bus_equipment_materials em ON em.equipment_materials_id = bmi.equipment_materials_id"). | ||||
| 			Where("bmi.equipment_materials_id in (?) ", ids). | ||||
| 			Fields("ROW_NUMBER() OVER (ORDER BY em.equipment_materials_name) AS 'Serial',em.type_specification_name AS 'typeSpecificationName', bmi.id AS 'id'," + | ||||
| 				"em.equipment_materials_name AS 'EquipmentMaterialsName', em.weight_id AS 'WeightId',em.quantity_count AS 'QuantityCount',bmi.out_put AS 'OutPut',bmi.number AS 'Number', bmi.operator AS 'Operator'," + | ||||
| 				"bmi.out_put_time AS 'OutPutTime',bmi.updated_at AS 'UpdateAt', bmi.disposition AS 'Disposition',bmi.residue AS 'Residue',bmi.recipient AS 'Recipient' ,bmi.shipper AS 'Shipper'"). | ||||
| 			Order("em.equipment_materials_name asc,bmi.out_put_time asc ,bmi.id asc"). | ||||
| 			Scan(&list) | ||||
| 		liberr.ErrIsNil(ctx, err, "获取材料id失败") | ||||
| 		//序列号 | ||||
| 		var x int | ||||
| 		//获取总条数 插入 出库入库结构体 | ||||
| 		listRes.List = make([]*model.BusEquipmentMaterialsExcelListRes, len(list)) | ||||
| 		for k, v := range list { | ||||
| 			x++ | ||||
| 			listRes.List[k] = &model.BusEquipmentMaterialsExcelListRes{ | ||||
| 				Id:                     v.Id, | ||||
| 				SerialNumber:           x, | ||||
| 				EquipmentMaterialsName: v.EquipmentMaterialsName, | ||||
| 				TypeSpecificationName:  v.TypeSpecificationName, | ||||
| 				WeightId:               v.WeightId, | ||||
| 				QuantityCount:          v.QuantityCount, | ||||
| 				Inventory:              &model.Inventory{}, | ||||
| 				Outbound:               &model.Outbound{}, | ||||
| 				Residue:                v.Residue, | ||||
| 				Disposition:            v.Disposition, | ||||
| 			} | ||||
| 			switch v.Output { | ||||
| 			//1是出库 | ||||
| 			case "1": | ||||
| 				Outbound := model.Outbound{ | ||||
| 					Number:     v.Number, | ||||
| 					Operator:   v.Operator, | ||||
| 					Recipient:  v.Recipient, | ||||
| 					Shipper:    v.Shipper, | ||||
| 					OutPutTime: v.OutputTime} | ||||
| 				listRes.List[k].Outbound = &Outbound | ||||
| 			//2是入库 | ||||
| 			case "2": | ||||
| 				Inventory := model.Inventory{ | ||||
| 					Number:     v.Number, | ||||
| 					Operator:   v.Operator, | ||||
| 					OutPutTime: v.OutputTime} | ||||
| 				listRes.List[k].Inventory = &Inventory | ||||
| 			} | ||||
|  | ||||
| 		} | ||||
| 		// 参数分页 如果pagenum为0返回项目所有数据 excel导出 | ||||
| 		if req.PageNum > 0 { | ||||
| 			list, total, totalPages := paginate(listRes.List, req.PageNum, req.PageSize) | ||||
| 			listRes.CurrentPage = totalPages | ||||
| 			listRes.Total = total | ||||
| 			listRes.List = list | ||||
| 		} else { | ||||
| 			listRes.Total = len(listRes.List) | ||||
| 		} | ||||
|  | ||||
| 	}) | ||||
| 	return | ||||
| } | ||||
|  | ||||
| // 数据分页 | ||||
| func paginate(listRes []*model.BusEquipmentMaterialsExcelListRes, pagenum int, pagesize int) ([]*model.BusEquipmentMaterialsExcelListRes, int, int) { | ||||
| 	total := len(listRes) | ||||
| 	//总页数 | ||||
| 	totalPages := int(math.Ceil(float64(total) / float64(pagesize))) | ||||
| 	// 检查页码是否有效 | ||||
| 	if pagenum <= 0 || pagenum > totalPages { | ||||
| 		pagenum = 1 // 默认第一页 | ||||
| 	} | ||||
|  | ||||
| 	start := (pagenum - 1) * pagesize | ||||
| 	end := start + pagesize | ||||
|  | ||||
| 	if start >= total { | ||||
| 		return []*model.BusEquipmentMaterialsExcelListRes{}, 0, 0 // 当前页码超过总页数,返回空数组 | ||||
| 	} | ||||
| 	if end > total { | ||||
| 		end = total | ||||
| 	} | ||||
| 	return listRes[start:end], total, totalPages | ||||
| } | ||||
|  | ||||
| func (s *sBusEquipmentMaterialsExcel) Edit(ctx context.Context, req *system.BusEquipmentMaterialsExcelEditReq) (err error) { | ||||
| 	err = g.Try(ctx, func(ctx context.Context) { | ||||
| 		_, err = dao.BusEquipmentMaterialsInventory.Ctx(ctx).WherePri(req.Id).Update(do.BusEquipmentMaterialsInventory{ | ||||
| 			Disposition: req.Disposition, | ||||
| 		}) | ||||
| 		liberr.ErrIsNil(ctx, err, "修改失败!") | ||||
| 	}) | ||||
| 	return | ||||
| } | ||||
		Reference in New Issue
	
	Block a user