ConstructionService.php 15 KB

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