<?php
/**
 * 问答中心-激励表
 * @author: houyingcai
 * @email:  594609175@qq.com
 * @date :  2017-05-19 17:43:157
 * @version $Id$
 */

namespace Common\Service;

use Common\Common\Constant;
use Common\Common\Integral;
use Common\Model\MedalModel;
use Common\Model\MedalRelationModel;
use Common\Model\PaperModel;
use Common\Model\RightModel;
use Common\Model\ClassModel;

class MedalService extends AbstractService
{

    /** @var RightModel */
    protected $_d_right;

    /** @var MedalRelationModel */
    protected $_d_relation;

    /** @var PaperModel */
    protected $_d_class;

    // 构造方法
    public function __construct()
    {
        $this->_d = new MedalModel();

        // 初始化权限表
        $this->_d_right = new RightModel();

        // 初始化试卷激励关系表
        $this->_d_relation = new MedalRelationModel();

        // 初始化分类表
        $this->_d_class = new ClassModel();

        parent::__construct();
    }

    /**
     * 【后台】 添加激励验证
     *
     * @author 何岳龙
     * @param array $params POST 参数
     *
     * @return bool
     */
    public function add_medal_validation($params = [])
    {
        // 激励行为为空
        if (empty($params['title'])) {

            E('_EMPTY_MEDAL_TITLE');
        }

        // 激励行为超过字符限制
        if (get_str_len($params['title']) > 20) {

            E('_ERR_MEDAL_TITLE_LENGTH');
        }

        // 激励描述超过字符限制
        if (!empty($params['em_desc'])) {

            if (get_str_len($params['em_desc']) > 120) {
                E('_ERR_MEDAL_DESC_LENGTH');
            }
        }

        // 激励类型
        if (!is_numeric($params['em_type']) && !in_array($params['em_type'],
                [Constant::EC_MEDAL_TYPE_INTEGRAL, Constant::EC_MEDAL_TYPE_MEDAL]
            )
        ) {
            E('_ERR_MEDAL_TYPE');
        }


        // 如果是勋章
        if ($params['em_type'] == Constant::EC_MEDAL_TYPE_MEDAL) {
            // 勋章ID不能为空
            if (empty($params['im_id'])) {
                E('_EMPTY_MEDAL_ID');
            }
        }

        // 如果是积分
        if ($params['em_type'] == Constant::EC_MEDAL_TYPE_INTEGRAL) {
            // 积分格式不正确
            if (empty($params['em_integral']) || intval($params['em_integral']) < 1) {
                E('_ERR_MEDAL_INTEGRAL');
            }
        }

        // 规则为空或者不是数组
        if (empty($params['em_rule']) || !is_array($params['em_rule'])) {
            E('_ERR_MEDAL_RULE');
        }

        // 遍历规则
        foreach ($params['em_rule'] as $v) {
            if (empty($v['class_id']) || empty($v['class_name'])) {
                E('_ERR_MEDAL_RULE_PARAMS');
            }
        }

        // 次数为空或者不是数字
        if (empty($params['em_number']) || !is_numeric($params['em_number']) || intval($params['em_number']) < 1) {
            E('_ERR_MEDAL_NUMBER');
        }


        // 判断是否全公司
        if (!is_numeric($params['is_all']) || !in_array($params['is_all'], [Constant::MEDAL_AUTH_NOT_ALL, Constant::MEDAL_AUTH_ALL])) {
            E('_ERR_AUTH_STATUS');
        }

        // 如果用户权限为指定权限
        if (Constant::MEDAL_AUTH_NOT_ALL == $params['is_all']) {
            // 指定权限不能为空
            if (empty($params['right'])) {
                E('_EMPTY_AUTH');
            }

            // 获取全部人员权限
            $mem_ids = array_filter(array_column($params['right']['user_list'], 'memID'));

            // 获取部门IDS
            $dp_ids = [];
            if (isset($params['right']['dp_list']) && !empty($params['right']['dp_list'])
                && is_array($params['right']['dp_list'])) {

                $dp_ids = array_filter(array_column($params['right']['dp_list'], 'dpID'));
            }

            // 获取标签IDS
            $tag_ids = [];
            if (isset($params['right']['tag_list']) && !empty($params['right']['tag_list'])
                && is_array($params['right']['tag_list'])) {

                $tag_ids = array_filter(array_column($params['right']['tag_list'], 'tagID'));
            }

            // 获取岗位IDS
            $jod_ids = [];
            if (isset($params['right']['job_list']) && !empty($params['right']['job_list'])
                && is_array($params['right']['job_list'])) {

                $jod_ids = array_filter(array_column($params['right']['job_list'], 'jobID'));
            }

            // 角色IDS
            $role_ids = [];
            if (isset($params['right']['role_list']) && !empty($params['right']['role_list'])
                && is_array($params['right']['role_list'])) {

                $role_ids = array_filter(array_column($params['right']['role_list'], 'roleID'));
            }

            // 有权限总数
            $right_total = count($mem_ids) + count($dp_ids) + count($tag_ids) + count($jod_ids) + count($role_ids);

            if (empty($right_total)) {
                E('_ERR_AUTH_NUM');
            }
        }

        return true;
    }

    /**
     * 【后台】添加激励行为
     *
     * @author 何岳龙
     * @param array $params POST参数
     *
     * @return bool
     */
    public function add_medal($params = [])
    {
        try {
            $this->start_trans();

            // 格式化规则
            $em_rule = serialize($params['em_rule']);
            $em_desc = '';
            if (isset($params['em_desc']) && !empty($params['em_desc'])) {

                $em_desc = $params['em_desc'];
            }

            $em_integral = 0;
            if (isset($params['em_integral']) && !empty($params['em_integral'])) {

                $em_integral = intval($params['em_integral']);
            }

            // 组装数据
            $medal_data = [
                'title' => $params['title'],
                'em_desc' => $em_desc,
                'em_type' => $params['em_type'],
                'im_id' => !empty($params['im_id']) ? $params['im_id'] : '',
                'em_integral' => $em_integral,
                'is_all' => $params['is_all'],
                'em_number' => $params['em_number'],
                'answer_type'=>$params['answer_type'],
                'em_rule' => $em_rule,
                'icon_type' => intval($params['icon_type'])
            ];

            // 写入数据
            $id = $this->_d->insert($medal_data);

            // 初始化激励规则
            $insert_data = [];

            // 遍历激励规则
            foreach ($params['em_rule'] as $v) {

                $insert_data[] = [
                    'class_id' => $v['class_id'],
                    'em_id' => $id
                ];
            }

            // 如果激励规则存在
            if (!empty($insert_data)) {
                $this->_d_relation->insert_all($insert_data);
            }

            // 指定权限数组
            $data = [];

            // 如果是指定人员
            if ($params['is_all'] == Constant::MEDAL_AUTH_NOT_ALL) {
                // 遍历人员权限
                foreach ($params['right']['user_list'] as $v) {

                    $data[] = [
                        'epc_id' => $id,
                        'uid' => $v['memID'],
                        'cd_id' => '',
                        'tag_id' => '',
                        'job_id' => '',
                        'role_id' => ''
                    ];
                }

                // 遍历部门权限
                if (isset($params['right']['dp_list']) && !empty($params['right']['dp_list'])) {

                    foreach ($params['right']['dp_list'] as $v) {

                        $data[] = [
                            'epc_id' => $id,
                            'uid' => '',
                            'cd_id' => $v['dpID'],
                            'tag_id' => '',
                            'job_id' => '',
                            'role_id' => ''
                        ];
                    }
                }


                // 遍历标签权限
                if (isset($params['right']['tag_list']) && !empty($params['right']['tag_list'])) {

                    foreach ($params['right']['tag_list'] as $v) {

                        $data[] = [
                            'epc_id' => $id,
                            'uid' => '',
                            'cd_id' => '',
                            'tag_id' => $v['tagID'],
                            'job_id' => '',
                            'role_id' => ''
                        ];
                    }
                }

                // 遍历岗位权限
                if (isset($params['right']['job_list']) && !empty($params['right']['job_list'])) {

                    foreach ($params['right']['job_list'] as $v) {

                        $data[] = [
                            'epc_id' => $id,
                            'uid' => '',
                            'cd_id' => '',
                            'tag_id' => '',
                            'job_id' => $v['jobID'],
                            'role_id' => ''
                        ];
                    }
                }

                // 遍历角色权限
                if (isset($params['right']['role_list']) && !empty($params['right']['role_list'])) {

                    foreach ($params['right']['role_list'] as $v) {

                        $data[] = [
                            'epc_id' => $id,
                            'uid' => '',
                            'cd_id' => '',
                            'tag_id' => '',
                            'job_id' => '',
                            'role_id' => $v['roleID']
                        ];
                    }
                }
            }

            // 如果指定有权限数据
            if (!empty($data)) {
                $this->_d_right->insert_all($data);
            }

            $this->commit();
        } catch (\Think\Exception $e) {
            \Think\Log::record($e);
            // 事务回滚
            $this->_set_error($e->getMessage(), $e->getCode());
            $this->rollback();

            return false;
        } catch (\Exception $e) {

            \Think\Log::record($e);
            $this->_set_error($e->getMessage(), $e->getCode());
            // 事务回滚
            $this->rollback();

            return false;
        }

        return true;
    }

    /**
     * 【后台】编辑激励验证
     *
     * @author 何岳龙
     * @param array $params POST 参数
     *
     * @return bool
     */
    public function save_medal_validation($params = [])
    {
        // 激励行为ID为空
        if (empty($params['em_id'])) {
            E('_EMPTY_MEDAL_EM_ID');
        }

        // 激励行为为空
        if (empty($params['title'])) {
            E('_EMPTY_MEDAL_TITLE');
        }

        // 激励行为超过字符限制
        if (get_str_len($params['title']) > 20) {
            E('_ERR_MEDAL_TITLE_LENGTH');
        }

        // 激励描述超过字符限制
        if (!empty($params['em_desc'])) {
            if (get_str_len($params['em_desc']) > 120) {
                E('_ERR_MEDAL_DESC_LENGTH');
            }
        }

        // 激励类型
        if (!is_numeric($params['em_type']) &&
            !in_array($params['em_type'], [Constant::EC_MEDAL_TYPE_INTEGRAL, Constant::EC_MEDAL_TYPE_MEDAL])
        ) {
            E('_ERR_MEDAL_TYPE');
        }

        // 如果是勋章
        if ($params['em_type'] == Constant::EC_MEDAL_TYPE_MEDAL) {

            // 勋章ID不能为空
            if (empty($params['im_id'])) {
                E('_EMPTY_MEDAL_ID');
            }
        }

        // 积分格式不正确
        if ($params['em_type'] == Constant::EC_MEDAL_TYPE_INTEGRAL) {

            // 积分不能为空
            if (empty($params['em_integral']) || intval($params['em_integral']) < 1) {
                E('_ERR_MEDAL_INTEGRAL');
            }
        }

        // 规则为空或者不是数组
        if (empty($params['em_rule']) || !is_array($params['em_rule'])) {
            E('_ERR_MEDAL_RULE');
        }

        // 遍历规则
        foreach ($params['em_rule'] as $v) {
            if (empty($v['class_id']) || empty($v['class_name'])) {
                E('_ERR_MEDAL_RULE_PARAMS');
            }
        }

        // 判断是否全公司
        if (!is_numeric($params['is_all']) || !in_array($params['is_all'], [Constant::MEDAL_AUTH_NOT_ALL, Constant::MEDAL_AUTH_ALL])) {

            E('_ERR_AUTH_STATUS');
        }

        // 如果用户权限为指定权限
        if ($params['is_all'] == Constant::MEDAL_AUTH_NOT_ALL) {
            // 指定权限不能为空
            if (empty($params['right'])) {
                E('_EMPTY_AUTH');
            }

            // 获取全部人员权限
            $mem_ids = [];
            if (isset($params['right']['user_list']) && !empty($params['right']['user_list'])) {

                $mem_ids = array_filter(array_column($params['right']['user_list'], 'memID'));
            }

            // 获取部门IDS
            $dp_ids = [];
            if (isset($params['right']['dp_list']) && !empty($params['right']['dp_list'])) {

                $dp_ids = array_filter(array_column($params['right']['dp_list'], 'dpID'));
            }

            // 获取标签IDS
            $tag_ids = [];
            if (isset($params['right']['tag_list']) && !empty($params['right']['tag_list'])) {

                $tag_ids = array_filter(array_column($params['right']['tag_list'], 'tagID'));
            }

            // 获取岗位IDS
            $jod_ids = [];
            if (isset($params['right']['job_list']) && !empty($params['right']['job_list'])) {

                $jod_ids = array_filter(array_column($params['right']['job_list'], 'jobID'));
            }

            // 角色IDS
            $role_ids = [];
            if (isset($params['right']['role_list']) && !empty($params['right']['role_list'])) {

                $role_ids = array_filter(array_column($params['right']['role_list'], 'roleID'));
            }

            // 有权限总数
            $right_total = count($mem_ids) + count($dp_ids) + count($tag_ids) + count($jod_ids) + count($role_ids);

            if (empty($right_total)) {

                E('_ERR_AUTH_NUM');
            }

        }

        return true;
    }

    /**
     * 【后台】编辑激励
     *
     * @author 何岳龙
     * @param array $params POST 参数
     *
     * @return bool
     */
    public function update_medal_data($params = [])
    {
        try {
            $this->start_trans();

            // 格式化规则
            $em_rule = serialize($params['em_rule']);
            // 组装数据
            $medal_data = [
                'title' => $params['title'],
                'em_desc' => $params['em_desc'],
                'em_type' => $params['em_type'],
                'im_id' => strval($params['im_id']),
                'em_integral' => intval($params['em_integral']),
                'is_all' => $params['is_all'],
                'em_number' => $params['em_number'],
                'em_rule' => $em_rule,
                'icon_type' => intval($params['icon_type'])
            ];

            // 更新数据
            $this->_d->update($params['em_id'], $medal_data);

            // 删除激励权限
            $this->_d_right->delete_by_conds(['epc_id' => $params['em_id']]);
            // 删除规则
            $this->_d_relation->delete_by_conds(['em_id' => $params['em_id']]);
            // 初始化激励规则
            $insert_data = [];

            // 遍历激励规则
            foreach ($params['em_rule'] as $v) {

                $insert_data[] = [
                    'class_id' => $v['class_id'],
                    'em_id' => $params['em_id']
                ];
            }

            // 如果激励规则存在
            if (!empty($insert_data)) {

                $this->_d_relation->insert_all($insert_data);
            }

            // 指定权限数组
            $data = [];

            // 如果是指定人员
            if (Constant::MEDAL_AUTH_NOT_ALL == $params['is_all']) {

                // 遍历人员权限
                foreach ($params['right']['user_list'] as $v) {

                    $data[] = [
                        'epc_id' => $params['em_id'],
                        'uid' => $v['memID'],
                        'cd_id' => '',
                        'tag_id' => '',
                        'job_id' => '',
                        'role_id' => ''
                    ];
                }

                // 遍历部门权限
                foreach ($params['right']['dp_list'] as $v) {

                    $data[] = [
                        'epc_id' => $params['em_id'],
                        'uid' => '',
                        'cd_id' => $v['dpID'],
                        'tag_id' => '',
                        'job_id' => '',
                        'role_id' => ''
                    ];
                }

                // 遍历标签权限
                foreach ($params['right']['tag_list'] as $v) {

                    $data[] = [
                        'epc_id' => $params['em_id'],
                        'uid' => '',
                        'cd_id' => '',
                        'tag_id' => $v['tagID'],
                        'job_id' => '',
                        'role_id' => ''
                    ];
                }

                // 遍历岗位权限
                foreach ($params['right']['job_list'] as $v) {
                    $data[] = [
                        'epc_id' => $params['em_id'],
                        'uid' => '',
                        'cd_id' => '',
                        'tag_id' => '',
                        'job_id' => $v['jobID'],
                        'role_id' => ''
                    ];
                }

                // 遍历角色权限
                foreach ($params['right']['role_list'] as $v) {
                    $data[] = [
                        'epc_id' => $params['em_id'],
                        'uid' => '',
                        'cd_id' => '',
                        'tag_id' => '',
                        'job_id' => '',
                        'role_id' => $v['roleID']
                    ];
                }
            }

            // 如果指定有权限数据
            if (!empty($data)) {
                $this->_d_right->insert_all($data);
            }

            $this->commit();
        } catch (\Think\Exception $e) {
            \Think\Log::record($e);
            // 事务回滚
            $this->_set_error($e->getMessage(), $e->getCode());
            $this->rollback();

            return false;
        } catch (\Exception $e) {

            \Think\Log::record($e);
            $this->_set_error($e->getMessage(), $e->getCode());
            // 事务回滚
            $this->rollback();

            return false;
        }
        return true;
    }


    /**
     * 【后台】获取激励详情
     *
     * @author 何岳龙
     * @param array $params POST 参数
     *
     * @return bool
     */
    public function medal_info_validation($params = [])
    {
        // 如果激励ID为空
        if (empty($params['em_id'])) {
            E('_EMPTY_MEDAL_EM_ID');
        }
        // 获取详情
        $info = $this->_d->get($params['em_id']);
        if (empty($info)) {
            E('_EMPTY_MEDAL_INFO');
        }
        return true;
    }

    /**
     * 【后台】获取激励详情
     *
     * @author 何岳龙
     * @param array $params POST 参数
     *
     * @return array
     */
    public function get_medal_info($params = [])
    {
        // 初始化数据
        $auth = [
            'user_list' => [],
            'dp_list' => [],
            'tag_list' => [],
            'job_list' => [],
            'role_list' => []
        ];

        // 获取详情
        $info = $this->_d->get($params['em_id']);

        // 如果不是全公司
        if (Constant::MEDAL_AUTH_NOT_ALL == $info['is_all']) {
            // 获取权限信息
            $auth = $this->get_auth(['epc_id' => $params['em_id']]);
        }

        // 初始化勋章数据
        $integral_data = [];

        // 如果是勋章类型
        if (Constant::EC_MEDAL_TYPE_MEDAL == $info['em_type']) {
            // 实例化勋章
            $integral = new Integral();
            // 勋章数据
            $integral_data = $integral->listMedal($info['im_id']);
        }

        // 反序列化
        $em_rule = unserialize($info['em_rule']);
        // 初始化规则
        $rule_data = [];

        // todo
        // 格式化序列话数据
        foreach ($em_rule as $v) {
            // 获取数据是否存在
            $total = $this->_d_class->count_by_conds([
                'class_id' => $v['class_id'],
            ]);

            // 如果数据存在
            if (!empty($total)) {
                $rule_data[] = [
                    'class_id' => intval($v['class_id']),
                    'class_name' => strval($v['class_name'])
                ];
            }
        }

        // 获取权限信息
        $data = [
            'em_id' => intval($info['em_id']),
            'icon_type' => intval($info['icon_type']),
            'title' => strval($info['title']),
            'em_desc' => strval($info['em_desc']),
            'em_type' => intval($info['em_type']),
            'answer_type'=>intval($info['answer_type']),
            'im_id' => intval($info['im_id']),
            'em_number' => intval($info['em_number']),
            'em_score' => intval($info['em_score']),
            'em_name' => $info['em_type'] == Constant::EC_MEDAL_TYPE_MEDAL ? $integral_data[0]['name'] : '',
            'em_integral' => intval($info['em_integral']),
            'is_all' => intval($info['is_all']),
            'right' => $auth,
            'em_rule' => $rule_data
        ];

        return $data;
    }

    /**
     * 【后台】获取激励列表
     *
     * @author 何岳龙
     * @param array $params POST参数
     *
     * @return array
     */
    public function get_medal_list($params = [])
    {

        // 每页条数
        $limit = empty($params['limit']) ? Constant::PAGING_DEFAULT_LIMIT : intval($params['limit']);
        $page = empty($params['page']) ? Constant::PAGING_DEFAULT_PAGE : $params['page'];

        list($start, $limit, $page) = page_limit($page, $limit);

        // 查询条件
        $cond = [];
        // 分页参数
        $page_option = [$start, $limit];
        // 标签ID升序
        $order_option = ['created' => 'DESC'];
        // 获取总数
        $total = $this->_d->count_by_conds($cond);

        // 获取分页数据
        $list = $this->_d->list_by_conds($cond, $page_option, $order_option);

        // 初始化数据表
        $data = [];

        // 遍历数据
        foreach ($list as $v) {

            // 初始化权限
            $auth = [];
            // 初始化勋章数据
            $integral_data = [];

            // 如果是勋章类型
            if (Constant::EC_MEDAL_TYPE_MEDAL == $v['em_type']) {
                // 实例化勋章
                $integral = new Integral();
                // 勋章数据
                $integral_data = $integral->listMedal($v['im_id']);
            }

            // 如果不是全公司
            if (Constant::MEDAL_AUTH_NOT_ALL == $v['is_all']) {
                // 获取权限信息
                $auth = $this->get_auth(['epc_id' => $v['em_id']]);
            }

            $data[] = [
                'em_id' => intval($v['em_id']),
                'title' => strval($v['title']),
                'em_desc' => strval($v['em_desc']),
                'em_type' => intval($v['em_type']),
                'em_name' => $v['em_type'] == Constant::EC_MEDAL_TYPE_MEDAL ? strval($integral_data[0]['name']) : '',
                'icon' => $v['em_type'] == Constant::EC_MEDAL_TYPE_MEDAL ? strval($integral_data[0]['icon']) : '',
                'em_integral' => intval($v['em_integral']),
                'is_all' => intval($v['is_all']),
                'right' => $auth,
                'em_rule' => unserialize($v['em_rule'])
            ];
        }

        // 返回数据
        return [
            'total' => intval($total),
            'page' => intval($page),
            'limit' => intval($limit),
            'list' => $data
        ];
    }
}