You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

442 lines
19 KiB

  1. <?php
  2. class update_channel
  3. {
  4. public function execUpdateChannel($channel_id,$supplier_name, $area_id, $manage_type, $company_name, $id_card, $sale, $sett_type, $sett_frequency, $account_bank, $account_num, $account_name, $link_info,$sales_man)
  5. {
  6. //1.获取销售方式详情数组
  7. $sale_array = $this->getSale($sale);
  8. //2.从数据库获取销售方式
  9. $db_sale_array = $this->getDbSale($channel_id);
  10. //3.获取联系人详情数组
  11. $link_array = $this->getLink($link_info);
  12. //4.从数据库获取联系人详情
  13. $db_link_array = $this->getDbLink($channel_id);
  14. //5.比较销售方式
  15. $sale = $this->compare($sale_array, $db_sale_array);
  16. //6,比较联系详情
  17. $link = $this->compare($link_array, $db_link_array);
  18. //7,从数据库获取渠道商详情
  19. $db_channel = $this->getDbChannel($channel_id);
  20. //8.操作数据库
  21. $channel_array = array('supplier_name' => $supplier_name,'area_id' => $area_id,'manage_type' => $manage_type,'company_name' => $company_name,'id_card' => $id_card,'sett_type' => $sett_type,'sett_frequency' => $sett_frequency,'account_bank' => $account_bank,'account_num' => $account_num,'account_name' => $account_name,'sales_man' => $sales_man);
  22. $res = $this -> updatePdoChannel($channel_id,$channel_array,$db_channel,$sale,$link);
  23. echo json_encode($res);
  24. exit();
  25. }
  26. //获取销售方式详情数组
  27. function getSale($sale)
  28. {
  29. if ($sale == '') {
  30. $sale_array = array();
  31. return $sale_array;
  32. }
  33. $sale = explode(",", $sale);
  34. $key = $_COOKIE['memcache'];
  35. $sale_memcache = get_memcache('ZHANGS_SALE' . $key);
  36. foreach ($sale as $item => $value) {
  37. $sale_array[$value] = $sale_memcache[$value];
  38. }
  39. return $sale_array;
  40. }
  41. //获取联系人详情数组
  42. function getLink($link_info)
  43. {
  44. if ($link_info == '') {
  45. $link_array = array();
  46. return $link_array;
  47. }
  48. $link_info = explode(",", $link_info);
  49. $key = $_COOKIE['memcache'];
  50. $link_memcache = get_memcache('ZHANGS_LINK' . $key);
  51. foreach ($link_info as $item => $value) {
  52. $status = 0;
  53. if (empty($link_memcache[$value]['link_name']) || empty($link_memcache[$value]['contact_name'])) {
  54. $status = 1;
  55. }
  56. if ($status == 0) {
  57. $link_array[$value] = $link_memcache[$value];
  58. }
  59. }
  60. return $link_array;
  61. }
  62. //从数据库获取销售方式详情数组
  63. function getDbSale($channel_id)
  64. {
  65. global $pdo;
  66. $sql = "SELECT
  67. id AS sale_id,
  68. prod_supplier_id,
  69. product_type,
  70. parent_type,
  71. sale_type,
  72. commision_flag,
  73. commision_type,
  74. back_commision_type,
  75. back_commision_method,
  76. back_percent,
  77. back_value
  78. FROM
  79. base_supplier_sale
  80. WHERE
  81. cancel_flag = 0
  82. AND supplier_id = " . $channel_id;
  83. writeLog("getDbSale base_supplier_sale:" . $sql);
  84. $result = $pdo->query($sql);
  85. $res = $result->fetchAll(PDO::FETCH_ASSOC);
  86. if(count($res) > 0)
  87. {
  88. foreach($res as $k => $v)
  89. {
  90. $result1[$v['sale_id']] = $v;
  91. }
  92. }
  93. else
  94. {
  95. $result1= array();
  96. }
  97. return $result1;
  98. }
  99. //从数据库获取联系人详情
  100. function getDbLink($channel_id)
  101. {
  102. global $pdo;
  103. $sql = "SELECT
  104. id as link_id,
  105. link_name,
  106. contact_name,
  107. contact_mobile,
  108. contact_telphone,
  109. fax,
  110. email,
  111. remark
  112. FROM
  113. base_supplier_link
  114. WHERE
  115. cancel_flag = 0
  116. AND supplier_id = " . $channel_id;
  117. writeLog("getDbLink base_supplier_link:" . $sql);
  118. $result = $pdo->query($sql);
  119. $res = $result->fetchAll(PDO::FETCH_ASSOC);
  120. if(count($res) > 0)
  121. {
  122. foreach($res as $k => $v)
  123. {
  124. $result1[$v['link_id']] = $v;
  125. }
  126. }
  127. else
  128. {
  129. $result1= array();
  130. }
  131. return $result1;
  132. }
  133. //从数据库获取渠道商详情
  134. function getDbChannel($channel_id)
  135. {
  136. global $pdo;
  137. $sql = "SELECT
  138. supplier_name,
  139. area_id,
  140. manage_type,
  141. company_name,
  142. id_card,
  143. sett_type,
  144. sett_frequency,
  145. account_bank,
  146. account_num,
  147. account_name,
  148. sales_man
  149. FROM
  150. base_supplier
  151. WHERE
  152. cancel_flag = 0
  153. AND id = " . $channel_id;
  154. writeLog("getDbChannel base_supplier:" . $sql);
  155. $result = $pdo->query($sql);
  156. $res = $result->fetchAll(PDO::FETCH_ASSOC);
  157. $res = $res[0];
  158. return $res;
  159. }
  160. //比较数组(前台数组,数据库数组)
  161. function compare($arr, $db_arr)
  162. {
  163. $result = array();
  164. if(count($db_arr) == 0 && count($arr) == 0)
  165. {
  166. return $result;
  167. }
  168. elseif (count($db_arr) == 0)
  169. {
  170. $result['insert'] = $arr;
  171. return $result;
  172. }
  173. elseif (count($arr) == 0)
  174. {
  175. $result['delete'] = $db_arr;
  176. return $result;
  177. }
  178. else
  179. {
  180. foreach ($arr as $key => $value) {
  181. if(isset($db_arr[$key]))
  182. {
  183. if($arr != $db_arr)
  184. {
  185. $result['update'][$key] = $value;
  186. }
  187. }
  188. else
  189. {
  190. $result['insert'][$key] = $value;
  191. }
  192. }
  193. foreach($db_arr as $key => $value)
  194. {
  195. if(!isset($arr[$key]))
  196. {
  197. $result['delete'][$key] = $value;
  198. }
  199. }
  200. return $result;
  201. }
  202. }
  203. //获取update操作中的set数据
  204. function getUpdateSet($arr)
  205. {
  206. $set_array = array();
  207. foreach($arr as $k => $v)
  208. {
  209. if(!empty($v))
  210. {
  211. $set_array[] = $k . " = '" . $v . "'";
  212. }
  213. else
  214. {
  215. if($k == 'account_bank' || $k == 'account_num' || $k == 'account_name' || $k == 'link_name' || $k == 'contact_name' || $k == 'contact_mobile' || $k == 'contact_telphone' || $k == 'fax' || $k == 'email' || $k == 'remark' || $k == 'id_card' || $k == 'sales_man')
  216. {
  217. $set_array[] = $k . " = ''";
  218. }
  219. else
  220. {
  221. $set_array[] = $k . " = '0'";
  222. }
  223. }
  224. }
  225. $set_str = implode(",",$set_array);
  226. return $set_str;
  227. }
  228. //操作数据库
  229. function updatePdoChannel($channel_id,$channel_array,$db_channel,$sale,$link)
  230. {
  231. global $pdo;
  232. $user_id = getUserId();//1;
  233. $time = date('Y-m-d H:i:s',time());
  234. try {
  235. $pdo->beginTransaction();
  236. //1.操作base_supplier表
  237. if($channel_array != $db_channel)//修改
  238. {
  239. $set_str = $this->getUpdateSet($channel_array);
  240. $update_supplier_sql = "UPDATE base_supplier
  241. SET update_user_id = " . $user_id . ",update_time = '" . $time . "'," . $set_str . "
  242. WHERE
  243. id = " . $channel_id;
  244. writeLog("UPDATE base_supplier:" . $update_supplier_sql);
  245. $result = $pdo->exec($update_supplier_sql);
  246. if (!$result) {
  247. throw new Exception("db die");
  248. }
  249. }
  250. //2.操作base_supplier_sale表
  251. if(count($sale) > 0)
  252. {
  253. if(count($sale['update']) > 0)//修改
  254. {
  255. $update_sale_array = array();
  256. foreach($sale['update'] as $key => $value)
  257. {
  258. unset($value['sale_id']);
  259. $set_str = $this->getUpdateSet($value);
  260. $update_sale_array[] = "UPDATE base_supplier_sale
  261. SET update_user_id = " . $user_id . ",update_time = '" . $time . "'," . $set_str . "
  262. WHERE
  263. id = " . $key;
  264. }
  265. $update_sale_sql = implode(";",$update_sale_array);
  266. writeLog("UPDATE base_supplier_sale:" . $update_sale_sql);
  267. $result = $pdo->exec($update_sale_sql);
  268. if (!$result) {
  269. throw new Exception("db die");
  270. }
  271. }
  272. if(count($sale['insert']) > 0)//增加
  273. {
  274. $insert_sale_sql = "INSERT INTO base_supplier_sale (
  275. create_user_id,
  276. create_time,
  277. supplier_id,
  278. prod_supplier_id,
  279. product_type,
  280. parent_type,
  281. sale_type,
  282. commision_flag,
  283. commision_type,
  284. back_commision_type,
  285. back_commision_method,
  286. back_percent,
  287. back_value
  288. )
  289. VALUES ";
  290. foreach($sale['insert'] as $key => $value)
  291. {
  292. $sale_sql_array[] = "(" . $user_id . ",'" . $time . "'," . $channel_id . "," . $value['prod_supplier_id'] . "," . $value['product_type'] . "," . $value['parent_type'] . "," . $value['sale_type'] . "," . $value['commision_flag'] . "," . $value['commision_type'] . "," . $value['back_commision_type'] . "," . $value['back_commision_method'] . ",'" . $value['back_percent'] . "','" . $value['back_value'] . "')";
  293. }
  294. $insert_sale_sql .= implode(",", $sale_sql_array);
  295. writeLog("INSERT base_supplier_purchase:" . $insert_sale_sql);
  296. $result = $pdo->exec($insert_sale_sql);
  297. if (!$result) {
  298. throw new Exception("db die");
  299. }
  300. }
  301. if(count($sale['delete']) > 0)//删除
  302. {
  303. $delete_sale_array = array();
  304. foreach($sale['delete'] as $key => $value)
  305. {
  306. $delete_sale_array[] = "UPDATE base_supplier_sale
  307. SET update_user_id = " . $user_id . ",update_time = '" . $time . "',cancel_flag = 1
  308. WHERE
  309. id = " . $key;
  310. }
  311. $delete_sale_sql = implode(";",$delete_sale_array);
  312. writeLog("delete base_supplier_sale:" . $delete_sale_sql);
  313. $result = $pdo->exec($delete_sale_sql);
  314. if (!$result) {
  315. throw new Exception("db die");
  316. }
  317. }
  318. }
  319. //3.操作base_supplier_link表
  320. if(count($link) > 0)
  321. {
  322. if(count($link['update']) > 0)//修改
  323. {
  324. $update_link_array = array();
  325. foreach($link['update'] as $key => $value)
  326. {
  327. unset($value['link_id']);
  328. $set_str = $this->getUpdateSet($value);
  329. $update_link_array[] = "UPDATE base_supplier_link
  330. SET update_user_id = " . $user_id . ",update_time = '" . $time . "'," . $set_str . "
  331. WHERE
  332. id = " . $key;
  333. }
  334. $update_link_sql = implode(";",$update_link_array);
  335. writeLog("UPDATE base_supplier_link:" . $update_link_sql);
  336. $result = $pdo->exec($update_link_sql);
  337. if (!$result) {
  338. throw new Exception("db die");
  339. }
  340. }
  341. if(count($link['insert']) > 0)//增加
  342. {
  343. $insert_link_sql = "INSERT INTO base_supplier_link (
  344. create_user_id,
  345. create_time,
  346. supplier_id,
  347. link_name,
  348. contact_name,
  349. contact_mobile,
  350. contact_telphone,
  351. fax,
  352. email,
  353. remark
  354. )
  355. VALUES";
  356. foreach($link['insert'] as $key => $value)
  357. {
  358. $link_sql_array[] = "(" . $user_id . ",'" . $time . "'," . $channel_id . ",'" . $value['link_name'] . "','" . $value['contact_name'] . "','" . $value['contact_mobile'] . "','" . $value['contact_telphone'] . "','" . $value['fax'] . "','" . $value['email'] . "','" . $value['remark'] . "')";
  359. }
  360. $insert_link_sql .= implode(",", $link_sql_array);
  361. writeLog("INSERT base_supplier_purchase:" . $insert_link_sql);
  362. $result = $pdo->exec($insert_link_sql);
  363. if (!$result) {
  364. throw new Exception("db die");
  365. }
  366. }
  367. if(count($link['delete']) > 0)//删除
  368. {
  369. $delete_link_array = array();
  370. foreach($link['delete'] as $key => $value)
  371. {
  372. $delete_link_array[] = "UPDATE base_supplier_link
  373. SET update_user_id = " . $user_id . ",update_time = '" . $time . "',cancel_flag = 1
  374. WHERE
  375. id = " . $key;
  376. }
  377. $delete_link_sql = implode(";",$delete_link_array);
  378. writeLog("delete base_supplier_sale:" . $delete_link_sql);
  379. $result = $pdo->exec($delete_link_sql);
  380. if (!$result) {
  381. throw new Exception("db die");
  382. }
  383. }
  384. }
  385. $pdo->commit();
  386. //4.删除缓存
  387. $key = $_COOKIE['memcache'];
  388. delete_memcache('ZHANGS_LINK' . $key);
  389. delete_memcache('ZHANGS_SALE' . $key);
  390. $json['code'] = '0';
  391. $json['info'] = '修改数据成功';
  392. } catch (PDOException $ex) {
  393. $pdo->rollBack();
  394. $json['code'] = '1';
  395. $json['info'] = '修改失败';
  396. }
  397. return $json;
  398. }
  399. }
  400. $channel_id = trim($_POST['channel_id']);//渠道商名称
  401. $supplier_name = trim($_POST['supplier_name']);//渠道商名称
  402. $area_id = trim($_POST['area_id']);//所属区域市id
  403. $manage_type = trim($_POST['manage_type']);//经营性质 1:公司 2:个人
  404. $company_name = trim($_POST['company_name']);//公司名称
  405. $id_card = trim($_POST['id_card']);//身份证号(企业传空)
  406. $sale = trim($_POST['sale']);//销售方式
  407. $sett_type = trim($_POST['sett_type']);//结算方式 (授信:275,预付:288,单结:292)
  408. $sett_frequency = trim($_POST['sett_frequency']);//结算周期(日结:293,周结:294,月结:295)
  409. $account_bank = trim($_POST['account_bank']);//开户银行
  410. $account_num = trim($_POST['account_num']);//银行账号
  411. $account_name = trim($_POST['account_name']);//账号名称
  412. $link_info = trim($_POST['link_info']);//联系人详情
  413. $sales_man = trim($_POST['sales_man']);//业务员
  414. $update_channel = new update_channel();
  415. $update_channel -> execUpdateChannel($channel_id,$supplier_name, $area_id, $manage_type, $company_name, $id_card, $sale, $sett_type, $sett_frequency, $account_bank, $account_num, $account_name, $link_info,$sales_man)
  416. ?>