MShopRostering.Class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /**
  3. * 商铺管理模块
  4. * Created by PhpStorm.
  5. * User: wxj
  6. * Date: 2019/10/31
  7. * Time: 15:02
  8. */
  9. namespace JinDouYun\Model\Shop;
  10. use AlibabaCloud\SDK\Dingtalk\Vcrm_1_0\Models\DescribeRelationMetaResponseBody\relationMetaDTOList\items\props\fields\relateProps;
  11. use Exception;
  12. use JinDouYun\Controller\Common\Logger;
  13. use JinDouYun\Dao\Department\DStaff;
  14. use JinDouYun\Dao\GoodsManage\DGoodsBasic;
  15. use JinDouYun\Dao\Shop\DShopProject;
  16. use JinDouYun\Dao\Shop\DShopRostering;
  17. use JinDouYun\Dao\Shop\DShopTemplate;
  18. use JinDouYun\Dao\UserCenter\DUserCenter;
  19. use JinDouYun\Model\Common\Location;
  20. use Mall\Framework\Core\ErrorCode;
  21. use Mall\Framework\Core\StatusCode;
  22. use Mall\Framework\Core\ResultWrapper;
  23. use JinDouYun\Dao\Shop\DShop;
  24. use JinDouYun\Dao\Shop\DEnterpriseBindShop;
  25. use Jindouyun\Cache\EnterpriseCache;
  26. use JinDouYun\Cache\RoleAclCache;
  27. use JinDouYun\Cache\ShopCache;
  28. use JinDouYun\Cache\SysAreaChinaCache;
  29. use JinDouYun\Cache\GoodsBasicRelevant;
  30. use JinDouYun\Model\ShopPartner\MShopPartner;
  31. use JinDouYun\Model\Stock\MWarehouse;
  32. use JinDouYun\Model\SysAreaChina\MSysAreaChina;
  33. use JinDouYun\Model\Customer\MCustomer;
  34. use JinDouYun\Model\Enterprise\MEnterprise;
  35. use JinDouYun\Model\Department\MStaff;
  36. use JinDouYun\Model\MBaseModel;
  37. use Monolog\Handler\IFTTTHandler;
  38. class MShopRostering extends MBaseModel
  39. {
  40. private $obj;
  41. private $cutTable = 250000;
  42. private $enterpriseId;
  43. private $userCenterId;
  44. private $objDShopTemplate;
  45. private $objStaff;
  46. public function __construct($enterpriseId, $userCenterId = '')
  47. {
  48. $this->enterpriseId = $enterpriseId;
  49. $this->userCenterId = $userCenterId;
  50. parent::__construct($this->enterpriseId, $this->userCenterId);
  51. $this->obj = new DShopRostering('default');
  52. $this->objDShopTemplate = new DShopTemplate('default');
  53. $this->objStaff = new DStaff('default');
  54. $this->obj->setTable('qianniao_shop_rostering_'.$enterpriseId);
  55. $this->objStaff->setTable('qianniao_staff_'.$enterpriseId);
  56. }
  57. /**
  58. * 列表
  59. * @param $where
  60. * @return mixed
  61. */
  62. public function list($where)
  63. {
  64. $limit = $where['limit'];
  65. unset($where['limit']);
  66. $offset = $where['offset'];
  67. unset($where['offset']);
  68. $file = ' a.*,b.staffName';
  69. $sql = 'SELECT'.$file.' FROM qianniao_shop_rostering'.$this->enterpriseId.' AS a
  70. LEFT JOIN qianniao_staff_'.$this->enterpriseId.' AS b ON a.uid = b.userCenterId
  71. WHERE a.shop_id = '. $where['shop_id'];
  72. if(isset($where['nickname'])) {
  73. $sql .= ' AND b.staffName LIKE "%' . $where['nickname'] . '%"';
  74. }
  75. if(isset($where['uid'])) {
  76. $sql .= ' AND a.uid = '.$where['uid'];
  77. }
  78. if(isset($where['time'])) {
  79. $time_field = '';
  80. foreach ($where['time'] as $item)
  81. {
  82. $time_field .= '"'.$item.'",';
  83. }
  84. $time_field = rtrim($time_field, ',');
  85. $sql .= ' AND a.time in ('.$time_field.')';
  86. }
  87. $total = count((array) $this->obj->query($sql));
  88. $sql .= ' ORDER BY createTime DESC LIMiT '.$offset.','.$limit;
  89. $dbResult = $this->obj->query($sql);
  90. if ($dbResult === false) {
  91. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  92. }
  93. foreach ($dbResult as &$item)
  94. {
  95. $temsql = 'SELECT * FROM qianniao_shop_template WHERE id IN ('.$item['template_id'].')';
  96. $item['template'] = $this->objDShopTemplate->query($temsql);
  97. }
  98. $return = [
  99. 'data' => $dbResult,
  100. 'total' => ($total) ? intval($total) : 0,
  101. ];
  102. if($return === false){
  103. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  104. }else{
  105. return ResultWrapper::success($return);
  106. }
  107. }
  108. /**
  109. * 详情
  110. * @param $where
  111. * @return mixed
  112. */
  113. public function details($where)
  114. {
  115. if(empty($where)){
  116. return ResultWrapper::success($where);
  117. }
  118. $dbResult = $this->obj->get($where);
  119. if(empty($dbResult)){
  120. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  121. }
  122. $temsql = 'SELECT * FROM qianniao_shop_template WHERE id IN ('.$dbResult['template_id'].')';
  123. $dbResult['template'] = $this->objDShopTemplate->query($temsql);
  124. return ResultWrapper::success(self::formatInfo($dbResult));
  125. }
  126. /**
  127. * Doc: (des="添加")
  128. * User: XMing
  129. * Date: 2020/7/15
  130. * Time: 10:42 上午
  131. * @throws Exception
  132. * @param array $params
  133. * @return ResultWrapper
  134. */
  135. public function insert(array $params)
  136. {
  137. // $times = $params['times'];
  138. // unset($params['times']);
  139. //
  140. //
  141. $this->obj->beginTransaction();
  142. $data = $this->obj->get(['uid' => $params['uid'], 'time' => $params['time']]);
  143. if ($data){
  144. $res = $this->obj->update([
  145. 'name' => $params['name'],
  146. 'template_id' => $params['template_id'],
  147. 'time' => $params['time'],
  148. 'time_slot' => $params['time_slot'],
  149. ], $data['id']);
  150. }else{
  151. $res = $this->obj->insert($params);
  152. }
  153. if ($res){
  154. $this->obj->commit();
  155. return ResultWrapper::success('添加成功');
  156. }else{
  157. $this->obj->rollBack();
  158. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  159. }
  160. }
  161. public function update($params, $id)
  162. {
  163. $data = $this->obj->get($id);
  164. if (!$data){
  165. return ResultWrapper::fail('数据不存在', ErrorCode::$dberror);
  166. }
  167. $dbResult = $this->obj->update($params,$id);
  168. if($dbResult === false){
  169. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  170. }
  171. return ResultWrapper::success('修改成功');
  172. }
  173. public function select($where = null, $fields = '*', $order = null, $limit = null, $offset = null, $data = array(), $multiple = true, $isExport = false)
  174. {
  175. if (isset($where['export'])){
  176. unset($where['export']);
  177. $data = $this->obj->select($where);
  178. foreach ($data as &$item)
  179. {
  180. $staff = $this->objStaff->get(['id' => $item['uid']]);
  181. $item['staffName'] = $staff['staffName'];
  182. $item['avatar'] = $staff['avatar'];
  183. $item['evaluate'] = $staff['evaluate'];
  184. $item['info'] = $staff['info'];
  185. }
  186. $this->Excel($data);
  187. exit;
  188. }
  189. $data = $this->obj->select($where, $fields, $order, $limit, $offset, $data, $multiple, $isExport);
  190. $count = $this->obj->count($where);
  191. if($data === false){
  192. return ResultWrapper::success($data);
  193. }else{
  194. foreach ($data as &$item)
  195. {
  196. $staff = $this->objStaff->get(['id' => $item['uid']]);
  197. $item['staffName'] = $staff['staffName'];
  198. $item['avatar'] = $staff['avatar'];
  199. $item['evaluate'] = $staff['evaluate'];
  200. $item['info'] = $staff['info'];
  201. $item['time_slot'] = json_decode($item['time_slot']);
  202. $item['clock'] = json_decode($item['clock']);
  203. }
  204. $return = [
  205. 'data' => $data,
  206. 'total' => $count,
  207. ];
  208. return ResultWrapper::success($return);
  209. }
  210. }
  211. /**
  212. * 导出
  213. * @param $inventoryOutData
  214. * @return void
  215. *
  216. */
  217. public function Excel($data)
  218. {
  219. //导出到本地
  220. header("Content-type:application/vnd.ms-excel");
  221. header("Content-Disposition:filename=考情记录.csv");
  222. header('Cache-Control: max-age=0');
  223. $fp = fopen('php://output', 'a');
  224. $head = ['ID', '员工名称','排班日期','排班时间', '打卡记录']; //定义标题
  225. foreach ($head as $i => $v) {
  226. $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8'); //将中文标题转换编码,否则乱码
  227. }
  228. fputcsv($fp, $head);
  229. $limit = 10000;
  230. $num = 0;//计数器
  231. foreach ($data as $value) {//循环数据
  232. $num++;
  233. if ($num == $limit) {
  234. ob_flush();//释放内存
  235. flush();
  236. }
  237. $clockData = [];
  238. if (!empty($value['clock'])){
  239. $clock = json_decode($value['clock']);
  240. foreach ($clock as $item)
  241. {
  242. $clockStr = '';
  243. if ($item->timeResult == 'NotSigned') $clockStr = '未打卡';
  244. if ($item->timeResult == 'Late') $clockStr = '迟到';
  245. if ($item->timeResult == 'Early') $clockStr = '早退';
  246. if ($item->timeResult == 'Normal') $clockStr = '正常';
  247. $clockData[] = [
  248. $item->baseCheckTime,
  249. $item->userCheckTime,
  250. $clockStr,
  251. ];
  252. }
  253. }
  254. $rows['id'] = isset($value['id']) ? $value['id'] : null; // ID
  255. $rows['staffName'] = isset($value['staffName']) ? $value['staffName'] : null; // 员工名称
  256. $rows['time'] = isset($value['time']) ? $value['time'] : null;
  257. $rows['time_slot'] = isset($value['time_slot']) ? $value['time_slot'] : null;
  258. $rows['clock'] = isset($value['clock']) ? json_encode($clockData) : '没有打卡记录';
  259. foreach ($rows as $kk => $vv) {
  260. $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码
  261. }
  262. fputcsv($fp, $rs);
  263. $rows = [];
  264. }
  265. }
  266. /**
  267. * 格式数据详情
  268. * @param $data
  269. * @return mixed
  270. */
  271. public function formatInfo($data)
  272. {
  273. return $data;
  274. }
  275. public function delete($id)
  276. {
  277. $res = $this->obj->delete(['id' => $id]);
  278. if ($res){
  279. return ResultWrapper::success('删除成功');
  280. }
  281. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  282. }
  283. }