CloudStockDeductionDetail.php 12 KB

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