| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- namespace addons\WriteOff\common\models;
- use Yii;
- use common\enums\StatusEnum;
- use addons\WriteOff\common\enums\WriteOffEnum;
- use common\models\user\User;
- use common\models\order\OrderDetail;
- use common\enums\DownloadEnum;
- use common\models\common\Download;
- use common\components\export\CsvTool;
- /**
- * This is the model class for table "{{%addons_write_off_promote_order_log}}".
- *
- * @property int $id ID
- * @property int $mall_id 商城ID
- * @property int $user_id 用户ID
- * @property int $order_id 订单ID
- * @property string $order_no 订单编号
- * @property float|null $pay_money 订单实付金额
- * @property int $buy_user_id 购买用户ID
- * @property int $status 状态[-1删除; 0禁用; 1正常]
- * @property int $created_at 创建时间
- * @property int $updated_at 更新时间
- */
- class WriteOffPromoteOrderLog extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_write_off_promote_order_log}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'order_id', 'buy_user_id', 'status', 'created_at', 'updated_at','order_detail_id'], 'integer'],
- [['pay_money'], 'number'],
- [['order_no'], 'string', 'max' => 100],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'user_id' => 'User ID',
- 'order_id' => 'Order ID',
- 'order_no' => 'Order No',
- 'pay_money' => 'Pay Money',
- 'buy_user_id' => 'Buy User ID',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- /**
- * 保存数据
- * @Author: lun
- * @DateTime: 2021/10/20 0020 18:01
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @return AddonsBossLevel|bool
- */
- public static function setData(array $params){
- $trans = Yii::$app->db->beginTransaction();
- try{
- if(!empty($params['id'])){
- $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]);
- if(empty($model)) throw new \Exception('数据不存在或已删除');
- }else{
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $params['mall_id'];
- }
- if(!$model->load($params,'') || !$model->save()){
- throw new \Exception($model->getErrorMessage());
- }
- $trans->commit();
- return $model;
- }catch(\Exception $e){
- $trans->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 导出字段
- */
- public static function exportFieldsList($field = null)
- {
- $fields = [
- 'log.user_id' => '会员ID',
- 'user.nickname' => '会员昵称',
- 'user.mobile' => '会员手机',
- 'log.goods_name' => '商品名称',
- 'log.pay_money' => '订单金额',
- 'log.order_no' => '订单编号',
- 'log.created_at' => '创建时间',
- ];
- $temp = [];
- foreach ($fields as $key => $val) {
- $showField = explode('.', $key)[1];
- if (strstr($showField, ' ')) {
- $showField = explode(' ', $showField)[1];
- }
- if ($field === null) {
- $temp[$showField] = $val;
- } else if (in_array($showField, $field)) {
- $temp[$key] = $val;
- }
- }
- return $temp;
- }
- /**
- * 导出资产
- */
- public static function exportAssets(array $data) {
- $download_id = $data['download_id'] ?? 0;
- $download = Download::findOne(['id' => $download_id]);
- if ($download) {
- try {
- $params = json_decode($download->params, true);
- $filename = $download->name;
- $type = $download->type;
- $fields = isset($params['fields']) ? $params['fields'] : null;
- $exportField = self::exportFieldsList($fields);
- $exportField = array_keys($exportField);
- if (in_array('user_id', $fields) || in_array('nickname', $fields) || in_array('mobile', $fields)) array_push($exportField,'log.buy_user_id');
- if (in_array('goods_name', $fields)) array_push($exportField,'log.order_detail_id');
- $where['where'][] = ['=', 'log.mall_id', $params['mall_id']];
- $where['where'][] = ['=', 'log.user_id',$params['user_id']];
- // 获取配置
- $config = Yii::$app->services->setting->addons->get($params['mall_id'], WriteOffEnum::ADDONS_NAME, 'basic');
- $compute_period = $config['compute_period'];
- $start_day = 0;
- $end_day = 0;
- switch ($config['compute_period']) {
- case 0:
- if (date('d') != 1) $continue = true;// 非当月1号直接退出
- $start_day = date("Ym01", strtotime("-1 month",$params['created_at']));
- $end_day = date('Ymd', strtotime(date('Y-m', $params['created_at']) . '-01 00:00:00') - 86400);
- break;
- }
- $start_day = strtotime($start_day);
- $end_day = strtotime(date('Y-m-d 23:59:59',strtotime($end_day)));
- $where['where'][] = ['between', 'log.created_at', $start_day, $end_day];
- if (!empty($params['date_start']) && !empty($params['date_end'])) {
- $where['where'][] = ['between', 'log.created_at', strtotime($params['date_start']), strtotime($params['date_end'])];
- }
- if(!empty($params['order_no'])){
- $where['where'][] = ['=','log.order_no',$params['order_no']];
- }
- // 根据搜索手机号查询用户id
- if (!empty($params['keyword'])) {
- $user_ids = User::find()->select("id")
- ->where(['mall_id' => $params['mall_id']])
- ->andWhere([
- 'or',
- ['=', 'id', $params['keyword']],
- ['like', 'mobile', $params['keyword'] . '%', false],
- ['like', 'nickname', $params['keyword'] . '%', false]
- ])
- ->asArray()
- ->column();
- $where['where'][] = ['in', 'log.buy_user_id', $user_ids];
- }
- $join = [['LEFT JOIN', User::tableName() . 'as user', 'log.buy_user_id = user.id']];
- $where['alias'] = 'log';
- $where['join'] = $join;
- $where['order'] = 'log.created_at desc,log.id desc';
- foreach($exportField as $key=> &$item){
- if($item == 'log.goods_name'){
- unset($exportField[$key]);
- }
- }
- $where['select'] = $exportField;
- $model = self::find();
- self::paramPack($where, $model);
- $list = [];
- $exportheader = [];
- $isSetHeader = true;
- //所有团队部分修改
- $i = 1;
- $page_size = 1000;
- while ($log_list = $model->asArray()->offset(($i - 1) * $page_size)->limit($page_size)->all()){
- foreach ($log_list as $l){
- $w_user = null;
- $temp = [];
- //按顺序组装
- if(in_array('user_id', $fields)) {
- $temp['buy_user_id'] = $l['buy_user_id'];
- $isSetHeader && array_push($exportheader, '会员ID');
- }
- if(in_array('nickname', $fields)) {
- $temp['nickname'] = $l['nickname'];
- $isSetHeader && array_push($exportheader, '会员昵称');
- }
- if(in_array('mobile', $fields)) {
- $temp['mobile'] = $l['mobile'];
- $isSetHeader && array_push($exportheader, '会员手机');
- }
- if(in_array('goods_name', $fields)) {
- $goods_name = '';
- $order_detail = OrderDetail::find()->where(['id'=>$l['order_detail_id']])->asArray()->one();
- if(!empty($order_detail)){
- $goods_name = $order_detail['goods_name'];
- }
- $temp['goods_name'] = $goods_name;
- $isSetHeader && array_push($exportheader, '商品名称');
- }
- if(in_array('order_no', $fields)) {
- $temp['order_no'] = $l['order_no'];
- $isSetHeader && array_push($exportheader, '订单编号');
- }
- if(in_array('pay_money', $fields)) {
- $temp['pay_money'] = $l['pay_money'];
- $isSetHeader && array_push($exportheader, '订单金额');
- }
- if(in_array('created_at', $fields)) {
- $temp['created_at'] = date('Y-m-d H:i:s', $l['created_at']);
- $isSetHeader && array_push($exportheader, '创建时间');
- }
- $list[] = $temp;
- $isSetHeader = false;
- }
- $i++;
- }
- if (!empty($list)) {
- $csv = new CsvTool();
- $csv->filename = $filename;
- $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
- $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
- $csv->header = $exportheader;
- $csv->data = $list;
- $csv->export();
- $csv->updateDown($download, $params['mall_id']);
- }
- } catch (\Exception $e) {
- $download->status = 3;
- $res = $download->save();
- var_dump($e->getMessage().$e->getFile().$e->getLine());
- return $e->getMessage();
- }
- }
- }
- }
|