GluingSalary.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. 'sum(time_salary) as 计时工资',
  150. ])
  151. ->select();
  152. }
  153. /**
  154. * 执行联表查询(需要部门筛选)
  155. */
  156. private function executeJoinedQuery($query, array $req, array $where)
  157. {
  158. // 移除原始的sczl_rq条件,改用别名
  159. if (isset($where['sczl_rq'])) {
  160. unset($where['sczl_rq']);
  161. $where['a.sczl_rq'] = ['like', $req['date'] . '%'];
  162. }
  163. // 添加部门条件
  164. $where['b.所在部门'] = ['like', $req['department'] . '%'];
  165. return $query
  166. ->alias('a')
  167. ->join('人事_基本资料 b', 'b.员工编号 = a.bh')
  168. ->where($where)
  169. ->group('a.bh, a.sczl_rq')
  170. ->field([
  171. 'a.bh as 员工编号',
  172. 'DATE_FORMAT(a.sczl_rq, "%Y.%m.%d") as 日期',
  173. 'a.name as 姓名',
  174. 'sum(a.salary) as 计件工资',
  175. 'sum(a.time_salary) as 计时工资',
  176. ])
  177. ->select();
  178. }
  179. /**
  180. * 处理查询结果数据
  181. */
  182. private function processResultData(array $results): array
  183. {
  184. $processedData = [];
  185. foreach ($results as $item) {
  186. $empNo = $item['员工编号'];
  187. if (!isset($processedData[$empNo])) {
  188. $processedData[$empNo] = $this->initEmployeeData($item);
  189. }
  190. $processedData[$empNo] = $this->updateEmployeeData(
  191. $processedData[$empNo],
  192. $item
  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. '月计件工资' => 0.00,
  209. '月计时工资' => 0.00,
  210. '每日明细' => []
  211. ];
  212. }
  213. /**
  214. * 更新员工数据
  215. */
  216. private function updateEmployeeData(array $empData, array $item): array
  217. {
  218. $pieceSalary = (float)($item['计件工资'] ?? 0);
  219. $timeSalary = (float)($item['计时工资'] ?? 0);
  220. $dailyTotal = $pieceSalary + $timeSalary;
  221. $empData['月工资总和'] += $dailyTotal;
  222. $empData['月计件工资'] += $pieceSalary;
  223. $empData['月计时工资'] += $timeSalary;
  224. $empData['每日明细'][] = [
  225. '日期' => $item['日期'],
  226. '计件工资' => number_format($pieceSalary, 2, '.', ''),
  227. '计时工资' => number_format($timeSalary, 2, '.', ''),
  228. '日工资总和' => number_format($dailyTotal, 2, '.', ''),
  229. ];
  230. return $empData;
  231. }
  232. /**
  233. * 对每日明细按日期排序并格式化最终数据
  234. */
  235. private function formatFinalData(array $processedData): array
  236. {
  237. foreach ($processedData as &$empData) {
  238. $empData['每日明细'] = $this->sortDailyDetails($empData['每日明细']);
  239. $empData['月工资总和'] = number_format($empData['月工资总和'], 2, '.', '');
  240. $empData['月计件工资'] = number_format($empData['月计件工资'], 2, '.', '');
  241. $empData['月计时工资'] = number_format($empData['月计时工资'], 2, '.', '');
  242. }
  243. unset($empData);
  244. return $processedData;
  245. }
  246. /**
  247. * 对每日明细按日期排序
  248. */
  249. private function sortDailyDetails(array $dailyDetails): array
  250. {
  251. usort($dailyDetails, function($a, $b) {
  252. // 使用更简单的日期转换方式
  253. $timeA = strtotime(str_replace('.', '-', $a['日期']));
  254. $timeB = strtotime(str_replace('.', '-', $b['日期']));
  255. return $timeA <=> $timeB;
  256. });
  257. return $dailyDetails;
  258. }
  259. /**
  260. *下方详情
  261. * @return void
  262. * @throws \think\db\exception\DataNotFoundException
  263. * @throws \think\db\exception\ModelNotFoundException
  264. * @throws \think\exception\DbException
  265. */
  266. public function getDetail()
  267. {
  268. if($this->request->isGet() === false){
  269. $this->error('请求错误');
  270. }
  271. $req = $this->request->param();
  272. if (!isset($req['date'])){
  273. $this->error('参数错误');
  274. }
  275. $list = \db('糊盒工资汇总')
  276. ->field('sczl_gdbh,sczl_gxmc,DATE_FORMAT(sczl_rq, "%Y.%m.%d") as 日期,sczl_jtbh,cpdh,cpmc,保养工时,装版工时,
  277. 异常工时,设备运行工时,rate as 分配比例,sczl_cl,price as 计件单价,salary as 计件工资,
  278. time_salary as 计时工资,time_duration as 计时时长,time_price as 计时单价,remark as 计时备注')
  279. ->where([
  280. 'sczl_rq' => ['like', $req['date'] . '%'],
  281. 'bh' => $req['code']
  282. ])
  283. ->order('sczl_rq')
  284. ->select();
  285. if (empty($list)) {
  286. $this->error('失败');
  287. }
  288. $this->success('成功', $list);
  289. }
  290. }