<?php

namespace app\common\model\project;

use app\common\model\Category;
use liuniu\BaseModel;
use think\Model;


class ProjectDonation extends BaseModel
{

    

    

    // 表名
    protected $name = 'project_donation';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'start_time_text',
        'end_time_text',
        'verify_time_text',
        'feedback_time_text',
        'status_text'
    ];
    

    
    public function getStatusList()
    {
        return ['-1' => __('Status -1'), '0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }


    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getVerifyTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['verify_time']) ? $data['verify_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getFeedbackTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['feedback_time']) ? $data['feedback_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setVerifyTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setFeedbackTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    public static function lst($where)
    {
        $model = new self;
        $where1 = null;
        if (isset($where['cid']) && $where['cid']>0) $where1['cid'] = $where['cid'];
        if (isset($where['title']) && $where['title'] !== '') $where1['title'] =['LIKE',"%$where[title]%"];
        if (isset($where['status']) && $where['status']>-2) $where1['status'] = $where['status'];
        if (isset($where['uid']) && $where['uid']>0) $where1['uid'] = $where['uid'];
        $list =  $model->where($where1)->order('sort desc,id desc')->page($where['page'],$where['limit'])->select();
        foreach ($list as &$v)
        {
             $sum = ProjectDonationInfo::where('pro_id', $v['id'])->group('pro_id')->field('sum(project_donation_num) as project_donation_num ,sum(received) as received')->find();
            $v['project_progress'] = bcdiv($sum['received'],$sum['project_donation_num'],2);
        }
        $count = $model->where($where1)->count();
        return compact('count','list');
    }
    public static function info($id)
    {
        $info = self::find($id);
        if($info) {
            $rs =  ProjectDonationInfo::where('pro_id', $id)->select();

            foreach ($rs as &$v)
            {
                $v['ify_tile'] = Category::where('id',$v['project_donation_id'])->value('name');
            }
            $info['project_donation_info'] =$rs;
            $sum = ProjectDonationInfo::where('pro_id', $id)->group('pro_id')->field('sum(project_donation_num) as project_donation_num ,sum(received) as received')->find();
            $info['project_progress'] = bcdiv($sum['received'],$sum['project_donation_num'],2);
            $info['sum']  = $sum;
        }
        return $info;
    }


}
