REAPI/app/merchant/service/DayService.php

163 lines
4.5 KiB
PHP
Raw Permalink Normal View History

2024-09-29 15:43:18 +08:00
<?php
namespace app\merchant\service;
use app\core\Service;
use think\admin\extend\CodeExtend;
use app\merchant\service\MerchantService;
/**
* 每天记录余额 ,暂时用不到
* Class CardService
* @package app\merchant\service
*/
class DayService extends Service
{
/**
* 设置默认操作表
* @var string
*/
public $table = 'merchant_day';
# 提交订单
public function get($mid, $day = false)
{
if (!$day) {
$day = date('Ymd');
}
if (strstr($day, ' ')) {
$temp = explode(' ', $day);
$day = str_replace('-', '', $temp[0]);
}
$where['mid'] = $mid;
$where['day'] = $day;
$info = $this->db()->where($where)->find();
return $info;
}
# 加款和减款
public function add($type, $mid, $day, $num, $account_type)
{
$info = $this->get($mid, $day);
if ($info) {
if ($type == 1) {
$update['account_add'] = $info['account_add'] + $num;
if ($account_type == 1) {
$update['account_surplus'] = $info['account_surplus'] + $num;
} elseif ($account_type == 2) {
$update['credit_surplus'] = $info['credit_surplus'] + $num;
}
}
if ($type == 2) {
$update['account_des'] = $info['account_des'] + $num;
if ($account_type == 1) {
$update['account_surplus'] = $info['account_surplus'] - $num;
} elseif ($account_type == 2) {
$update['credit_surplus'] = $info['credit_surplus'] - $num;
}
}
if ($type == 6) {
$update['credit_add'] = $info['credit_add'] + $num;
$update['credit_surplus'] = $info['credit_surplus'] + $num;
}
$this->db()->where(array('id' => $info['id']))->update($update);
}
}
# 更新数据
public function up($today, $data)
{
$day = str_replace('-', '', $today);
$today = $this->maketime($today) + 86400;
$today = date('Ymd', $today);
foreach ($data as $k => $v) {
$where['mid'] = $k;
$where['day'] = $day;
$info = $this->db()->where($where)->find();
if ($info) {
$total = $info['account_surplus'] + $info['credit_surplus'];
if ($info['account_surplus'] >= $v) {
$update['account_surplus'] = $info['account_surplus'] - $v;
} elseif ($info['credit_surplus'] >= $v) {
$update['credit_surplus'] = $info['credit_surplus'] - $v;
} elseif ($total >= $v) {
$v = $v - $info['account_surplus'];
$update['account_surplus'] = 0;
$update['credit_surplus'] = $info['credit_surplus'] - $v;
} else {
$update['account_surplus'] = $info['account_surplus'] - $v;
}
$update['account_consum'] = $v;
$update['status'] = 2;
$this->db()->where(array('id' => $info['id']))->update($update);
$toinfo = $this->db()->where(array('mid' => $k, 'day' => $today))->find();
if ($toinfo) {
$update['status'] = 1;
$this->db()->where(array('id' => $toinfo['id']))->update($update);
} else {
$update['mid'] = $k;
$update['day'] = $today;
$update['status'] = 1;
$this->db()->insert($update);
}
}
}
}
public function init()
{
# 初始化,或者所有数据
$day = date('Ymd');
$data = MerchantService::instance()->db()->select()->toArray();
foreach ($data as $k => $v) {
$this->initOne($v['id'], $day);
}
}
public function initOne($mid, $day)
{
# 初始化,获取所有的数据
$w['mid'] = $mid;
$w['account_type'] = 1;
$w['oper'] = 1;
$w['is_yue'] = 1;
$total = MerchantLogService::instance()->db()->where($w)->sum('num');
$w['oper'] = 2;
$consum = MerchantLogService::instance()->db()->where($w)->sum('num');
$update['account_surplus'] = round(($total - $consum), 2);
$w['oper'] = 1;
$w['account_type'] = 2;
$total = MerchantLogService::instance()->db()->where($w)->sum('num');
$w['oper'] = 2;
$consum = MerchantLogService::instance()->db()->where($w)->sum('num');
$update['credit_surplus'] = round(($total - $consum), 2);
$info = $this->db()->where(array('mid' => $mid, 'day' => $day))->find();
if ($info) {
$update['status'] = 1;
$this->db()->where(array('id' => $info['id']))->update($update);
} else {
$update['mid'] = $mid;
$update['day'] = $day;
$update['status'] = 1;
$this->db()->insert($update);
}
}
}