<?php

namespace app\adminapi\controller\v1\marketing;

use app\adminapi\controller\AuthController;
use crmeb\traits\CurdControllerTrait;
use crmeb\services\UtilService as Util;
use app\models\store\{StoreDescription, StoreAssistanceActive, StoreAssistance as StoreAssistanceModel, StoreProductAttr};

/**
 * 助力管理
 * Class StoreAssistance
 * @package app\admin\controller\store
 */
class StoreAssistance extends AuthController
{

	use CurdControllerTrait;

	protected $bindModel = StoreAssistanceModel::class;

	/**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $where = Util::getMore([
            ['page', 1],
            ['limit', 20],
            ['is_show', ''],
            ['store_name', '']
        ]);
        $where['mer_id'] = $this->merId ?: '';
        $list = StoreAssistanceModel::systemPage($where);
        return $this->success($list);
    }

    /**
     * 拼团统计
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function statistics()
    {
        $mer_id = $this->merId ?: '';
        $info = StoreAssistanceModel::getStatistics($mer_id);
        return $this->success($info);
    }

    /**
     * 详情
     * @param $id
     * @return mixed
     */
    public function read($id)
    {
        $info = StoreAssistanceModel::getOne($id);
        return $this->success(compact('info'));
    }

    /**
     * 保存新建的资源
     * @param int $id
     */
    public function save($id = 0)
    {
        $data = Util::postMore([
            [['product_id', 'd'], 0],
            [['mer_id', 'd'], ''],
            [['title', 's'], ''],
            [['info', 's'], ''],
            [['unit_name', 's'], ''],
            ['image', ''],
            ['images', []],
            ['section_time', []],
            [['is_host', 'd'], 0],
            [['is_show', 'd'], 0],
            [['num', 'd'], 0],
            [['temp_id', 'd'], 0],
            [['effective_time', 'd'], 0],
            [['people', 'd'], 0],
            [['description', 's'], ''],
            [['p_separate_account', 'd'], 0],
            [['c_separate_account', 'd'], 0],
            ['attrs', []],
            ['items', []],
            ['num', 1],
            ['sort', 0]
        ]);
        $this->validate($data, \app\adminapi\validates\marketing\StoreAssistanceValidate::class, 'save');
        $description = $data['description'];
        $detail = $data['attrs'];
        $items = $data['items'];
        $data['start_time'] = strtotime($data['section_time'][0]);
        $data['stop_time'] = strtotime($data['section_time'][1]);
        $data['images'] = json_encode($data['images']);
        $data['price'] = min(array_column($detail, 'price'));
        $data['quota'] = $data['quota_show'] = array_sum(array_column($detail, 'quota'));
        $data['stock'] = array_sum(array_column($detail, 'stock'));
        $data['mer_id'] = $this->merId ?: '';
        unset($data['section_time'], $data['description'], $data['attrs'], $data['items']);
        if ($id) {
            $product = StoreAssistanceModel::get($id);
            if (!$product) return $this->fail('数据不存在!');
            $data['product_id'] = $product['product_id'];
            StoreAssistanceModel::edit($data, $id);
            StoreProductAttr::createProductAttr($items, $detail, $id, 4);
            StoreDescription::saveDescription($description, $id, 4);
            return $this->success('编辑成功!');
        } else {
            $data['add_time'] = time();
            $assistance_id = StoreAssistanceModel::insertGetId($data);
            StoreProductAttr::createProductAttr($items, $detail, $assistance_id, 4);
            StoreDescription::saveDescription($description, $assistance_id, 4);
            return $this->success('添加助力成功!');
        }
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if (!$id) return $this->fail('数据不存在');
        $product = StoreAssistanceModel::get($id);
        if (!$product) return $this->fail('数据不存在!');
        if ($product['is_del']) return $this->fail('已删除!');
        $data['is_del'] = 1;
        if (!StoreAssistanceModel::edit($data, $id))
            return $this->fail(StoreAssistanceModel::getErrorInfo('删除失败,请稍候再试!'));
        else
            return $this->success('删除成功!');
    }

    /**
     * 修改状态
     * @param $id
     * @param $status
     * @return mixed
     */
    public function set_status($id, $status)
    {
        if ($status == '' || $id == 0) return $this->fail('参数错误');
        StoreAssistanceModel::where(['id' => $id])->update(['is_show' => $status]);
        return $this->success($status == 0 ? '关闭成功' : '开启成功');
    }

    /**助力列表
     * @return mixed
     */
    public function assistance_list()
    {
        $where = Util::getMore([
            ['page', 1],
            ['limit', 20],
            ['status', ''],
            ['data', ''],
        ], $this->request);
        $where['mer_id'] = $this->merId ?: '';
        $list = StoreAssistanceActive::systemPage($where);
        return $this->success($list);
    }

    /**助力人列表
     * @return mixed
     */
    public function order_assistance($id)
    {
        $mer_id = $this->merId ?: '';
        $StoreAssistance = StoreAssistanceActive::getAssistanceUserOne($id, $mer_id);
        if (!$StoreAssistance) return $this->fail('数据不存在!');
        $list = StoreAssistanceActive::getAssistanceMember($id);
        $list[] = $StoreAssistance;
        return $this->success(compact('list'));
    }
}