// +---------------------------------------------------------------------- namespace app\services\product\sku; use app\dao\product\sku\StoreProductAttrResultDao; use app\services\BaseServices; use crmeb\exceptions\AdminException; /** * Class StoreProductAttrResultService * @package app\services\product\sku * @mixin StoreProductAttrResultDao */ class StoreProductAttrResultServices extends BaseServices { /** * StoreProductAttrResultServices constructor. * @param StoreProductAttrResultDao $dao */ public function __construct(StoreProductAttrResultDao $dao) { $this->dao = $dao; } /** * 获取属性规格 * @param array $where * @return mixed */ public function getResult(array $where) { return json_decode($this->dao->value($where, 'result'), true); } /** * 删除属性 * @param int $id * @param int $type * @return bool */ public function del(int $id, int $type) { return $this->dao->del($id, $type); } /** * 设置属性 * @param array $data * @param int $id * @param int $type * @return bool * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function setResult(array $data, int $id, int $type) { $result = $this->dao->get(['product_id' => $id, 'type' => $type]); if ($result) { $res = $this->dao->update($result['id'], ['result' => json_encode($data), 'change_time' => time()]); } else { $res = $this->dao->save(['product_id' => $id, 'result' => json_encode($data), 'change_time' => time(), 'type' => $type]); } if (!$res) throw new AdminException('规格保存失败'); return true; } }