<?php

// +----------------------------------------------------------------------
// | Copyright (c) 2017-2018 富仁高科.
// +----------------------------------------------------------------------
// | This file is automatically generated. Do not manually modify!.
// +----------------------------------------------------------------------
// | Author:  Date: 2017-09-20 08:29:59 Version:V1.0
// +----------------------------------------------------------------------

namespace Home\Service;

use Think\Model;

/*
 * 潜油泵服务
 */
class StSubmergedPumpService extends CommonService {

	// 检索
	function search() {
		// 默认条件
		$condition = ' 1=1 and is_delete = 0 ';
		// 动态条件区域
		
		// 站点编号
		if (strcmp(I('search_station_sn'), "") != 0) {
			$condition = $condition . ' and station_sn =  ' . '"' . I('search_station_sn') . '"';
		}
		// 设备名称
		if (strcmp(I('search_name'), "") != 0) {
			$condition = $condition . ' and name like  ' . '"%' . I('search_name') . '%"';
		}
		// 设备编号
		if (strcmp(I('search_given_code'), "") != 0) {
			$condition = $condition . ' and given_code like  ' . '"%' . I('search_given_code') . '%"';
		}
		// COUNT
		$count = 0;
		if(strcmp($_SESSION['stSubmergedPump_Count'], '')==0 || strcmp(I('changepage'), '1')!=0){
			$count = M('st_submerged_pump')->where($condition)->count();
			$_SESSION['stSubmergedPump_Count'] = $count;
		}else{
			$count = $_SESSION['stSubmergedPump_Count'];
		}
		// COUNT > 0
		if (strcmp($count, 0) != 0) {
			// 默认排序
			$order='update_date DESC';
			// 排序
			if (strcmp(I('sortname'), '')!=0){
				$order='`'.I('sortname').'` '.I('sortorder');
			}
			// 分页
			$limit = (I('page') - 1) * I('pagesize') . ',' . I('pagesize');
			// 检索
			$list = M('st_submerged_pump')->where($condition)->order($order)->limit($limit)->select();
		} else {
			$list = array ();
		}
		return $data = array (
				'count' => $count,
				'data' => $list
		);
	}

	// 追加、更新
	function save(){
		$ret = 0;
		// 事物开始
		M()->startTrans();

		// 更新时间
		$data ['update_date'] = date("Y-m-d H:i:s", time());
		// 更新人
		$data ['updater'] = $_SESSION['user_sn'];
		/**  画面动态数据 */
		// 站点编号
		$data['station_sn'] = I('station_sn');
		// 设备名称
		$data['name'] = I('name');
		// 设备类型
		$data['part_type'] = I('part_type');
		// 品牌
		$data['brand'] = I('brand');
		// 型号
		$data['model'] = I('model');
		// 设备编号
		$data['given_code'] = I('given_code');
		// 状态
		$data['status'] = I('status');
		// 数量
		$data['number'] = I('number');
		// 使用寿命
		$data['service_life'] = I('service_life');
		// 生产日期
		if(strcmp(I('production_date'), "")!=0){
			$data['production_date'] = I('production_date');
		}else{
			$data['production_date'] = null;		
		}
		// 安装日期
		if(strcmp(I('install_date'), "")!=0){
			$data['install_date'] = I('install_date');
		}else{
			$data['install_date'] = null;		
		}
		// 使用日期
		if(strcmp(I('working_date'), "")!=0){
			$data['working_date'] = I('working_date');
		}else{
			$data['working_date'] = null;		
		}
		// 保养周期
		$data['maintenance_period'] = I('maintenance_period');	

		// 追加
		if (strcmp(I('id'), "") == 0) {
			// 创建时间
			$data ['create_date'] = date("Y-m-d H:i:s", time());
			// 创建人
			$data ['creater'] = $_SESSION['user_sn'];
			// 是否删除
			$data ['is_delete'] = 0;
			
			// ADD
			$ret = M('st_submerged_pump')->add($data);
			$temp_id = $ret;
		// 更新
		} else {
			$where['is_delete'] = 0;
			$where['id'] = I('id');
			// SAVE
			$ret1 = M('st_submerged_pump')->where($where)->save($data);
			// SAVE SUCCESS
			if($ret1!==false){
				$ret = 1;
			}
		}
		if($ret > 0 ) {
			// 操作正常交
			M()->commit();
			unset ($_SESSION['stSubmergedPump_Count']);
		} else {
			// 操作异常回滚
			M()->rollback();
		}
		// 返回结果集
		return $data = array ("ret"=> $ret);
	}

	// 获取详细内容
	function detail(){
		$where ['id'] = I('id');
		$where ['is_delete'] = 0;
		$result = M('st_submerged_pump')->where($where)->find();
		if(strcmp($result['production_date'], '')!=0){
			$result['production_date'] = substr($result['production_date'], 0,10);
		}
		if(strcmp($result['install_date'], '')!=0){
			$result['install_date'] = substr($result['install_date'], 0,10);
		}
		if(strcmp($result['working_date'], '')!=0){
			$result['working_date'] = substr($result['working_date'], 0,10);
		}
		return $ret = array (
				'ret' => $result
		);
	}

	// 伦理删除
	function del(){
		//事务开始
		$model = M('st_submerged_pump');
		$model->startTrans();
		$ids=I("ids"); 
		$idArr = explode("|", $ids);
		$flag = true;
		foreach($idArr as $key){
			$where['id'] = $key;
			$data ['is_delete'] = 1;
			$data ['update_date'] = date("Y-m-d H:i:s", time());
			$result = $model->where($where)->save($data);
			if($result>0){
			}else{
				$flag = false;
				break;
			}
		}
		if ($flag) {
			$model->commit();
			unset ($_SESSION['stSubmergedPump_Count']);
			$data = array ('ret' => 1);
		} else {
			//事务回滚
			$model->rollback();
			$data = array ('ret' => 0);
		}
		return $data;
	}

	// 导出
	function report(){
		$xlsName = "潜油泵报表";
		$xlsTitle = "潜油泵报表";
		
		$Cell=array(
			array(display=> "设备编号", name=> "given_code"),
			array(display=> "设备名称", name=> "name"),
			array(display=> "设备类型", name=> "part_type"),
			array(display=> "站点编号", name=> "station_sn"),
			array(display=> "状态", name=> "status"),
		);
		
		// 默认条件
		$condition = ' 1=1 and is_delete = 0 ';
		// 动态条件区域
		
		// 站点编号
		if (strcmp(I('search_station_sn'), "") != 0) {
			$condition = $condition . ' and station_sn =  ' . '"' . I('search_station_sn') . '"';
		}
		// 设备名称
		if (strcmp(I('search_name'), "") != 0) {
			$condition = $condition . ' and name like  ' . '"%' . I('search_name') . '%"';
		}
		// 设备编号
		if (strcmp(I('search_given_code'), "") != 0) {
			$condition = $condition . ' and given_code like  ' . '"%' . I('search_given_code') . '%"';
		}		
		$list = M('st_submerged_pump')->where($condition)->limit($limit)->select();
		// 循环类型赋值
		
		for($i =0;$i<count($list);$i++){
			if(strcmp($list[$i]['status'],"WORK_STATUS_00") == '0'){
				 $list[$i]['status'] = "未使用";
			}else if(strcmp($list[$i]['status'],"WORK_STATUS_01") == '0'){
				 $list[$i]['status'] = "使用中";
			}else if(strcmp($list[$i]['status'],"WORK_STATUS_02") == '0'){
				 $list[$i]['status'] = "已损坏";
			}
		}

		exportExcelCount($xlsName, $xlsTitle, $Cell, $list);		
	}

}