MShopRostering.Class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. var_dump($dbResult);
  120. if(empty($dbResult)){
  121. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  122. }
  123. $temsql = 'SELECT * FROM qianniao_shop_template WHERE id IN ('.$dbResult['template_id'].')';
  124. $dbResult['template'] = $this->objDShopTemplate->query($temsql);
  125. return ResultWrapper::success(self::formatInfo($dbResult));
  126. }
  127. /**
  128. * Doc: (des="添加")
  129. * User: XMing
  130. * Date: 2020/7/15
  131. * Time: 10:42 上午
  132. * @throws Exception
  133. * @param array $params
  134. * @return ResultWrapper
  135. */
  136. public function insert(array $params)
  137. {
  138. // $times = $params['times'];
  139. // unset($params['times']);
  140. //
  141. //
  142. $this->obj->beginTransaction();
  143. $data = $this->obj->get(['uid' => $params['uid'], 'time' => $params['time']]);
  144. if ($data){
  145. $res = $this->obj->update([
  146. 'name' => $params['name'],
  147. 'template_id' => $params['template_id'],
  148. 'time' => $params['time'],
  149. 'time_slot' => $params['time_slot'],
  150. ], $data['id']);
  151. }else{
  152. $res = $this->obj->insert($params);
  153. }
  154. if ($res){
  155. $this->obj->commit();
  156. return ResultWrapper::success('添加成功');
  157. }else{
  158. $this->obj->rollBack();
  159. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  160. }
  161. }
  162. public function update($params, $id)
  163. {
  164. $data = $this->obj->get($id);
  165. if (!$data){
  166. return ResultWrapper::fail('数据不存在', ErrorCode::$dberror);
  167. }
  168. $dbResult = $this->obj->update($params,$id);
  169. if($dbResult === false){
  170. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  171. }
  172. return ResultWrapper::success('修改成功');
  173. }
  174. public function select($where = null, $fields = '*', $order = null, $limit = null, $offset = null, $data = array(), $multiple = true, $isExport = false)
  175. {
  176. if (isset($where['export'])){
  177. unset($where['export']);
  178. $data = $this->obj->select($where);
  179. foreach ($data as &$item)
  180. {
  181. $staff = $this->objStaff->get(['id' => $item['uid']]);
  182. $item['staffName'] = $staff['staffName'];
  183. $item['avatar'] = $staff['avatar'];
  184. $item['evaluate'] = $staff['evaluate'];
  185. $item['info'] = $staff['info'];
  186. }
  187. $this->Excel($data);
  188. exit;
  189. }
  190. $data = $this->obj->select($where, $fields, $order, $limit, $offset, $data, $multiple, $isExport);
  191. $count = $this->obj->count($where);
  192. if($data === false){
  193. return ResultWrapper::success($data);
  194. }else{
  195. foreach ($data as &$item)
  196. {
  197. $staff = $this->objStaff->get(['id' => $item['uid']]);
  198. $item['staffName'] = $staff['staffName'];
  199. $item['avatar'] = $staff['avatar'];
  200. $item['evaluate'] = $staff['evaluate'];
  201. $item['info'] = $staff['info'];
  202. $item['time_slot'] = json_decode($item['time_slot']);
  203. $item['clock'] = json_decode($item['clock']);
  204. }
  205. $return = [
  206. 'data' => $data,
  207. 'total' => $count,
  208. ];
  209. return ResultWrapper::success($return);
  210. }
  211. }
  212. /**
  213. * 导出
  214. * @param $inventoryOutData
  215. * @return void
  216. *
  217. */
  218. public function Excel($data)
  219. {
  220. //导出到本地
  221. header("Content-type:application/vnd.ms-excel");
  222. header("Content-Disposition:filename=考情记录.csv");
  223. header('Cache-Control: max-age=0');
  224. $fp = fopen('php://output', 'a');
  225. $head = ['ID', '员工名称','排班日期','排班时间', '打卡记录']; //定义标题
  226. foreach ($head as $i => $v) {
  227. $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8'); //将中文标题转换编码,否则乱码
  228. }
  229. fputcsv($fp, $head);
  230. $limit = 10000;
  231. $num = 0;//计数器
  232. foreach ($data as $value) {//循环数据
  233. $num++;
  234. if ($num == $limit) {
  235. ob_flush();//释放内存
  236. flush();
  237. }
  238. $clockData = [];
  239. if (!empty($value['clock'])){
  240. $clock = json_decode($value['clock']);
  241. foreach ($clock as $item)
  242. {
  243. $clockStr = '';
  244. if ($item->timeResult == 'NotSigned') $clockStr = '未打卡';
  245. if ($item->timeResult == 'Late') $clockStr = '迟到';
  246. if ($item->timeResult == 'Early') $clockStr = '早退';
  247. if ($item->timeResult == 'Normal') $clockStr = '正常';
  248. $clockData[] = [
  249. $item->baseCheckTime,
  250. $item->userCheckTime,
  251. $clockStr,
  252. ];
  253. }
  254. }
  255. $rows['id'] = isset($value['id']) ? $value['id'] : null; // ID
  256. $rows['staffName'] = isset($value['staffName']) ? $value['staffName'] : null; // 员工名称
  257. $rows['time'] = isset($value['time']) ? $value['time'] : null;
  258. $rows['time_slot'] = isset($value['time_slot']) ? $value['time_slot'] : null;
  259. $rows['clock'] = isset($value['clock']) ? json_encode($clockData) : '没有打卡记录';
  260. foreach ($rows as $kk => $vv) {
  261. $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码
  262. }
  263. fputcsv($fp, $rs);
  264. $rows = [];
  265. }
  266. }
  267. /**
  268. * 格式数据详情
  269. * @param $data
  270. * @return mixed
  271. */
  272. public function formatInfo($data)
  273. {
  274. return $data;
  275. }
  276. public function delete($id)
  277. {
  278. $res = $this->obj->delete(['id' => $id]);
  279. if ($res){
  280. return ResultWrapper::success('删除成功');
  281. }
  282. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  283. }
  284. }