/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-07-21
 * 描述            ：复合牌号基础表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.qcm.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.qcm.mapper.ComBaseGradeCompDMapper;
import market.Qms.qcm.mapper.ComBaseGradeCompMMapper;
import market.Qms.qcm.service.ComBaseGradeCompMService;
import market.Qms.qcm.vo.ComBaseGradeCompD;
import market.Qms.qcm.vo.ComBaseGradeCompM;

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

@RemoteService(path="ComBaseGradeCompMService")
public class ComBaseGradeCompMServiceImpl extends BaseServiceImpl<ComBaseGradeCompM,Object> implements ComBaseGradeCompMService {
	/**
	 * 查询复合牌号信息
	 */
	public ResultModel getMSample(CommonPage<ComBaseGradeCompM> sum){
		ResultModel rm=new ResultModel();
		ComBaseGradeCompM cbgm=sum.getObject();
		String connect="";
		List<ComBaseGradeCompM> listcbgm=new ArrayList();
		try {
            if (cbgm.getComp_grade_name()!=null&&!cbgm.getComp_grade_name().equals("")) {
				connect+="COMP_GRADE_NAME like '%"+cbgm.getComp_grade_name()+"%'";
			}
            if (cbgm.getValidflag()!=null&&!cbgm.getValidflag().equals("")&&cbgm.getValidflag().equals("0")) {
            	listcbgm=this.getMapper(ComBaseGradeCompMMapper.class).findWithCondition(null, connect, null, 0, 0);
			}else {
				listcbgm=this.getMapper(ComBaseGradeCompMMapper.class).findWithCondition(null, connect+" and VALIDFLAG='1'", null, 0, 0);
			}
            rm.setData(listcbgm);
            rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("查询信息失败");
			rm.setErrorInfo(e.getMessage());
		}
		return rm;
	}
	/**
	 * 作废复合牌号信息
	 */
	public 	ResultModel toVoidMsmaple(CommonPage<ComBaseGradeCompM> sum){
		ResultModel rm=new ResultModel();
		List<ComBaseGradeCompM> listcbgm=sum.getObjectList();
		try {
			for (ComBaseGradeCompM cbgm : listcbgm) {
				  cbgm.setValidflag("0");
				  this.getMapper(ComBaseGradeCompMMapper.class).update(cbgm);
				  this.getMapper(ComBaseGradeCompDMapper.class).toVoidDsample(cbgm.getComp_grade_code());
			}
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("作废信息失败");
			rm.setErrorInfo(e.getMessage());
		}
		
		return rm;
	}
	/**
	 * 恢复复合牌号信息
	 * @param sum
	 * @return
	 */
	public 	ResultModel recoveryMsmaple(CommonPage<ComBaseGradeCompM> sum){
		ResultModel rm=new ResultModel();
		List<ComBaseGradeCompM> listcbgm=sum.getObjectList();
		try {
			for (ComBaseGradeCompM cbgm : listcbgm) {
				  cbgm.setValidflag("1");
				  this.getMapper(ComBaseGradeCompMMapper.class).update(cbgm);
				  this.getMapper(ComBaseGradeCompDMapper.class).recovryDSample(cbgm.getComp_grade_code());;
			}
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("恢复信息失败");
			rm.setErrorInfo(e.getMessage());
		}
		
		return rm;
	}
	public ResultModel saveMsample(ComBaseGradeCompM cbg,List<ComBaseGradeCompD> listcbgd){
		ResultModel rm=new ResultModel();
		ComBaseGradeCompM cbgm=cbg;
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {
			//判断是否已经存在复合牌号代码
			//获取最大复合牌号代码
			int count=0;
			List<ComBaseGradeCompM> qrlist=this.getMapper(ComBaseGradeCompMMapper.class)
					.findWithCondition(null, null, "COMP_GRADE_CODE DESC", 0, 1);
			if(qrlist!=null&&qrlist.size()>0){
				String s=qrlist.get(0).getComp_grade_code();
				count=Integer.parseInt(s.substring(s.length()-5, s.length()));
			}
			count++;
			String code="CP"+String.format("%05d",count);
			System.out.println(code);
			if (cbgm.getComp_grade_code()==null||cbgm.getComp_grade_code().equals("")) {
				cbgm.setComp_grade_code(code);
				cbgm.setCreate_name(username);
				cbgm.setCreate_time(new Date());
				this.getMapper(ComBaseGradeCompMMapper.class).insert(cbgm);
			}
		    int a=0;
		    int b=0;
			//根据生成的复合牌号最大值把信息插入COM_BASE_GRADE_COMP_D表
			if (listcbgd.size()>0) {
				for (ComBaseGradeCompD cbgd : listcbgd) {
					if (cbg.getComp_grade_code()==null||cbg.getComp_grade_code().equals("")) {
						if (a==0) {
							this.getMapper(ComBaseGradeCompDMapper.class).deleteBycode(code);
							a++;
						}
						cbgd.setComp_grade_code(code);
					}else {
						if (b==0) {
							this.getMapper(ComBaseGradeCompDMapper.class).deleteBycode(cbg.getComp_grade_code());
						      b++;
						}
						cbgd.setComp_grade_code(cbg.getComp_grade_code());
					}
					cbgd.setCreate_name(username);
					cbgd.setCreate_time(new Date());
					this.getMapper(ComBaseGradeCompDMapper.class).insert(cbgd);
				}
			}
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("保存信息失败");
			rm.setErrorInfo(e.getMessage());
		}
		return rm;
	}
}
