fnc_curr_reward_bal.sql 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. drop procedure if exists fnc_curr_reward_bal;
  2. DELIMITER //
  3. create procedure fnc_curr_reward_bal( _curr_date varchar(8), _branch int, _target int)
  4. BEGIN
  5. Declare _bal_date varchar(8);
  6. Declare _last_amt decimal(20,4);
  7. Declare _sum_amt decimal(20,4);
  8. Declare _avail_bal decimal(20,4);
  9. Declare _tmp_int int;
  10. -- setup 변수들 --
  11. Declare _setup text;
  12. Declare _reserve int;
  13. Declare _min_use int;
  14. if _is_first_date(_curr_date) then
  15. set _bal_date = '19000101';
  16. else
  17. set _bal_date = _curr_date;
  18. end if;
  19. -- 아래것 작동됨 ---- https://dololak.tistory.com/257
  20. -- Json Data 가져오기
  21. SELECT json_extract(setup_json, '$.ReservePoint'), json_extract(setup_json, '$.MinUsePoint')
  22. into _reserve, _min_use
  23. from dbr_setup
  24. where setup_code = 'user-reward';
  25. -- 전월 잔액
  26. select bal_amt into _last_amt
  27. from dbr_bal_reward
  28. where buyer_id = 10
  29. and yyyy_mm = _prev_month(_curr_date) and branch_id = _branch;
  30. -- 당월 변동잔액
  31. select sum(reward_amt*reward_status) into _sum_amt
  32. from dbr_reward_bd
  33. inner join dbr_deal_type as deal on deal.id = deal_type_id
  34. where buyer_id = _target
  35. and avail_date between _first_date(_bal_date) and _bal_date
  36. and branch_id = _branch
  37. group by buyer_id;
  38. -- 보관적립금, 최소사용적립금 계산식 --
  39. set _avail_bal = cast( ( (_last_amt - _reserve) / _min_use) as int) * _min_use;
  40. if _avail_bal < 0 then
  41. set _avail_bal = '0.0000';
  42. end if;
  43. select _last_amt as c1, _avail_bal as c2;
  44. END;
  45. //
  46. DELIMITER ;
  47. call fnc_curr_reward_bal('20230227', 1, 10) ;