<?php
namespace app\admin\controller;

use think\Db;

/**
 * 管理系统系统设置类
 */
class Clean extends Base
{
    /**
     *数据清理
     *
     * @access public
     */
    public function index()
    {
        // 表单提交.
        if (request()->isPost()) {
            $param = input('post.');
            //聊天数据
            $del_time = '';
            if($param['chat_data'] == 'one_month'){
                $del_time = time()-2592000;
            }
            if($param['chat_data'] == 'three_month'){
                $del_time = time()-7776000;
            }
            if($param['chat_data'] == 'six_month'){
                $del_time = time()-15552000;
            }
            if($param['chat_data'] == 'one_year'){
                $del_time = time()-31104000;
            }

            //客服操作记录
            $delete_time = '';
            if($param['operational_records'] == 'half_year'){
                $delete_time = date('Y-m-d',time()-15552000);
            }
            if($param['operational_records'] == 'one_year'){
                $delete_time = date('Y-m-d',time()-31104000);
            }
            if($param['operational_records'] == 'three_year'){
                $delete_time = date('Y-m-d',time()-93312000);
            }

            if($del_time != ''){
                $servicelog = array();
                $servicelog_id = db('service_log')->field('servicelog_id')->where('start_time','<',$del_time)->select();
                for($i=0;$i<count($servicelog_id);$i++){
                    $servicelog[] = $servicelog_id[$i]['servicelog_id'];
                }
                if($delete_time != ''){
                    // 启动事务
                    Db::startTrans();
                    try {
                        //聊天数据清理
                        db('service_log')->where('start_time','<',$del_time)->delete();
                        db('chat_log')->where('servicelog_id','in',$servicelog)->delete();
                        db('alarm')->where('servicelog_id','in',$servicelog)->delete();
                        //客服操作记录清理
                        db('kfonlinehistory')->where('wday','<',$delete_time)->delete();
                        db('kfstatetimes')->where('sday','<',$delete_time)->delete();
                        db('service_data')->where('add_date','<',$delete_time)->delete();
                        db('servicetimelog')->where('cdate','<',$delete_time)->delete();
                        // 提交事务
                        Db::commit();
                        return json(['code' => 1, 'data' => '', 'msg' => '数据清理成功']);
                    } catch (\Exception $e) {
                        // 回滚事务
                        Db::rollback();
                        return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
                    }
                }else{
                    // 启动事务
                    Db::startTrans();
                    try {
                        db('service_log')->where('start_time','<',$del_time)->delete();
                        db('chat_log')->where('servicelog_id','in',$servicelog)->delete();
                        db('alarm')->where('servicelog_id','in',$servicelog)->delete();
                        // 提交事务
                        Db::commit();
                        return json(['code' => 1, 'data' => '', 'msg' => '聊天数据清理成功']);
                    } catch (\Exception $e) {
                        // 回滚事务
                        Db::rollback();
                        return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
                    }
                }
            }else{
                if($delete_time != ''){
                    // 启动事务
                    Db::startTrans();
                    try {
                        db('kfonlinehistory')->where('wday','<',$delete_time)->delete();
                        db('kfstatetimes')->where('sday','<',$delete_time)->delete();
                        db('service_data')->where('add_date','<',$delete_time)->delete();
                        db('servicetimelog')->where('cdate','<',$delete_time)->delete();
                        // 提交事务
                        Db::commit();
                        return json(['code' => 1, 'data' => '', 'msg' => '客服操作记录清理成功']);
                    } catch (\Exception $e) {
                        // 回滚事务
                        Db::rollback();
                        return json(['code' => -3, 'data' => '', 'msg' => $e->getMessage()]);
                    }
                }else{
                    return json(['code' => -4, 'data' => '', 'msg' => '请选择时间']);
                }
            }
        }

        $this->assign([
                'status' => config('kf_status'),
        ]);

        return $this->fetch();

    }
}