GluingSalary.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Request;
  6. /**
  7. * 糊盒工资查询
  8. */
  9. class GluingSalary extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 左侧菜单
  15. * @return void
  16. * @throws \think\db\exception\DataNotFoundException
  17. * @throws \think\db\exception\ModelNotFoundException
  18. * @throws \think\exception\DbException
  19. */
  20. public function getTab()
  21. {
  22. if(!$this->request->isGet()){
  23. $this->error('请求方式错误');
  24. }
  25. $res=db('糊盒工资汇总')
  26. ->field('DATE_FORMAT(`sczl_rq`, "%Y%m") as sys_ny,bh')
  27. ->group('DATE_FORMAT(`sczl_rq`, "%Y%m"),bh')
  28. ->whereTime('sczl_rq', '>=', strtotime('-14 months'))
  29. ->where('bh','<>','000000')
  30. ->order('sczl_rq desc')
  31. ->select();
  32. foreach($res as $v){
  33. $arr[$v['sys_ny'].'('][]=$v['bh'];
  34. }
  35. $rs = db('人事_基本资料')->column('员工编号,所在部门');
  36. foreach($arr as $k=>$v){
  37. foreach($v as $value){
  38. if(array_key_exists($value,$rs)){
  39. $data[$k.count($v).'人)'][rtrim($rs[$value])][]=rtrim($rs[$value]);
  40. } }
  41. $j=0;
  42. foreach($data[$k.count($v).'人)'] as $keys=>$values){
  43. $data[$k.count($v).'人)']['bm'][$j]=$keys;
  44. $data[$k.count($v).'人)'][$keys]=count($values);
  45. $j++;
  46. }
  47. usort($data[$k.count($v).'人)']['bm'], function($a, $b) {
  48. $order = array(
  49. '胶印车间',
  50. '凹印车间',
  51. '丝印车间',
  52. '模切车间',
  53. '检验车间',
  54. '精品试验车间',
  55. '品保部',
  56. '人力资源部',
  57. '生产部',
  58. '营销部',
  59. '数字化车间',
  60. '精品车间'
  61. );
  62. $a_index = array_search($a, $order);
  63. $b_index = array_search($b, $order);
  64. return $a_index - $b_index;
  65. });
  66. foreach($data[$k.count($v).'人)']['bm'] as &$va){
  67. $va=$va.'('.$data[$k.count($v).'人)'][$va].'人)';
  68. }
  69. $data[$k.count($v).'人)']=$data[$k.count($v).'人)']['bm'];
  70. }
  71. $i=0;
  72. foreach($data as $k=>$v){
  73. $datas[$i]['label']=$k;
  74. $gdbh=substr($k,0,strpos($k,'('));
  75. foreach($v as $key=>$value){
  76. $datas[$i]['children'][$key]['label']=$value;
  77. $datas[$i]['children'][$key]['gdbh']=$gdbh;
  78. }
  79. $i++;
  80. }
  81. $this->success('成功',$datas);
  82. }
  83. /**
  84. * 上方列表
  85. * @return void
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. * @throws \think\exception\DbException
  89. */
  90. public function getList()
  91. {
  92. // 1. 请求验证
  93. if (!$this->request->isGet()) {
  94. $this->error('请求方式错误');
  95. }
  96. $req = $this->request->param();
  97. // 2. 获取查询条件
  98. $where = $this->buildWhereConditions($req);
  99. // 3. 执行查询
  100. $res = $this->executeQuery($req, $where);
  101. if (empty($res)) {
  102. $this->success('成功', []);
  103. }
  104. // 4. 处理数据
  105. $finalData = $this->processResultData($res);
  106. $this->success('成功', $finalData);
  107. }
  108. /**
  109. * 构建查询条件
  110. */
  111. private function buildWhereConditions(array $req): array
  112. {
  113. $where = [];
  114. // 确保日期条件始终存在
  115. if (!empty($req['date'])) {
  116. $where['sczl_rq'] = ['like', $req['date'] . '%'];
  117. }
  118. // 如果有搜索条件,添加员工编号/姓名的模糊查询
  119. if (!empty($req['search'])) {
  120. $where['bh|name'] = ['like', $req['search'] . '%'];
  121. }
  122. return $where;
  123. }
  124. /**
  125. * 执行数据库查询
  126. */
  127. private function executeQuery(array $req, array $where)
  128. {
  129. $query = db('糊盒工资汇总');
  130. // 判断是否需要联表查询
  131. if (empty($req['search']) && !empty($req['department'])) {
  132. return $this->executeJoinedQuery($query, $req, $where);
  133. }
  134. return $this->executeSimpleQuery($query, $where);
  135. }
  136. /**
  137. * 执行简单查询(不需要联表)
  138. */
  139. private function executeSimpleQuery($query, array $where)
  140. {
  141. return $query
  142. ->where($where)
  143. ->group('bh, sczl_rq')
  144. ->field([
  145. 'bh as 员工编号',
  146. 'DATE_FORMAT(sczl_rq, "%Y.%m.%d") as 日期',
  147. 'name as 姓名',
  148. 'sum(salary) as 计件工资'
  149. ])
  150. ->select();
  151. }
  152. /**
  153. * 执行联表查询(需要部门筛选)
  154. */
  155. private function executeJoinedQuery($query, array $req, array $where)
  156. {
  157. // 移除原始的sczl_rq条件,改用别名
  158. if (isset($where['sczl_rq'])) {
  159. unset($where['sczl_rq']);
  160. $where['a.sczl_rq'] = ['like', $req['date'] . '%'];
  161. }
  162. // 添加部门条件
  163. $where['b.所在部门'] = ['like', $req['department'] . '%'];
  164. return $query
  165. ->alias('a')
  166. ->join('人事_基本资料 b', 'b.员工编号 = a.bh')
  167. ->where($where)
  168. ->group('a.bh, a.sczl_rq')
  169. ->field([
  170. 'a.bh as 员工编号',
  171. 'DATE_FORMAT(a.sczl_rq, "%Y.%m.%d") as 日期',
  172. 'a.name as 姓名',
  173. 'sum(a.salary) as 计件工资'
  174. ])
  175. ->select();
  176. }
  177. /**
  178. * 处理查询结果数据
  179. */
  180. private function processResultData(array $results): array
  181. {
  182. $processedData = [];
  183. foreach ($results as $item) {
  184. $empNo = $item['员工编号'];
  185. $salary = (float)$item['计件工资'];
  186. if (!isset($processedData[$empNo])) {
  187. $processedData[$empNo] = $this->initEmployeeData($item);
  188. }
  189. $processedData[$empNo] = $this->updateEmployeeData(
  190. $processedData[$empNo],
  191. $item,
  192. $salary
  193. );
  194. }
  195. // 对每个员工的数据进行排序并格式化最终数据
  196. $processedData = $this->formatFinalData($processedData);
  197. return array_values($processedData);
  198. }
  199. /**
  200. * 初始化员工数据结构
  201. */
  202. private function initEmployeeData(array $item): array
  203. {
  204. return [
  205. '员工编号' => $item['员工编号'],
  206. '姓名' => $item['姓名'],
  207. '月工资总和' => 0.00,
  208. '每日明细' => []
  209. ];
  210. }
  211. /**
  212. * 更新员工数据
  213. */
  214. private function updateEmployeeData(array $empData, array $item, float $salary): array
  215. {
  216. // 累加工资总和
  217. $empData['月工资总和'] += $salary;
  218. // 添加每日明细
  219. $empData['每日明细'][] = [
  220. '日期' => $item['日期'],
  221. '计件工资' => $item['计件工资']
  222. ];
  223. return $empData;
  224. }
  225. /**
  226. * 对每日明细按日期排序并格式化最终数据
  227. */
  228. private function formatFinalData(array $processedData): array
  229. {
  230. foreach ($processedData as &$empData) {
  231. // 1. 对每日明细排序
  232. $empData['每日明细'] = $this->sortDailyDetails($empData['每日明细']);
  233. // 2. 强制格式化月工资总和为两位小数
  234. $empData['月工资总和'] = number_format($empData['月工资总和'], 2, '.', '');
  235. }
  236. unset($empData);
  237. return $processedData;
  238. }
  239. /**
  240. * 对每日明细按日期排序
  241. */
  242. private function sortDailyDetails(array $dailyDetails): array
  243. {
  244. usort($dailyDetails, function($a, $b) {
  245. // 使用更简单的日期转换方式
  246. $timeA = strtotime(str_replace('.', '-', $a['日期']));
  247. $timeB = strtotime(str_replace('.', '-', $b['日期']));
  248. return $timeA <=> $timeB;
  249. });
  250. return $dailyDetails;
  251. }
  252. /**
  253. *下方详情
  254. * @return void
  255. * @throws \think\db\exception\DataNotFoundException
  256. * @throws \think\db\exception\ModelNotFoundException
  257. * @throws \think\exception\DbException
  258. */
  259. public function getDetail()
  260. {
  261. if($this->request->isGet() === false){
  262. $this->error('请求错误');
  263. }
  264. $req = $this->request->param();
  265. if (!isset($req['date'])){
  266. $this->error('参数错误');
  267. }
  268. $list = \db('糊盒工资汇总')
  269. ->field('sczl_gdbh,sczl_gxmc,DATE_FORMAT(sczl_rq, "%Y.%m.%d") as 日期,sczl_jtbh,cpdh,cpmc,保养工时,装版工时,
  270. 异常工时,设备运行工时,rate as 分配比例,sczl_cl,price')
  271. ->where([
  272. 'sczl_rq' => ['like',$req['date'].'%'],
  273. 'bh' => $req['code']
  274. ])
  275. ->order('sczl_rq')
  276. ->select();
  277. if(empty($list)){
  278. $this->error('失败');
  279. }else{
  280. $this->success('成功',$list);
  281. }
  282. }
  283. }