BusinessBonusStore.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. namespace addons\BusinessBonus\common\models;
  3. use addons\BusinessBonus\common\enums\BusinessBonusEnums;
  4. use addons\Mch\common\models\Mch;
  5. use addons\Store\common\models\Store;
  6. use common\components\export\CsvTool;
  7. use common\enums\DownloadEnum;
  8. use common\enums\StatusEnum;
  9. use common\models\common\Download;
  10. use common\models\user\User;
  11. use Yii;
  12. use Exception;
  13. /**
  14. * This is the model class for table "{{%addons_business_bonus_store}}".
  15. *
  16. * @property int $id
  17. * @property int|null $mall_id 商城ID
  18. * @property int|null $store_id 门店、多商户的ID
  19. * @property int|null $store_type 类型:1门店,2多商户
  20. * @property int|null $store_user_id 店主、商户user_id
  21. * @property int|null $business_user_id 招商员user_id
  22. * @property float|null $history_income 累计收入
  23. * @property float|null $history_earn 累计利润
  24. * @property int|null $status 状态:-1删除,0禁用,1启用
  25. * @property int|null $created_at 创建时间
  26. * @property int|null $updated_at 更新时间
  27. */
  28. class BusinessBonusStore extends \common\models\base\BaseActiveRecord
  29. {
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public static function tableName()
  34. {
  35. return '{{%addons_business_bonus_store}}';
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function rules()
  41. {
  42. return [
  43. [['mall_id', 'store_id', 'store_type', 'store_user_id', 'business_user_id', 'status', 'store_created_at', 'created_at', 'updated_at'], 'integer'],
  44. [['history_income', 'history_earn'], 'number'],
  45. ];
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'id' => 'ID',
  54. 'mall_id' => '商城ID',
  55. 'store_id' => '门店、多商户的ID',
  56. 'store_type' => '类型:1门店,2多商户',
  57. 'store_user_id' => '店主、商户user_id',
  58. 'business_user_id' => '招商员user_id',
  59. 'history_income' => '累计收入',
  60. 'history_earn' => '累计利润',
  61. 'status' => '状态:-1删除,0禁用,1启用',
  62. 'store_created_at' => '门店创建时间',
  63. 'created_at' => '创建时间',
  64. 'updated_at' => '更新时间',
  65. ];
  66. }
  67. /**
  68. * 数据设置
  69. */
  70. public static function setData(array $params)
  71. {
  72. try{
  73. if (!isset($params['mall_id']) || empty($params['mall_id'])) throw new Exception('缺少商城ID');
  74. if (isset($params['store_id']) && !empty($params['store_id'])) {
  75. $model = self::findOne(['mall_id' => $params['mall_id'], 'store_id' => $params['store_id'], 'store_type' => $params['store_type'], 'status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]);
  76. if ($model) {
  77. if (isset($params['history_income'])) {
  78. $model->history_income += $params['history_income'];
  79. unset($params['history_income']);
  80. }
  81. if (isset($params['history_earn'])) {
  82. $model->history_earn += $params['history_earn'];
  83. unset($params['history_earn']);
  84. }
  85. }
  86. }
  87. if (!isset($model) || empty($model)) {
  88. $model = new self();
  89. $model->loadDefaultValues();
  90. $model->mall_id = $params['mall_id'];
  91. $model->status = StatusEnum::ENABLED;
  92. }
  93. if (!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
  94. return $model;
  95. }catch(Exception $e){
  96. self::setStaticError($e->getMessage());
  97. return false;
  98. }
  99. }
  100. /**
  101. * 门店店主
  102. * @return \yii\db\ActiveQuery
  103. */
  104. public function getStoreUser()
  105. {
  106. return $this->hasOne(User::class, ['id' => 'store_user_id'])->select('id,mobile,nickname,avatar_url,parent_id,username');
  107. }
  108. /**
  109. * 招商员
  110. * @return \yii\db\ActiveQuery
  111. */
  112. public function getBusinessUser()
  113. {
  114. return $this->hasOne(User::class, ['id' => 'business_user_id'])->select('id,mobile,nickname,avatar_url,parent_id,username');
  115. }
  116. /**
  117. * 处理门店列表
  118. */
  119. public static function dealStoreList(array $list, $mall_id)
  120. {
  121. $store_ids = $mch_ids = [];
  122. foreach ($list as $item) {
  123. if ($item['store_type'] == BusinessBonusEnums::STORE_TYPE_STORE) {
  124. $store_ids[] = $item['store_id'];
  125. } else {
  126. $mch_ids[] = $item['store_id'];
  127. }
  128. }
  129. $store_array = $mch_array = [];
  130. if ($store_ids) {
  131. $store_list = Store::find()
  132. ->select('id,user_id,store_name,expire_time,user_nums,created_at,province_id,city_id,district_id,address')
  133. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  134. ->andWhere(['in', 'id', $store_ids])
  135. ->andWhere(['=', 'mall_id', $mall_id])
  136. ->asArray()
  137. ->all();
  138. $store_array = array_column($store_list, null, 'id');
  139. }
  140. if ($mch_ids) {
  141. $mch_list = Mch::find()
  142. ->select('id,user_id,store_name,expire_time,user_nums,created_at,province_id,city_id,district_id,address')
  143. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  144. ->andWhere(['in', 'id', $mch_ids])
  145. ->andWhere(['=', 'mall_id', $mall_id])
  146. ->asArray()
  147. ->all();
  148. $mch_array = array_column($mch_list, null, 'id');
  149. }
  150. foreach ($list as $key => $val) {
  151. if ($val['store_type'] == BusinessBonusEnums::STORE_TYPE_STORE) {
  152. $val['store_info'] = $store_array[$val['store_id']] ?? [];
  153. } else if ($val['store_type'] == BusinessBonusEnums::STORE_TYPE_MCH) {
  154. $val['store_info'] = $mch_array[$val['store_id']] ?? [];
  155. }
  156. $list[$key] = $val;
  157. }
  158. return $list;
  159. }
  160. /**
  161. * 可导出的字段
  162. * @param null $field
  163. * @return array
  164. */
  165. public static function fieldsList($field = null, $store_type)
  166. {
  167. if ($store_type == BusinessBonusEnums::FROM_TYPE_STORE) {
  168. $store_name = '门店';
  169. $store_user_nums = '门店会员总数';
  170. } else {
  171. $store_name = '商户';
  172. $store_user_nums = '商户会员总数';
  173. }
  174. $fields = [
  175. 'id' => '序号',
  176. 'store_name' => $store_name,
  177. 'store_user_id' => '店长ID',
  178. 'store_user_nickname' => '店长昵称',
  179. 'store_user_mobile' => '店长手机号',
  180. 'business_user_id' => '招商员ID',
  181. 'business_user_nickname' => '招商员昵称',
  182. 'business_user_mobile' => '招商员手机号',
  183. 'history_income' => '累计收入',
  184. 'history_earn' => '累计利润',
  185. 'store_user_nums' => $store_user_nums,
  186. 'expire_time' => '有效期',
  187. 'store_created_at' => '创建时间',
  188. ];
  189. if ($field === null) return $fields;
  190. $temp = [];
  191. foreach ($field as $val) {
  192. $temp[$val] = $fields[$val] ?? $val;
  193. }
  194. return $temp;
  195. }
  196. /**
  197. * 导出处理手柄
  198. */
  199. public static function exportHandle(array $data)
  200. {
  201. $download_id = $data['download_id'] ?? 0;
  202. $download = Download::findOne(['id' => $download_id]);
  203. if ($download) {
  204. try {
  205. $params = json_decode($download->params, true);
  206. $condition = $params['condition'];
  207. $list = self::lists($condition, true);
  208. $list = self::dealStoreList($list, $params['mall_id']);
  209. $filename = $download->name;
  210. $type = $download->type;
  211. $fields = $params['fields'];
  212. $fields_arr = self::fieldsList(null, $params['store_type']);
  213. $have_select_field_arr = [];
  214. foreach ($fields as $value) {
  215. $have_select_field_arr[] = $fields_arr[$value];
  216. }
  217. $csv = new CsvTool();
  218. $csv->filename = $filename;
  219. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  220. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  221. $csv->header = $have_select_field_arr;
  222. $csv_data = [];
  223. foreach ($list as $data) {
  224. $row = [];
  225. foreach ($fields as $field) {
  226. switch ($field) {
  227. case 'store_name':
  228. $row[] = $data['store_info']['store_name'] ?? '';
  229. break;
  230. case 'store_user_mobile':
  231. $row[] = $data['storeUser']['mobile'] ?? '';
  232. break;
  233. case 'store_user_nickname':
  234. $row[] = $data['storeUser']['nickname'] ?? '';
  235. break;
  236. case 'business_user_nickname':
  237. $row[] = $data['businessUser']['nickname'] ?? '';
  238. break;
  239. case 'business_user_mobile':
  240. $row[] = $data['businessUser']['mobile'] ?? '';
  241. break;
  242. case 'store_user_nums':
  243. $row[] = $data['store_info']['user_nums'] ?? '';
  244. break;
  245. case 'expire_time':
  246. $time = $data['store_info']['expire_time'] ?? '';
  247. $row[] = $time ? date('Y-m-d H:i:s', $time) : '';
  248. break;
  249. case 'store_created_at':
  250. $time = $data['store_created_at'] ?? '';
  251. $row[] = $time ? date('Y-m-d H:i:s', $time) : '';
  252. break;
  253. default:
  254. $row[] = $data[$field] ?? '';
  255. break;
  256. }
  257. }
  258. $csv_data[] = $row;
  259. }
  260. $csv->data = $csv_data;
  261. $csv->export();
  262. $csv->updateDown($download, $params['mall_id']);
  263. return true;
  264. } catch (Exception $e) {
  265. $download->status = 3;
  266. $download->save();
  267. self::setStaticError($e->getMessage());
  268. return $e->getMessage();
  269. }
  270. }
  271. }
  272. }