/***文档注释***********************************************
 * 作者               :                   刘学
 * 创建日期      :                   2017.12.4
 * 描述               :        			质量检验项补充
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/
package market.Com.impl;

import java.util.Date;
import java.util.List;

import market.Com.mapper.ComBasePhyCMapper;
import market.Com.mapper.ComBasePhyItemMapper;
import market.Com.mapper.ComBasePhyRItemMapper;
import market.Com.service.ComBasePhyRItemService;
import market.Com.vo.ComBasePhyItem;
import market.Com.vo.ComBasePhyRItem;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;

import org.springframework.transaction.annotation.Transactional;

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

@RemoteService(path = "ComBasePhyRItemService")
public class ComBasePhyRItemServiceImpl extends BaseServiceImpl<ComBasePhyRItem, Object> implements ComBasePhyRItemService {
	
	/****
	 * 试样组关系管理——试样组查询
	 * @param cp
	 * @return
	 */
	public ResultModel doQueryPhyC(){
		ResultModel rm = new ResultModel();
		try {
			rm.setData(this.getMapper(ComBasePhyCMapper.class).doFind());
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！");
			e.printStackTrace();
		}
		return rm;
	}
	
	/****
	 * 试样组关系管理——查询方向和尺寸
	 * @return
	 */
	public ResultModel doQueryItem(){
		ResultModel rm = new ResultModel();
		String condition = "1=1";
		try {
			condition += " and VALIDFLAG = '1' and ITEM_STYLE in ('A','C')";
			rm.setData(this.getMapper(ComBasePhyItemMapper.class).findWithCondition(null, condition, null, 0, 0));
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！");
			e.printStackTrace();
		}
		return rm;
		
	}
	
	/********
	 * 试样组关系管理——查询
	 * @param cp
	 * 传入phyCodeM——试样组代码
	 * memo1（包含无效）: 0:不勾选 1：勾选
	 * @return
	 */
	public ResultModel doQueryPRIWeb(CommonPage<ComBasePhyRItem> cp){
		ResultModel rm = new ResultModel();
		ComBasePhyRItem cbp = cp.getObject();
		try {
			if ("0".equals(cp.getMemo1())) {
				rm.setData(this.getMapper(ComBasePhyRItemMapper.class).dofin(cbp.getPhyCodeM(), "1"));
			}else {
				rm.setData(this.getMapper(ComBasePhyRItemMapper.class).dofin(cbp.getPhyCodeM(), null));
			}
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！");
			e.printStackTrace();
		}
		return rm;
	}
	
	/***
	 * 试样组关系管理——保存
	 * @param cp
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doSavePRIWeb(CommonPage<ComBasePhyRItem> cp) throws Exception{
		ResultModel rm = new ResultModel();
		List<ComBasePhyRItem> cPhyRItems = cp.getObjectList();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			if (cPhyRItems != null) {
				for (ComBasePhyRItem comBasePhyRItem : cPhyRItems) {
					ComBasePhyRItem cbp = new ComBasePhyRItem();
					if ("".equals(comBasePhyRItem.getItemCodeD().trim()) || "".equals(comBasePhyRItem.getItemCodeS())) {
						rm.setState("500");
						rm.setMsgInfo("保存失败，试验方向与试验尺寸不能为空！");
						return rm;
					}
					//新增
					if (comBasePhyRItem.getOldItemCodeD() == null || "".equals(comBasePhyRItem.getOldItemCodeD())) {//没有老代码则新增
						int num = this.getMapper(ComBasePhyRItemMapper.class).getCount(comBasePhyRItem);
						if (num>0) {
							rm.setState("500");
							rm.setMsgInfo("新增失败，数据库中已经存在该数据！");
							throw new Exception("新增失败，数据库中已经存在该数据或者存在相同的无效数据！");
						}
						cbp.setCreateName(username);
						cbp.setCreateTime(new Date());
						cbp.setItemCodeD(comBasePhyRItem.getItemCodeD());
						cbp.setItemCodeS(comBasePhyRItem.getItemCodeS());
						cbp.setPhyCodeM(comBasePhyRItem.getPhyCodeM());
						cbp.setPhyCodeL(comBasePhyRItem.getPhyCodeL());
						cbp.setMemo(comBasePhyRItem.getMemo());
						cbp.setValidflag("1");
						this.getMapper(ComBasePhyRItemMapper.class).insert(cbp);
						rm.setState("200");
					}else {//修改
						//先删除这条数据
						ComBasePhyRItem cbpr =new ComBasePhyRItem();
						cbpr.setItemCodeD(comBasePhyRItem.getOldItemCodeD());
						cbpr.setItemCodeS(comBasePhyRItem.getOldItemCodeS());
						cbpr.setPhyCodeL(comBasePhyRItem.getPhyCodeL());
						cbpr.setPhyCodeM(comBasePhyRItem.getPhyCodeM());
						this.getMapper(ComBasePhyRItemMapper.class).doDelete(cbpr);
						//在插入数据
						int num = this.getMapper(ComBasePhyRItemMapper.class).getCount(comBasePhyRItem);
						if (num>0) {
							rm.setState("500");
							rm.setMsgInfo("修改失败，数据库中已经存在该数据！");
							throw new Exception("修改失败，数据库中已经存在该数据或者存在相同的无效数据！");
						}
						cbp.setCreateName(comBasePhyRItem.getCreateName());
						cbp.setCreateTime(comBasePhyRItem.getCreateTime());
						cbp.setItemCodeD(comBasePhyRItem.getItemCodeD());
						cbp.setItemCodeS(comBasePhyRItem.getItemCodeS());
						cbp.setPhyCodeM(comBasePhyRItem.getPhyCodeM());
						cbp.setPhyCodeL(comBasePhyRItem.getPhyCodeL());
						cbp.setMemo(comBasePhyRItem.getMemo());
						cbp.setValidflag(comBasePhyRItem.getValidflag());
						cbp.setUpdateName(username);
						cbp.setUpdateTime(new Date());
						this.getMapper(ComBasePhyRItemMapper.class).insert(cbp);
						rm.setState("200");
					}
				}
			}
		return rm;
	}
	
	/********
	 * 试样组关系管理——作废
	 * @param cp
	 * @return
	 */
	public ResultModel doCancelPRIWeb(CommonPage<ComBasePhyRItem> cp){
		ResultModel rm = new ResultModel();
		List<ComBasePhyRItem> cPhyRItems = cp.getObjectList();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {
			if (cPhyRItems != null) {
				for (ComBasePhyRItem comBasePhyRItem : cPhyRItems) {
					if ("0".equals(comBasePhyRItem.getValidflag())) {
						rm.setState("500");
						rm.setMsgInfo("无效数据无法作废，作废失败！");
						throw new Exception();
					}
					comBasePhyRItem.setDeleteName(username);
					comBasePhyRItem.setDeleteTime(new Date());
					comBasePhyRItem.setValidflag("0");
					this.getMapper(ComBasePhyRItemMapper.class).doCancel(comBasePhyRItem);
					rm.setState("200");
				}
			}else {
				rm.setState("500");
				rm.setMsgInfo("没有勾选作废的数据！");
			}
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("作废失败！");
			e.printStackTrace();
		}
		return rm;
	}
	
	/****
	 * 试样组关系管理——恢复
	 * @param cp
	 * @return
	 */
	public ResultModel doRecoverPRIWeb(CommonPage<ComBasePhyRItem> cp){
		ResultModel rm = new ResultModel();
		List<ComBasePhyRItem> cPhyRItems = cp.getObjectList();
		try {
			if (cPhyRItems != null) {
				for (ComBasePhyRItem comBasePhyRItem : cPhyRItems) {
					if ("1".equals(comBasePhyRItem.getValidflag())) {
						rm.setState("500");
						rm.setMsgInfo("有效数据无法恢复，恢复失败！");
						throw new Exception();
					}
					this.getMapper(ComBasePhyRItemMapper.class).doRecover(comBasePhyRItem);
					rm.setState("200");
				}
			}else {
				rm.setState("500");
				rm.setMsgInfo("没有勾选无效的数据！");
			}
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("恢复失败！");
			e.printStackTrace();
		}
		return rm;
	}
	//查询试样温度
	public ResultModel getTestSizeByTestDirection(String direction) {
		ResultModel rm = new ResultModel("200", "查询成功");
		try {
			List<ComBasePhyRItem> listcbpr = this.getMapper(
					ComBasePhyRItemMapper.class).getTestSizeByTestDirection(
					direction);
			rm.setData(listcbpr);
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setErrorInfo(e.getMessage());
		}
		return rm;
	}
	

	//根据方向和试样组查询
			public ResultModel getSizeByDirPL(String direction,String pl_code){
				ResultModel rm=new ResultModel("200","查询成功");
				try {
					List<ComBasePhyItem> listcbpr = this.getMapper(ComBasePhyItemMapper.class).getSizeByDirPL(direction,pl_code);
					rm.setData(listcbpr);
				} catch (Exception e) {
					// TODO: handle exception
					rm.setState("500");
					rm.setErrorInfo(e.getMessage());
				}
				return rm;
			}
}
