ProjectDonationOrder.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace app\common\model\project;
  3. use app\common\model\project\ProjectDonationInfo;
  4. use app\common\model\project\ProjectDonationOrderInfo;
  5. use app\common\model\project\ProjectDonationRocord;
  6. use app\common\model\project\ProjectDonationUser;
  7. use liuniu\BaseModel;
  8. use think\Exception;
  9. class ProjectDonationOrder extends BaseModel
  10. {
  11. // 表名
  12. protected $name = 'project_donation_order';
  13. // 自动写入时间戳字段
  14. protected $autoWriteTimestamp = false;
  15. // 定义时间戳字段名
  16. protected $createTime = false;
  17. protected $updateTime = false;
  18. protected $deleteTime = false;
  19. // 追加属性
  20. protected $append = [
  21. 'order_status_text',
  22. 'add_time_text',
  23. 'delivery_time_text',
  24. 'verify_time_text',
  25. 'receiving_time_text',
  26. 'feedback_time_text',
  27. 'out_delivery_time_text'
  28. ];
  29. public function getOrderStatusList()
  30. {
  31. return ['0' => __('Order_status 0'), '1' => __('Order_status 1'), '-1' => __('Order_status -1'), '2' => __('Order_status 2'), '3' => __('Order_status 3')];
  32. }
  33. public function getOrderStatusTextAttr($value, $data)
  34. {
  35. $value = $value ? $value : (isset($data['order_status']) ? $data['order_status'] : '');
  36. $list = $this->getOrderStatusList();
  37. return isset($list[$value]) ? $list[$value] : '';
  38. }
  39. public function getAddTimeTextAttr($value, $data)
  40. {
  41. $value = $value ? $value : (isset($data['add_time']) ? $data['add_time'] : '');
  42. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  43. }
  44. public function getDeliveryTimeTextAttr($value, $data)
  45. {
  46. $value = $value ? $value : (isset($data['delivery_time']) ? $data['delivery_time'] : '');
  47. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  48. }
  49. public function getVerifyTimeTextAttr($value, $data)
  50. {
  51. $value = $value ? $value : (isset($data['verify_time']) ? $data['verify_time'] : '');
  52. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  53. }
  54. public function getReceivingTimeTextAttr($value, $data)
  55. {
  56. $value = $value ? $value : (isset($data['receiving_time']) ? $data['receiving_time'] : '');
  57. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  58. }
  59. public function getFeedbackTimeTextAttr($value, $data)
  60. {
  61. $value = $value ? $value : (isset($data['feedback_time']) ? $data['feedback_time'] : '');
  62. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  63. }
  64. public function getOutDeliveryTimeTextAttr($value, $data)
  65. {
  66. $value = $value ? $value : (isset($data['out_delivery_time']) ? $data['out_delivery_time'] : '');
  67. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  68. }
  69. protected function setAddTimeAttr($value)
  70. {
  71. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  72. }
  73. protected function setDeliveryTimeAttr($value)
  74. {
  75. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  76. }
  77. protected function setVerifyTimeAttr($value)
  78. {
  79. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  80. }
  81. protected function setReceivingTimeAttr($value)
  82. {
  83. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  84. }
  85. protected function setFeedbackTimeAttr($value)
  86. {
  87. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  88. }
  89. protected function setOutDeliveryTimeAttr($value)
  90. {
  91. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  92. }
  93. /**
  94. * 获取订单信息
  95. * @param $where
  96. * @return array
  97. * @throws \think\db\exception\DataNotFoundException
  98. * @throws \think\db\exception\DbException
  99. * @throws \think\db\exception\ModelNotFoundException
  100. */
  101. public static function lst($where)
  102. {
  103. $model = new self;
  104. $xwhere = null;
  105. if($where['all'] == 0)
  106. {
  107. if (isset($where['status']) && $where['status']>-2) $xwhere['order_status'] = $where['status'];
  108. }
  109. else
  110. {
  111. if($where['status']==1)
  112. {
  113. $xwhere['order_status'] = ['>',0];
  114. }
  115. elseif($where['status']==-1)
  116. {
  117. $xwhere['order_status'] = -1;
  118. }
  119. }
  120. if (isset($where['uid']) && $where['uid']>0) $xwhere['uid'] = $where['uid'];
  121. if (isset($where['cid']) && $where['cid']>0) $xwhere['cid'] = $where['cid'];
  122. $count = $model->where($xwhere)->count();
  123. $list = $model->where($xwhere)->page($where['page'],$where['limit'])->order('id desc')->select();
  124. foreach ($list as &$v)
  125. {
  126. $v['item'] = ProjectDonationOrderInfo::where('order_id',$v['id'])->select();
  127. $v['user'] = ProjectDonationUser::where('id',$v['project_user_id'])->find();
  128. $v['matter'] = explode(",",$v['matter']);
  129. $v['qc'] = explode(",",$v['qc']);
  130. $v['worth'] = explode(",",$v['worth']);
  131. switch ($v['order_status'])
  132. {
  133. case 0:
  134. $v['status_txt'] = '待审核';
  135. break;
  136. case 1:
  137. $v['status_txt'] = '审核通过';
  138. break;
  139. case 2:
  140. $v['status_txt'] = '已发货';
  141. break;
  142. case 3:
  143. $v['status_txt'] = '捐赠品入库';
  144. break;
  145. case 4:
  146. $v['status_txt'] = '捐赠品出库';
  147. break;
  148. case 5:
  149. $v['status_txt'] = '发受捐者';
  150. break;
  151. case 6:
  152. $v['status_txt'] = '受捐者反馈';
  153. break;
  154. case -1:
  155. $v['status_txt'] = '拒绝';
  156. break;
  157. }
  158. }
  159. return compact('count','list');
  160. }
  161. /**
  162. * 生成订单唯一id
  163. * @param $uid 用户uid
  164. * @return string
  165. */
  166. public static function getNewOrderId()
  167. {
  168. list($msec, $sec) = explode(' ', microtime());
  169. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  170. $orderId = 'wx' . $msectime . mt_rand(10000, 99999);
  171. if (self::where(['order_sn' => $orderId])->find()) $orderId = 'wx' . $msectime . mt_rand(10000, 99999);
  172. return $orderId;
  173. }
  174. /**
  175. * 创建订单
  176. * @param $where
  177. */
  178. public static function create_order($where)
  179. {
  180. self::beginTrans();
  181. try {
  182. $donation = [];
  183. foreach ($where['item'] as $v)
  184. {
  185. $donation[] = $v['project_donation_id'];
  186. }
  187. if($where['project_id']>0 &&sizeof($where['item'])!=ProjectDonationInfo::where('pro_id',$where['project_id'])->where('project_donation_id','in',join(",",$donation))->count())
  188. {
  189. return self::setErrorInfo('物资参数不对');
  190. }
  191. $where['order_sn'] = self::getNewOrderId();
  192. $where['add_time'] = time();
  193. $where1 = $where;
  194. unset($where1['item']);
  195. unset($where1['item']);
  196. unset($where1['item']);
  197. $rs = self::create($where1);
  198. if($rs)
  199. {
  200. $data['project_id'] = $where['project_id'];
  201. $data['project_user_id'] = $where['project_user_id'];
  202. $data['order_id'] = $rs['id'];
  203. $data['uid'] = $rs['uid'];
  204. $data['cid'] = $rs['cid'];
  205. $num = 0;
  206. foreach ($where['item'] as $v)
  207. {
  208. $data['project_donation_id'] = $v['project_donation_id'];
  209. $data['project_donation_num'] = $v['project_donation_num'];
  210. $num += $v['project_donation_num'];
  211. $data['add_time'] = time();
  212. ProjectDonationOrderInfo::create($data);
  213. }
  214. self::where('id',$rs['id'])->update(['total_num'=>$num]);
  215. ProjectDonationRocord::bill('caeate_order',0,$where['project_id'],$rs['id'],$where['name']);
  216. self::commitTrans();
  217. return $rs;
  218. }
  219. self::rollbackTrans();
  220. return self::setErrorInfo('订单创建失败');
  221. }catch (Exception $e)
  222. {
  223. return self::setErrorInfo('生成订单时系统错误错误原因:' .$e->getFile().$e->getLine().$e->getMessage(),true);
  224. }
  225. }
  226. public static function info($id)
  227. {
  228. $info = self::find($id);
  229. $info['item'] = ProjectDonationOrderInfo::where('order_id',$id)->select();
  230. return $info;
  231. }
  232. }