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

use app\admin\model\MedicalCareSalesman;
use support\Db;

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

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

    /**
     * Notes:修改业务员
     * @param string $salesman_name
     * @param string $salesman_pwd
     * @param string $salesman_phone
     * @param string $salesman_resale_name
     * @param int $salesman_status
     * @param int $salesman_id
     * @param int $salesman_shop_id
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function updateSalesman($salesman_id, $salesman_shop_id, $salesman_name, $salesman_pwd, $salesman_phone, $salesman_resale_name, $salesman_status, $salf, $admin_id)
    {
        MedicalCareSalesman::affairBegin();
        try {
            $where = [];
            $where['salesman_id'] = $salesman_id;
            $data = [];
            $data['salesman_name'] = $salesman_name;
            $data['salesman_shop_id'] = $salesman_shop_id;

            if (!empty($salesman_pwd)){
                $data['salesman_salf'] = $salf;
                $data['salesman_pwd'] = $salesman_pwd;
            }
            $data['salesman_phone'] = $salesman_phone;
            $data['salesman_resale_name'] = $salesman_resale_name;
            $data['salesman_status'] = $salesman_status;
            $data['salesman_update_time'] = time();
            $result = MedicalCareSalesman::where($where)->update($data);
            if ($result !== false){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '修改业务员: ' . $salesman_id;
                plog('medical-salesman-update', '医疗-预约服务业务员-修改业务员', $msg);
                MedicalCareSalesman::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            MedicalCareSalesman::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:删除业务员
     * @param int $salesman_id
     * @return int
     * User: ZQ
     * Date: 2022/9/3
     */
    public static function delSalesman($salesman_id, $admin_id)
    {
        MedicalCareSalesman::affairBegin();
        try {
            $where = [];
            $where['salesman_id'] = $salesman_id;
            $data['salesman_del'] = 1;
            $result = MedicalCareSalesman::where($where)->update($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '删除业务员-编号: ' . $salesman_id;
                plog('medical-salesman-update', '医疗-预约服务业务员-删除业务员', $msg);
                MedicalCareSalesman::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            MedicalCareSalesman::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes: 添加业务员
     * @param int $salesman_shop_id
     * @param string $salesman_name
     * @param string $salesman_pwd
     * @param string $salesman_phone
     * @param string $salesman_resale_name
     * @param int $salesman_status
     * @return int
     * User: ZQ
     * Date: 2022/9/14
     */
    public static function insertSalesman($salesman_shop_id, $salesman_name, $salesman_pwd, $salesman_phone, $salesman_resale_name, $salesman_status, $salf, $admin_id)
    {

        MedicalCareSalesman::affairBegin();
        try {
            $data = [];
            $data['salesman_name'] = $salesman_name;
            $data['salesman_shop_id'] = $salesman_shop_id;
            $data['salesman_pwd'] = $salesman_pwd;
            $data['salesman_phone'] = $salesman_phone;
            $data['salesman_resale_name'] = $salesman_resale_name;
            $data['salesman_status'] = $salesman_status;
            $data['salesman_salf'] = $salf;
            $data['salesman_create_time'] = time();
            $result =  MedicalCareSalesman::insertGetId($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '添加业务员-编号: ' . $result;
                plog('medical-salesman-update', '医疗-预约服务业务员-添加业务员', $msg);
                MedicalCareSalesman::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            MedicalCareSalesman::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

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

        $where = [];
        $where['salesman_del'] = 0;
        $where['salesman_id'] = $salesman_id;
        $result = MedicalCareSalesman::where($where)
            ->leftJoin('merchant_shop', 'shop_id', '=', 'medical_care_salesman.salesman_shop_id')
            ->first(['medical_care_salesman.*','merchant_shop.shop_name']);
        if (!empty($result)){
            $result['salesman_create_time'] = date('Y-m-d H:i:s',$result['salesman_create_time']);
            if (!empty($result['salesman_update_time'])){
                $result['salesman_update_time'] = date('Y-m-d H:i:s',$result['salesman_update_time']);
            }
        }
        return $result;
    }

    /**
     * Notes:修改状态
     * @param int $salesman_status
     * @param int $salesman_id
     * @return int
     * User: ZQ
     * Date: 2022/9/3
     */
    public static function updateStatus($salesman_id, $salesman_status, $admin_id)
    {
        MedicalCareSalesman::affairBegin();
        try {
            $where = [];
            $where['salesman_id'] = $salesman_id;
            $data = [];
            $data['salesman_status'] = $salesman_status;
            $result = MedicalCareSalesman::where($where)->update($data);
            if ($result !== false){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '修改业务员状态-编号: ' . $salesman_id;
                plog('medical-salesman-update', '医疗-预约服务业务员-修改业务员状态', $msg);
                MedicalCareSalesman::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            MedicalCareSalesman::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

}
