OrganizationService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Employee;
  4. use App\Model\Organization;
  5. use App\Model\OrganizationDetails;
  6. use Illuminate\Support\Facades\DB;
  7. class OrganizationService extends Service
  8. {
  9. public function organizationEdit($data,$user){
  10. list($status,$msg) = $this->organizationRule($data, $user, false);
  11. if(!$status) return [$status,$msg];
  12. try {
  13. DB::beginTransaction();
  14. $model = Organization::where('id',$data['id'])->first();
  15. $model->code = $data['code'] ?? '';
  16. $model->title = $data['title'] ?? '';
  17. $model->industry_name = $data['industry_name'] ?? "";
  18. $model->industry_ranking = $data['industry_ranking'] ?? "";
  19. $model->address = $data['address'] ?? "";
  20. $model->founding_time = $data['founding_time'] ?? 0;
  21. $model->type = $data['type'] ?? 0;
  22. $model->decision_depart = $data['decision_depart'] ?? "";
  23. $model->decision_maker = $data['decision_maker'] ?? "";
  24. $model->new_product_rule = $data['new_product_rule'] ?? "";
  25. $model->old_product_rule = $data['old_product_rule'] ?? "";
  26. $model->save();
  27. $time = time();
  28. OrganizationDetails::where('del_time',0)
  29. ->where('organization_id', $model->id)
  30. ->update(['del_time' => $time]);
  31. $this->saveDetail($model->id, $time, $data);
  32. DB::commit();
  33. }catch (\Exception $exception){
  34. DB::rollBack();
  35. return [false,$exception->getMessage()];
  36. }
  37. return [true, ''];
  38. }
  39. public function organizationAdd($data,$user){
  40. list($status,$msg) = $this->organizationRule($data, $user);
  41. if(!$status) return [$status,$msg];
  42. try {
  43. DB::beginTransaction();
  44. $model = new Organization();
  45. $model->code = $data['code'] ?? '';
  46. $model->title = $data['title'] ?? '';
  47. $model->industry_name = $data['industry_name'] ?? "";
  48. $model->industry_ranking = $data['industry_ranking'] ?? "";
  49. $model->address = $data['address'] ?? "";
  50. $model->founding_time = $data['founding_time'] ?? 0;
  51. $model->type = $data['type'] ?? 0;
  52. $model->decision_depart = $data['decision_depart'] ?? "";
  53. $model->decision_maker = $data['decision_maker'] ?? "";
  54. $model->new_product_rule = $data['new_product_rule'] ?? "";
  55. $model->old_product_rule = $data['old_product_rule'] ?? "";
  56. $model->crt_id = $user['id'];
  57. $model->save();
  58. $this->saveDetail($model->id, time(), $data);
  59. DB::commit();
  60. }catch (\Exception $exception){
  61. DB::rollBack();
  62. return [false,$exception->getMessage()];
  63. }
  64. return [true, ''];
  65. }
  66. private function saveDetail($id, $time, $data){
  67. if(! empty($data['unit_list'])){
  68. $unit = [];
  69. foreach ($data['unit_list'] as $value){
  70. $unit[] = [
  71. 'organization_id' => $id,
  72. 'type' => $value['type'],
  73. 'unit' => $value['unit'],
  74. 'crt_time' => $time,
  75. ];
  76. }
  77. if(! empty($unit)) OrganizationDetails::insert($unit);
  78. }
  79. if(! empty($data['receipt_list'])){
  80. $receipt = [];
  81. foreach ($data['receipt_list'] as $value){
  82. $start_time = strtotime($value['start_time'] . "-01");
  83. $end_time = strtotime($value['end_time'] . "-01");
  84. if(empty($value['receipt'])) continue;
  85. $receipt[] = [
  86. 'organization_id' => $id,
  87. 'type' => $value['type'],
  88. 'start_time' => $start_time,
  89. 'end_time' => $end_time,
  90. 'receipt' => $value['receipt'],
  91. 'crt_time' => $time,
  92. ];
  93. }
  94. if(! empty($receipt)) OrganizationDetails::insert($receipt);
  95. }
  96. if(! empty($data['requirement_list'])){
  97. $requirement = [];
  98. foreach ($data['requirement_list'] as $value){
  99. $requirement[] = [
  100. 'organization_id' => $id,
  101. 'type' => $value['type'],
  102. 'requirement' => $value['requirement'],
  103. 'month_usage' => $value['month_usage'],
  104. 'reference_value' => $value['reference_value'],
  105. 'crt_time' => $time,
  106. ];
  107. }
  108. if(! empty($requirement)) OrganizationDetails::insert($requirement);
  109. }
  110. }
  111. private function getDetail($id){
  112. $data = OrganizationDetails::where('del_time',0)
  113. ->where('organization_id', $id)
  114. ->get()->toArray();
  115. $unit = $receipt = $requirement = [];
  116. foreach ($data as $value){
  117. if(in_array($value['type'], [OrganizationDetails::type_one, OrganizationDetails::type_two])) {
  118. $unit[] = [
  119. 'type' => $value['type'],
  120. 'unit' => $value['unit'],
  121. ];
  122. }elseif(in_array($value['type'], [OrganizationDetails::type_three])){
  123. $receipt[] = [
  124. 'type' => $value['type'],
  125. 'start_time' => date("Y-m", $value['start_time']),
  126. 'end_time' => date("Y-m", $value['end_time']),
  127. 'receipt' => $value['receipt'],
  128. ];
  129. }else{
  130. $requirement[] = [
  131. 'type' => $value['type'],
  132. 'requirement' => $value['requirement'],
  133. 'month_usage' => $value['month_usage'],
  134. 'reference_value' => $value['reference_value'],
  135. ];
  136. }
  137. }
  138. $detail = [
  139. 'unit_list' => $unit,
  140. 'receipt_list' => $receipt,
  141. 'requirement_list' => $requirement,
  142. ];
  143. foreach ($detail as $key => $value) {
  144. if (empty($value)) {
  145. $detail[$key] = (object)[]; // 转成 stdClass 对象
  146. }
  147. }
  148. return $detail;
  149. }
  150. public function organizationDel($data){
  151. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  152. try {
  153. DB::beginTransaction();
  154. $time = time();
  155. Organization::where('del_time',0)
  156. ->whereIn('id',$data['id'])
  157. ->update(['del_time' => $time]);
  158. OrganizationDetails::where('del_time',0)
  159. ->where('organization_id', $data['id'])
  160. ->update(['del_time' => $time]);
  161. DB::commit();
  162. }catch (\Exception $exception){
  163. DB::rollBack();
  164. return [false,$exception->getMessage()];
  165. }
  166. return [true, ''];
  167. }
  168. public function organizationDetail($data, $user){
  169. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  170. $customer = Organization::where('del_time',0)
  171. ->where('id',$data['id'])
  172. ->first();
  173. if(empty($customer)) return [false,'组织不存在或已被删除'];
  174. $customer = $customer->toArray();
  175. $customer['founding_time'] = ! empty($customer['founding_time']) ? date("Y-m-d", $customer['founding_time']) : "";
  176. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  177. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  178. $details = $this->getDetail($data['id']);
  179. $customer = array_merge($customer, $details);
  180. return [true, $customer];
  181. }
  182. public function organizationCommon($data,$user, $field = []){
  183. if(empty($field)) $field = Organization::$field;
  184. $model = Organization::Clear($user,$data);
  185. $model = $model->where('del_time',0)
  186. ->select($field)
  187. ->orderby('id', 'desc');
  188. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  189. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  190. if(! empty($data['type'])) $model->where('type', $data['type']);
  191. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  192. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  193. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  194. $model->where('crt_time','>=',$return[0]);
  195. $model->where('crt_time','<=',$return[1]);
  196. }
  197. return $model;
  198. }
  199. public function organizationList($data,$user){
  200. $model = $this->organizationCommon($data, $user);
  201. $list = $this->limit($model,'',$data);
  202. $list = $this->fillData($list,$user,$data);
  203. return [true, $list];
  204. }
  205. public function organizationRule(&$data, $user, $is_add = true){
  206. if(empty($data['code'])) return [false, '编码不能为空'];
  207. if(empty($data['title'])) return [false, '名称不能为空'];
  208. if(empty($data['type']) || ! isset(Organization::$type_name[$data['type']])) return [false, '组织类型错误'];
  209. if(! empty($data['founding_time'])) $data['founding_time'] = $this->changeDateToDate($data['founding_time']);
  210. if(! empty($data['unit_list'])){
  211. foreach ($data['unit_list'] as $value){
  212. if(empty($value['type'])) return [false, '上下级单位类型不能为空'];
  213. if(empty($value['unit'])) return [false, '上下级单位名称不能为空'];
  214. }
  215. }
  216. if(! empty($data['receipt_list'])){
  217. foreach ($data['receipt_list'] as $value){
  218. if(empty($value['type'])) return [false, '营收信息类型不能为空'];
  219. if(empty($value['start_time']) || empty($value['end_time'])) return [false, '营收日期范围不能为空'];
  220. $start_time = strtotime($value['start_time'] . "-01");
  221. $end_time = strtotime($value['end_time'] . "-01");
  222. if(! $start_time || ! $end_time) return [false, '营收日期范围错误'];
  223. if(empty($value['receipt'])) return [false, '营收数据不能为空'];
  224. }
  225. }
  226. if(! empty($data['requirement_list'])){
  227. foreach ($data['requirement_list'] as $value){
  228. if(empty($value['type'])) return [false, '需求信息类型不能为空'];
  229. if(empty($value['requirement'])) return [false, '需求信息名不能为空'];
  230. if(empty($value['month_usage'])) return [false, '月用量不能为空'];
  231. if(empty($value['reference_value'])) return [false, '参考价不能为空'];
  232. }
  233. }
  234. if($is_add){
  235. $bool = Organization::where('code',$data['code'])
  236. ->where('crt_id', $user['id'])
  237. ->where('del_time',0)
  238. ->exists();
  239. }else{
  240. if(empty($data['id'])) return [false,'ID不能为空'];
  241. $bool = Organization::where('code',$data['code'])
  242. ->where('crt_id', $user['id'])
  243. ->where('id','<>',$data['id'])
  244. ->where('del_time',0)
  245. ->exists();
  246. }
  247. if($bool) return [false, '编码已存在'];
  248. return [true, $data];
  249. }
  250. public function fillData($data, $user, $search){
  251. if(empty($data['data'])) return $data;
  252. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'id')));
  253. foreach ($data['data'] as $key => $value){
  254. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  255. $data['data'][$key]['founding_time'] = $value['founding_time'] ? date('Y-m-d',$value['founding_time']) : '';
  256. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  257. $data['data'][$key]['type_title'] = Organization::$type_name[$value['type']] ?? '';
  258. }
  259. return $data;
  260. }
  261. }