WaterQuery.php 5.9 KB

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