레이블이 함수인 게시물을 표시합니다. 모든 게시물 표시
레이블이 함수인 게시물을 표시합니다. 모든 게시물 표시

2024년 6월 27일 목요일

C# - 기본 및 활용

 
//-----------------------------------------------
        [AllowAnonymous]
        public ActionResult 함수_페이지(string reftype = null)
        {
            return View(new Tuple<클래스타입1, 클래스타입2[], 클래스타입3[], 클래스타입4[]>(객체1, 객체2, 객체3, 객체4));
        }
        @using Model.Common
        @model Tuple<클래스타입1, 클래스타입2[], 클래스타입3[], 클래스타입4[]>


//-----------------------------------------------
        [AllowAnonymous, ChildActionOnly]
        public PartialViewResult 함수_Partial()
        {
            return PartialView(new Tuple<Banner>(adBanner));
        }
        @using Model.Common
        @model Tuple<Banner>

        @Html.Action("함수_Partial", "Main")

//-----------------------------------------------
        [HttpPost]
        public JsonResult 함수_기능(int Idx)
        {
            //return Json(new {객체1, 객체2});
            return Json(객체);
        }

//-----------------------------------------------
        [NonAction]
        private object 함수_기능()
        {
            return list.Select(x => new
            {
                PROD_NUM = x.PROD_NUM,
                IDX = x.IDX
            });
        }


//-----------------------------------------------
[FilterConfig.cs]
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    public class UserAuthorizationAttribute : FilterAttribute, IAuthorizationFilter
    {
        public RoleType Role { get; set; }
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            if ((Role & RoleType.Wos) == RoleType.Wos)
            {
                if (!객체.IsWos) // Wos가 아니면
                {
                    throw new Exception("Wos 권한이 없습니다.");
                }
            }
        }
    }

    [Flags]
    public enum RoleType
    {
        Wos
    }


[View1.cs]
        [UserAuthorization(Role = RoleType.Wos)]
        public ActionResult WosCart(string cList)
        {
            ViewBag.Name = Common.객체1.Name;
            return View(Json(cList));
        }



//-----------------------------------------------
        private Dictionary<string, string> GetParameterResultMap(string parameter)
        {
            // + 기호가 공백으로 넘어 오는경우 처리
            string decString = parameter.Replace(" ","+");
            Dictionary<string, string> resultMap = parseStringToMap(decString);
            return resultMap;
        }

        public static System.Collections.Generic.Dictionary<string, string> parseStringToMap(string text)
        {
            System.Collections.Generic.Dictionary<string, string> retMap = new System.Collections.Generic.Dictionary<string, string>();
            if (string.IsNullOrEmpty(text) || string.IsNullOrWhiteSpace(text))
                return retMap;
            string[] arText = text.Split('&');
            for (int i = 0; i < arText.Length; i++)
            {
                string[] arKeyVal = arText[i].Split('=');
                retMap.Add(arKeyVal[0], arKeyVal[1]);
            }
            return retMap;
        }




//-----------------------------------------------
    public enum CancelState : int
    {
        [Description("취소요청")]
        RequestCancel = 0,
        [Description("취소대기")]
        ProcessingCancel = 1,
        [Description("취소불가")]
        ImpossibleCancel = 2,
        [Description("취소완료")]
        CompleteCancel = 3
    }




//-----------------------------------------------
    public class InicisRequest
    {
        #region 기본 데이터 필드
        [Required]
        [StringLength(20)]
        public string version { get; set; }
        [Required]
        [StringLength(10)]
        public string mid { get; set; }
        [Required]
        public int price { get; set; }
        public int tax { get; set; }
        #endregion
    }



2023년 12월 12일 화요일

MSSQL - 영업일 기준 날짜 만들기 (함수)



* 휴일만 임시 테이블로 관리
* 휴일을 수동으로 관리해야 하는 사항이지만 1년에 한번만 적용하면 끝
* startDate 기준으로 interval 값에 따라 (+)앞에 있는 날짜 (-)뒤에 있는 날짜 만들어짐
* 함수로 만들어 DB에서 호출하여 사용 가능



[기준일로 부터 영업일 수 만큼의 날짜 구하기]

-- select [dbo].[MFUNC_GetWorkDay](getdate(), -3)
-- select [dbo].[MFUNC_GetWorkDay]('20231111', 3)

CREATE FUNCTION [dbo].[MFUNC_GetWorkDay]
(
@startDate date,
@interval int
)
RETURNS INT
AS
BEGIN
--Declare @startDate date, @interval int
--SET @startDate = '20230607' --getdate()
--SET @interval = -3

DECLARE @holidayCalendar TABLE (CalendarDate date NULL)
DECLARE @AddDate date

if @interval is null set @interval = 0

-- 휴일만 등록 (23년, 24년)
insert into @holidayCalendar (CalendarDate)
values ('20230101'), ('20230121'), ('20230122'), ('20230123'), ('20230124')
, ('20230301')
, ('20230505'), ('20230527')
, ('20230606')
, ('20230815')
, ('20230928'), ('20230929'), ('20230930')
, ('20231003'), ('20231009')
, ('20231225')
, ('20240101')
, ('20240209'), ('20240210'), ('20240211'), ('20240212')
, ('20240301')
, ('20240410')
, ('20240505'), ('20240506'), ('20240515')
, ('20240606')
, ('20240815')
, ('20240916'), ('20240917'), ('20240918')
, ('20241003'), ('20241009')
, ('20241225')

--select * from @holidayCalendar

Declare @rCnt int, @cCnt int, @flag int
SET @rCnt = 0
SET @cCnt = 0

If @interval >= 0
set @flag = 1
else
set @flag = -1

set @AddDate = @startDate

while abs(@interval) > @rCnt
begin
set @cCnt = @cCnt + 1
set @AddDate = DateAdd(day,  @cCnt * @flag, @startDate)
if (datepart(dw, @AddDate) = 1) 
OR (datepart(dw, @AddDate) = 7)
OR exists(SELECT CalendarDate FROM @holidayCalendar WHERE CalendarDate = @AddDate)
begin
set @interval = @interval + (1 * @flag)
end
set @rCnt = @rCnt + 1
end

RETURN CONVERT(VARCHAR(10), @AddDate, 112)
END





[기간에 해당되는 영일 수 계산]

REATE FUNCTION [dbo].[fn_GetTotalWorkingDays]
(
    @DateFrom Date,
    @DateTo Date
)
RETURNS INT
AS
BEGIN
    DECLARE @TotDays INT = DATEDIFF(DAY, @DateFrom, @DateTo) + 1;
    DECLARE @TotWeeks INT = DATEDIFF(WEEK, @DateFrom, @DateTo) * 2;
    DECLARE @IsSunday INT = CASE WHEN DATENAME(WEEKDAY, @DateFrom) = 'Sunday' THEN 1 ELSE 0 END;
    DECLARE @IsSaturday INT = CASE WHEN DATENAME(WEEKDAY, @DateTo) = 'Saturday' THEN 1 ELSE 0 END;

    DECLARE @TotWorkingDays INT = @TotDays - @TotWeeks - @IsSunday + @IsSaturday;
    RETURN @TotWorkingDays;
END

GO

MSSQL - Cursor vs Temp Table

#테이블 변수사용의 예 use pubs go declare @tmptable table (     nid int identity(1,1) not null,     title varchar (80) not null ) -- 테이블 변수 선언 inse...