<?php
namespace app\api\logic;

use app\common\logic\BaseLogic;
use app\common\model\goods\Goods;
use app\common\model\goods_category\GoodsCategory;
use think\facade\Db;


/**
 * 服务商品逻辑处理
 * Class GoodsLogic
 * @package app\api\logic
 */
class GoodsLogic extends BaseLogic
{

    /**
     * @notes 文章详情
     * @param $goods_id
     * @param $userId
     * @return array
     * @author whitef
     * @date 2022/9/20 17:09
     */
    public static function detail($id,$type){
        if($type == 'category'){
            $goods = Goods::where(['goods_category_id'=>$id,'user_id'=>0,'is_agent'=>0,'is_activity'=>0])->visible([
                'id','goods_image','goods_name','goods_banners','good_unit','sell_num','base_service_fee',
                'service_total','service_fee','service_image','fee_schedule','warranty_period','goods_payment_type','goods_category_id'
            ])->select()->toArray();
        }else{
            $goods_category_id = Goods::where('id',$id)->value('goods_category_id');
            $goods = Goods::where(['goods_category_id'=>$goods_category_id,'user_id'=>0,'is_agent'=>0,'is_activity'=>0])->visible([
                'id','goods_image','goods_name','goods_banners','good_unit','sell_num','base_service_fee',
                'service_total','service_fee','service_image','fee_schedule','warranty_period','goods_payment_type','goods_category_id'
            ])->select()->toArray();
        }

        if(!empty($goods)){
            $data = [];
            foreach ($goods as $key => $value) {
                if($value['goods_payment_type'] == '2'){
                    $coupon = ($value['service_total']-$value['service_fee'])>0?'立减'.$value['service_total']-$value['service_fee']:'';
                }
                if($value['goods_payment_type'] == '3'){
                    $coupon = '首付'.$value['service_fee'];
                }
                $data[] = [
                    'id'=>$value['id'],
                    'goods_image'=>$value['goods_image'],
                    'goods_name'=>$value['goods_name'],
                    'good_unit'=>$value['good_unit'],
                    'goods_banners'=>!empty($value['goods_banners']) ? $value['goods_banners'] : [],
                    'goods_payment_type'=>$value['goods_payment_type'],
                    'sell_num'=>$value['sell_num'],
                    'base_service_fee'=>$value['base_service_fee'],
                    'service_total'=>$value['service_total'],
                    'service_fee'=>$value['service_fee'],
                    'coupon'=>!empty($coupon)?$coupon:'',
                    'service_image'=>$value['service_image'],
                    'warranty_period'=>$value['warranty_period'],
                    'fee_schedule'=>$value['fee_schedule'],
                    'goods_category_id'=>$value['goods_category_id']
                ];
            }

            $goods = $data;
        }

        return $goods;
    }

    public static function eventDetail($id){
        $goods = Goods::where(['id'=>$id])->visible([
                'id','goods_image','goods_name','goods_banners','good_unit','sell_num','base_service_fee',
                'service_total','service_fee','service_image','fee_schedule','warranty_period','goods_payment_type','goods_category_id'
            ])->select()->toArray();

        if(!empty($goods)){
            $data = [];
            foreach ($goods as $key => $value) {
                if($value['goods_payment_type'] == '2'){
                    $coupon = ($value['service_total']-$value['service_fee'])>0?'立减'.$value['service_total']-$value['service_fee']:'';
                }
                if($value['goods_payment_type'] == '3'){
                    $coupon = '首付'.$value['service_fee'];
                }
                $data[] = [
                    'id'=>$value['id'],
                    'goods_image'=>$value['goods_image'],
                    'goods_name'=>$value['goods_name'],
                    'good_unit'=>$value['good_unit'],
                    'goods_banners'=>!empty($value['goods_banners']) ? $value['goods_banners'] : [],
                    'goods_payment_type'=>$value['goods_payment_type'],
                    'sell_num'=>$value['sell_num'],
                    'base_service_fee'=>$value['base_service_fee'],
                    'service_total'=>$value['service_total'],
                    'service_fee'=>$value['service_fee'],
                    'coupon'=>!empty($coupon)?$coupon:'',
                    'service_image'=>$value['service_image'],
                    'warranty_period'=>$value['warranty_period'],
                    'fee_schedule'=>$value['fee_schedule'],
                    'goods_category_id'=>$value['goods_category_id']
                ];
            }

            $goods = $data;
        }

        return $goods;
    }

    public static function getHotData()
    {
        return Goods::where(['user_id'=>0,'is_hot'=>1])
            ->with('goodsCategory')
            ->visible(['id','goods_name','goods_image'])
            ->order(['top_weight' => 'desc', 'id' => 'desc'])
            ->select()->toArray();
    }

    public static function sync(array $params,int $userId): bool
    {
        Db::startTrans();
        try {
            if(isset($params['id'])){
                $model = Goods::findOrEmpty($params['id']);
                if($model->isEmpty()){
                    throw new \Exception('数据异常');
                }
            } else{
                $model = new Goods();
            }
            $params['goods_category_id'] = end($params['goods_category_ids']);
            $model->goods_category_id = $params['goods_category_id'];
            $model->category_type = GoodsCategory::where('id',$params['goods_category_id'])->value('category_type');
            $model->goods_category_ids =  $params['goods_category_ids'];
            $model->goods_category_id = $params['goods_category_id'];
            $model->goods_name = $params['goods_name'];
            $model->goods_image = $params['goods_image'];
            $model->goods_video = $params['goods_video'];
            $model->goods_banners = $params['goods_banners'];
            $model->goods_number = $params['goods_number'];
            $model->good_unit = $params['good_unit'];
            $model->goods_size = $params['goods_size'];
            $model->goods_type = $params['goods_type'];
            $model->goods_brand = $params['goods_brand'];
            $model->install_guide = $params['install_guide'];
            $model->goods_payment_type = $params['goods_payment_type'];
            $model->base_service_fee = $params['base_service_fee'];
            $model->service_total = $params['service_total'];
            $model->service_fee = $params['service_fee'];
            $model->service_image = $params['service_image'];
            $model->warranty_period = $params['warranty_period'];
            $model->fee_schedule = $params['fee_schedule'];
            $model->goods_status = $params['goods_status'];
            $model->user_id = $userId;
            $model->save();
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }

    public static function firmGoodsDetail($params){
        try{
            $data= Goods::where('id',$params['id'])
            ->where('user_id',$params['user_id'])
            ->with('goodsCategory')
            ->withoutField(['is_recommend','recommend_weight','is_top','top_weight','is_hot','hot_weight'])
            ->findOrEmpty()->toArray();
            if(!empty($data['goods_category_ids'])) {
                $data['goods_category_ids'] = array_map("intval", $data['goods_category_ids']);
            }
             return $data;
        }catch(\Exception $e){
            self::setError($e->getMessage());
            return false;
        }
    }
}