ConfirmController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: king/QQ:995265288
  5. * Date: 2019-06-19
  6. * Time: 16:56
  7. */
  8. namespace app\backend\modules\excelRecharge\controllers;
  9. use app\backend\models\excelRecharge\DetailModel;
  10. use app\backend\models\excelRecharge\RecordsModel;
  11. use app\common\helpers\Url;
  12. use app\common\services\credit\ConstService;
  13. use app\common\services\finance\BalanceChange;
  14. use app\common\services\finance\PointService;
  15. use Yunshop\Love\Common\Services\LoveChangeService;
  16. use app\backend\modules\member\models\Member;
  17. class ConfirmController extends PageController
  18. {
  19. private $fileContent;
  20. /**
  21. * @var int
  22. */
  23. private $handleTotal = 0;
  24. /**
  25. * @var int
  26. */
  27. private $failureTotal = 0;
  28. /**
  29. * @var string
  30. */
  31. private $handAmount = '0';
  32. /**
  33. * @var string
  34. */
  35. private $successAmount = '0';
  36. /**
  37. * @var string
  38. */
  39. private $phone = '';
  40. //批量充值提交
  41. public function index()
  42. {
  43. if ($this->rechargeType() && $this->rechargeTypeValidator()) {
  44. return $this->confirm();
  45. }
  46. return $this->typeError();
  47. }
  48. /**
  49. * 确认充值
  50. *
  51. * @return mixed|void
  52. */
  53. private function confirm()
  54. {
  55. $excelFile = $this->excelFile();
  56. if ($excelFile
  57. && $excelFile->isValid()
  58. && in_array($excelFile->getClientOriginalExtension(), ['xls', 'xlsx'])
  59. ) {
  60. return $this->rechargeStart();
  61. }
  62. return $this->excelError();
  63. }
  64. private function rechargeStart()
  65. {
  66. $recordsId = 0;
  67. $positionId = &$recordsId;
  68. $recordsModel = new RecordsModel();
  69. $handleRechargeData = $this->handleRecharge($positionId);
  70. $recordsModel->fill([
  71. 'uniacid' => \YunShop::app()->uniacid,
  72. 'total' => $this->handleTotal,
  73. 'amount' => $this->handAmount,
  74. 'failure' => $this->failureTotal,
  75. 'success' => $this->successAmount,
  76. 'source' => $this->rechargeType(),
  77. 'remark' => ''
  78. ]);
  79. $recordsModel->save();
  80. $positionId = $recordsModel->id;
  81. //插入充值详细记录
  82. DetailModel::insert($handleRechargeData);
  83. return $this->message($this->messageContent(), Url::absoluteWeb('excelRecharge.records.index'));
  84. }
  85. /**
  86. * @return string
  87. */
  88. private function messageContent()
  89. {
  90. $successTotal = $this->handleTotal - $this->failureTotal;
  91. return "执行数量{$this->handleTotal},成功数量{$successTotal},失败数量{$this->failureTotal}";
  92. }
  93. private function handleRecharge(&$recordsId)
  94. {
  95. // $this->uploadExcel();
  96. // $values = $this->getRow();
  97. $importData = \app\exports\ExcelService::importToArray($this->excelFile());
  98. //表格数据
  99. $values = $importData[0];
  100. array_shift($values); // 删除标题
  101. $data = [];
  102. if($this->genre() == 2){ //1 是会员ID, 2 是手机号
  103. $phones = array_column($values,null,0);
  104. $phone = array_keys($phones);
  105. $result = Member::uniacid()->select('uid','mobile')->whereIn('mobile',$phone)->get();
  106. foreach ($result as $value){
  107. $phones[$value->mobile][0] = $value->uid;
  108. }
  109. unset($phone);
  110. unset($value);
  111. unset($result);
  112. $values = $phones;
  113. }
  114. foreach ($values as $key => $value) {
  115. $this->phone = trim($value['0']);
  116. $amount = trim($value[1]);
  117. $memberId = trim($value[0]);
  118. $data[] = $this->_handleRecharge($recordsId, $memberId, $amount);
  119. }
  120. return $data;
  121. }
  122. private function _handleRecharge(&$recordsId, $memberId, $amount)
  123. {
  124. $this->handleTotal += 1;
  125. $this->handAmount += $amount;
  126. try {
  127. $rechargeFunc = $this->batchRechargeFunc();
  128. $result = $this->$rechargeFunc($memberId, $amount);;
  129. } catch (\Exception $exception) {
  130. $result = $exception->getMessage();
  131. }
  132. if ($result === true) {
  133. $status = 1;
  134. $remark = '';
  135. $this->successAmount += $amount;
  136. } else {
  137. $this->failureTotal += 1;
  138. if($this->phone){
  139. $remark = $this->phone."充值失败" ;
  140. }else{
  141. $remark = "充值失败" . $result;
  142. }
  143. $status = 0;
  144. }
  145. return [
  146. 'uniacid' => \YunShop::app()->uniacid,
  147. 'amount' => $amount,
  148. 'member_id' => $memberId,
  149. 'recharge_id' => &$recordsId,
  150. 'remark' => $remark,
  151. 'status' => $status,
  152. 'created_at' => time(),
  153. 'updated_at' => time()
  154. ];
  155. }
  156. /**
  157. * 充值类型错误
  158. *
  159. * @return mixed
  160. */
  161. private function typeError()
  162. {
  163. return $this->errorMessage("错误的批量充值类型");
  164. }
  165. /**
  166. * Excel 错误
  167. *
  168. * @return mixed
  169. */
  170. private function excelError()
  171. {
  172. return $this->errorMessage("错误的Excel文件");
  173. }
  174. /**
  175. * 错误
  176. *
  177. * @param $message
  178. * @return mixed
  179. */
  180. private function errorMessage($message)
  181. {
  182. $this->error($message);
  183. return parent::index();
  184. }
  185. /**
  186. * 批量充值类型方法
  187. *
  188. * @return string
  189. */
  190. private function batchRechargeFunc()
  191. {
  192. $batchType = $this->rechargeType();
  193. $func = 'batchRecharge' . ucfirst(strtolower($batchType));
  194. return $func;
  195. }
  196. /**
  197. * 批量充值类型
  198. *
  199. * @return string
  200. */
  201. private function rechargeType()
  202. {
  203. return request()->batch_type;
  204. }
  205. /**
  206. * 第一列的值是手机号还是会员ID
  207. * @return mixed
  208. */
  209. private function genre()
  210. {
  211. return request()->genre;
  212. }
  213. /**
  214. * 爱心值类型
  215. *
  216. * @return string
  217. */
  218. private function rechargeTypeOfLove()
  219. {
  220. return request()->love_type;
  221. }
  222. /**
  223. * 批量充值类型验证
  224. *
  225. * @return bool
  226. */
  227. private function rechargeTypeValidator()
  228. {
  229. if (is_callable(static::class, $this->batchRechargeFunc())) {
  230. return true;
  231. }
  232. return false;
  233. }
  234. /**
  235. * 批量充值Excel文件
  236. *
  237. * @return array|\Illuminate\Http\UploadedFile|null
  238. */
  239. private function excelFile()
  240. {
  241. return request()->file('batch_recharge');
  242. }
  243. /**
  244. * 上传批量充值Excel文件
  245. */
  246. private function uploadExcel()
  247. {
  248. $excelFile = $this->excelFile();
  249. $realPath = $excelFile->getRealPath();
  250. $extension = $excelFile->getClientOriginalExtension();
  251. $originalName = $excelFile->getClientOriginalName();
  252. $newOriginalName = md5($originalName . str_random(6)) . "." . $extension;
  253. \Storage::disk('recharge')->put($newOriginalName, file_get_contents($realPath));
  254. $this->fileContent = \Excel::load(storage_path($this->path) . '/' . $newOriginalName);
  255. }
  256. /**
  257. * @return array
  258. * @throws \PHPExcel_Exception
  259. */
  260. private function getRow()
  261. {
  262. $sheet = $this->fileContent->getActiveSheet();
  263. // $highestRow = $sheet->getHighestRow();
  264. // $highestColumn = $sheet->getHighestColumn();
  265. $highestRow = $sheet->getHighestDataRow();
  266. $highestColumn = $sheet->getHighestDataColumn();
  267. $highestColumnCount = \PHPExcel_Cell::columnIndexFromString($highestColumn);
  268. $row = 2;
  269. $values = [];
  270. while ($row <= $highestRow) {
  271. $rowValue = array();
  272. $col = 0;
  273. while ($col < $highestColumnCount) {
  274. $rowValue[] = (string)$sheet->getCellByColumnAndRow($col, $row)->getValue();
  275. ++$col;
  276. }
  277. $values[] = $rowValue;
  278. ++$row;
  279. }
  280. return $values;
  281. }
  282. private function batchRechargeBalance($memberId, $rechargeValue)
  283. {
  284. if ($rechargeValue >= 0) {
  285. return (new BalanceChange())->excelRecharge([
  286. 'member_id' => $memberId,
  287. 'change_value' => $rechargeValue,
  288. 'remark' => 'Excel批量充值' . $rechargeValue . "元",
  289. 'relation' => '',
  290. 'operator' => ConstService::OPERATOR_SHOP,
  291. 'operator_id' => \YunShop::app()->uid
  292. ]);
  293. }
  294. return (new BalanceChange())->excelRechargeSubtraction([
  295. 'member_id' => $memberId,
  296. 'change_value' => abs($rechargeValue),
  297. 'remark' => 'Excel批量充值' . $rechargeValue . "元",
  298. 'relation' => '',
  299. 'operator' => ConstService::OPERATOR_SHOP,
  300. 'operator_id' => \YunShop::app()->uid
  301. ]);
  302. }
  303. private function batchRechargePoint($memberId, $rechargeValue)
  304. {
  305. $result = (new PointService([
  306. 'point_mode' => PointService::POINT_MODE_EXCEL_RECHARGE,
  307. 'member_id' => $memberId,
  308. 'point' => $rechargeValue,
  309. 'remark' => 'Excel批量充值' . $rechargeValue,
  310. 'point_income_type' => $rechargeValue < 0 ? PointService::POINT_INCOME_LOSE : PointService::POINT_INCOME_GET
  311. ]))->changePoint();
  312. return $result ? true : false;
  313. }
  314. private function batchRechargeLove($memberId, $rechargeValue)
  315. {
  316. if ($rechargeValue >= 0) {
  317. return (new LoveChangeService($this->rechargeTypeOfLove()))->recharge([
  318. 'member_id' => $memberId,
  319. 'change_value' => $rechargeValue,
  320. 'operator' => ConstService::OPERATOR_MEMBER,
  321. 'operator_id' => 0,
  322. 'remark' => 'Excel批量充值' . $rechargeValue,
  323. ]);
  324. }
  325. return (new LoveChangeService($this->rechargeTypeOfLove()))->rechargeMinus([
  326. 'member_id' => $memberId,
  327. 'change_value' => abs($rechargeValue),
  328. 'operator' => ConstService::OPERATOR_MEMBER,
  329. 'operator_id' => 0,
  330. 'remark' => 'Excel批量充值' . $rechargeValue,
  331. ]);
  332. }
  333. }