MaintainJob.cs 9.71 KB
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.WebEntity.equipment;
using System;
using System.Linq;

namespace Quartz.Job.Jobs
{
    /// <summary>
    /// 保养规则生成
    /// </summary>
    /// <remarks>待完善</remarks>
    public class MaintainJob : JobBase
    {
        public override void ExecuteJob(IJobExecutionContext context)
        {
            var equipments = _dbContext.Queryable<base_equipment>().Where(x => x.isEnable).ToList();
            //保养规则
            var maintainRuleCodes = equipments.Select(x => x.equipmentMaintainRuleCode).Distinct().ToList();
            var busEquipmentMaintainRuleHeads = _dbContext.Queryable<bus_equipment_maintain_rule_head>().Where(x => maintainRuleCodes.Contains(x.equipmentRuleCode)).ToList();
            var ruleHeadKeys = busEquipmentMaintainRuleHeads.Select(x => x.keys).ToList();
            var busEquipmentMaintainRuleDetails = _dbContext.Queryable<bus_equipment_maintain_rule_detail>().Where(x => ruleHeadKeys.Contains(x
                .headKeys)).ToList();
            //设备部件
            var equipmentTypeCodes = equipments.Select(x => x.equipmentTypeCode).Distinct().ToList();
            var equipmentParts = _dbContext.Queryable<base_equipment_part>().Where(x => equipmentTypeCodes.Contains(x.equipmentTypeCode)).ToList();

            foreach (var equipment in equipments)
            {
                var ruleHead = busEquipmentMaintainRuleHeads.Where(x => x.equipmentRuleCode == equipment.equipmentMaintainRuleCode).First();
                //未匹配到维护规则,则跳过
                if (ruleHead == null)
                {
                    continue;
                }
                var ruleDetails = busEquipmentMaintainRuleDetails.Where(x => x.headKeys == ruleHead.keys).ToList();
                if (ruleDetails.Count == 0)
                {
                    continue;
                }

                var currentEquipmentParts = equipmentParts.Where(x => x.equipmentTypeCode == equipment.equipmentTypeCode).ToList();

                var busEquipmentMaintainRecordTop = _dbContext.Queryable<bus_equipment_maintain_record_top>().Where(x => x.equipmentCode == equipment.equipmentCode).First();
                if (busEquipmentMaintainRecordTop == null)
                {
                    busEquipmentMaintainRecordTop = new bus_equipment_maintain_record_top
                    {
                        keys = Guid.NewGuid(),
                        equipmentCode = equipment.equipmentCode,
                        equipmentName = equipment.equipmentName,
                        equipmentTypeCode = equipment.equipmentTypeCode,
                        factoryCode = equipment.factoryCode,
                        projectKeys = equipment.projectKeys,
                        createTime = DateTime.Now
                    };
                    busEquipmentMaintainRecordTop.id = _dbContext.Insertable(busEquipmentMaintainRecordTop).ExecuteReturnIdentity();
                }

                foreach (var equipmentPart in currentEquipmentParts)
                {
                    var busEquipmentMaintainRecordHead = _dbContext.Queryable<bus_equipment_maintain_record_head>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys && x.partCode == equipmentPart.partCode).First();
                    if (busEquipmentMaintainRecordHead == null)
                    {
                        busEquipmentMaintainRecordHead = new bus_equipment_maintain_record_head
                        {
                            keys = Guid.NewGuid(),
                            topKeys = busEquipmentMaintainRecordTop.keys,
                            equipmentCode = equipment.equipmentCode,
                            equipmentTypeCode = equipment.equipmentTypeCode,
                            factoryCode = equipment.factoryCode,
                            projectKeys = equipment.projectKeys,
                            partCode = equipmentPart.partCode,
                            partName = equipmentPart.partName,
                            lastMaintainTime = DateTime.Now,
                            createTime = DateTime.Now,
                        };
                        busEquipmentMaintainRecordHead.id = _dbContext.Insertable(busEquipmentMaintainRecordHead).ExecuteReturnIdentity();
                    }

                    //当前部件的保养规则
                    var currentRuleDetail = ruleDetails.Where(x => x.equipmentPartCode == equipmentPart.partCode).FirstOrDefault();
                    if (currentRuleDetail == null)
                    {
                        //当前部件未配置保养规则,则跳过
                        continue;
                    }

                    //保养周期类型未设定,则跳过
                    if (string.IsNullOrWhiteSpace(currentRuleDetail.indicatorType))
                    {
                        continue;
                    }

                    var indicatorTypes = currentRuleDetail.indicatorType.Split(',');
                    var indicator = currentRuleDetail.indicator;

                    //解析保养规则,生成保养记录,待完善
                    var busEquipmentMaintainRecordDetail = _dbContext.Queryable<bus_equipment_maintain_record_detail>().Where(x => x.headKeys == busEquipmentMaintainRecordHead.keys && x.equipmentCode == equipment.equipmentCode && x.partCode == equipmentPart.partCode && x.maintainTime == null).First();
                    //无记录,则需要新增
                    if (busEquipmentMaintainRecordDetail == null)
                    {
                        busEquipmentMaintainRecordDetail = new bus_equipment_maintain_record_detail
                        {
                            topKeys = busEquipmentMaintainRecordTop.keys,
                            headKeys = busEquipmentMaintainRecordHead.keys,
                            equipmentCode = equipment.equipmentCode,
                            equipmentTypeCode = equipment.equipmentTypeCode,
                            projectKeys = equipment.projectKeys,
                            factoryCode = equipment.factoryCode,
                            partCode = equipmentPart.partCode,
                            partName = equipmentPart.partName,
                            maintainPrice = currentRuleDetail.price,
                        };
                    }

                    //根据类型来生成记录
                    foreach (var indicatorType in indicatorTypes)
                    {
                        if (indicatorType == "类型1")
                        {
                            _dbContext.Insertable(busEquipmentMaintainRecordDetail).ExecuteCommand();
                            break;
                        }

                        if (indicatorType == "类型2")
                        {
                            _dbContext.Insertable(busEquipmentMaintainRecordDetail).ExecuteCommand();
                            break;
                        }
                    }

                    //更新Head表
                    busEquipmentMaintainRecordHead.sumPrice = _dbContext.Queryable<bus_equipment_maintain_record_detail>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys && x.headKeys == busEquipmentMaintainRecordHead.keys && x.maintainTime != null).Sum(x => x.maintainPrice);

                    var lastMaintainTime = _dbContext.Queryable<bus_equipment_maintain_record_detail>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys && x.headKeys == busEquipmentMaintainRecordHead.keys && x.maintainTime != null).Max(x => x.maintainTime) ?? DateTime.Now;

                    var nextMaintainTime = DateTime.Now;//待完善
                    busEquipmentMaintainRecordHead.lastMaintainTime = lastMaintainTime;
                    busEquipmentMaintainRecordHead.nextMaintainTime = nextMaintainTime;
                    busEquipmentMaintainRecordHead.nextMaintianDate = (nextMaintainTime - busEquipmentMaintainRecordHead.lastMaintainTime).Days;
                    busEquipmentMaintainRecordHead.updateTime = DateTime.Now;
                    _dbContext.Updateable(busEquipmentMaintainRecordHead).UpdateColumns(x => new
                    {
                        x.sumPrice,
                        x.nextMaintianDate,
                        x.lastMaintainTime,
                        x.nextMaintainTime,
                        x.status,
                        x.updateTime
                    }).ExecuteCommand();
                }

                //更新概述表
                busEquipmentMaintainRecordTop.maintainNumber = _dbContext.Queryable<bus_equipment_maintain_record_detail>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys && x.maintainTime != null).Count();
                busEquipmentMaintainRecordTop.toBeMaintainedNumber = _dbContext.Queryable<bus_equipment_maintain_record_detail>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys && x.maintainTime == null).Count();
                busEquipmentMaintainRecordTop.lastMaintainTime = _dbContext.Queryable<bus_equipment_maintain_record_head>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys).Max(x => x.lastMaintainTime);
                busEquipmentMaintainRecordTop.sumAmount = _dbContext.Queryable<bus_equipment_maintain_record_detail>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys && x.maintainTime != null).Sum(x => x.maintainPrice);
                busEquipmentMaintainRecordTop.updateTime = DateTime.Now;
                _dbContext.Updateable(busEquipmentMaintainRecordTop).UpdateColumns(x => new
                {
                    x.maintainNumber,
                    x.toBeMaintainedNumber,
                    x.lastMaintainTime,
                    x.sumAmount,
                    x.updateTime
                }).ExecuteCommand();
            }
        }
    }
}