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

use app\admin\model\LifeFood;

class FoodServer
{
    /**
     * Notes:获取健康美食列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/10/17
     */
    public static function getFoodList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  LifeFood::getFoodList($page, $limit, $keywords);
        if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['food_silde'] = explode(',',$v['food_silde']);
                $list[$k]['food_materials'] = json_decode($v['food_materials'],true);
                $list[$k]['food_condiment'] = json_decode($v['food_condiment'],true);
                $list[$k]['food_create_time'] = date('Y-m-d H:i:s',$v['food_create_time']);
                if (!empty($v['food_update_time'])){
                    $list[$k]['food_update_time'] = date('Y-m-d H:i:s',$v['food_update_time']);
                }
            }
        }
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:获取分类下的美食列表
     * @return int
     * User: ZQ
     * Date: 2022/10/17
     */
    public static function getFoodAll($food_category_id)
    {
        $where = [];
        $where['food_is_del'] = 0;
        $where['food_status'] = 0;
        $where['food_type'] = 1;
        if (!empty($food_category_id)){
            $where['food_category_id'] = $food_category_id;
        }
        $list = LifeFood::where($where)
            ->select(['food_id','food_name'])
            ->get()
            ->toArray();
        return $list;
    }

    /**
     * Notes:修改健康美食
     * @param string $food_name
     * @param int $food_id
     * @return int
     * User: ZQ
     * Date: 2022/10/17
     */
    public static function updateFood($food_silde, $food_shop_id, $food_storage, $food_packaging, $food_varieties, $food_attr, $food_postage_id, $food_weight, $food_id, $food_name, $food_img, $food_price, $food_category_id, $food_pack_price, $food_materials, $food_condiment, $food_type, $food_package, $food_label, $food_status, $food_content,$food_cater_time,$food_cater_type,$food_package_num, $food_week, $admin_id)
    {
        LifeFood::affairBegin();
        try {
            $where = [];
            $where['food_id'] = $food_id;
            $data = [];
            $data['food_name'] = $food_name;
            $data['food_img'] = $food_img;
            $data['food_price'] = $food_price;
            $data['food_category_id'] = $food_category_id;
            $data['food_pack_price'] = $food_pack_price;
            $data['food_materials'] = json_encode($food_materials);
            $data['food_condiment'] = json_encode($food_condiment);
            $data['food_type'] = $food_type;
            $data['food_package'] = $food_package;
            $data['food_label'] = $food_label;
            $data['food_status'] = $food_status;
            $data['food_content'] = $food_content;
            $data['food_silde'] = $food_silde;
            $data['food_shop_id'] = $food_shop_id;
            $data['food_storage'] = $food_storage;
            $data['food_packaging'] = $food_packaging;
            $data['food_varieties'] = $food_varieties;
            $data['food_attr'] = $food_attr;
            $data['food_postage_id'] = $food_postage_id;
            $data['food_weight'] = $food_weight;
            $data['food_cater_time'] = $food_cater_time;
            $data['food_cater_type'] = $food_cater_type;
            $data['food_package_num'] = $food_package_num;
            $data['food_week'] = $food_week ? implode(',',$food_week) : '';
            $data['food_update_time'] = time();
            $result = LifeFood::where($where)->update($data);
            if ($result !== false){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '修改健康美食-编号: ' . $food_id;
                plog('life-food-update', '悦活-健康美食-修改健康美食', $msg);
                LifeFood::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeFood::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:删除健康美食
     * @param int $food_id
     * @return int
     * User: ZQ
     * Date: 2022/10/17
     */
    public static function delFood($food_id,$admin_id)
    {
        LifeFood::affairBegin();
        try {
            $where = [];
            $where['food_id'] = $food_id;
            $data['food_is_del'] = 1;
            $result = LifeFood::where($where)->update($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '删除健康美食-编号: ' . $food_id;
                plog('life-food-delete', '悦活-健康美食-删除健康美食', $msg);
                LifeFood::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            LifeFood::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes: 添加健康美食
     * @param string $food_name
     * @param array $food_rules
     * @return int
     * User: ZQ
     * Date: 2022/10/17
     */
    public static function insertFood($food_silde, $food_shop_id, $food_storage, $food_packaging, $food_varieties, $food_attr, $food_postage_id, $food_weight, $food_name, $food_img, $food_price, $food_category_id, $food_pack_price, $food_materials, $food_condiment, $food_type, $food_package, $food_label, $food_status, $food_content,$food_cater_time,$food_cater_type,$food_package_num, $food_week, $admin_id)
    {

        LifeFood::affairBegin();
        try {
            $data = [];
            $data['food_name'] = $food_name;
            $data['food_img'] = $food_img;
            $data['food_price'] = $food_price;
            $data['food_category_id'] = $food_category_id;
            $data['food_pack_price'] = $food_pack_price;
            $data['food_materials'] = json_encode($food_materials);
            $data['food_condiment'] = json_encode($food_condiment);
            $data['food_type'] = $food_type;
            $data['food_package'] = $food_package;
            $data['food_label'] = $food_label;
            $data['food_status'] = $food_status;
            $data['food_content'] = $food_content;
            $data['food_silde'] = $food_silde;
            $data['food_shop_id'] = $food_shop_id;
            $data['food_storage'] = $food_storage;
            $data['food_packaging'] = $food_packaging;
            $data['food_varieties'] = $food_varieties;
            $data['food_attr'] = $food_attr;
            $data['food_postage_id'] = $food_postage_id;
            $data['food_weight'] = $food_weight;
            $data['food_cater_time'] = $food_cater_time;
            $data['food_cater_type'] = $food_cater_type;
            $data['food_package_num'] = $food_package_num;
            $data['food_week'] = $food_week ? implode(',',$food_week) : '';
            $data['food_create_time'] = time();
            $result =  LifeFood::insertGetId($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '添加健康美食-编号: ' . $result;
                plog('life-food-create', '悦活-健康美食-添加健康美食', $msg);
                LifeFood::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeFood::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询健康美食
     * @param int $food_id
     * @return int
     * User: ZQ
     * Date: 2022/10/17
     */
    public static function foodInfo($food_id)
    {
        $where = [];
        $where['food_id'] = $food_id;
        $result = LifeFood::where($where)
            ->leftJoin('category','category_id','=','life_healthy_food.food_category_id')
            ->first(['life_healthy_food.*','category.category_name']);
        if (!empty($result)){
            $food_package= explode(',',$result['food_package']);
            if (!empty($food_package)){
                for ($i=0;$i<count($food_package);$i++){
                    $intval[] = (int)$food_package[$i];
                }
                $result['food_package'] = $intval;
            }
            $result['food_materials'] = json_decode($result['food_materials'],true);
            $result['food_condiment'] = json_decode($result['food_condiment'],true);
            $result['food_create_time'] = date('Y-m-d H:i:s',$result['food_create_time']);
            $result['food_silde'] = explode(',',$result['food_silde']);
            $result['food_week'] = explode(',',$result['food_week']);
            if (!empty($result['food_update_time'])){
                $result['food_update_time'] = date('Y-m-d H:i:s',$result['food_update_time']);
            }

        }
        return $result;
    }

    /**
     * Notes:修改健康美食状态
     * @param string $food_name
     * @param int $food_status
     * @return int
     * User: ZQ
     * Date: 2022/9/15
     */
    public static function updateStatus($food_id, $food_status)
    {
        LifeFood::affairBegin();
        try {
            $where = [];
            $where['food_id'] = $food_id;
            $data = [];
            $data['food_status'] = $food_status;
            $result = LifeFood::where($where)->update($data);
            if ($result !== false){
                LifeFood::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeFood::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

}
