FyySqlServerService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace App\Service;
  3. use App\Model\SaleOrdersProduct;
  4. use Illuminate\Support\Facades\Config;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Redis;
  7. class FyySqlServerService extends Service
  8. {
  9. public $db = null;
  10. public $error = null;
  11. public function __construct()
  12. {
  13. $fyy_array = Redis::get('fyy_sql_server');
  14. if(empty($fyy_array)) {
  15. $this->error = 'SQLSERVER设置不存在!';
  16. return;
  17. }
  18. $fyy_array = json_decode($fyy_array,true);
  19. if(! $this->db){
  20. $config = [
  21. 'driver' => 'sqlsrv',
  22. 'host' => $fyy_array['sqlserver_host'] ?? '',
  23. 'port' => $fyy_array['sqlserver_port'] ?? '',
  24. 'database' => $fyy_array['sqlserver_database'] ?? '',
  25. 'username' => env('SQLSRV_USERNAME'),
  26. 'password' => env('SQLSRV_PASSWORD'),
  27. ];
  28. // 进行数据库连接
  29. Config::set('database.connections.sqlsrvs', $config);
  30. try {
  31. $pdo = DB::connection('sqlsrvs')->getPdo();
  32. if ($pdo instanceof \PDO) {
  33. // 连接成功的逻辑代码
  34. $this->db = DB::connection('sqlsrvs');
  35. } else {
  36. $this->error = '连接失败!';
  37. return;
  38. }
  39. } catch (\Throwable $e) {
  40. $this->error = $e->getMessage();
  41. }
  42. }
  43. }
  44. private function fakeData(){
  45. $return = [
  46. [
  47. 'out_order_no' => '001',
  48. 'out_order_no_time' => 1685928995,
  49. 'customer_no' => '201603012',
  50. 'customer_name' => '巴大胡',
  51. 'table_header_mark' => '表头备注',
  52. 'product_no' => '产品编码001',
  53. 'product_title' => '产品名称001',
  54. 'product_size' => '产品规格30c',
  55. 'product_unit' => '产品单位米',
  56. 'order_quantity' => 100,
  57. 'product_quantity_on_hand' => 50,
  58. 'technology_material' => '工艺材质',
  59. 'technology_name' => '工艺名称:同命',
  60. 'wood_name' => '木皮名称:梭梭树皮',
  61. 'process_mark' => '加工备注',
  62. 'table_body_mark' => '表体备注',
  63. 'out_crt_man' => '外部人员一',
  64. 'out_checker_man' => '外部人员二',
  65. 'out_checker_time' => 1685928995,
  66. ],
  67. [
  68. 'out_order_no' => '001',
  69. 'out_order_no_time' => 1685928995,
  70. 'customer_no' => '201603012',
  71. 'customer_name' => '巴大胡',
  72. 'table_header_mark' => '表头备注',
  73. 'product_no' => '产品编码002',
  74. 'product_title' => '产品名称002',
  75. 'product_size' => '产品规格30d',
  76. 'product_unit' => '产品单位米',
  77. 'order_quantity' => 100,
  78. 'product_quantity_on_hand' => 50,
  79. 'technology_material' => '工艺材质',
  80. 'technology_name' => '工艺名称:同命',
  81. 'wood_name' => '木皮名称:梭梭树皮',
  82. 'process_mark' => '加工备注',
  83. 'table_body_mark' => '表体备注',
  84. 'out_crt_man' => '外部人员一',
  85. 'out_checker_man' => '外部人员二',
  86. 'out_checker_time' => 1685928995,
  87. ],
  88. [
  89. 'out_order_no' => '002',
  90. 'out_order_no_time' => 1685928995,
  91. 'customer_no' => '201603012',
  92. 'customer_name' => '巴大胡',
  93. 'table_header_mark' => '表头备注',
  94. 'product_no' => '产品编码002',
  95. 'product_title' => '产品名称002',
  96. 'product_size' => '产品规格30d',
  97. 'product_unit' => '产品单位米',
  98. 'order_quantity' => 100,
  99. 'product_quantity_on_hand' => 50,
  100. 'technology_material' => '工艺材质',
  101. 'technology_name' => '工艺名称:同命',
  102. 'wood_name' => '木皮名称:梭梭树皮',
  103. 'process_mark' => '加工备注',
  104. 'table_body_mark' => '表体备注',
  105. 'out_crt_man' => '外部人员一',
  106. 'out_checker_man' => '外部人员二',
  107. 'out_checker_time' => 1685928995,
  108. ],
  109. ];
  110. return $return;
  111. }
  112. public function is_same_month($timestamp1,$timestamp2){
  113. date_default_timezone_set("PRC");
  114. // 格式化时间戳为年份和月份
  115. $year1 = date('Y', $timestamp1);
  116. $month1 = date('m', $timestamp1);
  117. $year2 = date('Y', $timestamp2);
  118. $month2 = date('m', $timestamp2);
  119. if ($year1 === $year2 && $month1 === $month2) {
  120. return true;
  121. } else {
  122. return false;
  123. }
  124. }
  125. //获取数据(点击引入)
  126. public function getDataFromSqlServer($data){
  127. if(! empty($this->error)) return [false,$this->error,''];
  128. if(empty($data['out_order_no_time'][0]) || empty($data['out_order_no_time'][1])) return [false,'制单日期不能为空!',''];
  129. $bool = $this->is_same_month($data['out_order_no_time'][0],$data['out_order_no_time'][1]);
  130. if(! $bool) return [false,'制单日期必须同月!',''];
  131. //查询产品主表副表数据
  132. date_default_timezone_set("PRC");
  133. $start = date('Y-m-d H:i:s.000',$data['out_order_no_time'][0]);
  134. $end = date('Y-m-d H:i:s.000',$data['out_order_no_time'][1]);
  135. $model = $this->db->table('SO_SOMain as a')
  136. ->leftJoin('SO_SODetails as b','b.cSOCode','a.cSOCode')
  137. ->whereBetween('a.dDate',[$start, $end])
  138. ->whereNotNull('a.cVerifier')
  139. ->select('a.cSOCode as out_order_no','a.dDate as out_order_no_time','a.cCusCode as customer_no','a.cCusName as customer_name','a.cMemo as table_header_mark','a.cMaker as out_crt_man','a.cVerifier as out_checker_man','a.dverifydate as out_checker_time','b.cInvCode as product_no','b.iQuantity as order_quantity','b.cDefine28 as technology_material','b.cFree1 as technology_name','b.cFree2 as wood_name','b.cDefine30 as process_mark','b.cMemo as table_body_mark');
  140. if(! empty($data['out_order_no'])) $model->where('a.cSOCode', 'LIKE', '%'.$data['out_order_no'].'%');
  141. if(! empty($data['customer_no'])) $model->where('a.cCusCode', 'LIKE', '%'.$data['customer_no'].'%');
  142. if(! empty($data['customer_name'])) $model->where('a.cCusName', 'LIKE', '%'.$data['customer_name'].'%');
  143. if(! empty($data['table_header_mark'])) $model->where('a.cMemo', 'LIKE', '%'.$data['table_header_mark'].'%');
  144. if(! empty($data['out_crt_man'])) $model->where('a.cMaker', 'LIKE', '%'.$data['out_crt_man'].'%');
  145. if(! empty($data['out_checker_man'])) $model->where('a.cVerifier', 'LIKE', '%'.$data['out_checker_man'].'%');
  146. if(! empty($data['out_checker_time'][0]) && ! empty($data['out_checker_time'][1])) {
  147. $start1 = date('Y-m-d H:i:s.000',$data['out_checker_time'][0]);
  148. $end1 = date('Y-m-d H:i:s.000',$data['out_checker_time'][1]);
  149. $model->whereBetween('a.dverifydate',[$start1, $end1]);
  150. }
  151. $result = $model->get()->toArray();
  152. if(empty($result)) return [false,'暂无数据,更新结束!',''];
  153. //查询附带的一些信息(比较少)
  154. $product_no = array_column($result,'product_no');
  155. $messageOne = $this->db->table('Inventory as a')
  156. ->join('ComputationUnit as b',function($join){
  157. $join->on('a.cGroupCode','=','b.cGroupCode')
  158. ->on('a.cComUnitCode','=','b.cComUnitCode');
  159. }, null,null,'left')
  160. ->whereIn('a.cInvCode',$product_no)
  161. ->select('a.cInvCode as product_no','a.cInvName as product_title','a.cInvStd as product_size','b.cComUnitName as product_unit')
  162. ->get()->toArray();
  163. $messageMap = array_column($messageOne,null,'product_no');
  164. //现存量查询开始 ---组织查询条件
  165. $args = '';
  166. foreach ($result as $value){
  167. $product = $value->product_no;
  168. $technology_name = $value->technology_name ?? '';
  169. $wood_name = $value->wood_name ?? '';
  170. $args .= "(a.cInvCode = '{$product}' and a.cFree1 = '{$technology_name}' and a.cFree2 = '{$wood_name}') OR ";
  171. }
  172. $args = rtrim($args,'OR ');
  173. $messageTwo = $this->db->table('CurrentStock as a')
  174. ->leftJoin('Warehouse as b','b.cWhCode','a.cWhCode')
  175. ->whereRaw("($args)")
  176. ->where('a.iQuantity','>',0)
  177. ->select('a.iQuantity as product_quantity_on_hand','a.cInvCode as product_no','a.cFree1 as technology_name','a.cFree2 as wood_name','b.cWhName as warehouse_name')
  178. ->get()->toArray();
  179. if(! empty($messageTwo)){
  180. foreach ($messageTwo as $key => $value){
  181. $messageTwo[$key] = (array)$value;
  182. }
  183. }
  184. //现存量查询结束
  185. foreach ($result as $key => $value){
  186. $keys = $value->product_no . $value->technology_name . $value->wood_name;
  187. $result[$key]->out_order_no_time = $value->out_order_no_time ? strtotime($value->out_order_no_time) : 0;
  188. $result[$key]->out_checker_time = $value->out_checker_time ? strtotime($value->out_checker_time) : 0;
  189. $result[$key]->product_title = $messageMap[$value->product_no]->product_title ?? '';
  190. $result[$key]->product_size = $messageMap[$value->product_no]->product_size ?? '';
  191. $result[$key]->product_unit = $messageMap[$value->product_no]->product_unit ?? '';
  192. $result[$key] = (array)$value;
  193. }
  194. return [true,$result,$messageTwo];
  195. }
  196. //获取数据(刷新现存量)
  197. public function getDataFromSqlServerForOnHand($data){
  198. if(! empty($this->error)) return [false,$this->error,''];
  199. if(empty($data['id'])) return [false,'数据不能为空!',''];
  200. $product = SaleOrdersProduct::whereIn('id',$data['id'])
  201. ->select('product_no','technology_name','wood_name')
  202. ->get()->toArray();
  203. //现存量查询开始 ---组织查询条件
  204. $args = '';
  205. foreach ($product as $value){
  206. $args .= "(a.cInvCode = '{$value['product_no']}' and a.cFree1 = '{$value['technology_name']}' and a.cFree2 = '{$value['wood_name']}') OR ";
  207. }
  208. $args = rtrim($args,'OR ');
  209. $message = $this->db->table('CurrentStock as a')
  210. ->leftJoin('Warehouse as b','b.cWhCode','a.cWhCode')
  211. ->whereRaw("($args)")
  212. ->where('a.iQuantity','>',0)
  213. ->select('a.iQuantity as product_quantity_on_hand','a.cInvCode as product_no','a.cFree1 as technology_name','a.cFree2 as wood_name','b.cWhName as warehouse_name')
  214. ->get()->toArray();
  215. if(! empty($message)){
  216. foreach ($message as $key => $value){
  217. $message[$key] = (array)$value;
  218. }
  219. }
  220. //现存量查询结束
  221. return [true,$message,$product];
  222. }
  223. }