ConstructionService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Construction;
  5. use App\Model\ConstructionInfo;
  6. use App\Model\ConstructionProductInfo;
  7. use App\Model\Customer;
  8. use App\Model\Employee;
  9. use Illuminate\Support\Facades\DB;
  10. class ConstructionService extends Service
  11. {
  12. public function constructionEdit($data,$user){
  13. list($status,$msg) = $this->constructionRule($data, $user, false);
  14. if(!$status) return [$status,$msg];
  15. try {
  16. DB::beginTransaction();
  17. $model = Construction::where('id', $data['id'])->first();
  18. $model->model_type = $data['model_type'];
  19. $model->order_number = $data['order_number'];
  20. $model->title = $data['title'] ?? '';
  21. $model->customer_id = $data['customer_id'] ?? 0;
  22. $model->customer_contact_id = $data['customer_contact_id'] ?? 0;
  23. $model->install_method = $data['install_method'] ?? 0;
  24. $model->install_position = $data['install_position'] ?? '';
  25. $model->sales_order_id = $data['sales_order_id'] ?? 0;
  26. $model->construction_fee = $data['construction_fee'] ?? 0;
  27. $model->construction_time = $data['construction_time'] ?? 0;
  28. $model->handover_time = $data['handover_time'] ?? 0;
  29. $model->urgency = $data['urgency'] ?? 0;
  30. $model->mark = $data['mark'] ?? '';
  31. $model->address1 = $data['address1'] ?? '';
  32. $model->address2 = $data['address2'] ?? '';
  33. $model->introduction = $data['introduction'] ?? '';
  34. $model->depart_id = $data['depart_id'] ?? 0;
  35. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  36. $model->save();
  37. $time = time();
  38. ConstructionInfo::where('del_time',0)
  39. ->where('construction_id',$data['id'])
  40. ->update(['del_time' => $time]);
  41. ConstructionProductInfo::where('del_time',0)
  42. ->where('construction_id',$data['id'])
  43. ->update(['del_time' => $time]);
  44. if(! empty($data['construction_contact'])){
  45. $insert = [];
  46. foreach ($data['construction_contact'] as $value){
  47. $insert[] = [
  48. 'construction_id' => $model->id,
  49. 'contact_type' => $value['id'],
  50. 'contact_info' => $value['info'],
  51. 'type' => ConstructionInfo::type_one,
  52. 'crt_time' => $time,
  53. ];
  54. }
  55. ConstructionInfo::insert($insert);
  56. }
  57. if(! empty($data['employee_one'])){
  58. $insert = [];
  59. foreach ($data['employee_one'] as $value){
  60. $insert[] = [
  61. 'construction_id' => $model->id,
  62. 'employee_id' => $value,
  63. 'type' => ConstructionInfo::type_two,
  64. 'crt_time' => $time,
  65. ];
  66. }
  67. ConstructionInfo::insert($insert);
  68. }
  69. if(! empty($data['product'])){
  70. $insert = [];
  71. foreach ($data['product'] as $value){
  72. $insert[] = [
  73. 'construction_id' => $model->id,
  74. 'product_id' => $value['product_id'],
  75. 'price' => $value['price'],
  76. 'number' => $value['number'],
  77. 'mark' => $value['mark'] ?? '',
  78. 'crt_time' => $time,
  79. ];
  80. }
  81. ConstructionProductInfo::insert($insert);
  82. }
  83. DB::commit();
  84. }catch (\Exception $exception){
  85. DB::rollBack();
  86. return [false,$exception->getMessage()];
  87. }
  88. return [true,''];
  89. }
  90. public function constructionAdd($data,$user){
  91. list($status,$msg) = $this->constructionRule($data,$user);
  92. if(!$status) return [$status,$msg];
  93. try {
  94. DB::beginTransaction();
  95. $model = new Construction();
  96. $model->model_type = $data['model_type'];
  97. $model->order_number = $data['order_number'];
  98. $model->title = $data['title'] ?? '';
  99. $model->customer_id = $data['customer_id'] ?? 0;
  100. $model->customer_contact_id = $data['customer_contact_id'] ?? 0;
  101. $model->install_method = $data['install_method'] ?? 0;
  102. $model->install_position = $data['install_position'] ?? '';
  103. $model->sales_order_id = $data['sales_order_id'] ?? 0;
  104. $model->construction_fee = $data['construction_fee'] ?? 0;
  105. $model->construction_time = $data['construction_time'] ?? 0;
  106. $model->handover_time = $data['handover_time'] ?? 0;
  107. $model->urgency = $data['urgency'] ?? 0;
  108. $model->mark = $data['mark'] ?? '';
  109. $model->address1 = $data['address1'] ?? '';
  110. $model->address2 = $data['address2'] ?? '';
  111. $model->introduction = $data['introduction'] ?? '';
  112. $model->crt_id = $user['id'];
  113. $model->depart_id = $data['depart_id'] ?? 0;
  114. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  115. $model->save();
  116. $time = time();
  117. if(! empty($data['construction_contact'])){
  118. $insert = [];
  119. foreach ($data['construction_contact'] as $value){
  120. $insert[] = [
  121. 'construction_id' => $model->id,
  122. 'contact_type' => $value['id'],
  123. 'contact_info' => $value['info'],
  124. 'type' => ConstructionInfo::type_one,
  125. 'crt_time' => $time,
  126. ];
  127. }
  128. ConstructionInfo::insert($insert);
  129. }
  130. if(! empty($data['employee_one'])){
  131. $insert = [];
  132. foreach ($data['employee_one'] as $value){
  133. $insert[] = [
  134. 'construction_id' => $model->id,
  135. 'employee_id' => $value,
  136. 'type' => ConstructionInfo::type_two,
  137. 'crt_time' => $time,
  138. ];
  139. }
  140. ConstructionInfo::insert($insert);
  141. }
  142. if(! empty($data['product'])){
  143. $insert = [];
  144. foreach ($data['product'] as $value){
  145. $insert[] = [
  146. 'construction_id' => $model->id,
  147. 'product_id' => $value['product_id'],
  148. 'price' => $value['price'],
  149. 'number' => $value['number'],
  150. 'mark' => $value['mark']?? '',
  151. 'crt_time' => $time,
  152. ];
  153. }
  154. ConstructionProductInfo::insert($insert);
  155. }
  156. DB::commit();
  157. }catch (\Exception $exception){
  158. DB::rollBack();
  159. return [false,$exception->getMessage()];
  160. }
  161. return [true,''];
  162. }
  163. public function constructionDel($data){
  164. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  165. try {
  166. DB::beginTransaction();
  167. Construction::whereIn('id',$data['id'])->update([
  168. 'del_time'=> time()
  169. ]);
  170. ConstructionInfo::where('del_time',0)
  171. ->where('construction_id',$data['id'])
  172. ->update(['del_time' => time()]);
  173. ConstructionProductInfo::where('del_time',0)
  174. ->where('construction_id',$data['id'])
  175. ->update(['del_time' => time()]);
  176. DB::commit();
  177. }catch (\Exception $exception){
  178. DB::rollBack();
  179. return [false,$exception->getMessage()];
  180. }
  181. return [true,''];
  182. }
  183. public function constructionDetail($data){
  184. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  185. $construction = Construction::where('del_time',0)
  186. ->where('id',$data['id'])
  187. ->first();
  188. if(empty($construction)) return [false,'施工订单不存在或已被删除'];
  189. $construction = $construction->toArray();
  190. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  191. $construction['customer_title'] = $customer_title;
  192. $emp_title = Employee::where('id',$construction['customer_contact_id'])->value('emp_name');
  193. $construction['customer_contact_title'] = $emp_title;
  194. $construction['employee_one'] = $construction['construction_contact'] = $construction['product'] = [];
  195. $array = [
  196. $construction['install_method'],
  197. $construction['urgency'],
  198. ];
  199. $basic_map = BasicType::whereIn('id',$array)
  200. ->pluck('title','id')
  201. ->toArray();
  202. $construction = [$construction];
  203. foreach ($construction as $key => $value){
  204. $construction[$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  205. $construction[$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  206. }
  207. $construction = $construction[0];
  208. $construction_info = ConstructionInfo::where('del_time',0)
  209. ->where('construction_id',$construction['id'])
  210. ->select('id','construction_id','employee_id','type','contact_type','contact_info')
  211. ->get()->toArray();
  212. $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
  213. ->pluck('emp_name','id')
  214. ->toArray();
  215. $basic_map2 = BasicType::whereIn('id',array_unique(array_column($construction_info,'contact_type')))
  216. ->pluck('title','id')
  217. ->toArray();
  218. foreach ($construction_info as $value){
  219. if($value['type'] == ConstructionInfo::type_one){
  220. $tmp = [
  221. 'id' => $value['contact_type'],
  222. 'title' => $basic_map2[$value['contact_type']] ?? '',
  223. 'info' => $value['contact_info']
  224. ];
  225. $construction['customer_contact'][] = $tmp;
  226. }elseif ($value['type'] == ConstructionInfo::type_two){
  227. $tmp = [
  228. 'id' => $value['employee_id'],
  229. 'name' => $emp_map[$value['employee_id']],
  230. ];
  231. $construction['employee_one'][] = $tmp;
  232. }
  233. }
  234. $p_info = ConstructionProductInfo::where('del_time',0)
  235. ->where('construction_id',$construction['id'])
  236. ->select('id','construction_id','product_id','mark','price','number')
  237. ->get()->toArray();
  238. foreach ($p_info as $value){
  239. $construction['product'][] = [
  240. 'product_id' => $value['product_id'],
  241. 'mark' => $value['mark'],
  242. 'price' => $value['price'],
  243. 'number' => $value['number'],
  244. ];
  245. }
  246. $construction['crt_name'] = $emp_map[$construction['crt_id']] ?? '';
  247. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  248. return [true, $construction];
  249. }
  250. public function constructionList($data,$user){
  251. $model = new Construction(['userData' => $user]);
  252. $model = $model->where('del_time',0)
  253. ->select('title','id','model_type','order_number','customer_id','customer_contact_id','install_method','install_position','sales_order_id','construction_fee','construction_time','handover_time','urgency','crt_id','crt_time','mark','state','address1','address2','introduction')
  254. ->orderby('id', 'desc')
  255. ->where('model_type',$data['model_type']);
  256. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  257. $list = $this->limit($model,'',$data);
  258. $list = $this->fillData($list);
  259. return [true, $list];
  260. }
  261. public function constructionRule(&$data, $user, $is_add = true){
  262. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  263. if(! in_array($data['model_type'],Construction::$model_type)) return [false,'工单模板类型错误'];
  264. if(empty($data['order_number'])) return [false,'工单编号不能为空'];
  265. if(empty($data['construction_time'])) return [false,'实施日期不能为空'];
  266. $data['construction_time'] = $this->changeDateToDateMin($data['construction_time']);
  267. if(empty($data['product'])) return [false,'请选择产品'];
  268. foreach ($data['product'] as $value){
  269. if(empty($value['number'])) return [false,'产品数量不能为空'];
  270. $res = $this->checkNumber($value['number']);
  271. if(! $res) return [false,'请输入正确的产品数量'];
  272. if(empty($value['price'])) return [false,'产品价格不能为空'];
  273. $res = $this->checkNumber($value['price']);
  274. if(! $res) return [false,'产品价格请输入不超过两位小数并且大于0的数值'];
  275. }
  276. if(empty($data['construction_contact'])) return [false,'联系方式不能为空'];
  277. if(! empty($data['construction_fee'])){
  278. $res = $this->checkNumber($data['construction_fee']);
  279. if(! $res) return [false,'施工费用请输入不超过两位小数并且大于0的数值'];
  280. }
  281. if($data['model_type'] == Construction::Model_type_one){
  282. if(empty($data['install_method'])) return [false,'安装方式不能为空'];
  283. if(empty($data['install_position'])) return [false,'安装地点不能为空'];
  284. }else{
  285. if(empty($data['address1']) || empty($data['address2'])) return [false,'地址不能为空'];
  286. }
  287. //所属部门 以及 顶级部门
  288. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  289. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  290. if($is_add){
  291. $bool = Construction::where('del_time',0)->where('order_number',$data['order_number'])->exists();
  292. if($bool) return [false,'工单编号已存在,请重新获取'];
  293. }else{
  294. if(empty($data['id'])) return [false,'ID不能为空'];
  295. }
  296. return [true, $data];
  297. }
  298. public function fillData($data){
  299. if(empty($data['data'])) return $data;
  300. $array = array_unique(array_merge_recursive(array_column($data['data'],'install_method'),array_column($data['data'],'urgency')));
  301. $basic_map = BasicType::whereIn('id',$array)
  302. ->pluck('title','id')
  303. ->toArray();
  304. $emp = Employee::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'crt_id'),array_column($data['data'],'customer_contact_id'))))
  305. ->pluck('emp_name','id')
  306. ->toArray();
  307. $customer = Customer::whereIn('id',array_unique(array_column($data['data'],'customer_id')))
  308. ->pluck('title','id')
  309. ->toArray();
  310. foreach ($data['data'] as $key => $value){
  311. $data['data'][$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  312. $data['data'][$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  313. $data['data'][$key]['customer_title'] = $customer[$value['customer_id']] ?? '';
  314. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  315. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  316. $data['data'][$key]['customer_contact_title'] = $emp[$value['customer_contact_id']] ?? '';
  317. }
  318. return $data;
  319. }
  320. public function constructionGet($data){
  321. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  322. if(! isset(Construction::$prefix[$data['model_type']])) return [false,'工单模板类型错误'];
  323. $prefix = Construction::$prefix[$data['model_type']];
  324. $order_number = OrderNoService::createConstructionOrderNumber($prefix);
  325. if(! $order_number) return [false,'工单编号生成失败!'];
  326. return [false,$order_number];
  327. }
  328. }