<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\services\company;

use app\model\customer\Company;
use app\model\customer\Contact;

/**
 * Undocumented class
 */
class CompanyService {

	/**
	 * @title 获取企业列表
	 *
	 * @param [type] $request
	 * @return void
	 */
	public function getCompanyList($request) {
		$param = $request->param();
		$map = [];
		if (isset($param['name']) && $param['name']) {
			$map[] = ['name', 'LIKE', '%' . $param['name'] . '%'];
		}
		if (isset($param['org_code']) && $param['org_code']) {
			$map[] = ['org_code', '=', $param['org_code']];
		}
		$list = Company::with(['contact'])->where($map)->order('id desc')->paginate($request->pageConfig);
		return $list->append(['area_t', 'industry_t']);
	}

	/**
	 * @title 获取企业详情
	 *
	 * @param [type] $request
	 * @return void
	 */
	public function getCompanyDetail($request) {
		$param = $request->param();
		if (!isset($param['id'])) {
			return [];
		}
		$map = [];
		$map[] = ['id', '=', $param['id']];
		$data = Company::with(['contact'])->where($map)->find();
		return $data->append(['area_t', 'industry_t']);
	}

	/**
	 * @title 导入数据
	 *
	 * @return void
	 */
	public function insertCompanyData($request) {
		$data = $request->post('data');
		$company = new Company();
		$contactM = new Contact();

		$resultData = [];
		foreach ($data as $k => $item) {
			$info = [];
			if (!is_array($item)) {
				continue;
			}
			foreach ($item as $key => $value) {
				if (isset($company->insertFieldAlis[$key])) {
					$info[$company->insertFieldAlis[$key]['table']][$company->insertFieldAlis[$key]['name']] = trim($value);
				}
			}
			$info['company']['uid'] = $info['contact']['uid'] = request()->user['uid'];

			if (isset($info['company']) && isset($info['company']['name'])) {
				$customer = $company->where('name', '=', trim($info['company']['name']))->find();
				$company_id = isset($customer['id']) ? $customer['id'] : 0;
				if (!$company_id) {
					$info['company']['turnover'] = '';
					$info['company']['staff_num'] = '';
					$info['company']['come_from'] = '';
					$info['company']['company_type'] = '';
					$info['company']['pc_count'] = '';
					$customer = Company::create($info['company']);
					$company_id = $customer->id;
				} else {
					$save_coustomer = $info['company'];
					if (!empty($save_coustomer)) {
						$save_coustomer['id'] = $company_id;
						$result = $customer->save($save_coustomer);
					}
				}
				if (isset($info['contact']['name'])) {
					$map = [];
					$map[] = ['name', '=', $info['contact']['name']];
					$map[] = ['mobile', '=', isset($info['contact']['mobile']) ? $info['contact']['mobile'] : ''];
					$map[] = ['company_id', '=', $company_id];
					$contact = $contactM->where($map)->find(); //每次查询重新实例化
					$contact_id = isset($contact['id']) ? $contact['id'] : 0;
					if (!$contact_id) {
						$info['contact']['company_id'] = $company_id;
						$res = Contact::create($info['contact']);
						$contact_id = $res->id;
					} else {
						$info['contact']['id'] = $contact_id;
						$contact->save($info['contact']);
					}
				} else {
					$contact_id = 0;
				}
				$resultData[] = ['company_id' => $company_id, 'contact_id' => (int) $contact_id, 'company' => $info['company'], 'contact' => $info['contact']];
			}
		}
		return empty($resultData) ? false : $resultData;
	}
}