<?php

namespace Cash\Lib;

use Dever;

class Cron
{
    private function check($info)
    {
        if (!$info['stat_day']) {
            $info['stat_day'] = 1;
        }

        if ($info['stat_type'] == 1) {
            $day = intval(date('d'));
            if ($day != $info['stat_day']) {
                return false;
            }
        } elseif ($info['stat_type'] == 2) {
            $day = intval(date('w'));
            if (!$day || $day == 0) {
                $day = 7;
            }
            if ($day != $info['stat_day']) {
                return false;
            }
        }
        return true;
    }

    # 处理门店对账单
    public function shop_api(){}
    public function shop()
    {
        $num = Dever::input('num', 1);

        $where['status'] = 2;
       
        $shop = Dever::db('shop/info')->select();

        foreach ($shop as $k => $v) {
        	$where['type'] = 1;
            $where['type_id'] = $v['id'];
        	$this->up($where, $v, 'shop', $num);
        }
    }

    public function store_api(){}
    public function store()
    {
        $num = Dever::input('num', 1);

        $where['status'] = $w['status'] = 2;
       
        $store = Dever::db('store/info')->select();

        foreach ($store as $k => $v) {
            $where['type'] = 2;
            $where['type_id'] = $v['id'];
            $this->up($where, $v, 'store', $num, 1);
        }
    }

    public function factory_api(){}
    public function factory()
    {
        $num = Dever::input('num', 1);

        $where['status'] = 2;
       
        $store = Dever::db('factory/info')->select();

        foreach ($store as $k => $v) {
            $where['source_type'] = 3;
            $where['source_id'] = $v['id'];
            $this->up($where, $v, 'factory', $num);
        }
    }

    private function up($where, $info, $type, $num, $k = 1)
    {
        $state = $this->check($info);
        if (!$state) {
            return;
        }
        if ($info['stat_type'] == 1) {
    		$method = 'month';
    	} elseif ($info['stat_type'] == 2) {
    		$method = 'week';
    	} else {
            $method = 'day';
        }

    	list($start, $end) = Dever::$method($num);

    	$where['start'] = $start;
        $where['end'] = $end;

        $data = array();
        $data[$type . '_id'] = $info['id'];
        $data['type'] = $info['stat_type'];
        $data['day'] = $start;
        $data['t'] = $k;
        $find = Dever::db('cash/' . $type)->find($data);
        if ($type == 'factory') {
            $msg_type = 4;
            $cash = Dever::db('cash/order')->getPCash($where);
            $data['cash'] = $cash['total'] ? $cash['total'] : 0;
        } else {
            $msg_type = 3;
            $cash = Dever::db('cash/order')->getCash($where);
            $data['cash'] = $cash['total'] ? $cash['total'] : 0;
        }
        
        $num = Dever::db('cash/order')->getNum($where);
        $data['num'] = $num['total'] ? $num['total'] : 0;
        $data['start'] = $start;
        $data['end'] = $end;
        if (!$find) {
            $id = Dever::db('cash/' . $type)->insert($data);
        } else {
            $id = $data['where_id'] = $find['id'];
            Dever::db('cash/' . $type)->update($data);
        }

        if ($id > 0 && $type == 'store') {
            $where['source_type'] = $where['type'];
            $where['source_id'] = $where['type_id'];
            unset($where['type']);
            unset($where['type_id']);
            $cash = Dever::db('cash/order')->getCash($where);
            $up['cash'] = $cash['total'] ? $cash['total'] : 0;

            $num = Dever::db('cash/order')->getNum($where);
            $up['num'] = $num['total'] ? $num['total'] : 0;

            $data['cash'] += $up['cash'];
            $data['num'] += $up['num'];
            $data['where_id'] = $id;
            Dever::db('cash/' . $type)->update($data);
        }

        if (($type == 'factory' || $type == 'store') && Dever::project('message') && $id) {
            $msg_param['type'] = 2;//消息头类型2是对账单消息
            $msg_param['id'] = $id;
            $msg_param = Dever::json_encode($msg_param);
            $msg = '您的'.Dever::load('cash/lib/set')->statDate($data['type'], $data['day']).'对账单已经生成，请您及时确认，确认之后方可结算';
            Dever::load('message/lib/data')->push(-1, $info['id'], '对账通知', $msg, 6, $msg_type, false, $msg_param);
        }
        return $id;
    }
}