reward_bd_act.sql 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. drop procedure if exists reward_bd_act;
  2. DELIMITER //
  3. create procedure reward_bd_act( _slip_code varchar(6), _target int, _act int)
  4. _procedure: SQL SECURITY INVOKER
  5. BEGIN
  6. Declare _created_on bigint ;
  7. Declare _updated_on bigint ;
  8. Declare _created_id int ;
  9. Declare _hd_id int(11) defauLt 0;
  10. Declare _bd_id int(11) defauLt 0;
  11. Declare _reward_id int(11) default 0;
  12. Declare _sorder_date varchar(8);
  13. Declare _avail_date varchar(8);
  14. Declare _branch_id int ;
  15. Declare _user_id int ;
  16. Declare _deal_type_id int;
  17. Declare _sorder_sum decimal(20,4);
  18. Declare _reward_rate decimal(4,2);
  19. Declare _reward_amt decimal(20,4);
  20. Declare _add_msg varchar(49);
  21. Declare _from_buyer_id int;
  22. Declare _to_buyer_id int;
  23. Declare _is_transfer char(1);
  24. Declare _deal_sordbd int(11) default 55; -- 적립금 발생
  25. Declare _deal_sorder int(11) default 56; -- 적립금 사용
  26. Declare _deal_reward int(11) default 57; -- 적립금 변경
  27. if _slip_code = 'sordbd' then -- 적립금 발생
  28. select sobd.id, sorder_date, date_format(date_add(_sorder_date, interval 1 day), '%Y%m%d'),
  29. branch_id, sohd.user_id, _deal_sordbd,
  30. buyer_id, sorder_sum, reward_rate, rdecimal((sorder_sum*reward_rate)/100,0),
  31. concat( sorder_no, ' / ', item_code )
  32. into _bd_id, _sorder_date, _avail_date, _branch_id, _user_id, _deal_type_id,
  33. _to_buyer_id, _sorder_sum, _reward_rate, _reward_amt, _add_msg
  34. from dbr_sorder as sohd
  35. inner join dbr_sorder_bd as sobd on sohd.id = sobd.sorder_id
  36. inner join dbr_item as itm on itm.id = sobd.item_id
  37. where sobd.id = _target;
  38. elseif _slip_code = 'reward' then -- sorder_date = reward_date = avail_date 로 한다.
  39. select id, reward_date, reward_date, branch_id, user_id, deal_type_id,
  40. is_transfer, from_buyer_id, to_buyer_id, '0.00', manual_amt, reward_no
  41. into _reward_id, _sorder_date, _avail_date, _branch_id, _user_id, _deal_type_id,
  42. _is_transfer, _from_buyer_id, _to_buyer_id, _reward_rate, _reward_amt, _add_msg
  43. from dbr_reward as rewd
  44. where rewd.id = _target;
  45. elseif _slip_code = 'sorder' then -- 적립금 사용
  46. select sohd.id, sorder_date, sorder_date,
  47. branch_id, sohd.user_id, _deal_sorder,
  48. buyer_id, ifnull(sum(sorder_sum), 0),'0.00', reward_use_amt,
  49. concat( sorder_no, ' / ', max(item_code) )
  50. into _hd_id, _sorder_date, _avail_date, _branch_id, _user_id, _deal_type_id,
  51. _to_buyer_id, _sorder_sum, _reward_rate, _reward_amt, _add_msg
  52. from dbr_sorder as sohd
  53. left join dbr_sorder_bd as sobd on sohd.id = sobd.sorder_id
  54. left join dbr_item as itm on itm.id = sobd.item_id
  55. where sohd.id = _target;
  56. end if;
  57. if _reward_amt = '0.0000' then
  58. leave _procedure;
  59. end if;
  60. if _act = 0 then
  61. insert into dbr_reward_bd
  62. ( created_on, hd_id, bd_id, reward_id, occur_date, avail_date, branch_id,
  63. user_id, deal_type_id, buyer_id, reward_rate, reward_amt,
  64. add_msg, is_from_buyer )
  65. values ( unix_timestamp(), _hd_id, _bd_id, _reward_id, _sorder_date, _avail_date, _branch_id,
  66. _user_id, _deal_type_id, _to_buyer_id, _reward_rate, _reward_amt,
  67. _add_msg, '0' );
  68. if _is_transfer = '1' then -- reward 에서만
  69. insert into dbr_reward_bd
  70. ( created_on, hd_id, bd_id, reward_id, occur_date, avail_date, branch_id,
  71. user_id, deal_type_id, buyer_id, reward_rate, reward_amt,
  72. add_msg, is_from_buyer )
  73. values ( unix_timestamp(), _hd_id, _bd_id, _reward_id, _sorder_date, _avail_date, _branch_id,
  74. _user_id, _deal_type_id, _from_buyer_id, _reward_rate, -1*_reward_amt,
  75. _add_msg, '1' );
  76. end if;
  77. elseif _act = 1 then
  78. update dbr_reward_bd
  79. set updated_on=unix_timestamp(),
  80. hd_id=_hd_id, bd_id=_bd_id, occur_date=_sorder_date, avail_date=_avail_date,
  81. branch_id=_branch_id, user_id = _user_id, deal_type_id = _deal_type_id,
  82. buyer_id = _to_buyer_id, reward_rate = _reward_rate, reward_amt = _reward_amt,
  83. add_msg = _add_msg
  84. where hd_id = _hd_id and bd_id = _bd_id and is_from_buyer = '0';
  85. if _is_transfer = '1' then -- reward 에서만
  86. update dbr_reward_bd
  87. set updated_on=unix_timestamp(),
  88. hd_id=_hd_id, bd_id=_bd_id, occur_date=_sorder_date, avail_date=_avail_date,
  89. branch_id=_branch_id, user_id = _user_id, deal_type_id = _deal_type_id,
  90. buyer_id = _from_buyer_id, reward_rate = _reward_rate, reward_amt = -1*_reward_amt,
  91. add_msg = _add_msg
  92. where hd_id = _hd_id and bd_id = _bd_id and is_from_buyer = '1';
  93. end if;
  94. elseif _act = -1 then
  95. delete from dbr_reward_bd
  96. where hd_id=_hd_id and bd_id= _bd_id and is_from_buyer = '0';
  97. if _is_transfer = '1' then -- reward 에서만
  98. delete from dbr_reward_bd
  99. where hd_id = _hd_id and bd_id = _bd_id and is_from_buyer = '1';
  100. end if;
  101. end if;
  102. END;
  103. //
  104. DELIMITER ;