<?php
namespace Application\Service\DB\Item;
use Interop\Container\ContainerInterface;

class Medication extends \Application\Service\DB\AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        $this->tableName = 'yikeen_item_medication';
        parent::__construct($dbAdapter, $container);
    }

    public function itemMedicationInsert($oldItemId,$newItemId){
        $whereOld['where'] = [
            'is_del' => 0,
            'item_id' => $oldItemId,
        ];
        $whereNew['where'] = [
            'is_del' => 0,
            'item_id' => $newItemId,
        ];
        $maxOrder = $this->LocalService()->itemMedication->getMaxOrder('medication_order', $whereNew['where']);
        $data = $this->LocalService()->itemMedication->fetchAll($whereOld);
        if (!empty($data)){
            foreach ($data as $k => $v) {
                unset($v['id']);
                $v['item_id'] = $newItemId;
                $v['medication_order'] = $maxOrder;
                $maxOrder += 10;
                $result = $this->LocalService()->itemMedication->save($v);

            }
        }


    }
}

