/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-02-02
 * 描述            ：制程炼钢牌号与成品规格对应表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
 ***********************************************************/
package market.Qms.metallurgy.impl;

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

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.metallurgy.mapper.ComMscGradeMapper;
import market.Qms.metallurgy.mapper.ComMscGradeRDeliverystateMapper;
import market.Qms.metallurgy.mapper.ComMscGradeRSpecMapper;
import market.Qms.metallurgy.service.ComMscGradeRSpecService;
import market.Qms.metallurgy.vo.ComMscGrade;
import market.Qms.metallurgy.vo.ComMscGradeRDeliverystate;
import market.Qms.metallurgy.vo.ComMscGradeRSpec;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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

@RemoteService(path = "ComMscGradeRSpecService")
public class ComMscGradeRSpecServiceImpl extends BaseServiceImpl<ComMscGradeRSpec, Object> implements ComMscGradeRSpecService {

	/****
	 * 炼钢牌号——查询与成品交货状态对应表
	 * 
	 * @param cp
	 * 传入参数——冶金规范码msc、全程产线号msc_pline
	 * @return
	 */
	public ResultModel doQueryGradeRDel(CommonPage<ComMscGrade> cp) {
		ResultModel rm = new ResultModel();
		ComMscGrade cmg = cp.getObject();
		String condition = "1=1 and MSC = '" + cmg.getMsc() + "' and MSC_PLINE = '"
		+ cmg.getMsc_pline() + "'";
		try {
			List<ComMscGrade> cGrades = this.getMapper(ComMscGradeMapper.class).findWithCondition(null, condition, null, 0, 0);
			if (cGrades != null || cGrades.size() != 0) {
				for (ComMscGrade comMscGrade : cGrades) {
					String conString = condition + " and GRADE_CODE = '" + comMscGrade.getGrade_code() + "'";
//					condition += " and GRADE_CODE = '" + comMscGrade.getGrade_code() + "'";
					comMscGrade.setCmgrDeliverystates(this.getMapper(ComMscGradeRDeliverystateMapper.class).findWithCondition(null, conString, null, 0, 0));
				}
			}
			rm.setData(cGrades);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！");
			e.printStackTrace();
		}
		return rm;
	}
	
	/*****
	 * 炼钢牌号——查询制程炼钢牌号与成品规格对应表
	 * @param cp
	 * memo1:查询有效无效
	 * 冶金规范码msc、全程产线号msc_pline、炼钢牌号代码grade_code
	 * @return
	 */
	public ResultModel doQueryGradeRSpec(CommonPage<ComMscGradeRSpec> cp){
		ResultModel rm = new ResultModel();
		ComMscGradeRSpec cmgrs = cp.getObject();
		String condition = "1=1";
		try {
			if ("0".equals(cp.getMemo1())) {
				condition += " and VALIDFLAG = '1'";
			}
			condition += " and MSC = '" + cmgrs.getMsc() + "' and MSC_PLINE = '"
			+ cmgrs.getMsc_pline() + "' and GRADE_CODE = '" + cmgrs.getGrade_code() + "'";
			rm.setData(this.getMapper(ComMscGradeRSpecMapper.class).findWithCondition(null, condition, null, 0, 0));
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！");
			e.printStackTrace();
		}
		return rm;
	}

	/*****
	 * 炼钢牌号——确认——保存
	 * @param cp
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doSave(CommonPage<ComMscGrade> cp) {
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		List<ComMscGrade> cmgs = cp.getObjectList();
		try {
			if (cmgs != null) {
				for (ComMscGrade cmg : cmgs) {
					// 保存全程产线号与炼钢牌号对应关系
					if (cmg != null) {
						String condition = "1=1 and MSC = '" + cmg.getMsc() + "' and MSC_PLINE = '" + cmg.getMsc_pline()
								+ "' and GRADE_CODE = '" + cmg.getGrade_code() + "'";
						int num = this.getMapper(ComMscGradeMapper.class).countWithCondition(null, condition);
						if (num != 0) {
							cmg.setUpdate_name(userName);
							cmg.setUpdate_time(new Date());
							this.getMapper(ComMscGradeMapper.class).update(cmg);
							rm.setState("200");
						} else {
							cmg.setCreate_name(userName);
							cmg.setCreate_time(new Date());
							cmg.setValidflag("1");
							this.getMapper(ComMscGradeMapper.class).insert(cmg);
							rm.setState("200");
						}
					}
					// 保存制程炼钢牌号与成品规格对应关系
					List<ComMscGradeRSpec> cmSpecs = cmg.getCmgrSpecs();
					if (cmSpecs != null) {
						for (ComMscGradeRSpec gradeRSpec : cmSpecs) {
//							gradeRSpec.setGrade_code(cmg.getGrade_code());
//							gradeRSpec.setMsc(cmg.getMsc());
//							gradeRSpec.setMsc_pline(cmg.getMsc_pline());
							String condition = "1=1";
							if (gradeRSpec.getSeq() != null) {
								condition += " and THICK_MIN_SIGN = '" + gradeRSpec.getThick_min_sign() + "' and THICK_MIN = '"
							+ gradeRSpec.getThick_min() + "' and THICK_MAX_SIGN = '" + gradeRSpec.getThick_max_sign()
							+ "' and THICK_MAX = '" + gradeRSpec.getThick_max() + "' and WIDTH_MIN_SIGN = '" + gradeRSpec.getWidth_min_sign() 
							+ "' and WIDTH_MIN = '" + gradeRSpec.getWidth_min() + "' and WIDTH_MAX_SIGN = '" + gradeRSpec.getWidth_max_sign()
							+ "' and WIDTH_MAX = '" + gradeRSpec.getWidth_max() + "' and SEQ <> '" + gradeRSpec.getSeq() + "'";
								int count = this.getMapper(ComMscGradeRSpecMapper.class).countWithCondition(null, condition);
								if (count != 0) {
									rm.setState("500");
									rm.setMsgInfo("修改失败，已经存在相同的适用规格！");
									throw new Exception("修改失败，已经存在相同的适用规格！");
								}
								gradeRSpec.setUpdate_name(userName);
								gradeRSpec.setUpdate_time(new Date());
								this.getMapper(ComMscGradeRSpecMapper.class).update(gradeRSpec);
							}else {
								condition += " and THICK_MIN_SIGN = '" + gradeRSpec.getThick_min_sign() + "' and THICK_MIN = '"
										+ gradeRSpec.getThick_min() + "' and THICK_MAX_SIGN = '" + gradeRSpec.getThick_max_sign()
										+ "' and THICK_MAX = '" + gradeRSpec.getThick_max() + "' and WIDTH_MIN_SIGN = '" + gradeRSpec.getWidth_min_sign() 
										+ "' and WIDTH_MIN = '" + gradeRSpec.getWidth_min() + "' and WIDTH_MAX_SIGN = '" + gradeRSpec.getWidth_max_sign()
										+ "' and WIDTH_MAX = '" + gradeRSpec.getWidth_max() + "'";
											int count = this.getMapper(ComMscGradeRSpecMapper.class).countWithCondition(null, condition);
											if (count != 0) {
												rm.setState("500");
												rm.setMsgInfo("新增失败，已经存在相同的适用规格！");
												throw new Exception("新增失败，已经存在相同的适用规格！");
											}
								long seq = this.getMapper(ComMscGradeRSpecMapper.class).getSeq();
								gradeRSpec.setSeq(seq);
								gradeRSpec.setCreate_name(userName);
								gradeRSpec.setCreate_time(new Date());
								gradeRSpec.setValidflag("1");
								this.getMapper(ComMscGradeRSpecMapper.class).insert(gradeRSpec);
							}
						}
					}
					// 保存炼钢牌号与成品交货状态对应关系
					List<ComMscGradeRDeliverystate> cmDeliverystates = cmg.getCmgrDeliverystates();
					ComMscGradeRDeliverystate cmgrDeliverystate = new ComMscGradeRDeliverystate();
					cmgrDeliverystate.setGrade_code(cmg.getGrade_code());
					cmgrDeliverystate.setMsc(cmg.getMsc());
					cmgrDeliverystate.setMsc_pline(cmg.getMsc_pline());
					this.getMapper(ComMscGradeRDeliverystateMapper.class).doDelete(cmgrDeliverystate);
					if (cmDeliverystates != null) {
						for (ComMscGradeRDeliverystate deliverystate : cmDeliverystates) {
//							deliverystate.setMsc(cmg.getMsc());
//							deliverystate.setMsc_pline(cmg.getMsc_pline());
							deliverystate.setGrade_code(cmg.getGrade_code());
							this.getMapper(ComMscGradeRDeliverystateMapper.class).insert(deliverystate);
						}
					}
				}
			}
			
			rm.setState("200");
			rm.setMsgInfo("保存成功！");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("保存失败！");
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚
		}
		return rm;
	}
	
	/****
	 * 炼钢牌号——确认——作废
	 * @param cp
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doCancel(CommonPage<ComMscGradeRSpec> cp){
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		List<ComMscGradeRSpec> cSpecs = cp.getObjectList();
		try {
			if (cSpecs == null || cSpecs.size() == 0) {
				rm.setState("500");
				rm.setMsgInfo("请选择数据！");
				return rm;
			}
			for (ComMscGradeRSpec comMscGradeRSpec : cSpecs) {
				ComMscGradeRSpec cSpec = this.getMapper(ComMscGradeRSpecMapper.class).findBySeq(comMscGradeRSpec.getSeq());
				if ("0".equals(cSpec.getValidflag())) {
					rm.setState("500");
					rm.setMsgInfo("无效数据无法作废！");
					throw new Exception("无效数据无法作废！");
				}
				cSpec.setDelete_name(userName);
				cSpec.setDelete_time(new Date());
				cSpec.setValidflag("0");
				this.getMapper(ComMscGradeRSpecMapper.class).update(cSpec);
			}
			rm.setState("200");
			rm.setMsgInfo("作废成功！");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("作废失败！");
			e.printStackTrace();
		}
		return rm;
	}
	
	/*****
	 * 炼钢牌号——确认——恢复
	 * @param cp
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doRecover(CommonPage<ComMscGradeRSpec> cp){
		ResultModel rm = new ResultModel();
		List<ComMscGradeRSpec> cSpecs = cp.getObjectList();
		try {
			if (cSpecs == null || cSpecs.size() == 0) {
				rm.setState("500");
				rm.setMsgInfo("请选择数据！");
				return rm;
			}
			for (ComMscGradeRSpec comMscGradeRSpec : cSpecs) {
				ComMscGradeRSpec cSpec = this.getMapper(ComMscGradeRSpecMapper.class).findBySeq(comMscGradeRSpec.getSeq());
				if ("1".equals(cSpec.getValidflag())) {
					rm.setState("500");
					rm.setMsgInfo("有效数据无法恢复！");
					throw new Exception("有效数据无法恢复！");
				}
				this.getMapper(ComMscGradeRSpecMapper.class).doRecover(cSpec);
			}
			rm.setState("200");
			rm.setMsgInfo("恢复成功！");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("恢复失败！");
			e.printStackTrace();
		}
		return rm;
	}

}
