PieceWorkSchedule.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use \think\Request;
  5. use \think\Db;
  6. /**
  7. * 计件工计时单维护接口
  8. */
  9. class PieceWorkSchedule extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 首页
  15. *
  16. */
  17. public function index()
  18. {
  19. $this->success('请求成功');
  20. }
  21. /**
  22. * 获取计件工计时单侧边栏
  23. * @ApiMethod (GET)
  24. */
  25. public function getTab()
  26. {
  27. //get请求
  28. if(!$this->request->isGet()){
  29. $this->error('请求方式错误');
  30. }
  31. $rows = db()->table('db_wgjs')
  32. ->field('LEFT(wgjs_rq, 7) as date')
  33. ->group('date')
  34. ->order('UniqId desc')
  35. ->limit(15)
  36. ->select();
  37. foreach($rows as $key=>$value){
  38. $rows[$key]['date'] = str_replace('-', '', $rows[$key]['date']);
  39. }
  40. $this->success('成功',$rows);
  41. }
  42. /**
  43. * 获取计件工计时单列表
  44. * @ApiMethod (GET)
  45. * @param string $date 时间
  46. * @param string $Sczl_bh1 员工编号
  47. */
  48. public function getList()
  49. {
  50. // 验证请求方式
  51. if (!$this->request->isGet()) {
  52. $this->error('请求方式错误');
  53. }
  54. $req = $this->request->param();
  55. // 必需参数校验
  56. if (empty($req['date'])) {
  57. $this->error('参数错误: 缺少日期参数');
  58. }
  59. $where['wgjs_rq'] = ['LIKE', $req['date'] . '%'];
  60. $usePagination = isset($req['page'], $req['limit']) && $req['page'] > 0 && $req['limit'] > 0;
  61. // 构建基础查询
  62. $query = db()->table('db_wgjs')
  63. ->field('LEFT(wgjs_rq, 10) as wgjs_rq, UniqId,
  64. wgjs_bh1, wgjs_js1, rtrim(wgjs_yy1) as wgjs_yy1,
  65. wgjs_bh2, wgjs_js2, rtrim(wgjs_yy2) as wgjs_yy2,
  66. wgjs_bh3, wgjs_js3, rtrim(wgjs_yy3) as wgjs_yy3,
  67. wgjs_bh4, wgjs_js4, rtrim(wgjs_yy4) as wgjs_yy4,
  68. wgjs_bh5, wgjs_js5, rtrim(wgjs_yy5) as wgjs_yy5,
  69. wgjs_bh6, wgjs_js6, rtrim(wgjs_yy6) as wgjs_yy6')
  70. ->where($where)
  71. ->order('wgjs_rq desc, UniqId asc');
  72. // 执行分页/全量查询
  73. if ($usePagination) {
  74. $rows = $query->page($req['page'], $req['limit'])->select();
  75. $total = db()->table('db_wgjs')->where($where)->count();
  76. } else {
  77. $rows = $query->select();
  78. $total = count($rows); // 避免额外COUNT查询
  79. }
  80. // 获取员工基础数据
  81. $employees = db()->table('人事_基本资料')->column('员工编号, 员工姓名, 所在部门');
  82. // 数据处理优化
  83. foreach ($rows as &$row) {
  84. // 处理主员工信息
  85. $mainId = $row['wgjs_bh1'];
  86. $hasMain = isset($employees[$mainId]);
  87. $row['wgjs_js1'] = $row['wgjs_js1'] == 0 ? '' : $row['wgjs_js1'];
  88. $row['department'] = $hasMain ? trim($employees[$mainId]['所在部门']) : '';
  89. $row['name1'] = $hasMain ? trim($employees[$mainId]['员工姓名']) : '';
  90. // 处理2-6号员工信息
  91. for ($i = 2; $i <= 6; $i++) {
  92. $fieldId = $row["wgjs_bh$i"];
  93. $row["wgjs_js$i"] = $row["wgjs_js$i"] == '0.0' ? '' : $row["wgjs_js$i"];
  94. $row["name$i"] = ($fieldId && isset($employees[$fieldId]))
  95. ? trim($employees[$fieldId]['员工姓名'])
  96. : ($fieldId === $mainId ? $row['name1'] : '');
  97. }
  98. }
  99. $this->success('成功', [
  100. 'total' => $total,
  101. 'rows' => $rows
  102. ]);
  103. }
  104. /**
  105. * 获取计件工计时单信息
  106. * @ApiMethod (GET)
  107. * @param string $UniqId UniqId
  108. */
  109. public function getInfo()
  110. {
  111. //get请求
  112. if(!$this->request->isGet()){
  113. $this->error('请求方式错误');
  114. }
  115. $req = $this->request->param();
  116. if (isset($req['UniqId']) && !empty($req['UniqId'])){
  117. $UniqId = $req['UniqId'];
  118. }else{
  119. $this->error('参数错误');
  120. }
  121. $rows = db()->table('db_wgjs')->alias('d')
  122. ->field('d.*, ')
  123. ->join('工单_基本资料 g', 'd.')
  124. ->where('d.UniqId',$UniqId)
  125. ->select();
  126. $this->success('成功',$rows);
  127. }
  128. /**
  129. * 定位
  130. * @ApiMethod GET
  131. */
  132. public function search(){
  133. if (Request::instance()->isGet() == false){
  134. $this->error('非法请求');
  135. }
  136. $req = Request::instance()->param();
  137. if (!isset($req['search']) || !isset($req['date'])){
  138. $this->error('参数错误');
  139. }
  140. $page = 1;
  141. $limit = 10;
  142. if (isset($req['page']) && !empty($req['page'])) $page = $req['page'];
  143. if (isset($req['limit']) && !empty($req['limit'])) $limit = $req['limit'];
  144. $year=substr($req['date'],0,4);
  145. $month=substr($req['date'],-2);
  146. $start_time = $year . '-' . $month . '-01 00:00:00';
  147. $end_time = date('Y-m-t', strtotime("$year-$month-01")) . ' 23:59:59';
  148. $yg = db()->table('人事_基本资料')->where('员工姓名',$req['search'])->value('员工编号');
  149. if($yg){
  150. $req['search']=$yg;
  151. }
  152. $where = [
  153. 'db_wgjs.wgjs_rq'=>['between',"$start_time,$end_time"],
  154. 'db_wgjs.wgjs_bh1|db_wgjs.wgjs_bh2|db_wgjs.wgjs_bh3|db_wgjs.wgjs_bh4|db_wgjs.wgjs_bh5|db_wgjs.wgjs_bh6'=>['like', '%'.$req['search'].'%']
  155. ];
  156. $rows = db()->table('db_wgjs')
  157. ->field('LEFT(db_wgjs.wgjs_rq, 10) as wgjs_rq,
  158. db_wgjs.wgjs_bh1,trim(rs1.所在部门) as 所在部门, db_wgjs.wgjs_js1, rtrim(db_wgjs.wgjs_yy1) as wgjs_yy1,
  159. db_wgjs.wgjs_bh2, db_wgjs.wgjs_js2, rtrim(db_wgjs.wgjs_yy2) as wgjs_yy2,
  160. db_wgjs.wgjs_bh3, db_wgjs.wgjs_js3, rtrim(db_wgjs.wgjs_yy3) as wgjs_yy3,
  161. db_wgjs.wgjs_bh4, db_wgjs.wgjs_js4, rtrim(db_wgjs.wgjs_yy4) as wgjs_yy4,
  162. db_wgjs.wgjs_bh5, db_wgjs.wgjs_js5, rtrim(db_wgjs.wgjs_yy5) as wgjs_yy5,
  163. db_wgjs.wgjs_bh6, db_wgjs.wgjs_js6, rtrim(db_wgjs.wgjs_yy6) as wgjs_yy6,
  164. rtrim(rs1.员工姓名) as name1,rtrim(rs2.员工姓名) as name2,rtrim(rs3.员工姓名) as name3,
  165. rtrim(rs4.员工姓名) as name4,rtrim(rs5.员工姓名) as name5,rtrim(rs6.员工姓名) as name6,
  166. db_wgjs.UniqId')
  167. ->join('人事_基本资料 rs1','rs1.员工编号=db_wgjs.wgjs_bh1','LEFT')
  168. ->join('人事_基本资料 rs2','rs2.员工编号=db_wgjs.wgjs_bh2','LEFT')
  169. ->join('人事_基本资料 rs3','rs3.员工编号=db_wgjs.wgjs_bh3','LEFT')
  170. ->join('人事_基本资料 rs4','rs4.员工编号=db_wgjs.wgjs_bh4','LEFT')
  171. ->join('人事_基本资料 rs5','rs5.员工编号=db_wgjs.wgjs_bh5','LEFT')
  172. ->join('人事_基本资料 rs6','rs6.员工编号=db_wgjs.wgjs_bh6','LEFT')
  173. ->where($where)
  174. ->order('wgjs_rq desc, UniqId asc')
  175. ->page($page,$limit)
  176. ->select();
  177. $total = db()->table('db_wgjs')->where($where)->count();
  178. $data = ['total'=> $total,'rows'=> $rows];
  179. if($rows){
  180. $this->success('成功',$data);
  181. }else{
  182. $this->error('失败');
  183. }
  184. }
  185. /**
  186. * 详情
  187. * @ApiMethod (GET)
  188. * @param string $wgjs_rq 日期
  189. * @param string $wgjs_bh1 员工编号
  190. */
  191. public function detail(){
  192. //get请求
  193. if(!$this->request->isGet()){
  194. $this->error('请求方式错误');
  195. }
  196. $req = $this->request->param();
  197. if (!(isset($req['UniqId']) && trim($req['UniqId'])!='')){
  198. $this->error('参数错误','UniqId',100);
  199. }
  200. $rows = db('db_wgjs')
  201. ->field('LEFT(wgjs_rq, 10) as wgjs_rq, db_wgjs.UniqId,
  202. wgjs_bh1, wgjs_js1, rtrim(wgjs_yy1) as wgjs_yy1,
  203. wgjs_bh2, wgjs_js2, rtrim(wgjs_yy2) as wgjs_yy2,
  204. wgjs_bh3, wgjs_js3, rtrim(wgjs_yy3) as wgjs_yy3,
  205. wgjs_bh4, wgjs_js4, rtrim(wgjs_yy4) as wgjs_yy4,
  206. wgjs_bh5, wgjs_js5, rtrim(wgjs_yy5) as wgjs_yy5,
  207. wgjs_bh6, wgjs_js6, rtrim(wgjs_yy6) as wgjs_yy6,
  208. rtrim(rs1.员工姓名) as name1,rtrim(rs2.员工姓名) as name2,rtrim(rs3.员工姓名)
  209. as name3,rtrim(rs4.员工姓名) as name4,rtrim(rs5.员工姓名) as name5,rtrim(rs6.员工姓名)
  210. as name6,wgjs_冲定额1,wgjs_冲定额2,wgjs_冲定额3,wgjs_冲定额4,wgjs_冲定额5,wgjs_冲定额6')
  211. ->join('人事_基本资料 rs1','rs1.员工编号=db_wgjs.wgjs_bh1','LEFT')
  212. ->join('人事_基本资料 rs2','rs2.员工编号=db_wgjs.wgjs_bh2','LEFT')
  213. ->join('人事_基本资料 rs3','rs3.员工编号=db_wgjs.wgjs_bh3','LEFT')
  214. ->join('人事_基本资料 rs4','rs4.员工编号=db_wgjs.wgjs_bh4','LEFT')
  215. ->join('人事_基本资料 rs5','rs5.员工编号=db_wgjs.wgjs_bh5','LEFT')
  216. ->join('人事_基本资料 rs6','rs6.员工编号=db_wgjs.wgjs_bh6','LEFT')
  217. ->where('db_wgjs.UniqId', $req['UniqId'])
  218. ->find();
  219. if($rows!==false){
  220. $this->success('成功',$rows);
  221. }else{
  222. $this->error('失败');
  223. }
  224. }
  225. /**
  226. * 修改
  227. * @ApiMethod POST
  228. */
  229. public function edit()
  230. {
  231. if(!$this->request->isPost()){
  232. $this->error('请求方式错误');
  233. }
  234. $req = $this->request->param();
  235. if (!(isset($req['UniqId']) && trim($req['UniqId'])!='')){
  236. $this->error('参数错误','UniqId',100);
  237. }
  238. $arr = [
  239. 'wgjs_rq',
  240. 'wgjs_bh1', 'wgjs_bh2', 'wgjs_bh3', 'wgjs_bh4', 'wgjs_bh5', 'wgjs_bh6',
  241. 'wgjs_js1', 'wgjs_js2', 'wgjs_js3', 'wgjs_js4', 'wgjs_js5', 'wgjs_js6',
  242. 'wgjs_yy1', 'wgjs_yy2', 'wgjs_yy3', 'wgjs_yy4', 'wgjs_yy5', 'wgjs_yy6',
  243. 'wgjs_冲定额1', 'wgjs_冲定额2', 'wgjs_冲定额3', 'wgjs_冲定额4', 'wgjs_冲定额5', 'wgjs_冲定额6'
  244. ];
  245. $data = [];
  246. foreach ($arr as $key => $value){
  247. if (!isset($req[$value])){
  248. continue;
  249. }
  250. $data[$value] = $req[$value];
  251. }
  252. if (count($data)==0){
  253. $this->error('参数错误','',111);
  254. }
  255. $data['wgjs_rq'] = $req['wgjs_rq'].' 00:00:00';
  256. $data['mod_rq'] = date('Y-m-d H:i:s');
  257. //开启事务
  258. db()->startTrans();
  259. try{
  260. $sql = db('db_wgjs')->where('UniqId',$req['UniqId'])->fetchSql(true)->update($data);
  261. $bool = db()->query($sql);
  262. // 提交事务
  263. db()->commit();
  264. } catch (\Exception $e) {
  265. // 回滚事务
  266. db()->rollback();
  267. $this->error($e->getMessage());
  268. }
  269. if($bool===false) $this->error('失败');
  270. $this->success('成功');
  271. }
  272. /**
  273. * 新增
  274. * @ApiMethod POST
  275. */
  276. public function add()
  277. {
  278. if(!$this->request->isPost()){
  279. $this->error('请求方式错误');
  280. }
  281. $req = $this->request->param();
  282. if (!isset($req['wgjs_rq']) || !isset($req['wgjs_bh1']) || !isset($req['sys_id']) ){
  283. $this->error('参数错误');
  284. }
  285. if (empty($req['wgjs_rq']) || empty($req['wgjs_bh1']) || empty($req['sys_id'])){
  286. $this->error('参数不能为空');
  287. }
  288. $UniqId = db()->table('db_wgjs')->order('UniqId desc')->value('UniqId');
  289. if ($UniqId < 1000000){
  290. $UniqId = 1000000;
  291. }else{
  292. $UniqId = $UniqId + 1;
  293. }
  294. $req['wgjs_rq'] = $req['wgjs_rq'].' 00:00:00';
  295. $req['sys_rq'] = date('Y-m-d H:i:s');
  296. $req['UniqId'] = $UniqId;
  297. $req['sys_id'] = $req['sys_id'];
  298. //开启事务
  299. db()->startTrans();
  300. try{
  301. $sql = db()->table('db_wgjs')->fetchSql(true)->insert($req);
  302. $res= db()->query($sql);
  303. // 提交事务
  304. db()->commit();
  305. } catch (\Exception $e) {
  306. // 回滚事务
  307. db()->rollback();
  308. $this->error($e->getMessage());
  309. }
  310. if($res===false) $this->error('失败');
  311. $this->success('成功');
  312. }
  313. /**
  314. * 删除
  315. * @ApiMethod (GET)
  316. * @param string $wgjs_rq 日期
  317. * @param string $wgjs_bh1 员工编号
  318. */
  319. public function del(){
  320. //get请求
  321. if(!$this->request->isGet()){
  322. $this->error('请求方式错误');
  323. }
  324. $req = $this->request->param();
  325. if (!(isset($req['UniqId']) && trim($req['UniqId'])!='')){
  326. $this->error('参数错误','UniqId',100);
  327. }
  328. //开启事务
  329. db()->startTrans();
  330. try{
  331. $bool = db('db_wgjs')->where('UniqId',$req['UniqId'])->delete();
  332. // 提交事务
  333. db()->commit();
  334. } catch (\Exception $e) {
  335. // 回滚事务
  336. db()->rollback();
  337. $this->error($e->getMessage());
  338. }
  339. if($bool===false) $this->error('失败');
  340. $this->success('成功');
  341. }
  342. }