MAgents.Class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /**
  3. * 代理商数据模型
  4. * Created by PhpStorm.
  5. * User: user
  6. * Date: 2021/6/24
  7. * Time: 12:00
  8. */
  9. namespace JinDouYun\Model\Agent;
  10. use JinDouYun\Dao\Agent\DOemDetails;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\ResultWrapper;
  13. use Mall\Framework\Core\StatusCode;
  14. use JinDouYun\Dao\Oem\DOem;
  15. use JinDouYun\Dao\Agent\DMeal;
  16. use JinDouYun\Dao\Agent\DAgents;
  17. class MAgents
  18. {
  19. private $objDAgents;
  20. private $objDOem;
  21. private $objDOemDetails;
  22. public function __construct()
  23. {
  24. $this->objDAgents = new DAgents('default');
  25. $this->objDOem = new DOem();
  26. $this->objDOem->setTable('qianniao_oem_1');
  27. $this->objDOemDetails = new DOemDetails();
  28. $this->objDOemDetails->setTable('qianniao_oem_details');
  29. }
  30. /**
  31. * 增加代理商
  32. * @param $params
  33. * @return ResultWrapper
  34. */
  35. public function addAgent($params)
  36. {
  37. $addAgent = $this->objDAgents->insert($params);
  38. if ($addAgent === false) {
  39. return ResultWrapper::fail($this->objAgents->error(), ErrorCode::$dberror);
  40. }
  41. return ResultWrapper::success($addAgent);
  42. }
  43. /**
  44. * 编辑代理商信息
  45. *
  46. * @param int|array $params 修改代理的信息
  47. *
  48. * @return ResultWrapper
  49. */
  50. public function editAgent($params)
  51. {
  52. if (empty($params['id'])) {
  53. return ResultWrapper::fail('id为空', ErrorCode::$paramError);
  54. }
  55. $id = $params['id'];
  56. unset($params['id']);
  57. $dbResult = $this->objDAgents->update($params, ['id' => $id]);
  58. if ($dbResult === false) {
  59. return ResultWrapper::fail($this->objDAgents->error(), ErrorCode::$dberror);
  60. }
  61. return ResultWrapper::success($dbResult);
  62. }
  63. /**
  64. * 删除代理信息
  65. * @param $id
  66. * @return ResultWrapper
  67. */
  68. public function deleteAgent($id)
  69. {
  70. $params = [
  71. 'deleteStatus' => StatusCode::$delete,
  72. 'updateTime' => time(),
  73. ];
  74. //本条id的数据
  75. $agentData = $this->objDAgents->get(['id'=>$id]);
  76. if ($agentData === false){
  77. return ResultWrapper::fail($this->objDAgents->error(),ErrorCode::$dberror);
  78. }
  79. $dbResult = $this->objDAgents->update($params, $id);//软删除
  80. if($dbResult === false){
  81. return ResultWrapper::fail($this->objDRole->error(), ErrorCode::$dberror);
  82. }
  83. return ResultWrapper::success($dbResult);
  84. }
  85. /**
  86. * 获取代理信息
  87. * @param $selectParams
  88. * @return ResultWrapper
  89. */
  90. public function getAllAgent($selectParams)
  91. {
  92. $limit = $selectParams['limit'];
  93. unset($selectParams['limit']);
  94. $offset = $selectParams['offset'];
  95. unset($selectParams['offset']);
  96. $dbResult = $this->objDAgents->select($selectParams, '*', 'createTime DESC', $limit, $offset);
  97. if ($dbResult === false) {
  98. return ResultWrapper::fail($this->objDAgents->error(), ErrorCode::$dberror);
  99. }
  100. $total = $this->objDAgents->count($selectParams);
  101. $return = [
  102. 'data' => self::format($dbResult),//格式化json数据
  103. 'total' => ($total) ? intval($total) : 0,
  104. ];
  105. return ResultWrapper::success($return);
  106. }
  107. /**
  108. * oem信息设置
  109. * @param int|array $params 需设置的key
  110. * @param int|number $enterpriseId 企业id
  111. * @return ResultWrapper
  112. */
  113. public function oemSystemSet($params,$enterpriseId)
  114. {
  115. $objDOem = new DOem();
  116. $is_null = $objDOem->get(['enterpriseId' => $enterpriseId]);
  117. //企业是否存在
  118. if (empty($is_null)) {
  119. return ResultWrapper::fail("暂无该企业信息", ErrorCode::$dberror);
  120. }
  121. //需要修改的字段 信息
  122. switch ($params['type']) {
  123. case 1://微信开放平台配置
  124. $updateDate = ['weixinOpen'=>json_encode($params['setData'],JSON_UNESCAPED_UNICODE)];
  125. break;
  126. case 2://短息配置
  127. $updateDate = ['sms'=>json_encode($params['setData'],JSON_UNESCAPED_UNICODE)];
  128. break;
  129. case 3://高德地图地址
  130. $updateDate = ['amapKey'=>isset($params['setData']['amapKey'])?$params['setData']['amapKey']:''];
  131. break;
  132. default:
  133. return ResultWrapper::fail("参数值错误", ErrorCode::$dberror);
  134. }
  135. $dbResult = $objDOem->update($updateDate, ['enterpriseId' => $enterpriseId]);
  136. if ($dbResult === false) {
  137. return ResultWrapper::fail($this->$objDOem->error(), ErrorCode::$dberror);
  138. }
  139. return ResultWrapper::success($dbResult);
  140. }
  141. /**
  142. * 根据企业id获取oem企业
  143. * @param $selectParams
  144. * @return ResultWrapper
  145. */
  146. public function getOemInfo($selectParams)
  147. {
  148. $objDOem = new DOem();
  149. $dbResult = $objDOem->select($selectParams);
  150. if ($dbResult === false) {
  151. return ResultWrapper::fail($this->objDAgents->error(), ErrorCode::$dberror);
  152. }
  153. $dataInfo = self::formatOem($dbResult);
  154. return ResultWrapper::success($dataInfo);
  155. }
  156. /**
  157. * 格式化联系人json数据
  158. */
  159. private function format($data)
  160. {
  161. foreach ($data as &$v) {
  162. if (isset($v['contact'])) {
  163. $v['contact'] = json_decode($v['contact'], true);
  164. }
  165. }
  166. return $data;
  167. }
  168. /**
  169. * 格式化oem json数据
  170. */
  171. private function formatOem($data)
  172. {
  173. $data[0]['tencentCloud'] = isset($data[0]['tencentCloud']) ? json_decode($data[0]['tencentCloud']):"";
  174. $data[0]['weixinOpen'] = isset($data[0]['weixinOpen']) ? json_decode($data[0]['weixinOpen']):"";
  175. $data[0]['sms'] = isset($data[0]['sms']) ? json_decode($data[0]['sms']):"";
  176. return $data;
  177. }
  178. public function getAllMeal()
  179. {
  180. $objDMeal = new DMeal();
  181. $selectParams['deleteStatus'] = StatusCode::$standard;
  182. $dbResult = $objDMeal->select($selectParams);
  183. if ($dbResult === false) {
  184. return ResultWrapper::fail($this->objDAgents->error(), ErrorCode::$dberror);
  185. }
  186. return ResultWrapper::success($dbResult);
  187. }
  188. /**
  189. * oem续费
  190. */
  191. public function renew($params)
  192. {
  193. // 查询当前企业的数据
  194. $dbResult = $this->objDOem->get(['enterpriseId'=>$params['enterpriseId']]);
  195. if ($dbResult === false) {
  196. return ResultWrapper::fail($this->objDOem->error(), ErrorCode::$dberror);
  197. }
  198. if(empty($dbResult)){
  199. return ResultWrapper::fail('当前企业不存在',ErrorCode::$notAllowAccess);
  200. }
  201. $afterMoney = bcsub($dbResult['balance'],$params['money'],2);
  202. //组装数据
  203. $detailsDate = [
  204. 'enterpriseId' => getArrayItem($params,'enterpriseId',0),
  205. 'enterpriseName' => getArrayItem($params,'enterpriseName',''),
  206. 'mealId' => getArrayItem($params,'mealId',0),
  207. 'meal' => getArrayItem($params,'meal',''),
  208. 'mobile' => getArrayItem($params,'mobile',0),
  209. 'type' => '续费',
  210. 'beforeMoney' => $dbResult['balance'],
  211. 'money' => getArrayItem($params,'money',0),
  212. 'afterMoney' => $afterMoney,
  213. 'explain' => '客户续费'.$params['years'].'年的'.$params['meal'].'共消费'.$params['money'].'元',
  214. 'createTime' => time(),
  215. ];
  216. //减完的钱不能小于零
  217. if($detailsDate['afterMoney']<0){
  218. return ResultWrapper::fail('余额不足',ErrorCode::$notAllowAccess);
  219. }
  220. unset($dbResult);
  221. $beginTransactionStatus = $this->objDOemDetails->beginTransaction();
  222. $dbResult = $this->objDOemDetails->insert($detailsDate);
  223. if ($dbResult === false) {
  224. $this->objDOemDetails->rollBack();
  225. return ResultWrapper::fail($this->objDOemDetails->error(), ErrorCode::$dberror);
  226. }
  227. // 扣代理商余额
  228. $update = $this->objDOem->update(['balance'=>$afterMoney],['enterpriseId'=>$params['enterpriseId']]);
  229. if ($update === false) {
  230. $this->objDOemDetails->rollBack();
  231. return ResultWrapper::fail($this->objDOem->error(), ErrorCode::$dberror);
  232. }
  233. if($beginTransactionStatus){
  234. $this->objDOemDetails->commit();
  235. }
  236. return ResultWrapper::success($dbResult);
  237. }
  238. /**
  239. * 消费记录列表
  240. */
  241. public function getAllRenew($selectParams)
  242. {
  243. $limit = $selectParams['limit'];
  244. unset($selectParams['limit']);
  245. $offset = $selectParams['offset'];
  246. unset($selectParams['offset']);
  247. $returnData = [
  248. 'data' => [],
  249. 'total' => 0,
  250. ];
  251. $whereSql = '';
  252. if (isset($selectParams['enterpriseId']) && !empty($selectParams['enterpriseId'])) {
  253. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  254. $whereSql .= $where . ' enterpriseId = ' . $selectParams['enterpriseId'];
  255. }
  256. if (isset($selectParams['mobile']) && !empty($selectParams['mobile'])) {
  257. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  258. $whereSql .= $where . ' mobile like "%' . $selectParams['mobile'].'%"';
  259. }
  260. if (isset($selectParams['mealId']) && !empty($selectParams['mealId'])) {
  261. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  262. $whereSql .= $where . ' mealId = ' . $selectParams['mealId'];
  263. }
  264. $sql = 'SELECT * FROM ' .$this->objDOemDetails->get_Table().$whereSql . ' ORDER BY createTime DESC LIMIT ' . $offset . ' , ' . $limit;
  265. $dbResult = $this->objDOemDetails->query($sql);
  266. if ($dbResult === false) {
  267. return ResultWrapper::fail($this->objDOemDetails->error(), ErrorCode::$dberror);
  268. }
  269. if(empty($dbResult)){
  270. return ResultWrapper::success($returnData);
  271. }
  272. $totalSql = 'SELECT COUNT(1) as count FROM ' .$this->objDOemDetails->get_Table() . $whereSql;
  273. $dbTotalResult = $this->objDOemDetails->query($totalSql);
  274. if ($dbTotalResult === false) {
  275. return ResultWrapper::fail($this->objDOemDetails->error(), ErrorCode::$dberror);
  276. }
  277. if(empty($dbTotalResult)){
  278. return ResultWrapper::success([]);
  279. }
  280. $return = [
  281. 'data' => $dbResult,
  282. 'total' => $dbTotalResult[0]['count']
  283. ];
  284. return ResultWrapper::success($return);
  285. }
  286. }