reward_bd_act.sql 4.5 KB

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