RecipientsController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Recipients;
  4. use App\Models\Prize;
  5. use App\Models\CheckIn;
  6. use App\Models\User;
  7. use App\Models\Department;
  8. use App\Http\Requests\StoreRecipientsRequest;
  9. use App\Http\Requests\UpdateRecipientsRequest;
  10. use App\Models\Activity;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\DB;
  13. class RecipientsController extends Controller
  14. {
  15. /**
  16. * Display a listing of the resource.
  17. *
  18. * @return \Illuminate\Http\Response
  19. */
  20. public function index()
  21. {
  22. return Recipients::all();
  23. }
  24. public function index_by_activity(UpdateRecipientsRequest $request)
  25. {
  26. return Recipients::join('prizes', 'recipients.prize_id', '=', 'prizes.id')
  27. ->join('users', 'recipients.user_id', '=', 'users.user_id')
  28. ->select(
  29. 'prizes.activity_id',
  30. 'recipients.user_id',
  31. 'users.name as user_name',
  32. 'recipients.prize_id',
  33. 'prizes.name as prize_name',
  34. 'prizes.provider',
  35. 'recipients.created_at'
  36. )
  37. ->where('prizes.activity_id', $request->activity_id)
  38. ->orderBy('recipients.created_at', 'DESC')
  39. ->get();
  40. }
  41. /**
  42. * Show the form for creating a new resource.
  43. *
  44. * @return \Illuminate\Http\Response
  45. */
  46. public function create()
  47. {
  48. //
  49. }
  50. /**
  51. * Store a newly created resource in storage.
  52. *
  53. * @param \App\Http\Requests\StoreRecipientsRequest $request
  54. * @return \Illuminate\Http\Response
  55. */
  56. public function store(StoreRecipientsRequest $request)
  57. {
  58. Recipients::insert([
  59. 'prize_id' => $request->prizeId,
  60. 'user_id' => $request->userId,
  61. 'is_claimed' => false,
  62. 'created_at' => now(),
  63. 'updated_at' => now(),
  64. ]);
  65. return 'success';
  66. }
  67. public function store_patch(StoreRecipientsRequest $request)
  68. {
  69. $data = [];
  70. foreach ($request->userIdList as $userId) {
  71. array_push($data, [
  72. 'prize_id' => $request->prizeId,
  73. 'user_id' => $userId,
  74. 'is_claimed' => false,
  75. 'created_at' => now(),
  76. 'updated_at' => now(),
  77. ]);
  78. }
  79. Recipients::insert($data);
  80. return 'success';
  81. }
  82. /**
  83. * Display the specified resource.
  84. *
  85. * @param \App\Models\Recipients $recipients
  86. * @return \Illuminate\Http\Response
  87. */
  88. public function show(Request $request, Recipients $recipients)
  89. {
  90. //
  91. }
  92. /**
  93. * Show the form for editing the specified resource.
  94. *
  95. * @param \App\Models\Recipients $recipients
  96. * @return \Illuminate\Http\Response
  97. */
  98. public function edit(Recipients $recipients)
  99. {
  100. //
  101. }
  102. /**
  103. * Update the specified resource in storage.
  104. *
  105. * @param \App\Http\Requests\UpdateRecipientsRequest $request
  106. * @param \App\Models\Recipients $recipients
  107. * @return \Illuminate\Http\Response
  108. */
  109. public function update(UpdateRecipientsRequest $request, Recipients $recipients)
  110. {
  111. Recipients::where('user_id', $request->userId)->update(['prize_id' => $request->prizeId]);
  112. return 'success';
  113. }
  114. /**
  115. * Remove the specified resource from storage.
  116. *
  117. * @param \App\Models\Recipients $recipients
  118. * @return \Illuminate\Http\Response
  119. */
  120. public function destroy(Recipients $recipients)
  121. {
  122. //
  123. }
  124. public function claim(Request $request)
  125. {
  126. $prize = '';
  127. $prize_id = 0;
  128. $isClaimed = false;
  129. $user_id = $request->user_id;
  130. $activity_id = $request->activity_id;
  131. $user = CheckIn::where('activity_id', '=', $activity_id)
  132. ->where('user_id', '=', $user_id)->get()[0];
  133. $department_id = CheckIn::where('user_id', '=', $user_id)->get()[0]->department_id;
  134. if ($department_id != '空') {
  135. $department_name = Department::where('department_id', '=', $department_id)->get()[0]->department_name;
  136. } else {
  137. $department_name = '空';
  138. }
  139. if (Recipients::where('user_id', '=', $user_id)->exists()) {
  140. $recipients = Recipients::where('user_id', '=', $user_id)->get()[0];
  141. $prize_id = $recipients->prize_id;
  142. //因為重新匯入後prize_id在prize下面有改動,但是在recipients下面沒有改動,因此會有問題產生
  143. //檢查prize_id是不是在activity_id底下
  144. $aid = Prize::where('id', '=', $prize_id)->get()[0]->activity_id;
  145. if ($aid == $activity_id) {
  146. $isRecipients = true;
  147. $isClaimed = $recipients->is_claimed;
  148. $prize = Prize::where('id', '=', $prize_id)->get()[0]->name;
  149. } else {
  150. $isRecipients = false;
  151. }
  152. } else {
  153. $isRecipients = false;
  154. }
  155. $response = [
  156. 'user' => $user,
  157. 'isRecipients' => $isRecipients,
  158. 'isClaimed' => $isClaimed,
  159. 'prize' => $prize,
  160. 'prize_id' => $prize_id,
  161. 'department_name' => $department_name,
  162. ];
  163. return response($response, 201);
  164. }
  165. public function isClaimed(UpdateRecipientsRequest $request)
  166. {
  167. $recipient = Recipients::where('prize_id', '=', $request->prize_id)
  168. ->where('user_id', '=', $request->user_id)->get()[0];
  169. $recipient->is_claimed = true;
  170. $recipient->save();
  171. return $recipient;
  172. }
  173. public function export(Request $request)
  174. {
  175. $activity_name = Activity::select('activity_name')
  176. ->where('id', '=', $request->activity_id)
  177. ->first()->activity_name;
  178. $recipients = Recipients::join('prizes', 'recipients.prize_id', '=', 'prizes.id')
  179. ->join('check_ins', 'recipients.user_id', '=', 'check_ins.user_id')
  180. ->select('prizes.id', 'prizes.name as prize', 'check_ins.user_id', 'check_ins.name')
  181. ->where('prizes.activity_id', '=', $request->activity_id)
  182. ->orderBy('prizes.id')
  183. ->get();
  184. $filename = $activity_name . "_中獎清單" . date('Y_m_d_H_i_s', time()) . ".csv";
  185. $filepath = "./upload/" . $filename;
  186. $handle = fopen($filepath, 'w+');
  187. fwrite($handle,chr(0xEF).chr(0xBB).chr(0xBF));
  188. fputcsv($handle, array('獎項', '工號', '姓名'));
  189. foreach ($recipients as $element) {
  190. fputcsv($handle, array($element->prize, $element->user_id, $element->name));
  191. }
  192. fclose($handle);
  193. $headers = array(
  194. 'Content-Type' => 'text/csv',
  195. );
  196. return response()->download($filepath, $filename, $headers);
  197. }
  198. public function indexByPrize(Request $request)
  199. {
  200. //
  201. $prize_id = $request->prize_id;
  202. $prizeList = DB::select("SELECT [check_ins].[user_id],[name],[is_claimed],[alias] FROM [recipients]
  203. LEFT JOIN [check_ins] ON [recipients].[user_id] = [check_ins].[user_id]
  204. LEFT JOIN [departments] ON [check_ins].[department_id] = [departments].[department_id]
  205. WHERE [prize_id] = ? ORDER BY CASE
  206. WHEN [check_ins].[department_id] = 'guest' then 1 END DESC,[check_ins].[department_id],[check_ins].[user_id];", [$prize_id]);
  207. $prize = DB::select("SELECT [name] FROM [prizes] WHERE [id] = ?;", [$prize_id]);
  208. $temp_alias = "";
  209. $temp_color = true;
  210. foreach($prizeList as $value){
  211. if($temp_alias != $value->alias){
  212. $temp_color = !$temp_color;
  213. $temp_alias = $value->alias;
  214. }
  215. $value->color = $temp_color;
  216. }
  217. $response = [
  218. "prize" => $prize,
  219. "list" => $prizeList,
  220. ];
  221. return response($response, 201);
  222. }
  223. }