<?php
namespace Admin\Controller;

use User\Api\UserApi;

/**
 * 结算单
 * @author ylw
 */
class WorkOrderController extends ThinkController
{
    public function list($row = 10, $p = 1)
    {
        $map = [];
        $map['_string'] = " status <> 2 ";
        if (I('order_no')) {
            $map['_string'] .= ' and order_no like "'.I('order_no').'%"';
        }
        if (I('user_account')) {
            $map['_string'] .= ' and user_account like "%'.I('user_account').'%"';
        }
        if (I('attachment_type', '')) {
            if (I('attachment_type') == 1) {
                $map['_string'] .= ' and attachment_url <> ""';
            } else {
                $map['_string'] .= ' and attachment_url = ""';
            }
        }
        if (I('server_id')) {
            $map['server_id'] = I('server_id');
        }
        if (I('handler_id')) {
            $map['handler_id'] = I('handler_id');
        }
        if (I('game_id')) {
            $map['game_id'] = I('game_id');
        }
        if (I('type')) {
            $map['type'] = I('type');
        }
        if (I('score')) {
            $map['score'] = I('score');
        }
        if (I('status')) {
            $map['status'] = I('status');
        }
        if (isset($_GET['created_time_start'])) {
            $map['_string'] .= " and created_time >= " . strtotime($_GET['created_time_start']);
        }
        if (isset($_GET['created_time_end'])) {
            $map['_string']  .= " and created_time < " . (strtotime($_GET['created_time_end']) + 86400);
        }
        $list = M('work_order_info', 'tab_')->where($map)->page($p, $row)->order("status desc, created_time desc")->select();
        $count = M('work_order_info', 'tab_')->where($map)->count();
        
        if ($list) {
            foreach ($list as &$item) {
                $item['status_desc'] = $this->getOrderStatus($item['status']);
            }
        }

        $this->checkListOrCountAuthRestMap($map,[]);

        $page = set_pagination($count, $row);
        if ($page) {
            $this->assign('_page', $page);
        }
        $this->assign('admin_users', M('member')->where(['status'=>1])->field('uid, nickname')->select());
        $this->assign('list_data', $list);
        $this->display('list');
    }

    public function store()
    {
        if (IS_POST) {
            // 存储
            $data['game_id'] = I('game_id');
            $data['game_name'] = I('game_name');
            $data['server_id'] = I('server_id');
            $data['server_name'] = I('server_name');
            $data['type'] = I('type');
            $data['creator_id'] = is_login();
            if (empty($data['game_id']) || empty($data['server_id'])) {
                $this->error('请选择游戏及区服');
            }
            $data['created_time'] = time();

            switch (I('type')) {
                case 1: // 资源工单
                    // 如果不是附件形式
                    $User = new UserApi;
                    $user_accounts = I('user_accounts');
                    $passwords = I('user_passwords');
                    if (count($user_accounts) == 1 && (empty($user_accounts[0]) && empty($passwords[0])) && empty(I('attachment_url'))) {
                        $this->error('请填写工单内容');
                    }
                    M('work_order_info', 'tab_')->startTrans();  //事物
                    if (!empty($user_accounts[0])) {
                        for ($i = 0; $i<sizeof($user_accounts); $i++) {
                            $user = M('user', 'tab_')->where(['account'=>$user_accounts[$i]])->find();
                            if (empty($user)) {M('work_order_info', 'tab_')->rollback(); $this->error($user_accounts[$i]."账号不存在");}
                            if ($this->think_ucenter_md5($passwords[$i], UC_AUTH_KEY) != $user['password'])  {M('work_order_info', 'tab_')->rollback(); $this->error(sprintf('%s 密码错误', $user_accounts[$i]));}
                            $info = $data;
                            $info['user_account'] = $user_accounts[$i];
                            $info['remark'] = I('remarks')[$i];
                            $info['role_name'] = I('role_names')[$i];
                            $info['resource_num'] = I('apply_resources')[$i];
                            $info['order_no'] = $info['type'] . date('YmdHis'). $i. rand(10, 99) . $info['creator_id'];
                            //$info['handler_id'] = $info['creator_id'];
                            $ins = M('work_order_info', 'tab_')->add($info);
    
                            if (!$ins) {M('work_order_info', 'tab_')->rollback(); $this->error("创建失败");}
                        }
                    }
                    if (!empty(I('attachment_url', ''))) {
                        // 添加一条附件信息
                        $attachement_info = $data;
                        $attachement_info['attachment_url'] = I('attachment_url', '');
                        //$attachement_info['handler_id'] = $attachement_info['creator_id'];
                        $attachement_info['order_no'] = $attachement_info['type'] . date('YmdHis'). rand(100, 999) . $attachement_info['creator_id'];
                        $ins = M('work_order_info', 'tab_')->add($attachement_info);
                        if (!$ins) {M('work_order_info', 'tab_')->rollback(); $this->error("创建失败");}
                    }
                    M('work_order_info', 'tab_')->commit();
                    return $this->success('创建工单成功', U('WorkOrder/list'));
                    break;
                case 2: // 反馈工单
                    $data['order_no'] = $data['type'] . date('YmdHis'). rand(100, 999) . is_login();
                    if (empty(I('user_account')) || empty(I('role_name')) || empty(I('feedback'))) {
                        $this->error('请填写必填的数据');
                    }
                    // if (empty('handler_id')) {
                    //     $this->error('请选择处理人');
                    // }
                    $user = M('user', 'tab_')->where(['account'=>I('user_account')])->find();
                    if (empty($user)) $this->error("账号不存在");
                    $data['attachment_url'] = I('attachment_url', '');
                    $data['user_account'] = I('user_account');
                    $data['role_name'] = I('role_name');
                    $data['feedback'] = I('feedback');
                    //$data['handler_id'] = I('handler_id');
                    $data['remark'] = I('remark', '');
                    $data['process_log'] = json_encode([['title'=>sprintf('%s于 %s 创建了工单%s,工单状态处理中。', get_admin_name($data['creator_id']), date('Y-m-d H:i:s'), $data['order_no']), 'type'=>1]]);
                    $ins = M('work_order_info', 'tab_')->add($data);
                    return $ins ? $this->success('创建工单成功', U('WorkOrder/list')) : $this->error('创建失败');
                    break;
                default:
                    $this->error('错误的工单类型');
            }
        } else {
            $this->assign('admin_users', M('member')->where(['status'=>1])->field('uid, nickname')->select());
            $this->display('store');
        }
    }

    public function modify($id=0)
    {
        $info = M('work_order_info', 'tab_')->where(['id'=>$id])->find();
        if (empty($info)) $this->error('未找到该资源');
        if (IS_POST) {
            $data['game_id'] = I('game_id');
            $data['game_name'] = I('game_name');
            $data['server_id'] = I('server_id');
            $data['server_name'] = I('server_name');
            $data['attachment_url'] = I('attachment_url', '');
            if (empty($data['game_id']) || empty($data['server_id'])) {
                $this->error('请选择游戏及区服');
            }
            switch($info['type']) {
                case 1:
                    $data = array_merge(I('resource'), $data);
                    if (!empty($info['attachment_url']) && empty($data['attachment_url'])) $this->error('请上传附件');
                    if (empty($info['attachment_url'])) {
                        $user = M('user', 'tab_')->where(['account'=>$data['user_account']])->find();
                        if (empty($user)) $this->error("账号不存在");
                        if ($this->think_ucenter_md5($data['user_password'], UC_AUTH_KEY) != $user['password'])   $this->error(sprintf('%s 密码错误', $user_accounts[$i]));
                        unset($data['user_password']);
                    }
                    
                    $upt = M('work_order_info', 'tab_')->where(['id'=>$id])->save($data);
                    return $upt ? $this->success('更新成功', U('WorkOrder/list')) : $this->error('更新失败');
                    break;
                case 2:
                    $data = array_merge(I('feedback'), $data);
                    $user = M('user', 'tab_')->where(['account'=>$data['user_account']])->find();
                    if (empty($user)) $this->error("账号不存在{$data['user_account']}");
                    $log = $this->modifyProcessLog($info, $data);
                    if ($log) {
                        $process_log = json_decode($info['process_log'], true) ?: [];
                        $log = ['title'=>sprintf("%s于%s 修改了工单%s内容", get_admin_name(is_login()), date('Y-m-d H:i:s'), $info['order_no']), 'detail'=>$log, 'type'=>2];
                        $data['process_log'] = json_encode(array_merge($process_log, [$log]));
                    }
                    $upt = M('work_order_info', 'tab_')->where(['id'=>$id])->save($data);
                    return $upt ? $this->success('更新成功', U('WorkOrder/list')) : $this->error('更新失败');
                    break;
                default:
                    $this->error('错误的工单类型');
            }
        } else {
            $info['process_log'] = json_decode($info['process_log'], true);
            $this->assign('data', $info);
            $this->assign('admin_users', M('member')->where(['status'=>1])->field('uid, nickname')->select());
            $this->display('modify');
        }
    }

    private function modifyProcessLog($oldData, $newData)
    {
        $key = ['game_name'=>'游戏名称', 'server_name'=>'区服名称', 'role_name'=>'角色名称', 'user_account'=>'玩家账号', 'feedback'=>'反馈原因', 'handler_id'=>'接单人员', 'remark'=>'备注', 'status'=>'状态', 'attachment_url'=>'附件'];
        $log = [];
        foreach ($newData as $k => $v) {
            if (array_key_exists($k, $oldData) && array_key_exists($k, $key) && $oldData[$k] != $newData[$k]) {
                if ($k == 'handler_id') {
                    $log[] = ['key_name'=>$key[$k], 'key'=>$k, 'old'=>get_admin_name($oldData[$k]), 'new'=>get_admin_name($newData[$k])];
                } else if ($k == 'status') {
                    $log[] = ['key_name'=>$key[$k], 'key'=>$k, 'old'=>$this->getOrderStatus($oldData[$k]), 'new'=>$this->getOrderStatus($newData[$k])];
                } else {
                    $log[] = ['key_name'=>$key[$k], 'key'=>$k, 'old'=>$oldData[$k], 'new'=>$newData[$k]];
                }
            }
        }
        return $log;
    }

    private function getOrderStatus($status)
    {
        switch($status) {
            case 1:
                return '完成';
            case 2:
                return '失败';
            case 3:
                return '处理中';
            default:
                return '未知';
        }
    }

    /**
     * 系统非常规MD5加密方法
     * @param  string $str 要加密的字符串
     * @return string
     */
    public function think_ucenter_md5($str, $key = 'ThinkUCenter'){
        return '' === $str ? '' : md5(sha1($str) . $key);
    }

    public function resourceApply($id=0)
    {
        $info = M('work_order_info', 'tab_')->where(['id'=>$id])->find();
        if (empty($info)) $this->error('未找到该资源');
        if ($info['type'] != 1) $this->error('错误的订单类型');
        if (IS_POST) {
            if (!empty($info['attachment_url'])) {
                if (!I('attachment_url')) $this->error('请上传附件');
            } else {
                if (!I('resource_num')) $this->error('请填写申请资源数量');
            }
            
            $data['creator_id'] = is_login();
            $data['created_time'] = time();
            $data['type'] = $info['type'];
            $data['server_id'] = $info['server_id'];
            $data['server_name'] = $info['server_name'];
            $data['game_id'] = $info['game_id'];
            $data['game_name'] = $info['game_name'];
            $data['role_name'] = $info['role_name'];
            $data['user_account'] = $info['user_account'];
            $data['order_no'] = $info['type'] . date('YmdHis', $data['created_time']). rand(100, 999) . $data['creator_id'];
            $data['resource_num'] = I('resource_num', 0);
            $data['remark'] = I('remark', '');
            $data['attachment_url'] = I('attachment_url', '');
            $ins = M('work_order_info', 'tab_')->add($data);
            return $ins ? $this->success('申请成功', U('WorkOrder/list')) : $this->error('申请失败');
        } else {
            $this->assign('data', $info);
            $this->display('resourceApply');
        }
    }

    // 反馈进度存储
    public function feedbackProcessLogStore($id=0)
    {
        $info = M('work_order_info', 'tab_')->where(['id'=>$id])->find();
        if (empty($info)) $this->error('未找到该资源');
        $info['process_log'] = json_decode($info['process_log'], true);
        if ($info['type'] != 2) $this->error('错误的订单类型');
        if (IS_POST) {
            $log = [['type'=>3, 'title'=>sprintf('%s于 %s 更新了工单%s进度,工单状态%s', get_admin_name(is_login()), date('Y-m-d H:i:s'), $info['order_no'], $this->getOrderStatus($info['status'])), 'detail'=>I('process_log')]];
            $upt = M('work_order_info', 'tab_')->where(['id'=>$id])->save(['process_log'=>json_encode(array_merge($info['process_log'], $log))]);
            return $upt ? $this->success('更新进度成功', U('WorkOrder/list')) : $this->error('更新进度失败');
        } else {
            $this->assign('data', $info);
            $this->display('feedbackProcessLogStore');
        }
    }

    // 评价
    public function evaluate($id=0)
    {
        $info = M('work_order_info', 'tab_')->where(['id'=>$id])->find();
        if (empty($info)) $this->error('未找到该资源');
        if (IS_POST) {
            if (empty(I('evaluate_content'))) $this->error('请填写评价内容');
            $data['score'] = I('evaluate', 5);
            $info['process_log'] = json_decode($info['process_log'], true);
            $log = [['type'=>3, 'title'=>sprintf('%s于 %s 评价了工单%s为%s分,工单状态%s', get_admin_name(is_login()), date('Y-m-d H:i:s'), $info['order_no'], $data['score'], $this->getOrderStatus($info['status'])), 'detail'=>I('evaluate_content')]];
            $data['process_log'] = json_encode(array_merge($info['process_log'], $log));
            $upt = M('work_order_info', 'tab_')->where(['id'=>$id])->save($data);
            return $upt ? $this->success('评价成功', U('WorkOrder/list')) : $this->error('评价失败');
        } else {
            $this->assign('data', $info);
            $this->display('feedbackEvaluate');
        }
    }

    // 删除
    public function delete($ids)
    {
        $delete = M('work_order_info', 'tab_')->where(['id'=>['in', $ids]])->delete();
        return $delete ? $this->success('删除成功') : $this->error('删除失败');
    }

    public function finish($id)
    {
        $info = M('work_order_info', 'tab_')->where(['id'=>$id])->find();
        if (empty($info)) $this->error('未找到该资源');
        $info['process_log'] = json_decode($info['process_log'], true);
        $log = [['type'=>1, 'title'=>sprintf('%s于 %s 完成了工单%s,工单状态完成', get_admin_name(is_login()), date('Y-m-d H:i:s'), $info['order_no'])]];
        $data['process_log'] = json_encode(array_merge($info['process_log'], $log));
        $data['status'] = 1;
        $delete = M('work_order_info', 'tab_')->where(['id'=>$id])->save($data);
        return $delete ? $this->success('成功完成工单') : $this->error('更新失败');
    }

    public function take($id)
    {
        $info = M('work_order_info', 'tab_')->where(['id'=>$id])->find();
        if (empty($info)) $this->error('未找到该资源');
        $userid = is_login();
        if ($info['status'] == 1) {
            $this->error('工单已完成');
        }
        if (!empty($info['handler_id'])) {
            if ($info['handler_id'] != $userid) {
                $this->error('工单已被他人领取');
            } else {
                $this->error('请勿重复领取工单');
            }
        }
        if ($info['type'] == 2) {
            $process_log = json_decode($info['process_log'], true) ?: [];
            $log = ['title'=>sprintf("%s于%s 接取了工单%s", get_admin_name(is_login()), date('Y-m-d H:i:s'), $info['order_no']), 'detail'=>$log, 'type'=>2];
            $data['process_log'] = json_encode(array_merge($process_log, [$log]));
        }
        $data['handler_id'] = $userid;
        $upt = M('work_order_info', 'tab_')->where(['id'=>$id])->save($data);
        return $upt ? $this->success('成功接取工单') : $this->error('接单失败');
    }

    public function statistics($row = 10, $p = 1)
    {
        $limit = ($p-1)*$row . ", {$row}";
        $order = '';
        if ($_REQUEST['data_order'] != '') {
            $data_order = reset(explode(',', $_REQUEST['data_order']));
            $data_order_type = end(explode(',', $_REQUEST['data_order']));
            $this->assign('userarpu_order', $data_order);
            $this->assign('userarpu_order_type', $data_order_type);
            $order = 'order by ' . $data_order_type . " " . ($data_order == 3 ? 'desc' : 'asc');
        }
        $map = '1=1';
        if (!empty(I('nickname'))) {
            $map .= " and t.nickname = '" . I('nickname') . "'";
        }
        // 获取所有传单或者接单用户id
        $userids = M('work_order_info', 'tab_')
            ->field("concat(creator_id, ',', handler_id) as userid")
            ->join('sys_member as t on creator_id = t.uid or handler_id = t.uid')
            ->where($map)
            ->select();
        if (empty($userids)) {
            $list = [];
            $count = 0;
        } else {
            $userids = implode(',', array_unique(array_filter(explode(',', implode(',', array_column($userids, 'userid'))))));
            $count = count($userids);
            $list = M('work_order_info', 'tab_')->query("SELECT
            t.id, 
            t.nickname,
            sum(total_create_count) as total_create_count,
            sum(total_accepted_count) as total_accepted_count,
            sum(total_handle_count) as total_handle_count,
            sum(total_complete_count) as total_complete_count
            FROM
                (
                    SELECT
                        u.uid as id,
                        u.nickname,
                        count(*) AS total_create_count,
                        count(
            
                            IF (i.`handler_id` > 0, 1, NULL)
                        ) AS total_accepted_count,
                        NULL AS total_handle_count,
                        NULL AS total_complete_count
                    FROM
                        sys_member u
                    JOIN tab_work_order_info i ON i.creator_id = u.uid
                    WHERE
                        u.uid in ({$userids})
                        GROUP BY u.uid
                    UNION
                        SELECT
                            u.uid as id,
                            u.nickname,
                            NULL AS total_create_count,
                            NULL AS total_accepted_count,
                            count(*) AS total_handle_count,
                            count(IF(i.`status` = 1, 1, NULL)) AS total_complete_count
                        FROM
                            sys_member u
                        JOIN tab_work_order_info i ON i.handler_id = u.uid
                        WHERE
                            u.uid in ({$userids})
                        GROUP BY u.uid
                ) AS t
            where {$map}
            GROUP BY t.id
            $order
            limit {$limit}");
        }

        $this->checkListOrCountAuthRestMap($map,[]);

        $page = set_pagination($count, $row);
        if ($page) {
            $this->assign('_page', $page);
        }
        $this->assign('list_data', $list);
        $this->display('statistics');
    }
}