<?php
declare (strict_types = 1);

namespace app\admin\controller\v1;

use app\admin\exception\InvalidOperateException;
use app\admin\model\v1\SysBannerItemModel;
use app\admin\model\v1\SysBannerProdCateMappingModel;
use app\admin\validate\v1\SysBannerItemValidate;
use think\facade\Log;

/**
 * 横幅数据项控制器
 */
class BannerItem
{
    // 分页
    public function index()
    {
        $param = request()->param([
            'title',
            'banner_id',
            'created_at',
            'page/d' => 1,
            'size/d' => 10
        ]);

        $banner_items = SysBannerItemModel::alias('item')
            ->field([
                'item.id',
                'item.title',
                'item.image',
                'item.sort',
                'item.status',
                'item.created_at',
                'bnr.name' => 'banner_name'
            ])
            ->join('sys_banner bnr', 'bnr.id = item.banner_id')
            ->where('bnr.language_id', '=', request()->lang_id)
            ->where(function($query) use($param){
                if (!empty($param['banner_id'])) {
                    if (is_array($param['banner_id']) || str_contains($param['banner_id'], ',')) {
                        $query->whereIn('item.banner_id', $param['banner_id']);
                    } else {
                        $query->where('item.banner_id', '=', $param['banner_id']);
                    }
                }
                if (!empty($param['title'])) {
                    $query->where('item.title', 'like', "%{$param['title']}%");
                }
                if (!empty($param['created_at'])) {
                    $value = explode(',', $param['created_at']);
                    if (count($value) > 1) {
                        if ($value[1] == $value[0]) {
                            $value[1] = date('Y-m-d 23:59:59', strtotime($value[1]));
                        }
                        $query->whereBetweenTime('item.created_at', $value[0], $value[1]);
                    } else {
                        $query->whereTime('item.created_at', '>=', $value[0]);
                    }
                }
            })
            ->order(['item.banner_id' => 'asc', 'item.sort' => 'asc', 'item.id' => 'desc'])
            ->paginate([
                'list_rows' => $param['size'],
                'page'      => $param['page']
            ])
            ->each(fn($item) => $item->image = thumb($item->image));

        return success('获取成功', $banner_items);
    }

    // 详情
    public function read()
    {
        $id = request()->param('id');

        $banner_item = SysBannerItemModel::with('prodMapping.category')
            ->withoutField([
                'created_at',
                'updated_at',
                'deleted_at'
            ])
            ->append(['rel_prod_cate_id', 'rel_prod_cate_name']) // 绑定产品分类关联模型中字段到父模型中
            ->find($id);
        if (empty($banner_item)) {
            return error('横幅不存在');
        }

        // 组装用于前台回显的链接数据
        $banner_item['link_echo_data'] = System::getEchoDataBySystemPageUrl($banner_item['link_to'], $banner_item['link']);

        return success('获取成功', $banner_item);
    }

    // 新增
    public function save()
    {
        $post = request()->post([
            'banner_id',
            'rel_prod_cate_id',
            'title',
            'title_txt_color',
            'short_title',
            'short_title_txt_color',
            'desc',
            'desc_txt_color',
            'type',
            'image',
            'extra_image',
            'video',
            'link_to' => 'custom',
            'link',
            'sort',
            'status' => 1
        ]);

        $validate = new SysBannerItemValidate;
        if (!$validate->scene('add')->check($post)) {
            return error($validate->getError());
        }

        SysBannerItemModel::startTrans();
        try {
            $banner_item = SysBannerItemModel::create($post);
            if ($banner_item->isEmpty()) {
                throw new InvalidOperateException('新增横幅失败');
            }
            if (!empty($post['rel_prod_cate_id'])) {
                $mapping = SysBannerProdCateMappingModel::create([
                    'banner_item_id'      => $banner_item->id,
                    'product_category_id' => $post['rel_prod_cate_id']
                ]);
                if ($mapping->isEmpty()) {
                    throw new InvalidOperateException('新增横幅与产品分类关联失败');
                }
            }

            SysBannerItemModel::commit();
        } catch (InvalidOperateException $e) {
            SysBannerItemModel::rollback();
            return error($e->getMessage());
        } catch (\Throwable $th) {
            SysBannerItemModel::rollback();
            Log::error(sprintf('%s:%s %s', $th->getFile(), $th->getLine(), $th->getMessage()));
            return error('操作失败');
        }

        return success('操作成功');
    }

    // 修改
    public function update()
    {
        $id  = request()->param('id');
        $put = request()->put([
            'banner_id',
            'rel_prod_cate_id',
            'title',
            'title_txt_color',
            'short_title',
            'short_title_txt_color',
            'desc',
            'desc_txt_color',
            'type',
            'image',
            'extra_image',
            'video',
            'link_to',
            'link',
            'sort',
            'status'
        ]);

        $validate = new SysBannerItemValidate;
        if (!$validate->check(array_merge($put, ['id' => $id]))) {
            return error($validate->getError());
        }

        SysBannerItemModel::startTrans();
        try {
            $banner_item = SysBannerItemModel::bypk($id)->find();
            if (empty($banner_item)) {
                throw new InvalidOperateException('请确认操作对象是否存在');
            }
            if ($put['type'] == 'video') {
                unset($put['link']);
                unset($put['link_to']);
            }
            if (!$banner_item->save($put)) {
                throw new InvalidOperateException('操作失败');
            }

            // 更新关联的产品分类
            if (!empty($put['rel_prod_cate_id'])) {
                SysBannerProdCateMappingModel::where('banner_item_id', '=', $id)->delete();
                $mapping = SysBannerProdCateMappingModel::create([
                    'banner_item_id'      => $id,
                    'product_category_id' => $put['rel_prod_cate_id']
                ]);
                if ($mapping->isEmpty()) {
                    throw new InvalidOperateException('更新横幅与产品分类关联失败');
                }
            }

            SysBannerItemModel::commit();
        } catch (InvalidOperateException $e) {
            SysBannerItemModel::rollback();
            return error($e->getMessage());
        } catch (\Throwable $th) {
            SysBannerItemModel::rollback();
            Log::error(sprintf('%s:%s %s', $th->getFile(), $th->getLine(), $th->getMessage()));
            return error('操作失败');
        }

        return success('操作成功');
    }

    // 设置排序值
    public function sort()
    {
        $id   = request()->param('id');
        $sort = request()->param('sort');

        $banner_item = SysBannerItemModel::bypk($id)->find();
        if (empty($banner_item)) {
            return error('请确认操作对象是否存在');
        }

        if ($banner_item->sort != $sort) {
            $banner_item->sort = $sort;
            if (!$banner_item->save()) {
                return error('操作失败');
            }
        }
        return success('操作成功');
    }

    // 导出
    public function export()
    {
        $schema = [
            'id'              => 'ID',
            'banner_name'     => '分类名称',
            'title'           => '横幅名称',
            'title_txt_color' => '横幅名称字体颜色',
            'short_title'     => '横幅简称',
            'desc'            => '描述',
            'desc_txt_color'  => '描述字体颜色',
            'type'            => '前台显示类型',
            'image'           => '图片地址',
            'extra_image'     => '额外图片地址',
            'video'           => '视频地址',
            'link_to'         => '链接类型',
            'link'            => '链接地址',
            'sort'            => '排序值',
            'status'          => '状态',
            'created_at'      => '添加时间'
        ];

        // 获取要导出的横幅项数据
        $banner_items = $this->getBannerExportData();

        // 导出
        return xlsx_writer($banner_items, $schema, 'banner列表' . date('YmdHis'));
    }
    // 获取导出数据
    private function getBannerExportData()
    {
        $server = request()->server();
        $image_host = $server['REQUEST_SCHEME'] . "://" . $server['SERVER_NAME'] . '/';
        $param = request()->param(['title', 'banner_id', 'created_at']);
        return SysBannerItemModel::alias('item')
            ->field([
                'item.id',
                'banner.name' => 'banner_name',
                'item.title',
                'item.title_txt_color',
                'item.short_title',
                'item.desc',
                'item.desc_txt_color',
                'item.type',
                'item.image',
                'item.extra_image',
                'item.video',
                'item.link_to',
                'item.link',
                'item.sort' ,
                'item.status',
                'item.created_at'
            ])
            ->join('sys_banner banner', 'banner.id = item.banner_id')
            ->where('banner.language_id', '=', request()->lang_id)
            ->where(function($query) use($param){
                if (!empty($param['banner_id'])) {
                    if (is_array($param['banner_id']) || str_contains($param['banner_id'], ',')) {
                        $query->whereIn('item.banner_id', $param['banner_id']);
                    } else {
                        $query->where('item.banner_id', '=', $param['banner_id']);
                    }
                }
                if (!empty($param['title'])) {
                    $query->where('item.title', 'like', "%{$param['title']}%");
                }
                if (!empty($param['created_at'])) {
                    $value = explode(',', $param['created_at']);
                    if (count($value) > 1) {
                        if ($value[1] == $value[0]) {
                            $value[1] = date('Y-m-d 23:59:59', strtotime($value[1]));
                        }
                        $query->whereBetweenTime('item.created_at', $value[0], $value[1]);
                    } else {
                        $query->whereTime('item.created_at', '>=', $value[0]);
                    }
                }
            })
            ->order(['item.sort' => 'asc', 'item.id' => 'desc'])
            ->select()
            ->each(function($item) use($image_host) {
                $item->image       = !empty($item->image) ? url_join($image_host, $item->image) : '';
                $item->extra_image = !empty($item->extra_image) ? url_join($image_host, $item->extra_image) : '';
                $item->video       = !empty($item->video) ? url_join($image_host, $item->video) : '';
                return $item;
            });
    }

    // 删除
    public function delete()
    {
        $id = request()->param('id');

        $banner_item = SysBannerItemModel::bypk($id)->find();
        if (empty($banner_item)) {
            return error('请确认操作对象是否存在');
        }

        if (!$banner_item->delete()) {
            return error('操作失败');
        }
        return success('操作成功');
    }
}
