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

use app\admin\model\RaiseOldGoods;

class RaiseOldGoodsServer
{
    /**
     * Notes:获取养老产品列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/10/31
     */
    public static function getGoodsList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  RaiseOldGoods::getGoodsList($page, $limit, $keywords);
        if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['goods_add_time'] = date('Y-m-d H:i:s',$v['goods_add_time']);
                if (!empty($v['goods_update_time'])){
                    $list[$k]['goods_update_time'] = date('Y-m-d H:i:s',$v['goods_update_time']);
                }
            }
        }
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:修改养老产品
     * @param string $goods_name
     * @param int $goods_id
     * @return int
     * User: ZQ
     * Date: 2022/10/31
     */
    public static function updateGoods($goods_id, $goods_name, $goods_title, $goods_time, $goods_category_id, $goods_banner, $goods_label, $goods_status, $goods_service_population, $goods_service_consumables, $goods_service_qualifications, $goods_service_explain, $goods_service_standard, $goods_service_process, $goods_technological, $goods_tips, $goods_shop_id, $goods_price, $admin_id)
    {
        RaiseOldGoods::affairBegin();
        try {
            $where = [];
            $where['goods_id'] = $goods_id;
            $data = [];
            $data['goods_name'] = $goods_name;
            $data['goods_title'] = $goods_title;
            $data['goods_time'] = $goods_time;
            $data['goods_category_id'] = $goods_category_id;
            $data['goods_banner'] = $goods_banner;
            $data['goods_label'] = $goods_label;
            $data['goods_service_population'] = $goods_service_population;
            $data['goods_service_consumables'] = $goods_service_consumables;
            $data['goods_service_qualifications'] = $goods_service_qualifications;
            $data['goods_service_explain'] = $goods_service_explain;
            $data['goods_service_standard'] = $goods_service_standard;
            $data['goods_service_process'] = $goods_service_process;
            $data['goods_technological'] = $goods_technological;
            $data['goods_tips'] = $goods_tips;
            $data['goods_status'] = $goods_status;
            $data['goods_price'] = $goods_price;
            $data['goods_shop_id'] = $goods_shop_id;
            $data['goods_update_time'] = time();
            $result = RaiseOldGoods::where($where)->update($data);
            if ($result !== false){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '修改万悦养老产品-编号: ' . $goods_id;
                plog('life-goods-update', '养老服务-养老产品-修改养老产品', $msg);
                RaiseOldGoods::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            RaiseOldGoods::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:删除养老产品
     * @param int $goods_id
     * @return int
     * User: ZQ
     * Date: 2022/10/31
     */
    public static function delGoods($goods_id,$admin_id)
    {
        RaiseOldGoods::affairBegin();
        try {
            $where = [];
            $where['goods_id'] = $goods_id;
            $data['goods_del'] = 1;
            $result = RaiseOldGoods::where($where)->update($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '删除万悦养老产品-编号: ' . $goods_id;
                plog('life-goods-delete', '养老服务-养老产品-删除养老产品', $msg);
                RaiseOldGoods::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            RaiseOldGoods::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes: 添加养老产品
     * @param string $goods_name
     * @param array $goods_rules
     * @return int
     * User: ZQ
     * Date: 2022/10/31
     */
    public static function insertGoods($goods_name, $goods_title, $goods_time, $goods_category_id, $goods_banner, $goods_label, $goods_status, $goods_service_population, $goods_service_consumables, $goods_service_qualifications, $goods_service_explain, $goods_service_standard, $goods_service_process, $goods_technological, $goods_tips, $goods_shop_id, $goods_price, $admin_id)
    {

        RaiseOldGoods::affairBegin();
        try {
            $data = [];
            $data['goods_name'] = $goods_name;
            $data['goods_title'] = $goods_title;
            $data['goods_time'] = $goods_time;
            $data['goods_category_id'] = $goods_category_id;
            $data['goods_banner'] = $goods_banner;
            $data['goods_label'] = $goods_label;
            $data['goods_service_population'] = $goods_service_population;
            $data['goods_service_consumables'] = $goods_service_consumables;
            $data['goods_service_qualifications'] = $goods_service_qualifications;
            $data['goods_service_explain'] = $goods_service_explain;
            $data['goods_service_standard'] = $goods_service_standard;
            $data['goods_service_process'] = $goods_service_process;
            $data['goods_technological'] = $goods_technological;
            $data['goods_tips'] = $goods_tips;
            $data['goods_status'] = $goods_status;
            $data['goods_price'] = $goods_price;
            $data['goods_shop_id'] = $goods_shop_id;
            $data['goods_add_time'] = time();
            $result =  RaiseOldGoods::insertGetId($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '添加万悦养老产品-编号: ' . $result;
                plog('life-goods-create', '养老服务-养老产品-添加养老产品', $msg);
                RaiseOldGoods::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            RaiseOldGoods::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询养老产品
     * @param int $goods_id
     * @return int
     * User: ZQ
     * Date: 2022/10/31
     */
    public static function goodsInfo($goods_id)
    {
        $where = [];
        $where['goods_id'] = $goods_id;
        $result = RaiseOldGoods::where($where)
            ->leftJoin('category','category_id','=','raise_old_goods.goods_category_id')
            ->leftJoin('merchant_shop','shop_id','=','raise_old_goods.goods_shop_id')
            ->first(['raise_old_goods.*','category.category_name','merchant_shop.shop_name']);
        if (!empty($result)){
            $result['goods_add_time'] = date('Y-m-d H:i:s',$result['goods_add_time']);
            if (!empty($result['goods_update_time'])){
                $result['goods_update_time'] = date('Y-m-d H:i:s',$result['goods_update_time']);
            }

        }
        return $result;
    }

    /**
     * Notes:修改养老产品状态
     * @param string $goods_name
     * @param int $goods_status
     * @return int
     * User: ZQ
     * Date: 2022/9/15
     */
    public static function updateStatus($goods_id, $goods_status)
    {
        RaiseOldGoods::affairBegin();
        try {
            $where = [];
            $where['goods_id'] = $goods_id;
            $data = [];
            $data['goods_status'] = $goods_status;
            $result = RaiseOldGoods::where($where)->update($data);
            if ($result !== false){
                RaiseOldGoods::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            RaiseOldGoods::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

}
