WaterQuery.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/11
  6. */
  7. namespace app\admin\controller\water;
  8. use app\admin\controller\AuthController;
  9. use app\admin\model\user\User;
  10. use app\admin\model\water\WaterMaterial;
  11. use crmeb\services\{ExpressService,
  12. JsonService,
  13. JsonService as Json,
  14. MiniProgramService,
  15. PHPExcelService,
  16. WechatService,
  17. FormBuilder as Form,
  18. CacheService,
  19. UtilService as Util};
  20. use think\facade\Route as Url;
  21. use think\facade\Validate;
  22. Use app\admin\model\water\WaterQuery as model;
  23. /**
  24. * 订单管理控制器 同一个订单表放在一个控制器
  25. * Class StoreOrder
  26. * @package app\admin\controller\store
  27. */
  28. class WaterQuery extends AuthController
  29. {
  30. /**
  31. * @return mixed
  32. */
  33. public function index()
  34. {
  35. return $this->fetch();
  36. }
  37. public function list()
  38. {
  39. $where = Util::getMore([
  40. ['page', 1],
  41. ['limit', 20],
  42. ['name', ''],
  43. ['card', ''],
  44. ['order_id', ''],
  45. ]);
  46. return Json::successlayui(model::list($where));
  47. }
  48. /**
  49. * 显示创建资源表单页.
  50. *
  51. * @return \think\Response
  52. */
  53. public function create($id = 0)
  54. {
  55. $f = [];
  56. $f[] = Form::number('water_mouth', '水口价格');
  57. $f[] = Form::number('partition', '隔板价格');
  58. $f[] = Form::number('tax_point', '税点(百分比)');
  59. $f[] = Form::textarea('remarks', '备注');
  60. $f[] = Form::hidden('id', $id);
  61. $form = Form::make_post_form('添加', $f, Url::buildUrl('save'));
  62. $this->assign(compact('form'));
  63. return $this->fetch('public/form-builder');
  64. }
  65. public function save()
  66. {
  67. $model = new model;
  68. $data = Util::postMore([
  69. 'water_mouth',
  70. 'partition',
  71. 'tax_point',
  72. 'id',
  73. 'remarks',
  74. ]);
  75. $validate = Validate::rule([
  76. 'water_mouth' => 'require',
  77. 'partition' => 'require',
  78. 'tax_point' => 'require',
  79. ]);
  80. $validate->message([
  81. 'water_mouth.require' => '输入水口价格',
  82. 'partition.require' => '隔板价格',
  83. 'tax_point.require' => '输入税点',
  84. ]);
  85. if (!$validate->check($data)) {
  86. return Json::fail($validate->getError());
  87. }
  88. $query = model::where('id', $data['id'])->find();
  89. if (!$query) return Json::fail('查询订单不存在');
  90. $money = $query['price'] + $data['water_mouth'] + $data['partition'];
  91. $res = \app\admin\model\water\WaterOrder::create([
  92. 'uid' => $query['uid'],
  93. 'query_id' => $query['id'],
  94. 'order_id' => $this->getNewOrderId(),
  95. 'long' => $query['long'],
  96. 'wide' => $query['wide'],
  97. 'high' => $query['high'],
  98. 'is_warm' => $query['is_warm'],
  99. 'is_channel' => $query['is_channel'],
  100. 'is_ladder' => $query['is_ladder'],
  101. 'is_gc' => $query['is_gc'],
  102. 'water_mouth' => $data['water_mouth'],
  103. 'partition' => $data['partition'],
  104. 'tax_point' => $data['tax_point'],
  105. 'tax' => $money * ($data['tax_point']/100),
  106. 'price' => $money,
  107. 'weight' => $query['weight'],
  108. 'remarks' => $data['remarks'],
  109. ]);
  110. if ($res) return Json::successful('转为订单成功');
  111. return Json::fail('添加失败');
  112. }
  113. /**
  114. * 生成订单唯一id
  115. * @param $uid 用户uid
  116. * @return string
  117. */
  118. public function getNewOrderId()
  119. {
  120. do {
  121. list($msec, $sec) = explode(' ', microtime());
  122. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  123. $orderId = 'w' . $msectime . mt_rand(10000, 99999);
  124. } while (\app\admin\model\water\WaterOrder::be(['order_id' => $orderId]));// $orderId = 'wx' . $msectime . mt_rand(10000, 99999);
  125. return $orderId;
  126. }
  127. /**
  128. * 详情
  129. * @param $id
  130. * @return string
  131. * @throws \Exception
  132. */
  133. public function details($id)
  134. {
  135. $model = new WaterMaterial();
  136. $model = $model->where('query_id', $id);
  137. // $model = $model->order('id desc');
  138. $this->assign( WaterMaterial::page($model, null, null, '100'));
  139. return $this->fetch();
  140. }
  141. /**
  142. * 删除
  143. * @param $id
  144. * @return void
  145. * @throws \Exception
  146. */
  147. public function delete($id)
  148. {
  149. if (!$id) Json::fail('删除失败');
  150. $model = new model;
  151. $res = model::destroy($id);
  152. if ($res){
  153. return Json::success('删除成功!');
  154. }else{
  155. return Json::fail($model->getErrorInfo());
  156. }
  157. }
  158. public function excel($id)
  159. {
  160. $details = \app\admin\model\water\WaterQuery::where('id', $id)->find();
  161. $list = WaterMaterial::where('query_id', $id)->select();
  162. $user = User::where('uid', $details['uid'])->find();
  163. $export = [];
  164. foreach ($list as $item)
  165. {
  166. $export[] = [
  167. $item['title'],
  168. $item['name'],
  169. $item['specifications'],
  170. $item['number'],
  171. $item['unit_price'],
  172. $item['ot_price'],
  173. $item['company'],
  174. $item['create_time'],
  175. ];
  176. }
  177. PHPExcelService::setExcelHeader(['标题', '名称', '规格', '数量', '单价', '总价', '单位','时间'])
  178. ->setExcelTile('用户'.$user['phone'].'计算记录导出', '计算信息' . time(), '记录ID'.$details['id'].'-总价'.$details['price'])
  179. ->setExcelContent($export)
  180. ->ExcelSave();
  181. }
  182. }