0
0

MateApplyUse.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Exception;
  5. class MateApplyUse extends Base
  6. {
  7. protected $createTime = 'create_time';
  8. protected $updateTime = 'update_time';
  9. public $table = 'mate_apply_use';
  10. protected $validateName = 'MateApplyUse';
  11. public function updates($userId){
  12. $data = request()->post();
  13. $data['org_id'] = cur_org_id();
  14. $result = validate($this->validateName)->check($data,[],'');
  15. if(true !== $result){
  16. $this->error = validate($this->validateName)->getError();
  17. return false;
  18. }
  19. $curTime = date('Y-m-d H:i:s');
  20. Db::startTrans();
  21. try{
  22. $applyId = Db::name('mate_apply')->insertGetId([
  23. 'org_id' => $data['org_id'],
  24. 'create_time' => $curTime,
  25. 'user_id' => $userId,
  26. 'sn' => get_unique_id(),
  27. 'remark' => $data['remark'],
  28. 'name' => $data['name'],
  29. 'phone' => $data['phone'],
  30. 'type' => $data['type']
  31. ]);
  32. if(!$applyId){
  33. \exception('操作失败');
  34. }
  35. $arr = [];
  36. foreach ($data['goods'] as $k=>$v){
  37. // 重新计算平均价
  38. $info = Db::name('mate_goods')->where('id',$k)->find();
  39. if($data['type'] == 1){
  40. $price = ($info['price'] *$info['nums'] + $v['price']*$v['nums']) / ($info['nums']+$v['nums']);
  41. $ret = Db::name('mate_goods')->where('id',$k)->update([
  42. 'price' => round($price,2),
  43. 'nums' => $info['nums']+$v['nums'],
  44. 'update_time' => $curTime
  45. ]);
  46. if(!$ret){
  47. \exception('操作失败');
  48. }
  49. }else{
  50. $ret = Db::name('mate_goods')->where('id',$k)->update([
  51. 'nums' => $info['nums'] - $v['nums'],
  52. 'update_time' => $curTime
  53. ]);
  54. if(!$ret){
  55. \exception('操作失败');
  56. }
  57. }
  58. $arr[] = [
  59. 'apply_id' => $applyId,
  60. 'goods_id' => $k,
  61. 'nums' => $v['nums'],
  62. 'price' => $data['type'] == 1?$v['price']:$info['price']
  63. ];
  64. }
  65. $ret = Db::name('mate_apply_goods')->insertAll($arr);
  66. if($ret != count($arr)){
  67. \exception('操作失败');
  68. }
  69. Db::commit();
  70. }catch (Exception $e){
  71. Db::rollback();
  72. $this->error = $e->getMessage();
  73. return false;
  74. }
  75. return true;
  76. }
  77. public function saveData($data){
  78. $data['create_time'] = date('Y-m-d H:i:s');
  79. $data['user_id'] = is_login();
  80. $data['sn'] = get_unique_id();
  81. $ret = $this->allowField(true)->save($data);
  82. if(!$ret){
  83. $this->error = '操作失败';
  84. return false;
  85. }
  86. return $this->getLastInsID();
  87. }
  88. public function info($id){
  89. $info = $this
  90. ->where('id',$id)
  91. ->find()
  92. ->toArray();
  93. if(!$info){
  94. return false;
  95. }
  96. $info['userName'] = Db::name('user')
  97. ->where('id',$info['user_id'])
  98. ->value('real_name');
  99. $info['goods'] = Db::name('mate_apply_use_goods')
  100. ->alias('a')
  101. ->join('mate_goods c','c.id=a.goods_id')
  102. ->where('a.apply_id',$info['id'])
  103. ->field('a.*,c.title,c.unit,c.brand,c.spec')
  104. ->select();
  105. return $info;
  106. }
  107. }