public DataResult GetFaultDeviceDeleteLog([FromQuery] DateTime startTime, DateTime endTime, int condition,
                                                  string code, DateTime eStartTime, DateTime eEndTime, int qId, string maintainer = "-1", int faultType = -1, int priority = -1, int grade = -1, int state = -1)
        {
            var faults = FaultDeviceHelper.GetDeleteFaultDeviceDetails(startTime, endTime, condition,
                                                                       code, eStartTime, eEndTime, qId, maintainer, faultType, priority, grade, state);
            var result = new DataResult();

            result.datas.AddRange(faults.OrderByDescending(x => x.FaultTime).ThenByDescending(x => x.DeviceCode));
            return(result);
        }
        public Result PutFaultDevice([FromBody] List <FaultDevice> faultDevices)
        {
            var ids             = faultDevices.Select(x => x.Id);
            var oldFaultDevices = FaultDeviceHelper.GetDetails(ids);

            if (oldFaultDevices.Count() != faultDevices.Count)
            {
                return(Result.GenError <Result>(Error.FaultDeviceNotExist));
            }
            var markedDateTime = DateTime.Now;

            foreach (var faultDevice in faultDevices)
            {
                faultDevice.MarkedDateTime = markedDateTime;
            }
            FaultDeviceHelper.Instance.Update <FaultDevice>(faultDevices);
            return(Result.GenError <Result>(Error.Success));
        }
        public Result DeleteFaultDevice([FromBody] BatchDelete batchDelete)
        {
            var ids  = batchDelete.ids;
            var data =
                ServerConfig.ApiDb.Query <FaultDevice>("SELECT * FROM `fault_device_repair` WHERE Id IN @id AND `State` != @state AND MarkedDelete = 0;",
                                                       new { id = ids, state = RepairStateEnum.Complete });

            if (data.Count() != ids.Count())
            {
                return(Result.GenError <Result>(Error.FaultDeviceNotExist));
            }

            FaultDeviceHelper.Cancel(ids);
            foreach (var d in data.GroupBy(x => x.FaultTime).Select(x => x.Key))
            {
                AnalysisHelper.FaultCal(d);
            }
            return(Result.GenError <Result>(Error.Success));
        }
        public Result AssignFaultDevice([FromBody] List <FaultDevice> faultDevices)
        {
            var ids             = faultDevices.Select(x => x.Id);
            var oldFaultDevices = FaultDeviceHelper.GetDetails(ids);

            if (oldFaultDevices.Count() != faultDevices.Count)
            {
                return(Result.GenError <Result>(Error.FaultDeviceNotExist));
            }

            var maintainerAccounts = faultDevices.SelectMany(x => x.Maintainers).GroupBy(y => y).Select(z => z.Key);
            var maintainers        =
                ServerConfig.ApiDb.Query <Maintainer>("SELECT * FROM `maintainer` WHERE Account IN @Account AND `MarkedDelete` = 0;", new { Account = maintainerAccounts });

            if (maintainers.Count() != maintainerAccounts.Count())
            {
                return(Result.GenError <Result>(Error.MaintainerNotExist));
            }

            var time = DateTime.Now;

            foreach (var faultDevice in faultDevices)
            {
                faultDevice.AssignTime = time;
            }
            ServerConfig.ApiDb.Execute(
                "UPDATE fault_device_repair SET `AssignTime` = @AssignTime, `Maintainer` = @Maintainer, `Priority` = @Priority, `Grade` = @Grade WHERE `Id` = @Id;", faultDevices);

            var faultTypes =
                ServerConfig.ApiDb.Query <FaultType>("SELECT Id, FaultTypeName FROM `fault_type` WHERE Id IN @Id;", new { Id = oldFaultDevices.Where(y => faultDevices.Any(z => z.Id == y.Id)).Select(x => x.FaultTypeId) });

            foreach (var faultDevice in oldFaultDevices)
            {
                var assignors = faultDevices.First(x => x.Id == faultDevice.Id).Maintainers;
                var atMobiles = maintainers.Where(x => assignors.Contains(x.Account)).Where(y => !y.Phone.IsNullOrEmpty()).Select(z => z.Phone).ToArray();
                var faultType = faultTypes.First(x => x.Id == faultDevice.FaultTypeId).FaultTypeName ?? "";
                faultDevice.FaultTypeName = faultType;
                var content = NotifyFormat.Format(NotifyMsgEnum.FaultAssign, faultDevice);
                HNotifyHelper.NotifyChat(NotifyTypeEnum.Repair, NotifyMsgEnum.FaultAssign, content, NotifyMsgTypeEnum.text, atMobiles);
            }
            return(Result.GenError <Result>(Error.Success));
        }
        public Result RepairFaultDevice([FromBody] List <RepairRecord> faultDevices)
        {
            var ids             = faultDevices.Select(x => x.Id);
            var oldFaultDevices = FaultDeviceHelper.GetDetails(ids).Select(ClassExtension.ParentCopyToChild <FaultDevice, RepairRecord>);

            if (oldFaultDevices.Count() != faultDevices.Count)
            {
                return(Result.GenError <Result>(Error.FaultDeviceNotExist));
            }

            var states = faultDevices.GroupBy(x => x.State).Select(y => y.Key);

            if (states.Any(x => x != RepairStateEnum.Confirm && x != RepairStateEnum.Repair && x != RepairStateEnum.Complete))
            {
                return(Result.GenError <Result>(Error.ParamError));
            }

            var stateDictionary = new Dictionary <RepairStateEnum, List <RepairStateEnum> >
            {
                { RepairStateEnum.Confirm, new List <RepairStateEnum> {
                      RepairStateEnum.Default
                  } },
                { RepairStateEnum.Repair, new List <RepairStateEnum> {
                      RepairStateEnum.Default, RepairStateEnum.Confirm
                  } },
                { RepairStateEnum.Complete, new List <RepairStateEnum> {
                      RepairStateEnum.Repair
                  } },
            };

            var have = false;

            foreach (var state in stateDictionary)
            {
                if (states.All(x => x == state.Key))
                {
                    have = true;
                    if (oldFaultDevices.Any(x => !state.Value.Contains(x.State)))
                    {
                        return(Result.GenError <Result>(Error.FaultDeviceStateError));
                    }
                }
            }

            if (!have)
            {
                return(Result.GenError <Result>(Error.FaultDeviceStateError));
            }

            var markedDateTime = DateTime.Now;
            var info           = Request.GetIdentityInformation();

            foreach (var faultDevice in faultDevices)
            {
                var old = oldFaultDevices.FirstOrDefault(x => x.Id == faultDevice.Id);
                if (!old.Maintainers.Contains(info))
                {
                    return(Result.GenError <Result>(Error.FaultDeviceRepairMaintainerError));
                }

                if (faultDevice.State == RepairStateEnum.Confirm || faultDevice.State == RepairStateEnum.Repair && faultDevice.StartTime == default(DateTime))
                {
                    faultDevice.StartTime = markedDateTime;
                }
                faultDevice.MarkedDateTime = markedDateTime;
                faultDevice.Remark         = faultDevice.Remark ?? old.Remark;
                faultDevice.FaultSolver    = faultDevice.FaultSolver ?? old.FaultSolver;
                faultDevice.SolvePlan      = faultDevice.SolvePlan ?? old.SolvePlan;
                faultDevice.FaultTypeId1   = faultDevice.FaultTypeId1 == 0 ? old.FaultTypeId1 : faultDevice.FaultTypeId1;
            }
            RepairRecordHelper.Instance.Update <RepairRecord>(faultDevices);
            return(Result.GenError <Result>(Error.Success));
        }