<?php

namespace app\common\model;

use liuniu\BaseModel;

use think\Exception;
use traits\model\SoftDelete;

class Help extends BaseModel
{

    use SoftDelete;



    // 表名
    protected $name = 'help';

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

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

    // 追加属性
    protected $append = [
        'isrec_text',
        'status_text'
    ];



    public function getIsrecList()
    {
        return ['0' => __('Isrec 0'), '1' => __('Isrec 1')];
    }

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


    public function getIsrecTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['isrec']) ? $data['isrec'] : '');
        $list = $this->getIsrecList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public static function help_create($where)
    {
        self::beginTrans();
        try {
            list($help['cid'],$help['category_id'],$help['user_id'],$help['full_name'],$help['contact'],$help['id_card'],$help['title'],$help['info'],$help['userimage'],$help['userimages'],$help['status']) =
                [$where['cid'],$where['category_id'],$where['user_id'],$where['full_name'],$where['contact'],$where['id_card'],$where['title'],$where['info'],$where['userimage'],$where['userimages'],0];
            $res = self::create($help);
            list($HelpExt['cid'],$HelpExt['help_id'],$HelpExt['sex'],$HelpExt['politics'],$HelpExt['birthday'],$HelpExt['address'],$HelpExt['company'],$HelpExt['unit_nature'],$HelpExt['monthly_income'],$HelpExt['annual_household_income'],$HelpExt['annual_household_income_average'],$HelpExt['identity'],$HelpExt['marriage'],$HelpExt['medical_insurance'],$HelpExt['object_features'],$HelpExt['reason']) =
                [$where['cid'],$res['id'],$where['sex'],$where['politics'],$where['birthday'],$where['address'],$where['company'],$where['unit_nature'],$where['monthly_income'],$where['annual_household_income'],$where['annual_household_income_average'],$where['identity'],$where['marriage'],$where['medical_insurance'],$where['object_features'],$where['reason']];
            $res1 = HelpExt::create($HelpExt);
            foreach ($where['family'] as $v)
            {
                $v['cid'] = $where['cid'];
                $v['help_id'] = $res['id'];
                if(isset($v['full_name']) && isset($v['relation']))
                {
                    HelpFamily::create($v);
                }
            }
            self::commitTrans();
            return $res;

        }catch (Exception $e)
        {
            self::setErrorInfo($e->getLine().'--'.$e->getMessage(),true);
        }
    }
    public static function lst($where)
    {
        $model = new self;
        if(isset($where['cid']) && $where['cid']>0) $model->where('cid',$where['cid']);
        if(isset($where['user_id']) && $where['user_id']>0) $model->where('user_id',$where['user_id']);
        if(isset($where['category_id']) && $where['category_id']>0) $model->where('category_id',$where['category_id']);
        if(isset($where['status']) && $where['status']>-2) $model->where('status',$where['status']);
        if(isset($where['is_rec']) && $where['is_rec']==1) $model->where('is_rec',$where['is_rec']);
        $data = $model->order('id desc')->page($where['page'],$where['limit'])->select();
        foreach ($data as &$v)
        {
            $v['is_apply'] = HelpApply::where('help_id',$v['id'])->where('user_id',$where['user_id_1'])->find();
            $v['apply_sum'] = HelpApply::where('help_id',$v['id'])->value('count(id)')?:0;
            $v['category'] = Category::where('id',$v['category_id'])->value('name');
            if($v['apply_user_id']>0)
            {
                $v['apply_user'] = HelpApply::where('help_id',$v['id'])->where('user_id',$v['apply_user_id'])->find();
            }
            else
            {
                $v['apply_user'] = null;
            }
        }
        return $data;
    }
    public static function info($cid,$id,$user_id=0)
    {
        $info = self::where('cid',$cid)->where('id',$id)->find();
        if(!$info)return self::setErrorInfo('非法数据');
        $info['ext'] = HelpExt::where('help_id',$info['id'])->find();
        $info['family'] = HelpFamily::where('help_id',$info['id'])->select();
        $info['lave'] = Lave::lst(['cid'=>$cid,'paid'=>1,'help_id'=>$info['id'],'page'=>1,'limit'=>99999]);
        $info['sum_lave'] = Lave::where(['cid'=>$cid,'paid'=>1,'help_id'=>$id])->sum('amount');
        $info['is_apply'] = HelpApply::where('help_id',$info['id'])->where('user_id',$user_id)->find();
        $info['apply_sum'] = HelpApply::where('help_id',$info['id'])->value('count(id)')?:0;
        $info['category'] = Category::where('id',$info['category_id'])->value('name');
        return $info;
    }




}