CloudStockIncreaseDetail.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. namespace addons\CloudStock\common\models;
  3. use addons\CloudStock\common\enums\CloudStockEnum;
  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\user\User;
  9. use common\components\export\CsvTool;
  10. use common\enums\DownloadEnum;
  11. use common\models\common\Download;
  12. use Yii;
  13. /**
  14. * This is the model class for table "{{%addons_cloud_stock_increase_detail}}".
  15. *
  16. * @property int $id 自增id
  17. * @property int $mall_id 商城id
  18. * @property int $goods_id 商品id
  19. * @property int $changes_type 库存变动类型;100向上级补货;101上级给自己配货;102退货退库存;103系统添加;104平台补货赠送;105购买礼包赠送;
  20. * @property int $stock_log_id 库存变动表qimall_addons_cloud_stock_goods_stock_log的id
  21. * @property int $user_id 增加库存代理商用户id
  22. * @property int $order_id 订单id
  23. * @property int $order_type order类型,1:会员购买订单,2:补货订单;
  24. * @property int $num 增加数量
  25. * @property int $status 状态[-1:删除;0:禁用;1启用]
  26. * @property int $created_at
  27. * @property int $updated_at
  28. */
  29. class CloudStockIncreaseDetail extends BaseActiveRecord
  30. {
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public static function tableName()
  35. {
  36. return '{{%addons_cloud_stock_increase_detail}}';
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function rules()
  42. {
  43. return [
  44. [['mall_id', 'goods_id', 'changes_type', 'stock_log_id', 'user_id', 'num'], 'required'],
  45. [['mall_id', 'goods_id', 'changes_type', 'stock_log_id', 'user_id', 'order_id', 'order_type', 'num', 'status', 'created_at', 'updated_at'], 'integer'],
  46. ];
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function attributeLabels()
  52. {
  53. return [
  54. 'id' => '自增id',
  55. 'mall_id' => '商城id',
  56. 'goods_id' => '商品id',
  57. 'changes_type' => '库存变动类型;100向上级补货;101上级给自己配货;102退货退库存;103系统添加;104平台补货赠送;105购买礼包赠送;',
  58. 'stock_log_id' => '库存变动表qimall_addons_cloud_stock_goods_stock_log的id',
  59. 'user_id' => '增加库存代理商用户id',
  60. 'order_id' => '订单id',
  61. 'order_type' => 'order类型,1:会员购买订单,2:补货订单;',
  62. 'num' => '增加数量',
  63. 'status' => '状态[-1:删除;0:禁用;1启用]',
  64. 'created_at' => 'Created At',
  65. 'updated_at' => 'Updated At',
  66. ];
  67. }
  68. public function getUser()
  69. {
  70. return $this->hasOne(User::class, ['id' => 'user_id'])->select(['id','mobile','nickname','avatar_url']);
  71. }
  72. public function getGoods()
  73. {
  74. return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select(['id', 'goods_name', 'cover_pic']);
  75. }
  76. public static function setData(array $data)
  77. {
  78. if (!empty($data['id'])) {
  79. $model = self::findOne(['status' => StatusEnum::ENABLED, 'id' => $data['id']]);
  80. } else {
  81. $model = new self();
  82. $model->loadDefaultValues();
  83. }
  84. foreach ($data as $key => $val) {
  85. $model->$key = $val;
  86. }
  87. // dd($model);
  88. if (!$model->save()) {
  89. self::$static_error = '保存数据失败:' . $model->getErrorMessage();
  90. return false;
  91. }
  92. return $model;
  93. }
  94. /**
  95. * 导出字段数组
  96. * @Author: hua
  97. * @DateTime: 2021/10/11 18:06
  98. * @Copyright: copyright (c) 2021 广东七件事集团
  99. * @return \string[][]
  100. */
  101. public static function fieldsLists($field = null){
  102. $fields = [
  103. 'agent_user_id' => '代理商用户id',
  104. 'agent_user_nickname' => '代理商',
  105. 'goods_id' => '商品id',
  106. 'goods_name' => '商品名称',
  107. 'created_at' => '自提时间',
  108. 'num' => '增加库存数量',
  109. 'changes_type' => '增加库存类型',
  110. 'order_id' => '订单id',
  111. 'created_at' => '时间',
  112. ];
  113. if ($field === null) return $fields;
  114. $temp = [];
  115. foreach ($field as $val) {
  116. if (isset($fields[$val])) $temp[$val] = $fields[$val];
  117. }
  118. return $temp;
  119. }
  120. /**
  121. * 导出
  122. */
  123. public static function exportOrder(array $data) {
  124. $download_id = $data['download_id'] ?? 0;
  125. $download = Download::findOne(['id' => $download_id]);
  126. if ($download) {
  127. try {
  128. $params = json_decode($download->params, true);
  129. $filename = $download->name;
  130. $type = $download->type;
  131. $fields = isset($params['fields']) ? $params['fields'] : null;
  132. $exportField = self::fieldsLists($fields);
  133. $exportField = array_keys($exportField);
  134. $where[] = ['=', 'mall_id', $params['mall_id']];
  135. $where[] = ['in', 'status', [StatusEnum::DISABLED, StatusEnum::ENABLED]];
  136. if (!empty($params['keyword'])) {
  137. $user_list = User::find()->where(['or',
  138. ['id' => $params['keyword']],
  139. ['like', 'mobile', $params['keyword']],
  140. ['like', 'nickname', $params['keyword']]])->select('id')->asArray()->all();
  141. $user_id_arr = array_column($user_list, 'id');
  142. $where[] = ['user_id' => $user_id_arr];
  143. } else {
  144. if ($params['keyword'] != '') {
  145. $where[] = ['user_id' => 0];
  146. }
  147. }
  148. if (isset($params['mobile']) && $params['mobile'] !== '') {
  149. $user_id_arr = User::find()
  150. ->where([
  151. 'mall_id' => $params['mall_id'],
  152. 'status' => StatusEnum::ENABLED
  153. ])
  154. ->andWhere(['like', 'mobile', $params['mobile']])
  155. ->select('id')
  156. ->column();
  157. $where[] = ['user_id' => $user_id_arr];
  158. }
  159. if (!empty($params['goods_name'])) {
  160. $goods_list = Goods::find()->where([
  161. 'like', 'goods_name', $params['goods_name']])->select('id')->asArray()->all();
  162. $goods_list_arr = array_column($goods_list, 'id');
  163. $where[] = ['goods_id' => $goods_list_arr];
  164. }
  165. if (!empty($params['order_id'])) $where[] = ['order_id' => $params['order_id']];
  166. if (!empty($params['changes_type'])) $where[] = ['changes_type' => $params['changes_type']];
  167. if (!empty($params['start'])) $where[] = ['>=', 'created_at', strtotime($params['start'] . ' 00:00:00')];
  168. if (!empty($params['end'])) $where[] = ['<=', 'created_at', strtotime($params['end'] . ' 00:00:00')];
  169. if(!empty($params['order_no'])){
  170. $buying_order_list = CloudStockBuyingOrder::find()
  171. ->alias('c')
  172. ->leftJoin(['order'=>Order::tableName()],'order.id = c.order_id')
  173. ->where(['order.order_no'=>$params['order_no']])
  174. ->select('c.id,c.order_id')->all();
  175. $buying_order_ids = array_column($buying_order_list,'id');
  176. $order_ids = array_column($buying_order_list,'order_id');
  177. $fill_order_ids = CloudStockFillOrder::find()->where(['order_no'=>$params['order_no']])->select('id')->column();
  178. $order_ids = array_merge($buying_order_ids,$order_ids,$fill_order_ids);
  179. $where[] = ['order_id' => $order_ids];
  180. }
  181. $wheres['order'] = 'id desc';
  182. $wheres['with'] = ['user', 'goods'];
  183. $wheres['where'] = $where;
  184. $model = self::find();
  185. self::paramPack($wheres, $model);
  186. $list = [];
  187. $exportheader = [];
  188. $isSetHeader = true;
  189. //所有团队部分修改
  190. $i = 1;
  191. $page_size = 1000;
  192. $change_types = CloudStockEnum::getStockChangeTypeMap();
  193. while ($log_list = $model->asArray()->offset(($i - 1) * $page_size)->limit($page_size)->all()){
  194. foreach ($log_list as $l){
  195. $temp = [];
  196. //按顺序组装
  197. if(in_array('id', $fields)) {
  198. $temp['id'] = $l['id'];
  199. $isSetHeader && array_push($exportheader, '订单ID');
  200. }
  201. if(in_array('agent_user_id', $fields)) {
  202. if (isset($l['user']['id'])) {
  203. $temp['agent_user_id'] = $l['user']['id'];
  204. } else {
  205. $temp['agent_user_id'] = $l['user_id'];
  206. }
  207. $isSetHeader && array_push($exportheader, '代理商用户id');
  208. }
  209. if(in_array('agent_user_nickname', $fields)) {
  210. if (isset($l['user']['id'])) {
  211. $temp['agent_user_nickname'] = $l['user']['nickname'];
  212. } else {
  213. $temp['agent_user_nickname'] = '平台';
  214. }
  215. $isSetHeader && array_push($exportheader, '代理商用户昵称');
  216. }
  217. if(in_array('goods_id', $fields)) {
  218. $temp['goods_id'] = $l['goods_id'];
  219. $isSetHeader && array_push($exportheader, '商品id');
  220. }
  221. if(in_array('goods_name', $fields)) {
  222. $temp['goods_name'] = $l['goods']['goods_name'];
  223. $isSetHeader && array_push($exportheader, '商品名称');
  224. }
  225. if(in_array('num', $fields)) {
  226. $temp['num'] = $l['num'];
  227. $isSetHeader && array_push($exportheader, '增加库存数');
  228. }
  229. if(in_array('changes_type', $fields)) {
  230. switch($l['changes_type']) {
  231. case 200:
  232. $temp['changes_type'] = '会员买货';
  233. break;
  234. case 201:
  235. $temp['changes_type'] = '给下级配货';
  236. break;
  237. case 202:
  238. $temp['changes_type'] = '下级向自己补货';
  239. break;
  240. case 203:
  241. $temp['changes_type'] = '自提货到线下';
  242. break;
  243. case 204:
  244. $temp['changes_type'] = '系统扣减';
  245. break;
  246. case 100:
  247. $temp['changes_type'] = '向上级补货';
  248. break;
  249. case 101:
  250. $temp['changes_type'] = '上级给自己配货';
  251. break;
  252. case 102:
  253. $temp['changes_type'] = '退货退库存';
  254. break;
  255. case 103:
  256. $temp['changes_type'] = '系统添加';
  257. break;
  258. case 104:
  259. $temp['changes_type'] = '平台补货赠送';
  260. break;
  261. case 105:
  262. $temp['changes_type'] = '购买礼包赠送';
  263. break;
  264. case \addons\CloudStock\common\enums\CloudStockEnum::CHANGE_TYPE_PICKOFFLINE_RETURN:
  265. $temp['changes_type'] = '取消自提';
  266. break;
  267. case 107:
  268. $temp['changes_type'] = '购买赠送方案赠送';
  269. break;
  270. default:
  271. $temp['changes_type'] = '';
  272. };
  273. $isSetHeader && array_push($exportheader, '增加库存类型');
  274. }
  275. if(in_array('order_id', $fields)) {
  276. $temp['order_id'] = $l['order_id'];
  277. $isSetHeader && array_push($exportheader, '订单id');
  278. }
  279. if(in_array('created_at', $fields)) {
  280. $temp['created_at'] = date('Y-m-d H:i:s', $l['created_at']);
  281. $isSetHeader && array_push($exportheader, '时间');
  282. }
  283. $list[] = $temp;
  284. $isSetHeader = false;
  285. }
  286. $i++;
  287. }
  288. if (!empty($list)) {
  289. $csv = new CsvTool();
  290. $csv->filename = $filename;
  291. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  292. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  293. $csv->header = $exportheader;
  294. $csv->data = $list;
  295. $csv->export();
  296. $csv->updateDown($download, $params['mall_id']);
  297. }
  298. }catch (\Exception $e) {
  299. $download->status = 3;
  300. $res = $download->save();
  301. var_dump($e->getMessage().$e->getFile().$e->getLine());
  302. self::setStaticError($e->getMessage());
  303. return $e->getMessage();
  304. }
  305. }
  306. }
  307. }