<?php

namespace app\api\controller;

use DateTime;
use app\common\model\MoneyLog as MonuyModel;
use app\common\model\StockLog;
use think\exception\ValidateException;
use app\api\validate\Money as MoneyValidate;
use app\common\model\StockDetail;
use think\facade\Db;

//记账记录表
class MoneyLog extends Base
{

      //记账记录
      public function moneylog(MonuyModel $monuyModel)
      {
            $time = $this->request->post('time/s', '');
            $count = $monuyModel::getCountMonthBalance($time);
            $list  = $monuyModel->with('users')->where('status', MonuyModel::STATUS_NORMAL);
            if(!empty($time)) $list = $list->whereMonth('create_date', $time);              
            $list = $list->order('create_date desc,id desc')->paginate(10);
            $this->success('ok', compact('list', 'count'));
      }


      //记账统计每天
      public function getCountDay(MonuyModel $monuyModel)
      {
            $time = $this->request->post('time/s', '');
            $result = $monuyModel::getCountDayBalance($time);
            $this->success('ok', $result);
      }


      //记账统计
      public function getCount(MonuyModel $monuyModel)
      {
            
            $type = $this->request->post('type/d, 1');
            $time = $this->request->post('time/s', date('Y-m'));
            //月统计 
            if($type == 1){
                  $result = $monuyModel::getCountMonthBalance($time);
                  //日均支出
                  $day = getDaysOfMonth($time);
                  $result['day_expenditure'] = ($result['expenditure'] > 0)? bcdiv($result['expenditure'], $day, 2): 0;
            }else{
                  $result = $monuyModel::getCountYearBalance($time);
                  //日均支出
                  $day = getDaysOfYear($time);
                  $result['day_expenditure'] = ($result['expenditure'] > 0)? bcdiv($result['expenditure'], $day, 2): 0;
            }
            $this->success('ok', $result);
      }

      //添加记账
      public function money(MonuyModel $monuyModel)
      {     
            $data = $this->request->post();
            $result = false;
            Db::startTrans();
            try {
                  validate(MoneyValidate::class)->scene('add')->check($data);
                  //发货数据
                  $data['user_id'] = $this->userinfo['id'];
                  $data['create_month'] = date('Ym', strtotime($data['create_date']));;//创建月份
                  $result = $monuyModel::create($data);

                  Db::commit();
            }catch (ValidateException $e) {
                  Db::rollback();
                  return $this->error($e->getError());
            } catch (\Exception $e) {
                  Db::rollback();
                  $this->error($e->getMessage());
            }
            if ($result === false) {
                  $this->error(__('没有新增任何数据'));
            }
            $this->success();
      }

      //账单编辑
      public function moneyedit(MonuyModel $monuyModel)
      {
            $data = $this->request->post();
            $result = false;
            Db::startTrans();
            try {
                  validate(MoneyValidate::class)->scene('edit')->check($data);
                  
                  $rows = $monuyModel->where('id', $data['id'])->where('user_id', $this->userinfo['id'])->where('status', $monuyModel::STATUS_NORMAL)->find();
                  if(!$rows) throw new \Exception('订单不存在');

                  $dateString = date('Ymd', strtotime($rows->createtime . ' +2 days'));
                  // 将日期字符串转换为时间戳
                  if(date('Ymd') > $dateString) throw new \Exception('已超出修改时间限制');
                  
                  //发货数据
                  $data['create_month'] = date('Ym', strtotime($data['create_date']));//创建月份
                  $result = $rows->save($data);

                  Db::commit();
            }catch (ValidateException $e) {
                  Db::rollback();
                  return $this->error($e->getError());
            } catch (\Exception $e) {
                  Db::rollback();
                  $this->error($e->getMessage());
            }
            if ($result === false) {
                  $this->error(__('没有更新任何数据'));
            }
            $this->success();
      }

      //删除账单
      public function moneydel(MonuyModel $monuyModel)
      {
            $id = $this->request->post('id/d');
            $result = false;
            Db::startTrans();
            try {
                  $rows = $monuyModel->where('id', $id)->where('user_id', $this->userinfo['id'])->whereDay('createtime')->where('status', $monuyModel::STATUS_NORMAL)->find();
                  if(!$rows) $this->error('数据不存在');
                  //发货数据
                  $rows->status = $monuyModel::STATUS_HIDDEN;
                  $result = $rows->save();
                  Db::commit();
            } catch (\Exception $e) {
                  Db::rollback();
                  $this->error($e->getMessage());
            }
            if ($result === false) {
                  $this->error(__('没有删除任何数据'));
            }
            $this->success();
      }


      //支收总览
      public function getCountAll(MonuyModel $monuyModel)
      {   

            $type = $this->request->post('type/d', 0); //1月 2年 3自定义
            $time = $this->request->post('time/s', '');
            $uids = $this->request->post('user_ids/a', []);

            //支收
            $count = $monuyModel::getCountAllBalance($type, $time, $uids);

            //记账账户余额
            $account = $monuyModel::getCountAllUserBalance($type, $time, $uids);

      
            //月统计
            $month = $monuyModel::getCountAllMonthBalance($type, $time, $uids);
         
            $this->success('ok', compact('count', 'account', 'month'));
      }


      /**
     * @return void 全部类型图标
      */
      public function getConfig()
      {     
            $type = $this->request->post('type/s', 'bank_account');
            if(!in_array($type, ['bank_account', 'money_in_type', 'money_out_type'])) $this->error('参数有误');
            $this->success('提交成功', site_config('addonsd.'.$type));
      }

   

   
}