<?php

namespace JinDouYun\Model\Holders;
use JinDouYun\Dao\Customer\DCustomer;
use JinDouYun\Dao\Holders\DHoldersRecord;
use JinDouYun\Dao\Holders\Holders as DHolders;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\ResultWrapper;

class Holders
{
    /**
     * 企业ID
     * @var
     */
    private $onlineEnterpriseId;
    /**
     * 规格名表
     * @var Holders
     */
    private $objHolders;
    private $DHoldersRecord;


    /**
     * MUnits constructor.
     * @param $onlineUserId
     * @param $onlineEnterpriseId
     * @throws \Exception
     */
    public function __construct($onlineEnterpriseId = '')
    {
        $this->onlineEnterpriseId = $onlineEnterpriseId;
        $this->objHolders = new DHolders('default');
        $this->DHoldersRecord = new DHoldersRecord('default');
    }

    // 添加股东
    public function  add($data){
        if(empty($data))  return ResultWrapper::fail('参数错误！',ErrorCode::$dberror);

        if($data['id']){
            $where['id']=$data['id'];
            $dbResult = $this->objHolders->update($data,$where);
        }else{
            $dbResult = $this->objHolders->insert($data);
        }
        return $dbResult;

     }

    // 获取股东信息
    public function  getinfo($where){
        if(empty($where))  return ResultWrapper::fail('参数错误！',ErrorCode::$dberror);
        $dbResult = $this->objHolders->get($where);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objHolders->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);

    }

//     获取股东列表
    public function Lst($selectParams){
        $limit = $selectParams['limit'];
        unset($selectParams['limit']);
        $dbResult = $this->objHolders->select($selectParams,'*', 'createTime desc', $limit);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objHolders->error(), ErrorCode::$dberror);
        }
        $countResult = $this->objHolders->count($selectParams);
        if ($countResult === false) {
            return ResultWrapper::fail($this->objHolders->error(), ErrorCode::$dberror);
        }
        $return = [
            'data' =>$dbResult,
            'total' => ($countResult) ? intval($countResult) : 0,
        ];
        return ResultWrapper::success($return);
    }


    /**
     * 增加减少股份
     * @param $parma
     * @return bool
     */
    public function edit_shares($parma)
    {
        $user = $this->objHolders->get(['id' => $parma['id']]);
        $str = '';
        if ($parma['pm'] == 1) {
            if ($parma['type'] == 1){
                $before = $user['shares'];
                $after = $before + $parma['number'];
                $str = 'shares';
            }elseif ($parma['type'] == 2){
                $before = $user['dends'];
                $after = $before + $parma['number'];
                $str = 'dends';
            }
        }elseif ($parma['pm'] == 2) {
            if ($parma['type'] == 1){
                $str = 'shares';
                $before = $user['shares'];
                $after = $before - $parma['number'];
            }elseif ($parma['type'] == 2){
                $str = 'dends';
                $before = $user['dends'];
                $after = $before - $parma['number'];
            }
        }

        if ($after <= 0) $after = 0;

        $insert = [
            'hol_id' =>  $parma['id'],
            'en_id' => $this->onlineEnterpriseId,
            'number' => $parma['number'],
            'before' => $before,
            'after' => $after,
            'pm' => $parma['pm'],
            'type' => $parma['type'],
            'remarks' => $parma['remarks']
        ];
        $res1 = $this->DHoldersRecord->insert($insert);
        $res = $this->objHolders->update([$str => $after], $parma['id']);
        if ($res1 and $res){
            return true;
        }
        return false;
    }


    public function getSum($id)
    {
        $where['en_id'] = $id;
        $res = $this->objHolders->select($where,'sum(shares) as shares,sum(dends) as dends');
        if ($res){
            return $res[0];
        }
        return false;
    }

    public function delete($where)
    {
        $data = $this->objHolders->get(['id' => $where['id']]);
        $res = $this->objHolders->delete($where);
        if ($res){
            $db = new DCustomer('default');
            $db->setTable('qianniao_customer_'.$this->onlineEnterpriseId);
            $db->update(['is_shareholder' => 0], ['id' => $data['us_id']]);

            return ResultWrapper::success('删除成功');
        }
        return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
    }














}