<?php
namespace Base\Task;

use Base\Service\MarketService;

/**
 * 任务处理
 * @author elf<360197197@qq.com>
 */
class MarketShiftTask extends BaseTask
{
    public function run()
    {
        $id = $this->params['market_shift_id'];
        $record = M('market_shift', 'tab_')->where(['id' => $id])->find();
        if (!$record) {
            throw new \Exception('换绑记录不存在');
        }
        if ($record['status'] != 0) {
            throw new \Exception('换绑记录状态错误');
        }
        try {
            $marketService = new MarketService();
            $marketService->shift($record);
            M('market_shift', 'tab_')->where(['id' => $id])->save(['status' => 1]);
        } catch (\Exception $e) {
            M('market_shift', 'tab_')->where(['id' => $id])->save(['status' => 2]);
            throw new \Exception($e->getMessage());
        }
    }
}