AgentCommissionDetails.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace addons\Agent\common\models;
  3. use common\components\export\CsvTool;
  4. use common\enums\DownloadEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\common\Download;
  7. use common\models\order\Order;
  8. use common\models\order\OrderDetail;
  9. use common\models\user\User;
  10. /**
  11. * This is the model class for table "{{%agent_commission_details}}".
  12. *
  13. * @property int $id ID
  14. * @property int $mall_id 商城ID
  15. * @property int $user_id 经销商用户ID
  16. * @property int $agent_id 经销商ID
  17. * @property int $order_id 订单ID
  18. * @property int $level 经销商等级
  19. * @property float $order_price 订单金额
  20. * @property float $commission 佣金金额
  21. * @property string $reward_type 奖励类型
  22. * @property string $order_source 来源,order:主订单表
  23. * @property int $status 状态[-1删除; 0 禁用; 1正常]
  24. * @property int $created_at 创建时间
  25. * @property int $updated_at 更新时间
  26. */
  27. class AgentCommissionDetails extends \common\models\base\BaseActiveRecord
  28. {
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public static function tableName()
  33. {
  34. return '{{%agent_commission_details}}';
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['mall_id', 'order_id'], 'required'],
  43. [['mall_id', 'user_id', 'agent_id', 'order_id', 'level', 'status', 'created_at', 'updated_at'], 'integer'],
  44. [['order_price', 'commission'], 'number'],
  45. [['reward_type'], 'string', 'max' => 32],
  46. [['order_source'], 'string'],
  47. ];
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function attributeLabels()
  53. {
  54. return [
  55. 'id' => 'ID',
  56. 'mall_id' => '商城ID',
  57. 'user_id' => '经销商用户ID',
  58. 'agent_id' => '经销商ID',
  59. 'order_id' => '订单ID',
  60. 'level' => '经销商等级',
  61. 'order_price' => '订单金额',
  62. 'commission' => '佣金金额',
  63. 'reward_type' => '奖励类型',
  64. 'order_source' => '来源,order:主订单表',
  65. 'status' => '状态[-1删除; 0 禁用; 1正常]',
  66. 'created_at' => '创建时间',
  67. 'updated_at' => '更新时间',
  68. ];
  69. }
  70. public function getUser(){
  71. return $this->hasOne(User::class, ['id' => 'user_id']);
  72. }
  73. public function getAgentLevel(){
  74. return $this->hasOne(AgentLevel::class, ['level' => 'level','mall_id'=>'mall_id'])->andWhere(['<>','status',StatusEnum::DELETE]);
  75. }
  76. /**
  77. * 导出字段
  78. */
  79. public static function exportFieldsList($field = null)
  80. {
  81. $fields = [
  82. 'user_id' => '用户ID',
  83. 'nickname' => '昵称',
  84. 'mobile' => '手机号',
  85. 'created_at' => '时间',
  86. 'commission' => '累计佣金',
  87. 'level' => '经销商等级',
  88. 'order_no' => '订单号',
  89. 'order_price' => '订单金额',
  90. 'goods_name' => '商品名称'
  91. ];
  92. return $fields;
  93. $temp = [];
  94. foreach ($fields as $key => $val) {
  95. $showField = explode('.', $key)[1];
  96. if (strstr($showField, ' ')) {
  97. $showField = explode(' ', $showField)[1];
  98. }
  99. if ($field === null) {
  100. $temp[$showField] = $val;
  101. } else if (in_array($showField, $field)) {
  102. $temp[$key] = $val;
  103. }
  104. }
  105. return $temp;
  106. }
  107. public static function fieldsList($field = null)
  108. {
  109. $fields = [
  110. 'user_id' => '用户ID',
  111. 'nickname' => '昵称',
  112. 'mobile' => '手机号',
  113. 'created_at' => '时间',
  114. 'commission' => '累计佣金',
  115. 'level' => '经销商等级',
  116. 'order_no' => '订单号',
  117. 'order_price' => '订单金额',
  118. 'goods_name' => '商品名称'
  119. ];
  120. if ($field === null) return $fields;
  121. $temp = [];
  122. foreach ($field as $val) {
  123. if (isset($fields[$val])) $temp[$val] = $fields[$val];
  124. }
  125. return $temp;
  126. }
  127. /**
  128. * @name:
  129. * @msg: 处理导出数据,这个方法是异步队列处理时使用的,请别修改
  130. * @param {array} $data
  131. * @return bool
  132. */
  133. public static function exportHandle(array $data)
  134. {
  135. $download_id = $data['download_id'] ?? 0;
  136. $download = Download::findOne(['id' => $download_id]);
  137. if ($download) {
  138. try {
  139. $params = json_decode($download->params, true);
  140. $filename = $download->name;
  141. $type = $download->type;
  142. $fields = $params['fields'];
  143. $fields_arr = self::fieldsList();
  144. $have_select_field_arr = [];
  145. foreach ($fields as $value) {
  146. $have_select_field_arr[] = $fields_arr[$value];
  147. }
  148. $csv = new CsvTool();
  149. $csv->filename = $filename;
  150. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  151. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  152. $csv->header = $have_select_field_arr;
  153. $model = self::find();
  154. $model->where(['mall_id' => $params['mall_id']]);
  155. $model->andWhere(['>=', 'status', StatusEnum::DISABLED]);
  156. $user_id_arr = [];
  157. //拼接查询条件
  158. if (!empty($params['level'])) $model->andWhere(['=', 'level', $params['level']]);
  159. if (!empty($params['keyword'])) {
  160. $condition['where'] = [[
  161. 'or',
  162. ['like', 'nickname', $params['keyword']],
  163. ['like', 'mobile', $params['keyword']],
  164. ['id' => $params['keyword']]
  165. ]];
  166. $user_list = User::lists($condition);
  167. $user_id_arr = array_column($user_list, 'id');
  168. }
  169. if ($user_id_arr) {
  170. $model->andWhere(['in', 'user_id', $user_id_arr]);
  171. }
  172. if ($params['date_start']) {
  173. $model->andWhere(['>=', 'created_at', $params['date_start']]);
  174. }
  175. if ($params['date_end']) {
  176. $model->andWhere(['<=', 'created_at', $params['date_end']]);
  177. }
  178. if ($params['order_no']) {
  179. $order_list = Order::lists([
  180. 'where' => [
  181. ['mall_id' => $params['mall_id']],
  182. ['like', 'order_no', $params['order_no']]
  183. ],
  184. 'select' => 'id',
  185. ]);
  186. $model->andWhere(['order_id' => array_column($order_list, 'id') ?? []]);
  187. }
  188. $model->with(['user', 'agentLevel' => function ($query) {
  189. $query->where(['status' => StatusEnum::ENABLED]);
  190. }]);
  191. //遍历组装数据
  192. foreach ($model->asArray()->batch() as $list) {
  193. $order_detail_list = OrderDetail::lists([
  194. 'where' => [
  195. ['mall_id' => $params['mall_id']],
  196. ['order_id' => array_column($list, 'order_id') ?? []]
  197. ],
  198. 'select' => 'id, goods_name, order_id',
  199. 'index' => 'order_id',
  200. 'with' => [
  201. 'order' => function ($query) {
  202. return $query->select('id, order_no');
  203. }
  204. ]
  205. ]);
  206. //查询上级昵称数组
  207. $csv->data = array_map(function ($agent) use ($fields, $order_detail_list) {
  208. $row = [];
  209. //遍历所选字段
  210. foreach ($fields as $field) {
  211. if (in_array($field, ['created_at'])) {
  212. $row[] = !empty($agent[$field]) ? date('Y-m-d H:i:s', $agent[$field]) : '';
  213. } elseif (in_array($field, ['nickname', 'mobile'])) {
  214. $row[] = !empty($agent['user'][$field]) ? $agent['user'][$field] : '';
  215. } elseif (in_array($field, ['level'])) {
  216. $row[] = !empty($agent['agentLevel']['name']) ? $agent['agentLevel']['name'] : '';
  217. } elseif (in_array($field, ['order_no'])) {
  218. $row[] = isset($order_detail_list[$agent['order_id']]) ? $order_detail_list[$agent['order_id']]['order']['order_no'] : '';
  219. } elseif (in_array($field, ['goods_name'])) {
  220. $row[] = isset($order_detail_list[$agent['order_id']]) ? $order_detail_list[$agent['order_id']]['goods_name'] : '';
  221. } else {
  222. $row[] = !empty($agent[$field]) ? $agent[$field] : '';
  223. }
  224. }
  225. return $row;
  226. }, $list);
  227. $csv->export();
  228. }
  229. $csv->updateDown($download, $params['mall_id']);
  230. return true;
  231. } catch (\Exception $e) {
  232. $download->status = 3;
  233. $res = $download->save();
  234. var_dump($e->getMessage());
  235. self::setStaticError($e->getMessage());
  236. return false;
  237. }
  238. }
  239. }
  240. /**
  241. * @desc:保存数据
  242. * @Author: hua
  243. * @Date: 2021/10/23
  244. * @Time: 17:23
  245. * @Copyright: copyright (c) 2021 广东七件事集团
  246. * @param array $params
  247. * @return Agent|false|null
  248. */
  249. public static function setData(array $params)
  250. {
  251. try {
  252. if(!empty($params['id'])){
  253. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  254. if (empty($model)) throw new \Exception('服务不存在或已删除');
  255. }else{
  256. $model = new self();
  257. $model->loadDefaultValues();
  258. }
  259. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  260. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  261. return $model;
  262. } catch (\Exception $e) {
  263. self::$static_error = $e->getMessage();
  264. return false;
  265. }
  266. }
  267. }