<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\api\controller\user;

use app\api\controller\Base;
use think\Db;


class Subscribe extends Base
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function getList(){
        global $_W;

        $config = $_W['base']['subscribemsg'];
        $workorder = $_W['base']['workorder'];
        
        $list = [];

        $where = ['uniacid'=>UNIACID,'user_id'=>$this->user['id'],'tid'=>'','tid_name'=>''];
        
        if(!empty($config['outofstock_tid']['tid']) && $this->user['isAdmin'] == 1){
            $where['tid'] = $config['outofstock_tid']['tid'];
            $where['tid_name'] = 'outofstock_tid';
            $num = Db::name("bbfx_user_wxapp_subscribe")->where($where)->value("num");
            $num = intval($num);
            $list[] = ['title'=>'缺货提醒通知','tid'=>$config['outofstock_tid']['tid'],'num'=>$num,'tid_name'=>'outofstock_tid'];
        }
        if(!empty($config['xiadan_tid']['tid']) && $this->user['isAdmin'] == 1){
            $where['tid'] = $config['xiadan_tid']['tid'];
            $where['tid_name'] = 'xiadan_tid';
            $num = Db::name("bbfx_user_wxapp_subscribe")->where($where)->value("num");
            $num = intval($num);
            $list[] = ['title'=>'用户下单通知','tid'=>$config['xiadan_tid']['tid'],'num'=>$num,'tid_name'=>'xiadan_tid'];
        }
        if(!empty($config['updatenotice_tid']['tid'])){
            $where['tid'] = $config['updatenotice_tid']['tid'];
            $where['tid_name'] = 'updatenotice_tid';
            $num = Db::name("bbfx_user_wxapp_subscribe")->where($where)->value("num");
            $num = intval($num);
            $list[] = ['title'=>'系统更新通知','tid'=>$config['updatenotice_tid']['tid'],'num'=>$num,'tid_name'=>'updatenotice_tid'];
        }
        if(!empty($config['shippingnotice_tid']['tid'])){
            $where['tid'] = $config['shippingnotice_tid']['tid'];
            $where['tid_name'] = 'shippingnotice_tid';
            $num = Db::name("bbfx_user_wxapp_subscribe")->where($where)->value("num");
            $num = intval($num);
            $list[] = ['title'=>'订单发货通知','tid'=>$config['shippingnotice_tid']['tid'],'num'=>$num,'tid_name'=>'shippingnotice_tid'];
        }
        if(!empty($config['signin_remind_tid']['tid'])){
            $where['tid'] = $config['signin_remind_tid']['tid'];
            $where['tid_name'] = 'signin_remind_tid';
            $num = Db::name("bbfx_user_wxapp_subscribe")->where($where)->value("num");
            $num = intval($num);
            $list[] = ['title'=>'签到提醒通知','tid'=>$config['signin_remind_tid']['tid'],'num'=>$num,'tid_name'=>'signin_remind_tid'];
        }
        if(!empty($config['message_center_tid']['tid'])){
            $where['tid'] = $config['message_center_tid']['tid'];
            $where['tid_name'] = 'message_center_tid';
            $num = Db::name("bbfx_user_wxapp_subscribe")->where($where)->value("num");
            $num = intval($num);
            $list[] = ['title'=>'消息中心通知','tid'=>$config['message_center_tid']['tid'],'num'=>$num,'tid_name'=>'message_center_tid'];
        }
        if($workorder['status'] == 1){
            if(!empty($config['workorder_complete_tid']['tid'])){
                $where['tid'] = $config['workorder_complete_tid']['tid'];
                $where['tid_name'] = 'workorder_complete_tid';
                $num = Db::name("bbfx_user_wxapp_subscribe")->where($where)->value("num");
                $num = intval($num);
                $list[] = ['title'=>'工单完成通知','tid'=>$config['workorder_complete_tid']['tid'],'num'=>$num,'tid_name'=>'workorder_complete_tid'];
            }
            $workorder_user_list = Db::name("bbfx_workorder_user")->where(['uniacid'=>UNIACID])->field("user_id")->select();
            $uids = [];
            if(!empty($workorder_user_list)){
                $uids = array_column($workorder_user_list,"user_id");
            }
            if(!empty($config['workorder_allocate_tid']['tid']) && in_array($this->user['id'],$uids)){
                $where['tid'] = $config['workorder_allocate_tid']['tid'];
                $where['tid_name'] = 'workorder_allocate_tid';
                $num = Db::name("bbfx_user_wxapp_subscribe")->where($where)->value("num");
                $num = intval($num);
                $list[] = ['title'=>'工单分配通知','tid'=>$config['workorder_allocate_tid']['tid'],'num'=>$num,'tid_name'=>'workorder_allocate_tid'];
            }
        }
        
        __success("操作成功",['list'=>$list]);
    }

    public function submit(){
        global $_W;

        $config = $_W['base']['subscribemsg'];
        $tid_name = $this->param("tid_name/s");
        if(empty($tid_name)){
            __error("模板名称错误");
        }
        

        if(empty($config[$tid_name]['tid'])){
            __error("订阅通知不存在");
        }
        $where = ['uniacid'=>UNIACID,'user_id'=>$this->user['id'],'tid'=>$config[$tid_name]['tid'],'tid_name'=>$tid_name];
        
        $log = Db::name("bbfx_user_wxapp_subscribe")->where($where)->find();
        if(!empty($log)){
            Db::name("bbfx_user_wxapp_subscribe")->where(['id'=>$log['id']])->setInc("num",1);
        }
        else{
            $data = [
                'uniacid'=>UNIACID,
                'user_id'=>$this->user['id'],
                'tid_name'=>$tid_name,
                'tid'=>$config[$tid_name]['tid'],
                'num'=>1
            ];
            Db::name("bbfx_user_wxapp_subscribe")->insert($data);
        }
        
        __success("操作成功");
    }
}