OrganizationService.php 12 KB

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