/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-02-26
 * 描述            ：产能井月产能设置表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.com.service.impl;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.com.mapper.ComBaseWellMonthMapper;
import market.Qms.com.service.ComBaseWellMonthService;
import market.Qms.com.vo.ComBaseWellMonth;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.service.support.PageModel;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path="ComBaseWellMonthService")
public class ComBaseWellMonthServiceImpl extends BaseServiceImpl<ComBaseWellMonth,Object> implements ComBaseWellMonthService {
	public ResultModel doQueryWeb(CommonPage<ComBaseWellMonth> sum) {
		ResultModel rm=new ResultModel();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		try{
			PageModel<ComBaseWellMonth> pm = new PageModel<ComBaseWellMonth>(pageIndex,pageSize);
			pm.setRows(this.getMapper(ComBaseWellMonthMapper.class).findWithCondition(sum.getObject(), null, " well_code ", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(ComBaseWellMonthMapper.class).count(sum.getObject()));
			rm.setState("200");
			rm.setMsgInfo("查询成功！");
			rm.setData(pm);
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
		
	}
	/**
	 * 添加数据
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doAddWeb(CommonPage<ComBaseWellMonth> sum) {
		ResultModel rm=new ResultModel();
		ComBaseWellMonth object = sum.getObject();
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {
			ComBaseWellMonth month=this.getMapper(ComBaseWellMonthMapper.class).findById(object.getWell_code(),object.getYear());
			if(month!=null){
				rm.setMsgInfo("已经存在此产能井的【"+object.getYear()+"】年份信息");
				rm.setState("403");
				return rm;
			}
			ComBaseWellMonth cbw =sum.getObject()==null?(new ComBaseWellMonth()):sum.getObject();
			cbw.setCreate_name(userName);
			cbw.setCreate_time(new Date());
			this.getMapper(ComBaseWellMonthMapper.class).insert(cbw);
			rm.setState("200");
			rm.setMsgInfo("新增成功！");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，新增失败!");
			e.printStackTrace();
		}
		return rm;
	}
	public ResultModel doupdate(CommonPage<ComBaseWellMonth> sum) {
		ResultModel rm=new ResultModel();
		ComBaseWellMonth object = sum.getObject();
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {
			ComBaseWellMonth cbw =sum.getObject()==null?(new ComBaseWellMonth()):sum.getObject();
			cbw.setUpdate_name(userName);
			cbw.setUpdate_time(new Date());
			this.getMapper(ComBaseWellMonthMapper.class).update(cbw);
			rm.setMsgInfo("修改成功！");
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，新增失败!");
			e.printStackTrace();
		}
		return rm;
	}
	/**
	 * 作废数据
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doInvaild(CommonPage<ComBaseWellMonth> oci) {
		ResultModel rm=new ResultModel();
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		List<ComBaseWellMonth> objectList = oci.getObjectList();
		Date date = new Date();
		try {
//			ComBaseWellMonth object = oci.getObject();
			for(ComBaseWellMonth object:objectList)
			{
			ComBaseWellMonth c = new ComBaseWellMonth();
			c.setWell_code(object.getWell_code());
			c.setYear(object.getYear());
			List<ComBaseWellMonth> list = this.getMapper(ComBaseWellMonthMapper.class).find(c, 0, 0);
			if(!list.isEmpty()){
				if(list.get(0).getValidflag().equals("0")){
					rm.setMsgInfo("该信息已经作废！");
					rm.setState("500");
					return rm;
				}
			}
			object.setDelete_name(userName);
			object.setDelete_time(date);
			object.setValidflag("0");
			this.getMapper(ComBaseWellMonthMapper.class).update(object);
			}
				rm.setState("200");	
			rm.setMsgInfo("作废成功！");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("修改失败!");
			e.printStackTrace();
		}
		return rm;
	}	
	
	/**
	 * 动作恢复
	 * @param bd
	 * @return
	 */
	public ResultModel doRecover(CommonPage<ComBaseWellMonth> oci) {
		ResultModel rm=new ResultModel();
		List<ComBaseWellMonth> objectList = oci.getObjectList();
		try {
//			ComBaseWellMonth object = oci.getObject();
			for(ComBaseWellMonth object:objectList){
			ComBaseWellMonth c = new ComBaseWellMonth();
			c.setWell_code(object.getWell_code());
			c.setYear(object.getYear());
			List<ComBaseWellMonth> list = this.getMapper(ComBaseWellMonthMapper.class).find(c, 0, 0);
			if(!list.isEmpty()){
				if(list.get(0).getValidflag().equals("1")){
					rm.setMsgInfo("该信息已经恢复！");
					rm.setState("500");
					return rm;
				}
			}
			this.getMapper(ComBaseWellMonthMapper.class).doRecovery(object.getWell_code(),object.getYear());
			}
				rm.setState("200");	
			rm.setMsgInfo("恢复成功！");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("修改失败!");
			e.printStackTrace();
		}
		return rm;
}
}
