<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\common\model\group_activity;

use app\common\model\BaseModel;
use think\model\concern\SoftDelete;


/**
 * 拼团活动模型
 * Class GroupActivity
 * @package app\common\model\group_activity
 */
class GroupActivity extends BaseModel
{
    use SoftDelete;
    protected $name = 'group_activity';
    protected $deleteTime = 'delete_time';
    public function equity()
    {
        return $this->hasOne(GroupEquity::class, 'id', 'equity_id')
            ->field('id,equity_id');
    }

    public static function getDetail($id) {
        $detail = GroupActivity::alias('a')->leftJoin('group_activity_category b','a.group_category_id=b.id')
                    ->where('a.id',$id)
                    ->field('a.id,a.title,a.image,a.equity_id,a.origin_price,a.price,a.work_amount,a.group_category_id,b.property_head_id,b.start_time,b.end_time,b.service_time,b.area,b.participant_num,b.form_time_limit,b.is_simulate_form,b.simulate_num,b.type')
                    ->findOrEmpty()
                    ->toArray();
        if(!empty($detail)){
            $detail['start_time'] = date('Y-m-d H:i:s',$detail['start_time']);
            $detail['service_time'] = date('Y-m-d',$detail['service_time']);
            $detail['area'] = explode(",",$detail['area']);
            $detail['price'] = explode(",",$detail['price']);
            $detail['participant_num'] = explode(",",$detail['participant_num']);
        }
        return $detail;
    }

    public static function getActivityList($catetory_id,$block_key = ''){
        $where['group_category_id'] = $catetory_id;
        if ($block_key) {
            $where['block_key'] = $block_key;
        }
        $detail = GroupActivityCategory::where('id',$catetory_id)
                ->field('id,title,images,start_time,end_time,service_time,area,participant_num,form_time_limit,is_simulate_form,simulate_num,type,block_data')
                ->findOrEmpty()
                ->toArray();
        $category = [
            'id' => $detail['id'],
            'title' => $detail['title'],
            'images' => explode(",",$detail['images']),
            'block_data' => json_decode($detail['block_data'],true),
        ];
        unset($detail['block_data']);
        $detail = array_diff_key($detail,$category);
        $detail['area'] = explode(",",$detail['area']);
        $detail['participant_num'] = explode(",",$detail['participant_num']);
        $activity = GroupActivity::where($where)
            ->field('id,title,image,equity_id,block_key,origin_price,price,work_amount')
            ->select()
            ->toArray();
        $list = [];
        foreach($activity as $item) {
            $item['price'] = explode(",",$item['price']);
            $item = array_merge($item,$detail);
            $list[$item['block_key']]['block_key'] = $item['block_key'];
            $list[$item['block_key']]['activity'][] = $item;
        }

        foreach($category['block_data'] as &$item) {
            if (isset($list[$item['block_key']])) {
                $item['activity'] = $list[$item['block_key']]['activity'];
            }
            if ($block_key && $item['block_key'] == $block_key) {
                return $item;
            }
        }
        return $category;
    }
    
}