<?php

namespace app\api\controller;

use think\facade\Lang;

/**
 * ============================================================================
 * 
 * ============================================================================
 * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司，并保留所有权利。
 * 网站地址: https://www.valimart.net/
 * ----------------------------------------------------------------------------
 * 
 * ============================================================================
 * 举报控制器
 */
class Memberinform extends MobileMember {

    public function initialize() {
        parent::initialize(); // TODO: Change the autogenerated stub
        Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/memberinfom.lang.php');
    }

    /**
     * @api {POST} api/Memberinform/index 获取当前用户的举报列表
     * @apiVersion 1.0.0
     * @apiGroup Memberinform
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     * 
     * @apiParam {Int} page 页码
     * @apiParam {Int} per_page 每页数量
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object[]} result.inform_list  举报列表
     * @apiSuccess {String} result.inform_list.inform_content  举报内容
     * @apiSuccess {Int} result.inform_list.inform_datetime  举报时间
     * @apiSuccess {Int} result.inform_list.inform_goods_id  商品ID
     * @apiSuccess {String} result.inform_list.inform_goods_image  商品图片名称
     * @apiSuccess {String} result.inform_list.inform_goods_image_url  商品图片完整路径
     * @apiSuccess {String} result.inform_list.inform_goods_name  商品名称
     * @apiSuccess {Int} result.inform_list.inform_handle_datetime  处理时间
     * @apiSuccess {Int} result.inform_list.inform_handle_member_id  处理用户ID
     * @apiSuccess {String} result.inform_list.inform_handle_message  处理描述
     * @apiSuccess {Int} result.inform_list.inform_handle_type  举报处理结果(1无效举报/2恶意举报/3有效举报)
     * @apiSuccess {Int} result.inform_list.inform_id  举报ID
     * @apiSuccess {Int} result.inform_list.inform_member_id  举报用户ID
     * @apiSuccess {String} result.inform_list.inform_member_name  举报用户名称
     * @apiSuccess {String} result.inform_list.inform_pic1  举报凭证1
     * @apiSuccess {String} result.inform_list.inform_pic2  举报凭证2
     * @apiSuccess {String} result.inform_list.inform_pic3  举报凭证3
     * @apiSuccess {Int} result.inform_list.inform_state  举报状态(1未处理/2已处理)
     * @apiSuccess {Int} result.inform_list.inform_store_id  店铺ID
     * @apiSuccess {String} result.inform_list.inform_store_name  店铺名称
     * @apiSuccess {String} result.inform_list.informsubject_content  举报主题内容
     * @apiSuccess {Int} result.inform_list.informsubject_id  举报主题ID
     * @apiSuccess {Int} result.inform_list.informsubject_state  举报主题状态 1:可用 2:失效
     * @apiSuccess {Int} result.inform_list.informsubject_type_id  举报类型ID
     * @apiSuccess {String} result.inform_list.informsubject_type_name  举报类型名称
     * @apiSuccess {Int} result.page_total  总页数
     * @apiSuccess {Boolean} result.hasmore  是否有更多 true是false否
     */
    public function index() {
        /*
         * 得到当前用户的举报列表
         */
        $inform_model = model('inform');
		$goods_model   = model('goods');
        $condition = array();
        $inform_state = intval(input('param.select_inform_state'));
        if ($inform_state > 0) {
            $condition[] = array('inform.inform_state', '=', $inform_state);
        }
        $condition[] = array('inform.inform_member_id', '=', $this->member_info['member_id']);
        $inform_list = $inform_model->getInformList($condition, 10);
        foreach ($inform_list as $key => $val) {
			$condition   = array();
			$condition[] = array('goods_id','=',$val['inform_goods_id']);
			$goodsinfo = $goods_model->getGoodsInfo($condition,'goods_price');
			$inform_list[$key]['inform_goods_image_url'] = goods_cthumb($val['inform_goods_image']);
			$inform_list[$key]['inform_goods_price'] = $goodsinfo['goods_price'];
        }
        $result = array_merge(array('inform_list' => $inform_list), mobile_page($inform_model->page_info));
        ds_json_encode(10000, '', $result);
    }

    /**
     * @api {POST} api/Memberinform/inform_submit 获取新增/编辑举报公共信息
     * @apiVersion 1.0.0
     * @apiGroup Memberinform
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     * 
     * @apiParam {Int} goods_id 商品ID
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object} result.goods_info  举报商品信息
     * @apiSuccess {Object[]} result.type_list  举报类型列表
     * @apiSuccess {String} result.type_list.informtype_desc  举报类型描述
     * @apiSuccess {Int} result.type_list.informtype_id  举报类型ID
     * @apiSuccess {String} result.type_list.informtype_name  举报类型名称
     * @apiSuccess {Int} result.type_list.informtype_state  举报类型状态 1:有效 2:失效
     */
    public function inform_submit() {

        //检查当前用户是否允许举报
        $this->check_member_allow_inform();

        $goods_id = intval(input('param.goods_id'));

        //获取商品详细信息
        $goods_info = $this->get_goods_info_byid($goods_id);

        $seller_model = model('seller');
        $seller_info = $seller_model->getSellerInfo(array('member_id' => $this->member_info['member_id']));
        //检查是否是本店商品
        if (!$seller_info) {
            if ($goods_info['store_id'] == $seller_info['store_id']) {
                ds_json_encode(10001, lang('param_error'));
            }
        }

        $inform_model = model('inform');
        //检查是否当前正在举报
        if ($inform_model->isProcessOfInform($goods_id)) {
            ds_json_encode(10001, lang('inform_handling'));
        }

        //获取举报类型
        $informsubjecttype_model = model('informsubjecttype');
        $inform_subject_type_list = $informsubjecttype_model->getActiveInformsubjecttypeList();
        if (empty($inform_subject_type_list)) {
            ds_json_encode(10001, lang('inform_type_null'));
        }

        ds_json_encode(10000, '', array('goods_info' => $goods_info, 'type_list' => $inform_subject_type_list));
    }

    /**
     * @api {POST} api/Memberinform/inform_save 保存用户提交的商品举报
     * @apiVersion 1.0.0
     * @apiGroup Memberinform
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     * 
     * @apiParam {Int} inform_goods_id 商品ID
     * @apiParam {String} inform_subject 举报主题
     * @apiParam {String} inform_content 举报内容
     * @apiParam {Array} pic_name 举报凭证 用,分隔多个举报凭证
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     */
    public function inform_save() {

        //检查当前用户是否允许举报
        $this->check_member_allow_inform();

        $goods_id = intval(input('post.inform_goods_id'));

        //获取商品详细信息
        $goods_info = $this->get_goods_info_byid($goods_id);

        $seller_model = model('seller');
        $seller_info = $seller_model->getSellerInfo(array('member_id' => $this->member_info['member_id']));
        //检查是否是本店商品
        if (!$seller_info) {
            if ($goods_info['store_id'] == $seller_info['store_id']) {
                ds_json_encode(10001, lang('param_error'));
            }
        }

        //实例化举报模型
        $inform_model = model('inform');
        //检查是否当前正在举报
        if ($inform_model->isProcessOfInform($goods_id)) {
            ds_json_encode(10001, lang('inform_handling'));
        }
        //处理用户输入的数据
        $input = array();
        $input['inform_member_id'] = $this->member_info['member_id'];
        $input['inform_member_name'] = $this->member_info['member_name'];
        $input['inform_goods_id'] = $goods_id;
        $input['inform_goods_name'] = $goods_info['goods_name'];
        $input['inform_goods_image'] = $goods_info['goods_image'];
        list($input['informsubject_id'], $input['informsubject_content']) = explode(",", trim(input('post.inform_subject')));
        $input['inform_content'] = trim(input('post.inform_content'));

        $pic_name = input('post.pic_name/a');

        $input['inform_pic1'] = isset($pic_name[0]) ? $pic_name[0] : '';
        $input['inform_pic2'] = isset($pic_name[1]) ? $pic_name[1] : '';
        $input['inform_pic3'] = isset($pic_name[2]) ? $pic_name[2] : '';

        $input['inform_datetime'] = TIMESTAMP;
        $input['inform_store_id'] = $goods_info['store_id'];
        $input['inform_store_name'] = $goods_info['store_name'];
        $input['inform_state'] = 1;
        $input['inform_handle_message'] = '';
        $input['inform_handle_member_id'] = 0;
        $input['inform_handle_datetime'] = 1;

        //验证输入的数据
        $data = [
            'inform_content' => $input["inform_content"],
            'informsubject_content' => $input["informsubject_content"]
        ];

        $res=word_filter($input['inform_content']);
        if(!$res['code']){
            ds_json_encode(10001,$res['msg']);
        }
        $input['inform_content']=$res['data']['text'];
        
        $inform_validate = ds_validate('inform');
        if (!$inform_validate->scene('inform_save')->check($data)) {
            ds_json_encode(10001, $inform_validate->getError());
        }

        //保存
        if ($inform_model->addInform($input)) {
            ds_json_encode(10000, lang('inform_success'));
        } else {
            ds_json_encode(10001, lang('inform_fail'));
        }
    }

    /**
     * @api {POST} api/Memberinform/inform_cancel 取消用户提交的商品举报
     * @apiVersion 1.0.0
     * @apiGroup Memberinform
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     * 
     * @apiParam {Int} inform_id 举报ID
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     */
    public function inform_cancel() {

        $inform_id = intval(input('param.inform_id'));
        $inform_info = $this->get_inform_info($inform_id);

        if (intval($inform_info['inform_state']) === 1) {
            $pics = array();
            if (!empty($inform_info['inform_pic1'])) {
                $pics[] = $inform_info['inform_pic1'];
            }
            if (!empty($inform_info['inform_pic2'])) {
                $pics[] = $inform_info['inform_pic2'];
            }
            if (!empty($inform_info['inform_pic3'])) {
                $pics[] = $inform_info['inform_pic3'];
            }
            $this->drop_inform($inform_id, $pics);
            ds_json_encode(10000, lang('inform_cancel_success'));
        } else {
            ds_json_encode(10001, lang('inform_cancel_fail'));
        }
    }

    /**
     * @api {POST} api/Memberinform/inform_info 商品举报详细
     * @apiVersion 1.0.0
     * @apiGroup Memberinform
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     * 
     * @apiParam {Int} inform_id 举报ID
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object} result.goods_info  商品信息
     * @apiSuccess {Int} result.goods_info.areaid_1 一级地区id
     * @apiSuccess {Int} result.goods_info.areaid_2 二级地区id
     * @apiSuccess {Int} result.goods_info.brand_id 品牌ID
     * @apiSuccess {Int} result.goods_info.color_id 颜色规格值ID
     * @apiSuccess {Int} result.goods_info.evaluation_count 评论数
     * @apiSuccess {Int} result.goods_info.evaluation_good_star 好评评分
     * @apiSuccess {Int} result.goods_info.gc_id 分类ID
     * @apiSuccess {Int} result.goods_info.gc_id_1 一级分类ID
     * @apiSuccess {Int} result.goods_info.gc_id_2 二级分类ID
     * @apiSuccess {Int} result.goods_info.gc_id_3 三级分类ID
     * @apiSuccess {String} result.goods_info.goods_addtime 添加时间，Unix时间戳
     * @apiSuccess {String} result.goods_info.goods_advword 广告词
     * @apiSuccess {Int} result.goods_info.goods_click 商品点击次数
     * @apiSuccess {Int} result.goods_info.goods_collect 收藏数
     * @apiSuccess {Int} result.goods_info.goods_commend 商品推荐 1:是 0:否
     * @apiSuccess {Int} result.goods_info.goods_commonid 商品公共ID
     * @apiSuccess {Int} result.goods_info.goods_edittime 编辑时间，Unix时间戳
     * @apiSuccess {Float} result.goods_info.goods_freight 运费
     * @apiSuccess {Int} result.goods_info.goods_id 商品ID
     * @apiSuccess {String} result.goods_info.goods_image 商品图片名称
     * @apiSuccess {Float} result.goods_info.goods_marketprice 商品市场价
     * @apiSuccess {String} result.goods_info.goods_mgdiscount 会员等级折扣
     * @apiSuccess {String} result.goods_info.goods_name 商品名称
     * @apiSuccess {Float} result.goods_info.goods_price 商品价格
     * @apiSuccess {Float} result.goods_info.goods_promotion_price 商品促销价
     * @apiSuccess {String} result.goods_info.goods_promotion_type 商品促销类型
     * @apiSuccess {Int} result.goods_info.goods_salenum 销售量
     * @apiSuccess {String} result.goods_info.goods_serial 货号
     * @apiSuccess {String} result.goods_info.goods_spec 商品规格序列化
     * @apiSuccess {String} result.goods_info.goods_state 商品状态 0:下架 1:正常 10:违规（禁售）
     * @apiSuccess {String} result.goods_info.goods_stcids 店铺分类id 首尾用,隔开
     * @apiSuccess {Int} result.goods_info.goods_storage 库存
     * @apiSuccess {Int} result.goods_info.goods_storage_alarm 预警库存
     * @apiSuccess {Int} result.goods_info.goods_vat 是否开具增值税发票 1:是 0:否
     * @apiSuccess {Object} result.goods_info.goods_verify 已审核 1是0否
     * @apiSuccess {Int} result.goods_info.is_appoint 是否是预约商品 1:是 0:否
     * @apiSuccess {Int} result.goods_info.is_goodsfcode 是否是F码商品 1:是 0:否
     * @apiSuccess {Int} result.goods_info.is_have_gift 是否含赠品 1:是 0:否
     * @apiSuccess {Int} result.goods_info.is_platform_store 是否自营商品 1:是 0:否
     * @apiSuccess {Int} result.goods_info.is_virtual 是否是虚拟商品 1:是 0:否
     * @apiSuccess {Int} result.goods_info.region_id 地区ID
     * @apiSuccess {Int} result.goods_info.store_id 店铺ID
     * @apiSuccess {String} result.goods_info.store_name 店铺名称
     * @apiSuccess {Int} result.goods_info.transport_id 商品售卖区域ID
     * @apiSuccess {Int} result.goods_info.virtual_indate 虚拟商品有效期
     * @apiSuccess {Int} result.goods_info.virtual_invalid_refund 是否允许过期退款 1:是 0:否
     * @apiSuccess {Int} result.goods_info.virtual_limit 虚拟商品购买上限
     * @apiSuccess {Object} result.inform_info  举报信息
     * @apiSuccess {String} result.inform_info.inform_content  举报内容
     * @apiSuccess {Int} result.inform_info.inform_datetime  举报时间
     * @apiSuccess {Int} result.inform_info.inform_goods_id  商品ID
     * @apiSuccess {String} result.inform_info.inform_goods_image  商品图片名称
     * @apiSuccess {String} result.inform_info.inform_goods_image_url  商品图片完整路径
     * @apiSuccess {String} result.inform_info.inform_goods_name  商品名称
     * @apiSuccess {Int} result.inform_info.inform_handle_datetime  处理时间
     * @apiSuccess {Int} result.inform_info.inform_handle_member_id  处理用户ID
     * @apiSuccess {String} result.inform_info.inform_handle_message  处理描述
     * @apiSuccess {Int} result.inform_info.inform_handle_type  举报处理结果(1无效举报/2恶意举报/3有效举报)
     * @apiSuccess {Int} result.inform_info.inform_id  举报ID
     * @apiSuccess {Int} result.inform_info.inform_member_id  举报用户ID
     * @apiSuccess {String} result.inform_info.inform_member_name  举报用户名称
     * @apiSuccess {String} result.inform_info.inform_pic1  举报凭证1
     * @apiSuccess {String} result.inform_info.inform_pic2  举报凭证2
     * @apiSuccess {String} result.inform_info.inform_pic3  举报凭证3
     * @apiSuccess {Int} result.inform_info.inform_state  举报状态(1未处理/2已处理)
     * @apiSuccess {Int} result.inform_info.inform_store_id  店铺ID
     * @apiSuccess {String} result.inform_info.inform_store_name  店铺名称
     * @apiSuccess {String} result.inform_info.informsubject_content  举报主题内容
     * @apiSuccess {Int} result.inform_info.informsubject_id  举报主题ID
     * @apiSuccess {String[]} result.inform_pic  举报凭证列表
     * @apiSuccess {Object} result.subject_info  举报主题信息
     * @apiSuccess {String} result.subject_info.informsubject_content  举报主题内容
     * @apiSuccess {Int} result.subject_info.informsubject_id  举报主题ID
     * @apiSuccess {Int} result.subject_info.informsubject_state  举报主题状态
     * @apiSuccess {Int} result.subject_info.informsubject_type_id  举报类型ID
     * @apiSuccess {String} result.subject_info.informsubject_type_name  举报类型名称
     */
    public function inform_info() {

        $inform_id = intval(input('param.inform_id'));
        $inform_info = $this->get_inform_info($inform_id);
        switch ($inform_info['inform_handle_type']) {
            case 1:
                $inform_info['inform_handle_type_text'] = lang('inform_handle_type_unuse');
                break;
            case 2:
                $inform_info['inform_handle_type_text'] = lang('inform_handle_type_venom');
                break;
            case 3:
                $inform_info['inform_handle_type_text'] = lang('inform_handle_type_valid');
                break;
        }
        // 商品信息
        $goods_info = model('goods')->getGoodsInfoByID($inform_info['inform_goods_id']);
        // 投诉类型
        $subject_info = model('informsubject')->getOneInformsubject(array('informsubject_id' => $inform_info['informsubject_id']));

        $inform_pic = array();
        for ($i = 1; $i <= 3; $i++) {
            if (!empty($inform_info['inform_pic' . $i])) {
                $inform_pic[] = ds_get_pic( ATTACH_PATH . DIRECTORY_SEPARATOR . 'inform' , $inform_info['inform_pic' . $i]);
            }
        }
        ds_json_encode(10000, '', array('inform_pic' => $inform_pic, 'goods_info' => $goods_info, 'inform_info' => $inform_info, 'subject_info' => $subject_info));
    }

    /*
     * 根据id获取投诉详细信息
     */

    private function get_inform_info($inform_id) {

        if (empty($inform_id)) {
            ds_json_encode(10001, lang('param_error'));
        }

        $inform_model = model('inform');
        $inform_info = $inform_model->getOneInform(array('inform_id' => $inform_id));

        if (empty($inform_info)) {
            ds_json_encode(10001, lang('param_error'));
        }

        if (intval($inform_info['inform_member_id']) !== intval($this->member_info['member_id'])) {
            ds_json_encode(10001, lang('param_error'));
        }

        return $inform_info;
    }

    /*
     * 根据id获取投诉详细信息
     */

    private function drop_inform($inform_id, $inform_pics) {

        $inform_model = model('inform');
        //删除图片
        if (!empty($inform_pics)) {
            foreach ($inform_pics as $pic) {
                $this->inform_delete_pic($pic);
            }
        }
        $inform_model->delInform(array('inform_id' => $inform_id));
    }

    /*
     * 根据id获取商品详细信息
     */

    private function get_goods_info_byid($goods_id) {

        if (empty($goods_id)) {
            ds_json_encode(10001, lang('param_error'));
        }

        $goods_model = model('goods');
        $goods_info = $goods_model->getGoodsOnlineInfoByID($goods_id);

        //检查该商品是否存在
        if (empty($goods_info)) {
            ds_json_encode(10001, lang('goods_null'));
        }

        return $goods_info;
    }

    /*
     * 检查当前用户是否允许举报
     */

    private function check_member_allow_inform() {

        //检查是否允许举报
        $member_model = model('member');
        if (!$member_model->isMemberAllowInform($this->member_info['member_id'])) {
            ds_json_encode(10001, lang('deny_inform'));
        }
    }

    /**
     * @api {POST} api/Memberinform/upload_pic 上传用户提供的举报图片
     * @apiVersion 1.0.0
     * @apiGroup Memberinform
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {File} inform_pic 举报凭证
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {String} result.file_name  文件名称
     * @apiSuccess {String} result.pic  文件完整路径
     */
    public function upload_pic() {
        if (!empty($_FILES['inform_pic']['name'])) {
            $file_name = $this->member_info['member_id'] . '_' . date('YmdHis') . rand(10000, 99999) . '.png';
            $res = ds_upload_pic('home' . DIRECTORY_SEPARATOR . 'inform', 'inform_pic', $file_name);
            if ($res['code']) {
                $file_name = $res['data']['file_name'];
            } else {
                ds_json_encode(10001, $res['msg']);
            }
            $pic = ds_get_pic( ATTACH_PATH . DIRECTORY_SEPARATOR . 'inform' , $file_name);
            ds_json_encode(10000, '', array('file_name' => $file_name, 'pic' => $pic));
        } else {
            ds_json_encode(10001, lang('file_empty'));
        }
    }

    /*
     * 上传用户提供的举报图片
     */

    private function inform_delete_pic($pic_name) {

        //上传路径
        $pic = BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_PATH . DIRECTORY_SEPARATOR . 'inform' . DIRECTORY_SEPARATOR . $pic_name;
        if (file_exists($pic)) {
            @unlink($pic);
        }
    }

    /**
     * @api {POST} api/Memberinform/get_subject_by_typeid 根据举报类型id获取，举报具体列表
     * @apiVersion 1.0.0
     * @apiGroup Memberinform
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} type_id 类型ID
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object[]} result.inform_subject_list  举报主题列表
     * @apiSuccess {String} result.inform_subject_list.informsubject_content  举报主题内容
     * @apiSuccess {Int} result.inform_subject_list.informsubject_id  举报主题ID
     */
    public function get_subject_by_typeid() {
        $informsubject_type_id = intval(input('param.type_id'));

        if (empty($informsubject_type_id)) {
            ds_json_encode(10001, lang('param_error'));
        } else {
            /*
             * 获得举报主题列表
             */
            $informsubject_model = model('informsubject');

            //搜索条件
            $condition = array();
            $condition[] = array('informsubject_type_id', '=', $informsubject_type_id);
            $condition[] = array('informsubject_state', '=', 1);
            $inform_subject_list = $informsubject_model->getInformsubjectList($condition, 10, 'informsubject_id,informsubject_content');

            ds_json_encode(10000, '', array('inform_subject_list' => $inform_subject_list));
        }
    }

}
