コード例 #1
0
        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));
        }
コード例 #2
0
        public Result PostFaultDevice([FromBody] List <FaultDeviceDetail> faultDevices)
        {
            IEnumerable <DeviceDetail> devices = null;

            if (faultDevices.Any(x => x.DeviceId == 0))
            {
                var cnt =
                    ServerConfig.ApiDb.Query <int>("SELECT COUNT(1) FROM `device` WHERE `Code` IN @Code AND `MarkedDelete` = 0;",
                                                   new { Code = faultDevices.Where(x => x.DeviceId == 0).Select(y => y.DeviceCode) }).FirstOrDefault();
                if (cnt > 0)
                {
                    return(Result.GenError <Result>(Error.ReportDeviceCodeIsExist));
                }
            }
            else
            {
                devices = ServerConfig.ApiDb.Query <DeviceDetail>("SELECT a.*, IFNULL(b.Phone, '') Phone FROM `device` a JOIN maintainer b ON a.Administrator = b.Account WHERE a.Id IN @DeviceId AND a.MarkedDelete = 0 AND b.MarkedDelete = 0;",
                                                                  new { DeviceId = faultDevices.Select(x => x.DeviceId) });
            }

            var createUserId = Request.GetIdentityInformation();
            var now          = DateTime.Now;

            foreach (var faultDevice in faultDevices)
            {
                faultDevice.CreateUserId   = createUserId;
                faultDevice.MarkedDateTime = now;
                faultDevice.IsReport       = true;

                faultDevice.Administrator = "";
                faultDevice.Maintainer    = "";
                faultDevice.Images        = faultDevice.Images.IsNullOrEmpty() ? "[]" : faultDevice.Images;
                if (devices != null)
                {
                    faultDevice.DeviceCode    = devices.FirstOrDefault(x => x.Id == faultDevice.DeviceId)?.Code ?? (faultDevice.DeviceCode ?? "");
                    faultDevice.Administrator = devices.FirstOrDefault(x => x.Id == faultDevice.DeviceId)?.Admin ?? "";
                    faultDevice.Maintainer    = devices.FirstOrDefault(x => x.Id == faultDevice.DeviceId)?.Admin ?? "";
                    faultDevice.Phone         = devices.FirstOrDefault(x => x.Id == faultDevice.DeviceId)?.Phone ?? "";
                }
            }

            FaultDeviceHelper.Instance.Add(faultDevices);

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

            foreach (var faultDevice in faultDevices)
            {
                var atMobiles = new string[] { };
                if (!faultDevice.Phone.IsNullOrEmpty())
                {
                    atMobiles = new[] { faultDevice.Phone };
                }

                var faultType = faultTypes.First(x => x.Id == faultDevice.FaultTypeId).FaultTypeName ?? "";
                faultDevice.FaultTypeName = faultType;
                var content = NotifyFormat.Format(NotifyMsgEnum.FaultReport, faultDevice);
                HNotifyHelper.NotifyChat(NotifyTypeEnum.Repair, NotifyMsgEnum.FaultReport, content, NotifyMsgTypeEnum.markdown, atMobiles);
            }
            return(Result.GenError <Result>(Error.Success));
        }