WaterQuery.php 5.8 KB

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