CloudStockBuyingOrder.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. namespace addons\CloudStock\common\models;
  3. use common\enums\PayTypeEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\base\BaseActiveRecord;
  6. use common\models\goods\Goods;
  7. use common\models\order\Order;
  8. use common\models\order\OrderDetail;
  9. use common\models\user\User;
  10. use common\components\export\CsvTool;
  11. use common\enums\DownloadEnum;
  12. use common\models\common\Download;
  13. use Yii;
  14. /**
  15. * This is the model class for table "{{%addons_cloud_stock_buying_order".
  16. *
  17. * @property int $id 自增id
  18. * @property int $mall_id 商城id
  19. * @property int $user_id 买货用户id
  20. * @property float $after_distribution_price 商品的实际支付金额扣除其他分润后的金额;未扣奖励前货款;
  21. * @property int $order_id 订单id;order表
  22. * @property int $order_detail_id 订单详情id;order_detail表
  23. * @property int $goods_id 商品id
  24. * @property int $num 商品数量
  25. * @property int $is_return 是否有退库存
  26. * @property int $status 状态[-1:删除;0:禁用;1启用]
  27. * @property int $created_at
  28. * @property int $updated_at
  29. * @property int $payment_type
  30. */
  31. class CloudStockBuyingOrder extends BaseActiveRecord
  32. {
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public static function tableName()
  37. {
  38. return '{{%addons_cloud_stock_buying_order}}';
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function rules()
  44. {
  45. return [
  46. [['mall_id', 'user_id', 'goods_id', 'num'], 'required'],
  47. [['mall_id', 'user_id', 'order_id', 'order_detail_id', 'goods_id', 'num', 'is_return', 'status', 'created_at', 'updated_at','payment_type'], 'integer'],
  48. [['after_distribution_price'], 'number'],
  49. ];
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function attributeLabels()
  55. {
  56. return [
  57. 'id' => '自增id',
  58. 'mall_id' => '商城id',
  59. 'user_id' => '买货用户id',
  60. 'after_distribution_price' => '商品的实际支付金额扣除其他分润后的金额;未扣奖励前货款;',
  61. 'order_id' => '订单id;order表',
  62. 'order_detail_id' => '订单详情id;order_detail表',
  63. 'goods_id' => '商品id',
  64. 'num' => '商品数量',
  65. 'is_return' => '是否有退库存',
  66. 'status' => '状态[-1:删除;0:禁用;1启用]',
  67. 'created_at' => 'Created At',
  68. 'updated_at' => 'Updated At',
  69. 'payment_type' => '支付方式',
  70. ];
  71. }
  72. public function getUser()
  73. {
  74. return $this->hasOne(User::class, ['id' => 'user_id'])->select(['id', 'nickname', 'avatar_url', 'mobile']);
  75. }
  76. public function getOrder()
  77. {
  78. return $this->hasOne(Order::class, ['id' => 'order_id'])->select(['order_no', 'order_status', 'id','is_feedback','pay_money','payment_type']);
  79. }
  80. public function getOrderDetail()
  81. {
  82. return $this->hasOne(OrderDetail::class, ['id' => 'order_detail_id'])->select(['price', 'is_feedback', 'id']);
  83. }
  84. public function getGoods()
  85. {
  86. return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select(['id', 'goods_name', 'cover_pic']);
  87. }
  88. public static function setData(array $params)
  89. {
  90. try {
  91. if(!empty($params['id'])){
  92. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  93. if (empty($model)) throw new \Exception('服务不存在或已删除');
  94. }else{
  95. $model = new self();
  96. $model->loadDefaultValues();
  97. }
  98. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  99. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  100. return $model;
  101. } catch (\Exception $e) {
  102. self::$static_error = $e->getMessage();
  103. return false;
  104. }
  105. }
  106. /**
  107. * 导出字段数组
  108. * @Author: hua
  109. * @DateTime: 2021/10/11 18:06
  110. * @Copyright: copyright (c) 2021 广东七件事集团
  111. * @return \string[][]
  112. */
  113. public static function fieldsLists($field = null){
  114. $fields = [
  115. 'order_id' => '订单ID',
  116. 'user_id' => '买家用户id',
  117. 'nickname' => '用户昵称',
  118. 'goods_name' => '商品名称',
  119. 'goods_num' => '购买商品数量',
  120. 'created_at' => '订单创建时间',
  121. 'order_no' => '订单编号',
  122. 'pay_money' => '实际支付金额',
  123. 'after_distribution_price' => '货款金额',
  124. 'order_status' => '订单状态',
  125. 'payment_type' => '支付方式',
  126. ];
  127. if ($field === null) return $fields;
  128. $temp = [];
  129. foreach ($field as $val) {
  130. if (isset($fields[$val])) $temp[$val] = $fields[$val];
  131. }
  132. return $temp;
  133. }
  134. /**
  135. * 导出
  136. */
  137. public static function exportOrder(array $data) {
  138. $download_id = $data['download_id'] ?? 0;
  139. $download = Download::findOne(['id' => $download_id]);
  140. if ($download) {
  141. try {
  142. $params = json_decode($download->params, true);
  143. $filename = $download->name;
  144. $type = $download->type;
  145. $fields = isset($params['fields']) ? $params['fields'] : null;
  146. $exportField = self::fieldsLists($fields);
  147. $exportField = array_keys($exportField);
  148. $where[] = ['=', 'mall_id', $params['mall_id']];
  149. $where[] = ['in', 'status', [StatusEnum::DISABLED, StatusEnum::ENABLED]];
  150. if (!empty($params['keyword'])) {
  151. $user_list = User::find()->where(['or',
  152. ['id' => $params['keyword']],
  153. // ['mobile' => $params['keyword']],
  154. ['nickname' => $params['keyword']]])->select('id')->asArray()->all();
  155. $user_id_arr = array_column($user_list, 'id');
  156. $where[] = ['user_id' => $user_id_arr];
  157. }
  158. if (!empty($params['goods_name'])) {
  159. $goods_list = Goods::lists(['where' => [['like', 'goods_name', $params['goods_name']]]]);
  160. if ($goods_list) {
  161. $goods_id_arr = array_column($goods_list, 'id');
  162. $where[] = ['goods_id' => $goods_id_arr];
  163. }
  164. }
  165. if (!empty($params['payment_type'])) $where[] = ['=', 'payment_type', $params['payment_type'] ];
  166. if (!empty($params['start'])) $where[] = ['>=', 'created_at', strtotime($params['start'])];
  167. if (!empty($params['end'])) $where[] = ['<=', 'created_at', strtotime($params['end'])];
  168. if (!empty($params['id'])) {
  169. $where[] = ['=', 'id', $params['id']];
  170. }
  171. $wheres['order'] = 'id desc';
  172. $wheres['with'] = ['user', 'order', 'orderDetail', 'goods'];
  173. $wheres['where'] = $where;
  174. $model = self::find();
  175. self::paramPack($wheres, $model);
  176. $list = [];
  177. $exportheader = [];
  178. $isSetHeader = true;
  179. //所有团队部分修改
  180. $i = 1;
  181. $page_size = 1000;
  182. $payment_type_list= PayTypeEnum::getMap();
  183. while ($log_list = $model->asArray()->offset(($i - 1) * $page_size)->limit($page_size)->all()){
  184. foreach ($log_list as $l){
  185. $temp = [];
  186. //按顺序组装
  187. if(in_array('id', $fields)) {
  188. $temp['id'] = $l['id'];
  189. $isSetHeader && array_push($exportheader, '订单ID');
  190. }
  191. if(in_array('user_id', $fields)) {
  192. $temp['user_id'] = $l['user_id'];
  193. $isSetHeader && array_push($exportheader, '买家用户id');
  194. }
  195. if(in_array('nickname', $fields)) {
  196. $temp['nickname'] = $l['user']['nickname'];
  197. $isSetHeader && array_push($exportheader, '用户昵称');
  198. }
  199. if(in_array('goods_name', $fields)) {
  200. $temp['goods_name'] = $l['goods']['goods_name'];
  201. $isSetHeader && array_push($exportheader, '商品名称');
  202. }
  203. if(in_array('num', $fields)) {
  204. $temp['num'] = $l['num'];
  205. $isSetHeader && array_push($exportheader, '购买商品数量');
  206. }
  207. if(in_array('created_at', $fields)) {
  208. $temp['created_at'] = date('Y-m-d H:i:s', $l['created_at']);
  209. $isSetHeader && array_push($exportheader, '订单创建时间');
  210. }
  211. if(in_array('order_no', $fields)) {
  212. $temp['order_no'] = $l['order']['order_no'];
  213. $isSetHeader && array_push($exportheader, '订单编号');
  214. }
  215. if(in_array('pay_money', $fields)) {
  216. $temp['pay_money'] = $l['order']['pay_money'];
  217. $isSetHeader && array_push($exportheader, '实际支付金额');
  218. }
  219. if(in_array('after_distribution_price', $fields)) {
  220. $temp['after_distribution_price'] = $l['after_distribution_price'];
  221. $isSetHeader && array_push($exportheader, '货款金额');
  222. }
  223. if(in_array('order_status', $fields)) {
  224. switch($l['order']['order_status']) {
  225. case 0:
  226. $temp['order_status'] = '待支付';
  227. break;
  228. case 1:
  229. $temp['order_status'] = '待发货(已付款)';
  230. break;
  231. case 2:
  232. $temp['order_status'] = '已发货';
  233. break;
  234. case 3:
  235. $temp['order_status'] = '已收货';
  236. break;
  237. case 4:
  238. $temp['order_status'] = '已完成';
  239. break;
  240. default:
  241. $temp['order_status'] = '';
  242. };
  243. $isSetHeader && array_push($exportheader, '订单状态');
  244. }
  245. if(in_array('payment_type',$fields)){
  246. $isSetHeader && array_push($exportheader, '支付方式');
  247. $temp['payment_type']=$payment_type_list[$l['order']['payment_type']]??'';
  248. }
  249. $list[] = $temp;
  250. $isSetHeader = false;
  251. }
  252. $i++;
  253. }
  254. if (!empty($list)) {
  255. $csv = new CsvTool();
  256. $csv->filename = $filename;
  257. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  258. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  259. $csv->header = $exportheader;
  260. $csv->data = $list;
  261. $csv->export();
  262. $csv->updateDown($download, $params['mall_id']);
  263. }
  264. }catch (\Exception $e) {
  265. $download->status = 3;
  266. $res = $download->save();
  267. var_dump($e->getMessage().$e->getFile().$e->getLine());
  268. self::setStaticError($e->getMessage());
  269. return $e->getMessage();
  270. }
  271. }
  272. }
  273. }