<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/1/19
 * Time: 下午10:50
 */
class importer_check
{
    private $goods;
    private $goods_common;
    private $goods_images;
    private $album_pic;

    public function __construct()
    {
        $this->goods = Model('goods');
        $this->goods_common = Model('goods_common');
        $this->goods_images = Model('goods_images');
        $this->album_pic = Model('album_pic');
    }

    private function check_goods($comm_id)
    {
        $goods = $this->goods->field('goods_id,color_id')->where(array('goods_commonid' => $comm_id))->limit(false)->select();
        if(empty($goods)) {
            return false;
        }

        if(count($goods) == 0) {
            return false;
        }

        return $goods;
    }

    private function check_goods_common($comm_id)
    {
        $goods = $this->goods_common->field('goods_commonid')->where(array('goods_commonid' => $comm_id))->select();
        if(empty($goods)) {
            return false;
        }

        if(count($goods) == 0) {
            return false;
        }

        return $goods;
    }

    private function check_images($comm_id)
    {
        $goods = $this->goods_images->field('goods_image_id,color_id')->where(array('goods_commonid' => $comm_id))->group('filesig')->limit(false)->select();
        if(empty($goods)) {
            return false;
        }

        if(count($goods) == 0) {
            return false;
        }

        return $goods;
    }

    public function proc($del = true)
    {
        $commonids = $this->goods_common->field('goods_commonid')->limit(false)->select();
        if(empty($commonids)) {
            Log::record("cannot find any goods from goods_common table.",Log::DEBUG);
            return;
        }

        foreach($commonids as $val)
        {
            $cid = $val['goods_commonid'];
            $goods = $this->check_goods($cid);
            $images = $this->check_images($cid);

            if($goods == false || $images == false)
            {
                if($del) {
                    $this->goods_common->where(array('goods_commonid' => $cid))->delete();
                    $this->goods->where(array('goods_commonid' => $cid))->delete();
                    $this->goods_images->where(array('goods_commonid' => $cid))->delete();

                    Log::record("delete goods when goods_commonid = {$cid}", Log::DEBUG);
                } else {
                    Log::record("goods_commonid = {$cid} goods has error.",Log::DEBUG);
                }
            }
        }

        $commonids = $this->goods->field('goods_commonid')->group('goods_commonid')->limit(false)->select();

        if(empty($commonids)) {
            Log::record("cannot find any goods_commonid from goods table.",Log::DEBUG);
            return;
        }

        foreach($commonids as $val)
        {
            $cid = $val['goods_commonid'];
            $goods  = $this->check_goods_common($cid);
            $images = $this->check_images($cid);

            if($goods == false || $images == false)
            {
                if($del) {
                    $this->goods_common->where(array('goods_commonid' => $cid))->delete();
                    $this->goods->where(array('goods_commonid' => $cid))->delete();
                    $this->goods_images->where(array('goods_commonid' => $cid))->delete();
                    Log::record("delete goods when goods_commonid = {$cid}",Log::DEBUG);
                } else {
                    Log::record("goods_commonid = {$cid} goods has error.",Log::DEBUG);
                }
            }
        }
    }
}