ProjectDonationOrder.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. if (isset($where['project_id']) && $where['project_id']>0) $xwhere['project_id'] = $where['project_id'];
  123. $count = $model->where($xwhere)->count();
  124. $list = $model->where($xwhere)->page($where['page'],$where['limit'])->order('id desc')->select();
  125. foreach ($list as &$v)
  126. {
  127. $v['item'] = ProjectDonationOrderInfo::where('order_id',$v['id'])->select();
  128. $v['user'] = ProjectDonationUser::where('id',$v['project_user_id'])->find();
  129. $v['matter'] = explode(",",$v['matter']);
  130. $v['qc'] = explode(",",$v['qc']);
  131. $v['worth'] = explode(",",$v['worth']);
  132. switch ($v['order_status'])
  133. {
  134. case 0:
  135. $v['status_txt'] = '待审核';
  136. break;
  137. case 1:
  138. $v['status_txt'] = '审核通过';
  139. break;
  140. case 2:
  141. $v['status_txt'] = '已发货';
  142. break;
  143. case 3:
  144. $v['status_txt'] = '捐赠品入库';
  145. break;
  146. case 4:
  147. $v['status_txt'] = '捐赠品出库';
  148. break;
  149. case 5:
  150. $v['status_txt'] = '发受捐者';
  151. break;
  152. case 6:
  153. $v['status_txt'] = '受捐者反馈';
  154. break;
  155. case -1:
  156. $v['status_txt'] = '拒绝';
  157. break;
  158. }
  159. }
  160. return compact('count','list');
  161. }
  162. /**
  163. * 生成订单唯一id
  164. * @param $uid 用户uid
  165. * @return string
  166. */
  167. public static function getNewOrderId()
  168. {
  169. list($msec, $sec) = explode(' ', microtime());
  170. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  171. $orderId = 'wx' . $msectime . mt_rand(10000, 99999);
  172. if (self::where(['order_sn' => $orderId])->find()) $orderId = 'wx' . $msectime . mt_rand(10000, 99999);
  173. return $orderId;
  174. }
  175. /**
  176. * 创建订单
  177. * @param $where
  178. */
  179. public static function create_order($where)
  180. {
  181. self::beginTrans();
  182. try {
  183. $donation = [];
  184. foreach ($where['item'] as $v)
  185. {
  186. $donation[] = $v['project_donation_id'];
  187. }
  188. if($where['project_id']>0 &&sizeof($where['item'])!=ProjectDonationInfo::where('pro_id',$where['project_id'])->where('project_donation_id','in',join(",",$donation))->count())
  189. {
  190. return self::setErrorInfo('物资参数不对');
  191. }
  192. $where['order_sn'] = self::getNewOrderId();
  193. $where['add_time'] = time();
  194. $where1 = $where;
  195. unset($where1['item']);
  196. unset($where1['item']);
  197. unset($where1['item']);
  198. $rs = self::create($where1);
  199. if($rs)
  200. {
  201. $data['project_id'] = $where['project_id'];
  202. $data['project_user_id'] = $where['project_user_id'];
  203. $data['order_id'] = $rs['id'];
  204. $data['uid'] = $rs['uid'];
  205. $data['cid'] = $rs['cid'];
  206. $num = 0;
  207. foreach ($where['item'] as $v)
  208. {
  209. $data['project_donation_id'] = $v['project_donation_id'];
  210. $data['project_donation_num'] = $v['project_donation_num'];
  211. $num += $v['project_donation_num'];
  212. $data['add_time'] = time();
  213. ProjectDonationOrderInfo::create($data);
  214. }
  215. self::where('id',$rs['id'])->update(['total_num'=>$num]);
  216. ProjectDonationRocord::bill($rs['cid'],'caeate_order',0,$where['project_id'],$rs['id'],$where['name']);
  217. self::commitTrans();
  218. return $rs;
  219. }
  220. self::rollbackTrans();
  221. return self::setErrorInfo('订单创建失败');
  222. }catch (Exception $e)
  223. {
  224. return self::setErrorInfo('生成订单时系统错误错误原因:' .$e->getFile().$e->getLine().$e->getMessage(),true);
  225. }
  226. }
  227. public static function info($id)
  228. {
  229. $info = self::find($id);
  230. $info['item'] = ProjectDonationOrderInfo::where('order_id',$id)->select();
  231. return $info;
  232. }
  233. }