记录

version

描述:

目录结构

  1. .
  2. ├── Controller
  3. ├── Install
  4. ├── Libs 核心实现库
  5. ├── Records 记录实体类
  6. ├── Model
  7. ├── Service 服务
  8. └── Uninstall

使用指南

1.创建你的记录实体类

Record/Records/ 目录下创建消息实体类,并继承 Record\Libs\Record

  1. <?php
  2. namespace Record\Records;
  3. use Record\Libs\Record;
  4. class TradeRecord extends Record {
  5. public $table_name = 'RecordTrade';
  6. public function __construct($to, $target_type, $target) {
  7. $this->setTo($to);
  8. $this->setTargetType($target_type);
  9. $this->setTarget($target);
  10. }
  11. }

2.添加记录

使用 Record\Service\RecordService::createRrcord($record) 添加消息

  1. <?php
  2. namespace Record\Controller;
  3. use Common\Controller\AdminBase;
  4. use Record\Records\TradeRecord;
  5. use Record\Service\RecordService;
  6. use Record\Service\TradeRecordService;
  7. class TestController extends AdminBase {
  8. public function index() {
  9. $recode = new TradeRecord('1', 'wxpay', '1232122');
  10. $recode->setIncome(100);
  11. $this->ajaxReturn(RecordService::createRrcord($recode));
  12. // $this->ajaxReturn(TradeRecordService::createTradeRecord('1', 'wxpay', '1232122', 100, 50));
  13. }
  14. }

3.进一步封装 service

  1. <?php
  2. namespace Record\Service;
  3. use Record\Libs\Record;
  4. use Record\Model\RecordModel;
  5. use System\Service\BaseService;
  6. class RecordService extends BaseService {
  7. static function createRrcord(Record $record) {
  8. //获取上一条合法的record id
  9. $where['status'] = RecordModel::STATUS_VAILD;
  10. $where['to'] = $record->getTo();
  11. $where['to_type'] = $record->getToType();
  12. $last_vaild_record = M($record->table_name)->where($where)->order('id desc')->find();
  13. $last_vaild_balance = self::getBalance($record)['data']; //获取最近的余额信息
  14. $data = [
  15. 'parent_id' => $last_vaild_record ? $last_vaild_record['id'] : 0,
  16. 'to' => $record->getTo(),
  17. 'to_type' => $record->getToType(),
  18. 'from' => $record->getFrom(),
  19. 'from_type' => $record->getFromType(),
  20. 'target' => $record->getTarget(),
  21. 'target_type' => $record->getTargetType(),
  22. 'income' => $record->getIncome(),
  23. 'pay' => $record->getPay(),
  24. 'balance' => ($last_vaild_balance + $record->getIncome() - $record->getPay()),//计算当前记录的余额
  25. 'detail' => $record->getDetail(),
  26. 'status' => $record->getStatus(),
  27. 'create_time' => time(),
  28. 'remark' => $record->getRemark()
  29. ];
  30. $result = M($record->table_name)->add($data);
  31. if ($result) {
  32. return self::createReturn(true, $result);
  33. }
  34. return self::createReturn(false, null, '操作失败');
  35. }
  36. static function getBalance(Record $record) {
  37. $where = [
  38. 'to' => $record->getTo(),
  39. 'to_type' => $record->getToType(),
  40. 'status' => RecordModel::STATUS_VAILD
  41. ];
  42. $lists = M($record->table_name)->field('income,pay')->where($where)->select();
  43. $total = 0;
  44. for ($i = 0; $i < count($lists); $i++) {
  45. $total = $total + $lists[$i]['income'] - +$lists[$i]['pay'];
  46. }
  47. return self::createReturn(true, $total, 'ok');
  48. }
  49. }