/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2020-07-08
 * 描述            ：操作日志表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Slm.BaseInformation.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import market.Com.pb.PbMethod;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.BaseInformation.mapper.SlmOperateLogMapper;
import market.Slm.BaseInformation.service.SlmOperateLogService;
import market.Slm.BaseInformation.vo.SlmOperateLog;

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

@RemoteService(path="SlmOperateLogService")
public class SlmOperateLogServiceImpl extends BaseServiceImpl<SlmOperateLog,Object> implements SlmOperateLogService {

	
	/*
	 * 根据交接班的机组，作业日期，班次，班组获取当班的平整机换辊记录
	 * 20230224刘侣
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel getHgLog (CommonPage<SlmOperateLog> sum) {
		ResultModel rm=new ResultModel("200", "查询成功");
		SlmOperateLog log = sum.getObject();
		String condition = " (WRK_DATE = '"+ log.getWrk_date() +"' and WRK_ORD = '"+ log.getWrk_ord() +"' and WRK_GRP = '"+ log.getWrk_grp() +"')" +
        " or (DOWN_MACHINE_TIME is null and ((WRK_DATE = '" + log.getWrk_date() + "' and WRK_ORD <= '" + log.getWrk_ord() + "') or WRK_DATE < '" + log.getWrk_date() + "'))" +
        " or (DOWN_MACHINE_TIME is not null and update_msg = '"+ log.getWrk_date() + log.getWrk_ord() + log.getWrk_grp() +"')";
		List<SlmOperateLog> logs = this.getMapper(SlmOperateLogMapper.class).findWithCondition(null, condition, "CREATE_TIME", 0, 0);
		rm.setData(logs);
		return rm;
	}
	
	/*
	 * 根据交接班的机组，作业日期，班次，班组获取当班的平整机换辊记录
	 * 20230224刘侣
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doInsertHgLog (CommonPage<SlmOperateLog> sum) throws Exception {
		ResultModel rm=new ResultModel("200", "新增成功");
		Date now = new Date();
//		String opr_time = new SimpleDateFormat("yyyyMMddHHmmss").format(now); //操作时间
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		
		try {
			String id=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+PbMethod.RandomCreate(6);
			SlmOperateLog log = sum.getObject();
			String logPk = sum.getMemo1();
			// 如果logpk存在，那么执行更新，而不是执行插入：
			if (logPk != null) {
				List<SlmOperateLog> prevLog = this.getMapper(SlmOperateLogMapper.class).findWithCondition(null, " LOG_PK = " + logPk, null, 0, 1);
				if (prevLog != null && prevLog.size() > 0) {
					// 执行更新：
					log.setLog_pk(logPk);
					log.setUpdate_man(username);
					log.setUpdate_time(now);
					log.setUpdate_msg(log.getWrk_date() + log.getWrk_ord() + log.getWrk_grp());
					this.getMapper(SlmOperateLogMapper.class).doUpdate(log);
					rm.setMsgInfo("修改成功；");
				} else {
					throw new Exception("未找到logpk为：" + logPk + " 的数据");
				}
			} else {
				// 执行插入：
				log.setLog_pk(id);
				log.setCreate_man(username);
				log.setCreate_time(now);
				this.getMapper(SlmOperateLogMapper.class).insert(log);
//				rm.setMsgInfo("新增完成；");
			}
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，新增平整机换辊记录失败!"+e.getMessage());
			e.printStackTrace();
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		
		return rm;
	}
	
	/*
	 * 根据交接班的机组，作业日期，班次，班组获取当班的平整机换辊记录
	 * 20230224刘侣
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doDeleteHgLog (CommonPage<SlmOperateLog> sum) throws Exception {
		ResultModel rm=new ResultModel("200", "删除成功");
		Date now = new Date();
//		String opr_time = new SimpleDateFormat("yyyyMMddHHmmss").format(now); //操作时间
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		
		try {
			SlmOperateLog log = sum.getObject();
			if (log.getLog_pk() == null) {
				throw new Exception("数据主键为空，请单击选择一行删除");
			}
//			log.setDelete_man(username);
//			log.setDelete_time(new Date());
			this.getMapper(SlmOperateLogMapper.class).delete(log.getLog_pk());
			
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，删除平整机换辊记录失败!"+e.getMessage());
			e.printStackTrace();
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		
		return rm;
	}
	
	
	
}
