public Result PutSpotCheckPlan([FromRoute] int id, [FromBody] SpotCheckPlan spotCheckPlan)
        {
            if (spotCheckPlan.Plan.IsNullOrEmpty())
            {
                return(Result.GenError <Result>(Error.SpotCheckPlanNotEmpty));
            }
            var data =
                ServerConfig.ApiDb.Query <SpotCheckPlan>("SELECT * FROM `spot_check_plan` WHERE Id = @id AND MarkedDelete = 0;", new { id }).FirstOrDefault();

            if (data == null)
            {
                return(Result.GenError <Result>(Error.SpotCheckPlanNotExist));
            }

            var cnt =
                ServerConfig.ApiDb.Query <int>("SELECT COUNT(1) FROM `spot_check_plan` WHERE Id != @id AND Plan = @Plan AND MarkedDelete = 0;", new { id, spotCheckPlan.Plan }).FirstOrDefault();

            if (cnt > 0)
            {
                return(Result.GenError <Result>(Error.SpotCheckPlanIsExist));
            }

            spotCheckPlan.Id             = id;
            spotCheckPlan.MarkedDateTime = DateTime.Now;
            ServerConfig.ApiDb.Execute(
                "UPDATE spot_check_plan SET `MarkedDateTime` = @MarkedDateTime, `Plan` = @Plan WHERE `Id` = @Id;", spotCheckPlan);

            return(Result.GenError <Result>(Error.Success));
        }
        public Result PostSpotCheckPlan([FromBody] SpotCheckPlan spotCheckPlan)
        {
            if (spotCheckPlan.Plan.IsNullOrEmpty())
            {
                return(Result.GenError <Result>(Error.SpotCheckPlanNotEmpty));
            }
            var cnt =
                ServerConfig.ApiDb.Query <int>("SELECT COUNT(1) FROM `spot_check_plan` WHERE Plan = @Plan AND MarkedDelete = 0;", new { spotCheckPlan.Plan }).FirstOrDefault();

            if (cnt > 0)
            {
                return(Result.GenError <Result>(Error.SpotCheckPlanIsExist));
            }

            if (spotCheckPlan.SpotCheckItems != null && spotCheckPlan.SpotCheckItems.Count() != spotCheckPlan.SpotCheckItems.GroupBy(x => x.Item).Count())
            {
                return(Result.GenError <Result>(Error.SpotCheckItemDuplicate));
            }

            if (spotCheckPlan.SpotCheckItems != null && spotCheckPlan.SpotCheckItems.Any(x => x.Item.IsNullOrEmpty()))
            {
                return(Result.GenError <Result>(Error.SpotCheckItemNotEmpty));
            }

            spotCheckPlan.CreateUserId   = Request.GetIdentityInformation();
            spotCheckPlan.MarkedDateTime = DateTime.Now;
            var id = ServerConfig.ApiDb.Query <int>(
                "INSERT INTO spot_check_plan (`CreateUserId`, `MarkedDateTime`, `Plan`) VALUES (@CreateUserId, @MarkedDateTime, @Plan);SELECT LAST_INSERT_ID();",
                spotCheckPlan).FirstOrDefault();

            if (spotCheckPlan.SpotCheckItems != null)
            {
                foreach (var spotCheckItem in spotCheckPlan.SpotCheckItems)
                {
                    spotCheckItem.PlanId         = id;
                    spotCheckItem.CreateUserId   = Request.GetIdentityInformation();
                    spotCheckItem.MarkedDateTime = DateTime.Now;
                }
                ServerConfig.ApiDb.Execute(
                    "INSERT INTO spot_check_item (`CreateUserId`, `MarkedDateTime`, `Item`, `PlanId`, `Enable`, `Remind`, `Min`, `Max`, `Unit`, `Reference`, `Remarks`, `Interval`, `Day`, `Month`, `NormalHour`, `Week`, `WeekHour`) " +
                    "VALUES (@CreateUserId, @MarkedDateTime, @Item, @PlanId, @Enable, @Remind, @Min, @Max, @Unit, @Reference, @Remarks, @Interval, @Day, @Month, @NormalHour, @Week, @WeekHour);",
                    spotCheckPlan.SpotCheckItems);
            }

            return(Result.GenError <Result>(Error.Success));
        }