/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-06-12
 * 描述            ：二级接口-轧机_L2_机组停机检测
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Mes.Rolling.Performance.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Mes.Rolling.Performance.mapper.IfRoll01L2StopMapper;
import market.Mes.Rolling.Performance.service.IfRoll01L2StopService;
import market.Mes.Rolling.Performance.vo.IfRoll01L2Stop;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

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 = "IfRoll01L2StopService")
public class IfRoll01L2StopServiceImpl extends BaseServiceImpl<IfRoll01L2Stop,Object> implements IfRoll01L2StopService {
	
	public ResultModel doQueryRollStop(CommonPage<IfRoll01L2Stop> cp) {
		ResultModel rm=new ResultModel();
		int pageIndex=cp.getPageIndex();
		int pageSize=cp.getPageSize();
		
		PageModel<IfRoll01L2Stop> pm = new PageModel<IfRoll01L2Stop>(pageIndex,pageSize);
		try {
			String startTime = cp.getStartTime().replace("-", "").replace(":", "").replace(" ", "");
			String endTime = cp.getEndTime().replace("-", "").replace(":", "").replace(" ", "");
			String condition = "";
			if (StringUtils.isNotEmpty(startTime)) {
				condition += " and time >= '"+startTime+"'";
			}
			if (StringUtils.isNotEmpty(endTime)) {
				condition += " and time <= '"+endTime+"'";
			}
			List<IfRoll01L2Stop> rlList = this.getMapper(IfRoll01L2StopMapper.class).findWithCondition(null, condition, "time desc", (pageIndex-1)*pageSize, pageSize);
			if (CollectionUtils.isNotEmpty(rlList)) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss");
				for (IfRoll01L2Stop rl : rlList) {
					//20210321082908 转 2021-03-21 08:29:08
					rl.setTime(sdf.format(sdf2.parse(rl.getTime())));
					if (rl.getCoil_no() != null && !"".equals(rl.getCoil_no())) {
						rl.setCoil_no(rl.getCoil_no().substring(0,rl.getCoil_no().length()-1));
					}
					
				}
			}
			pm.setRows(rlList);
			pm.setTotal(this.getMapper(IfRoll01L2StopMapper.class).countWithCondition(null, condition));
			rm.setState("200");
			rm.setData(pm);
		}
		catch(Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 调整停机信息
	 * @param cp
	 * @return
	 * @throws Exception 
	 */
	public ResultModel doUpdateRollStop(CommonPage<IfRoll01L2Stop> cp) throws Exception{
		ResultModel rm = new ResultModel();
		String opr_man = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date now = new Date();
		List<IfRoll01L2Stop> rlList = cp.getObjectList();
		if (CollectionUtils.isEmpty(rlList)) {
			throw new Exception("请传入参数");
		}
		try {			
			for (IfRoll01L2Stop rl : rlList) {
				rl.setOpr_man(opr_man);
				rl.setOpr_time(now);
				this.getMapper(IfRoll01L2StopMapper.class).update(rl);
			}
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
		return rm;
	}
	
	
}
