<?php
namespace app\admin\server\notice;

use app\admin\model\Notice;

class NoticetServer
{
    /**
     * Notes:获取业务员列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/9/27
     */
    public static function getNoticeList(int $page, int $limit,string $keywords)
    {
        [$list,$count] =  Notice::getNoticeList($page, $limit, $keywords);
        if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['shop_create_time'] = date('Y-m-d H:i:s', $v['shop_create_time']);
                if (!empty($v['shop_update_time'])){
                    $list[$k]['shop_update_time'] = date('Y-m-d H:i:s', $v['shop_update_time']);
                }
            }
        }

        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:修改业务员
     * @param string $notice_title
     * @param string $notice_introduce
     * @param string $notic_content
     * @param int $notice_id
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function updateNotice($notice_id, $notice_title, $notice_introduce, $notic_content)
    {
        Notice::affairBegin();
        try {
            $where = [];
            $where['notice_id'] = $notice_id;
            $data = [];
            if (!empty($notice_title)){
                $data['notice_title'] = $notice_title;
            }
            if (!empty($notice_introduce)){
                $data['notice_introduce'] = $notice_introduce;
            }
            if (!empty($notic_content)){
                $data['notic_content'] = $notic_content;
            }
            $result = Notice::where($where)->update($data);
            if ($result !== false){
                Notice::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            Notice::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:删除业务员
     * @param int $notice_id
     * @return int
     * User: ZQ
     * Date: 2022/9/3
     */
    public static function delNotice($notice_id)
    {
        Notice::affairBegin();
        try {
            $where = [];
            $where['menu_id'] = $notice_id;
            $data['notice_del'] = 2;
            $result = Notice::where($where)->update($data);
            if (!empty($result)){
                Notice::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            Notice::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes: 添加业务员
     * @param int $notice_pid
     * @param string $notice_name
     * @param int $notice_type
     * @param int $notice_sort
     * @return int
     * User: ZQ
     * Date: 2022/9/14
     */
    public static function insertNotice($notice_title, $notice_introduce, $notic_content)
    {

        Notice::affairBegin();
        try {
            $data = [];
            $data['notice_title'] = $notice_title;
            $data['notice_introduce'] = $notice_introduce;
            $data['notic_content'] = $notic_content;
            $result =  Notice::insertGetId($data);
            if (!empty($result)){
                Notice::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            Notice::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询业务员
     * @param int $notice_id
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function noticeInfo($notice_id)
    {

        $where = [];
        $where['notice_del'] = 0;
        $where['notice_id'] = $notice_id;
        $result = Notice::where($where)->first();
        return $result;
    }

}
