新版订单消耗系统

OrderMcn.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2021/4/9
  6. * Time: 15:53
  7. */
  8. namespace App\Models;
  9. use App\RedisModel;
  10. use Illuminate\Database\Eloquent\Model;
  11. use Illuminate\Support\Facades\DB;
  12. class OrderMcn extends Model
  13. {
  14. protected $table = 'order_mcn';
  15. public $timestamps = false;
  16. protected static $unguarded = true;
  17. const ORDER_MCN_MONTH_PRE = 'OrderConsumption.OrderMcnMonthRds_';
  18. const COLUMNS = [
  19. 'task_id',
  20. 'original_month',
  21. 'order_id',
  22. 'month',
  23. 'mcn_handler',
  24. 'mcn_name',
  25. 'mcn_rebate',
  26. 'mcn_handler_id',
  27. 'mcn_user_id',
  28. 'mcn_user',
  29. 'agreed_rebate_percentage',
  30. 'organization_amount',
  31. 'original_mcn_rebate',
  32. 'mcn_final_amount',
  33. 'is_ca',
  34. 'mcn_invoice_amount',
  35. 'mcn_invoice_date',
  36. 'mcn_document_number',
  37. 'mcn_confirmation_certificate'
  38. ];
  39. public static function setMcnRds($month)
  40. {
  41. $sql = "select om.*,
  42. oi.company,
  43. oi.advertiser_account,
  44. oi.advertiser_id,
  45. oi.salesman,
  46. oi.saler_id,
  47. oi.handler,
  48. oi.handlers_id,
  49. oi.customer_name,
  50. oi.final_customer_name,
  51. oi.origin_customer_name,
  52. oi.project_name,
  53. oi.launch_start_date,
  54. oi.launch_end_date,
  55. oi.launch_platform,
  56. oi.media_name,
  57. oi.cost_company_name,
  58. oi.cost_owner,
  59. oi.amount,
  60. oi.internet_celebrity_name,
  61. oi.internet_celebrity_id,
  62. oi.status
  63. from order_income as oi
  64. left join order_mcn as om on oi.order_id = om.order_id
  65. where om.enable=1 and om.month='{$month}'
  66. order by om.id desc ";
  67. $data = DB::select($sql);
  68. $data = json_decode(json_encode($data), true);
  69. #数据处理
  70. foreach($data as &$v){
  71. $v['task_id'] .= '';
  72. //处理超长小数百分比显示问题
  73. $v['agreed_rebate_percentage'] = Order::longDecimalFormat($v['agreed_rebate_percentage']);
  74. $v['month'] = substr($v['month'], 0, 7);
  75. }
  76. $rk = static::ORDER_MCN_MONTH_PRE . $month;
  77. RedisModel::set($rk, json_encode($data));
  78. RedisModel::expire($rk, 86400);
  79. return $data;
  80. }
  81. /**
  82. * 拼接条件语句
  83. */
  84. public static function getSearchQuery($search, $user)
  85. {
  86. return DB::table('order_mcn as om')
  87. ->leftJoin('order_income as oi', 'oi.order_id', '=', 'om.order_id')
  88. ->where('om.enable', 1)
  89. ->where(function($query) use($search, $user) {
  90. //if(isset($search['saler_id']) && $search['saler_id']>0) $query->whereRaw('(saler_id='.$user->id.' or handlers_id='.$user->id.')');
  91. if (isset($search['is_statements']) && 1 == $search['is_statements']) {
  92. if(in_array($user->role_id, [9, 13])) {
  93. // 2023-06-14 白凤要求新来的媒介郭姣蓉 113 可以看到和她 13 一样的数据
  94. if(113 == $user->id) {
  95. $query->whereRaw('(mcn_handler_id = 113 or mcn_handler_id = 19)');
  96. } else {
  97. $query->whereRaw('(mcn_handler_id='.$user->id.')');
  98. }
  99. }
  100. } else {
  101. if(in_array($user->role_id, [9, 13]) && $user->id !=53) {
  102. if(113 == $user->id) {
  103. $query->whereRaw('(mcn_user_id=13 or mcn_handler_id=13 or mcn_user_id = 113 or mcn_handler_id = 113)');
  104. } else {
  105. $query->whereRaw('(mcn_user_id='.$user->id.' or mcn_handler_id='.$user->id.')');
  106. }
  107. }
  108. }
  109. if(isset($search['internet_celebrity_name']) && !empty($search['internet_celebrity_name']) ){
  110. if( count($search['internet_celebrity_name']) == 1){
  111. $query->where('oi.internet_celebrity_name', current($search['internet_celebrity_name']) );
  112. } else {
  113. $query->whereIn('oi.internet_celebrity_name', $search['internet_celebrity_name']);
  114. }
  115. }
  116. if(isset($search['company']) && !empty($search['company']) ){
  117. if( count($search['company']) == 1){
  118. $query->where('oi.company', current($search['company']) );
  119. } else {
  120. $query->whereIn('oi.company', $search['company']);
  121. }
  122. }
  123. if(isset($search['project_name']) && !empty($search['project_name']) ){
  124. if( count($search['project_name']) == 1){
  125. $query->where('oi.project_name', current($search['project_name']) );
  126. } else {
  127. $query->whereIn('oi.project_name', $search['project_name']);
  128. }
  129. }
  130. if(isset($search['salesman']) && !empty($search['salesman']) ){
  131. if( count($search['salesman']) == 1){
  132. $query->where('oi.salesman', current($search['salesman']) );
  133. } else {
  134. $query->whereIn('oi.salesman', $search['salesman']);
  135. }
  136. }
  137. if(isset($search['handler']) && !empty($search['handler']) ){
  138. if( count($search['handler']) == 1){
  139. $query->where('oi.handler', current($search['handler']) );
  140. } else {
  141. $query->whereIn('oi.handler', $search['handler']);
  142. }
  143. }
  144. if(isset($search['customer_name']) && !empty($search['customer_name']) ){
  145. if( count($search['customer_name']) == 1){
  146. $query->where('oi.customer_name', current($search['customer_name']) );
  147. } else {
  148. $query->whereIn('oi.customer_name', $search['customer_name']);
  149. }
  150. }
  151. if(isset($search['launch_platform']) && !empty($search['launch_platform']) ){
  152. if( count($search['launch_platform']) == 1){
  153. $query->where('oi.launch_platform', current($search['launch_platform']) );
  154. } else {
  155. $query->whereIn('oi.launch_platform', $search['launch_platform']);
  156. }
  157. }
  158. if(isset($search['cost_company_name']) && !empty($search['cost_company_name']) ){
  159. if( count($search['cost_company_name']) == 1){
  160. $query->where('oi.cost_company_name', current($search['cost_company_name']) );
  161. } else {
  162. $query->whereIn('oi.cost_company_name', $search['cost_company_name']);
  163. }
  164. }
  165. if(isset($search['cost_owner']) && !empty($search['cost_owner']) ){
  166. if( count($search['cost_owner']) == 1){
  167. $query->where('oi.cost_owner', current($search['cost_owner']) );
  168. } else {
  169. $query->whereIn('oi.cost_owner', $search['cost_owner']);
  170. }
  171. }
  172. if(isset($search['advertiser_account']) && !empty($search['advertiser_account']) ){
  173. if( count($search['advertiser_account']) == 1){
  174. $query->where('oi.advertiser_account', current($search['advertiser_account']) );
  175. } else {
  176. $query->whereIn('oi.advertiser_account', $search['advertiser_account']);
  177. }
  178. }
  179. if(isset($search['advertiser_id']) && !empty($search['advertiser_id']) ){
  180. if( count($search['advertiser_id']) == 1){
  181. $query->where('oi.advertiser_id', current($search['advertiser_id']) );
  182. } else {
  183. $query->whereIn('oi.advertiser_id', $search['advertiser_id']);
  184. }
  185. }
  186. if(isset($search['media_name']) && !empty($search['media_name']) ){
  187. if( count($search['media_name']) == 1){
  188. $query->where('oi.media_name', current($search['media_name']) );
  189. } else {
  190. $query->whereIn('oi.media_name', $search['media_name']);
  191. }
  192. }
  193. if(isset($search['mcn_handler']) && !empty($search['mcn_handler'])) {
  194. if(count($search['mcn_handler']) == 1) {
  195. $query->where('om.mcn_handler', current($search['mcn_handler']));
  196. } else {
  197. $query->whereIn('om.mcn_handler', $search['mcn_handler']);
  198. }
  199. }
  200. if(isset($search['mcn_name']) && !empty($search['mcn_name'])) {
  201. if(count($search['mcn_name']) == 1) {
  202. $query->where('om.mcn_name', current($search['mcn_name']));
  203. } else {
  204. $query->whereIn('om.mcn_name', $search['mcn_name']);
  205. }
  206. }
  207. if(isset($search['verify_status']) && $search['verify_status']>0){
  208. $status = $search['verify_status'];
  209. switch ($status) {
  210. case 1:
  211. $status = [1, 2];
  212. break;
  213. case 2:
  214. $status = [4];
  215. break;
  216. case 3:
  217. $status = [3, 5];
  218. break;
  219. default:
  220. $status = [];
  221. break;
  222. }
  223. if(!empty($status)) $query->whereIn('om.verify_status', $status);
  224. }
  225. if(!empty($search['statements_status'])) {
  226. if(-1 == $search['statements_status']) {
  227. $search['statements_status'] = 0;
  228. }
  229. $query->where('om.statements_status', $search['statements_status']);
  230. }
  231. if(isset($search['month']) && $search['month']) {
  232. if(count($search['month']) == 1) {
  233. $query->where('om.month', '=', date('Y-m-01', strtotime(current($search['month'])) ) );
  234. } else {
  235. $monthList = array_map(function($value){
  236. return date('Y-m-01', strtotime($value));
  237. }, $search['month']);
  238. $query->whereIn('om.month', $monthList);
  239. }
  240. }
  241. if(isset($search['task_id']) && !empty($search['task_id'])) $query->where('om.task_id', 'like', '%'.$search['task_id'].'%');
  242. // if(isset($search['month'])) $query->where('om.month', $search['month']);
  243. if(isset($search['order_part_id'])) $query->where('oi.order_part_id', 'like', '%'.$search['order_part_id']);
  244. $query->where('om.order_id', '>', '');
  245. });
  246. }
  247. }