WriteOffController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\admin\controller\finance;
  3. use app\common\Util;
  4. use app\model\SysCategory;
  5. use app\model\SysDept;
  6. use support\Db;
  7. use support\Request;
  8. class WriteOffController
  9. {
  10. public function list(Request $request)
  11. {
  12. $page = $request->get('page',1);
  13. $pageSize = $request->get('pageSize',20);
  14. $days = $request->get('days', ['2024-06-01','2024-06-30']);
  15. if (!empty($days)) {
  16. $days[0] = strtotime($days[0]);
  17. $days[1] = strtotime($days[1]);
  18. if (date('m', $days[0]) != date('m', $days[1])) {
  19. return json_fail('暂不支持跨月查询');
  20. }
  21. $month = date('Ym', $days[0]);
  22. $days[1] = strtotime(date('Y-m-d', $days[1]) . " 23:59:59");
  23. } else {
  24. $month = date('Ym');
  25. }
  26. if(!Util::schema()->hasTable("data_used_{$month}")){
  27. return json_fail('暂无数据');
  28. }
  29. // $rows = Db::table("data_used_{$month} as du")
  30. // ->leftJoin('goods as g','g.goods_id','=',"JSON_EXTRACT(du.join_data_used_object_json,'$.goods_id')")
  31. // ->leftJoin('goods_sku as gs','gs.goods_sku_id','=',"JSON_EXTRACT(du.join_data_used_object_json,'$.goods_sku_id')")
  32. // ->leftJoin('sys_user as wu','wu.user_id','=','du.join_data_used_user_id')
  33. // ->leftJoin('sys_user as su','su.user_id' ,'=', "JSON_EXTRACT(du.join_data_used_object_json,'$.salesman_id')")
  34. // ->orderBy('data_used_addtimes','DESC')
  35. // ->get();
  36. $totalSql = "
  37. select count(*) as total from app_data_used_{$month}";
  38. $total = Db::select($totalSql);
  39. $recordStart = ($page - 1) * 20;
  40. $sql = "select
  41. g.goods_name,g.goods_sales_price,g.join_goods_category_id,gs.goods_sku_specs_json,wu.user_name as wu_username, su.user_name as su_username,du.data_used_addtimes,
  42. JSON_EXTRACT(du.join_data_used_object_json,'$.charge.charge_premises') as write_off_premises, su.join_user_dept_id as premises_id
  43. from app_data_used_{$month} as du
  44. left join app_goods as g on JSON_EXTRACT(du.join_data_used_object_json,'$.goods_id') = g.goods_id
  45. left join app_goods_sku as gs on JSON_EXTRACT(du.join_data_used_object_json,'$.goods_sku_id') = gs.goods_sku_id
  46. left join app_sys_user as wu on du.join_data_used_user_id = wu.user_id
  47. left join app_sys_user as su on JSON_EXTRACT(du.join_data_used_object_json,'$.salesman_id') = su.user_id
  48. order by du.data_used_addtimes desc
  49. limit " . $recordStart . ',' . $pageSize;
  50. $rows = Db::select($sql);
  51. foreach ($rows as &$row){
  52. $row->salesman_premises = '';
  53. if (!empty($row->premises_id) && $row->premises_id != 0){
  54. $premises = SysDept::where('dept_id',$row->premises_id)
  55. ->select('dept_id','dept_name')
  56. ->first();
  57. $row->salesman_premises = !empty($premises) ? $premises->dept_name : '';
  58. }
  59. if(!empty($row->goods_sku_specs_json)){
  60. $specsJson = json_decode($row->goods_sku_specs_json,true);
  61. $skuTitle = '';
  62. foreach($specsJson as $key => $item){
  63. if(is_array($item)){
  64. $item = implode('',$item);
  65. }
  66. $skuTitle .= $key.':'.$item.',';
  67. }
  68. $row->sku_title = rtrim($skuTitle,',');
  69. }
  70. $row->category = '';
  71. if(!empty($row->join_goods_category_id)){
  72. $row->category = SysCategory::where('category_id',$row->join_goods_category_id)->value('category_name') ?? '';
  73. }
  74. $row->write_off_premises = trim($row->write_off_premises,"\"");
  75. }
  76. return json_success('', [
  77. 'page' => $page,
  78. 'pageSize' => $pageSize,
  79. 'total' => $total[0]->total,
  80. 'rows' => $rows
  81. ]);
  82. }
  83. public function records()
  84. {
  85. $sql = "select a.appointment_id as benefit_id,a.appointment_addtimes as addtimes,m.member_mobile,c.member_cert_name,b.member_benefit_name as benefit_name,a.appointment_done_json as used_json,a.appointment_done_datetime from app_appointment as a
  86. left join app_member as m on m.member_id = a.join_appointment_member_id
  87. left join app_member_cert as c on c.join_cert_member_id = a.join_appointment_member_id
  88. left join app_member_benefit as b on b.join_benefit_member_id = a.join_appointment_member_id
  89. UNION
  90. select q.member_quota_id as benefit_id,q.member_quota_addtimes as addtimes,q.member_quota_used_json as used_json, q.member_quota_name as benefit_name,m.member_mobile,c.member_cert_name from app_member_quota as q
  91. left join app_member as m on m.member_id = q.join_quota_member_id
  92. left join app_member_cert as c on c.join_cert_member_id = q.join_quota_member_id
  93. order by addtimes desc limit 0,10";
  94. $records = Db::select($sql);
  95. dump($records);
  96. }
  97. }