package market.Qms.com.service.impl;

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

import market.Qms.com.mapper.BaseGradeChemMapper;
import market.Qms.com.mapper.BaseGradeMapper;
import market.Qms.com.service.BaseGradeService;
import market.Qms.com.vo.BaseGrade;
import market.Qms.common.DataHelper;
import market.Qms.common.RequestModel;
import market.Qms.common.ReturnMsg;
import market.Qms.metallurgy.impl.ComBaseGradeRPlineServiceImpl;
import market.Qms.metallurgy.impl.ComBaseGradeRSteelServiceImpl;
import market.Qms.metallurgy.mapper.ComBaseGradeRPlineMapper;
import market.Qms.metallurgy.mapper.ComBaseGradeRSteelMapper;
import market.Qms.metallurgy.vo.ComBaseGradeRPline;
import market.Qms.metallurgy.vo.ComBaseGradeRSteel;

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(serviceInterface=BaseGradeService.class)
public class BaseGradeServiceImpl extends BaseServiceImpl<BaseGrade, String> implements BaseGradeService {

	@Override
	public ReturnMsg<List<BaseGrade>> GetGrade(RequestModel<BaseGrade> steel) {
		// TODO Auto-generated method stub
		ReturnMsg<List<BaseGrade>> msg=new ReturnMsg<>();
		List<BaseGrade> list=new ArrayList<>();
		try{
			BaseGradeMapper mapper=this.getMapper(BaseGradeMapper.class);
			String condition="";
			if(steel.getModel()!=null&&!DataHelper.IsNullOrSpace(steel.getModel().getGradeName())){
				condition=" LOWER(GRADE_NAME) like '%'||LOWER('"+steel.getModel().getGradeName()+"')||'%'";
				steel.getModel().setGradeName(null);
			}
			

			list=mapper.findWithCondition(steel.getModel(), condition, "GRADE_CODE DESC", steel.getSkipResults(), steel.getPageSize());
			int count=mapper.countWithCondition(steel.getModel(),condition);
			List<String> listStr=new ArrayList<>();
			for (BaseGrade grade : list) {
				listStr.add(grade.getGradeCode());
			}
			condition=DataHelper.CreateSqlCondition(listStr, "grade_code");
			List<ComBaseGradeRPline> listPline=this.getMapper(ComBaseGradeRPlineMapper.class).findWithCondition(null, condition, null, 0, 0);
			List<ComBaseGradeRSteel> listSteel=this.getMapper(ComBaseGradeRSteelMapper.class).findWithCondition(null, condition, null, 0, 0);
			
			for (BaseGrade grade : list) {
				String codes="";
				String names="";
				for (ComBaseGradeRPline _pline : listPline) {
					if(grade.getGradeCode().equals(_pline.getGrade_code())){
						codes+=","+_pline.getPline_code();
						names+=","+_pline.getPline_name();
					}
				}
				grade.setPline_codes(codes.replaceFirst(",", ""));
				grade.setPline_names(names.replaceFirst(",", ""));
				codes="";
				names="";
				for (ComBaseGradeRSteel _steel : listSteel) {
					if(grade.getGradeCode().equals(_steel.getGrade_code())){
						codes+=","+_steel.getSteel_code();
						names+=","+_steel.getSteel_name();
					}
				}
				grade.setSteel_codes(codes.replaceFirst(",", ""));
				grade.setSteel_names(names.replaceFirst(",", ""));
			}
			
			msg.setTotal(count);
			msg.setMessage("获取数据成功");
			msg.setSuccess(true);
			msg.setResult(list);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		
		return msg;
	}

	@Override
	public ReturnMsg DelOrRenewGrade(String deleteName,String gradeCode, Boolean flag) {
		// TODO Auto-generated method stub
		ReturnMsg msg=new ReturnMsg<>();
		try{
			// TODO Auto-generated method stub
			BaseGrade steel=new BaseGrade();
			steel.setGradeCode(gradeCode);
			steel.setValidflag(flag?"0":"1");
			if(flag){
				steel.setDeleteTime(new Date());
				steel.setDeleteName(deleteName);
				this.getMapper(BaseGradeChemMapper.class).DelOrRenewSteelChem("0", gradeCode);
				this.getMapper(BaseGradeMapper.class).update(steel);
				//修改俩子表的状态
				this.getMapper(ComBaseGradeRSteelMapper.class).UpdateSteel("0",gradeCode);
				this.getMapper(ComBaseGradeRSteelMapper.class).UpdateSteel("0",gradeCode);
				
			}else{
				steel.setUpdateTime(new Date());
				steel.setUpdateName(deleteName);
				this.getMapper(BaseGradeChemMapper.class).DelOrRenewSteelChem("1", gradeCode);
				this.getMapper(BaseGradeMapper.class).RenewSteel(steel);
				//修改俩子表的状态
				this.getMapper(ComBaseGradeRSteelMapper.class).UpdateSteel("1",gradeCode);
				this.getMapper(ComBaseGradeRSteelMapper.class).UpdateSteel("1",gradeCode);
			}
			
			msg.setMessage(flag?"废除成功":"恢复成功");
			msg.setSuccess(true);
		}catch(Exception ex){
			msg.setMessage(ex.getMessage());
			msg.setSuccess(false);
		}
		return msg;
	}
	
	@Override
	public ReturnMsg<BaseGrade> SaveGrade(BaseGrade grad) {
		// TODO Auto-generated method stub
		ReturnMsg<BaseGrade> msg=new ReturnMsg<>();
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		msg.setResult(grad);
		try{
			BaseGradeMapper mapper=this.getMapper(BaseGradeMapper.class);
			BaseGrade _grade=new BaseGrade();
			_grade.setGradeName(grad.getGradeName());
			List<BaseGrade> listGrad=mapper.find(_grade, 0, 0);
			if(listGrad.size()>0&&!listGrad.get(0).getGradeCode().equals(grad.getGradeCode())){
				msg.setMessage("已经存在相同名称的炼钢牌号");
				msg.setSuccess(false);
				return msg;
			}
			
			
			if(DataHelper.IsNullOrSpace(grad.getGradeCode())){
				String maxCode=mapper.GetMaxCode();
				String code=DataHelper.InitSerial(maxCode, "GZ", 6);
				grad.setGradeCode(code);
				grad.setCreateName(userName);
				grad.setCreateTime(new Date());
				mapper.insert(grad);
			}else{
				grad.setUpdateName(userName);
				grad.setUpdateTime(new Date());
				mapper.update(grad);
			}
			msg.setMessage("保存数据成功");
			msg.setSuccess(true);
			msg.setResult(grad);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		return msg;
	}
	
	//对三个表进行新增
	@Transactional(value="tm", rollbackFor=Exception.class)
	@Override
	public ReturnMsg<BaseGrade> SaveGradeSteel(BaseGrade grad,
			List<ComBaseGradeRPline> Plinelist, List<ComBaseGradeRSteel> Steellist) {
		
		ReturnMsg<BaseGrade> msg=new ReturnMsg<>();
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		msg.setResult(grad);
		try{
			BaseGradeMapper mapper=this.getMapper(BaseGradeMapper.class);
			BaseGrade _grade=new BaseGrade();
			_grade.setGradeName(grad.getGradeName());
			List<BaseGrade> listGrad=mapper.find(_grade, 0, 0);
			if(listGrad.size()>0&&!listGrad.get(0).getGradeCode().equals(grad.getGradeCode())){
				msg.setMessage("已经存在相同名称的炼钢牌号");
				msg.setSuccess(false);
				return msg;
			}
			
			
			if(DataHelper.IsNullOrSpace(grad.getGradeCode())){
				String maxCode=mapper.GetMaxCode();
				String code=DataHelper.InitSerial(maxCode, "GZ", 6);
				grad.setGradeCode(code);
				grad.setCreateName(userName);
				grad.setCreateTime(new Date());
				mapper.insert(grad);
				
				//两个子表的多条记录新增
				boolean count=false;
				boolean counttwo=false;
				count=this.getBean(ComBaseGradeRPlineServiceImpl.class).doSaveGradeRPlinexz(Plinelist, code);
				counttwo=this.getBean(ComBaseGradeRSteelServiceImpl.class).doSaveGradeRSteelxz(Steellist,code);
				if(count && counttwo){
					msg.setMessage("保存数据成功");
				}
			}else{
				grad.setUpdateName(userName);
				grad.setUpdateTime(new Date());
				mapper.update(grad);
				
				//两个子表的多条记录新增
				boolean count=false;
				boolean counttwo=false;
				count=this.getBean(ComBaseGradeRPlineServiceImpl.class).doSaveGradeRPlinexz(Plinelist,grad.getGradeCode());
				counttwo=this.getBean(ComBaseGradeRSteelServiceImpl.class).doSaveGradeRSteelxz(Steellist,grad.getGradeCode());
				if(count && counttwo){
					msg.setMessage("保存数据成功");
				}
			}
			
			msg.setMessage("保存数据成功");
			msg.setSuccess(true);
			msg.setResult(grad);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		return msg;
	}
	
		//对三个表进行修改
		@Transactional(value="tm", rollbackFor=Exception.class)
		@Override
		public ReturnMsg<BaseGrade> UpdateSaveGradeSteel(BaseGrade grad,
				List<ComBaseGradeRPline> Plinelist, List<ComBaseGradeRSteel> Steellist) {
			
			ReturnMsg<BaseGrade> msg=new ReturnMsg<>();
			String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			msg.setResult(grad);
			try{
				BaseGradeMapper mapper=this.getMapper(BaseGradeMapper.class);
				BaseGrade _grade=new BaseGrade();
				_grade.setGradeName(grad.getGradeName());
				
				if(!DataHelper.IsNullOrSpace(grad.getGradeCode())){
					grad.setUpdateName(userName);
					grad.setUpdateTime(new Date());
					mapper.update(grad);
					
					//两个子表的多条记录新增
					boolean count=false;
					boolean counttwo=false;
					count=this.getBean(ComBaseGradeRPlineServiceImpl.class).doSaveGradeRPlinexz(Plinelist,grad.getGradeCode());
					counttwo=this.getBean(ComBaseGradeRSteelServiceImpl.class).doSaveGradeRSteelxz(Steellist,grad.getGradeCode());
					if(count && counttwo){
						msg.setMessage("数据修改成功");
						msg.setSuccess(true);
						msg.setResult(grad);
					}else{
						msg.setMessage("数据修改失败");
						msg.setSuccess(false);
					}
				}else{
					msg.setMessage("获取数据失败");
					msg.setSuccess(false);
				}
				
				msg.setMessage("数据修改成功");
				msg.setSuccess(true);
			}catch(Exception ex){
				msg.setSuccess(false);
				msg.setMessage(ex.getMessage());
			}
			return msg;
		}

}
