package market.Qms.com.service.impl;
import java.util.Date;
import java.util.List;

import market.Qms.com.mapper.BaseAdditionalMapper;
import market.Qms.com.mapper.BaseStdAlphaMapper;
import market.Qms.com.mapper.BaseStdMapper;
import market.Qms.com.mapper.BaseStdRSpecMapper;
import market.Qms.com.mapper.PscMapper;
import market.Qms.com.service.BaseStdService;
import market.Qms.com.vo.BaseStd;
import market.Qms.com.vo.BaseStdAlpha;
import market.Qms.com.vo.Psc;
import market.Qms.common.DataHelper;
import market.Qms.common.RequestModel;
import market.Qms.common.ReturnMsg;

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

@RemoteService(serviceInterface=BaseStdService.class)
public class BaseStdServiceImpl extends BaseServiceImpl<BaseStd, String> implements BaseStdService {

	@Override
	public ReturnMsg<List<BaseStd>> GetAssayStd(RequestModel<BaseStd> baseStd) {
		// TODO Auto-generated method stub
		ReturnMsg<List<BaseStd>> msg=new ReturnMsg<>();
		try{
			BaseStdMapper mapper=this.getMapper(BaseStdMapper.class);
			if(baseStd.getModel()==null){
				baseStd.setModel(new BaseStd());
			}
			String condition=" STD_TYPE in ('S','P','T')";
			
			if(baseStd.getModel()!=null&&!DataHelper.IsNullOrSpace(baseStd.getModel().getStdName())){
				condition+=" and LOWER(STD_NAME) like '%'||LOWER('"+baseStd.getModel().getStdName()+"')||'%'";
				baseStd.getModel().setStdName(null);
			}
			List<BaseStd> list=mapper.findWithCondition(baseStd.getModel(), condition, "STD_CODE DESC", baseStd.getSkipResults(), baseStd.getPageSize());
			int count=mapper.countWithCondition(baseStd.getModel(),condition);
			msg.setTotal(count);
			msg.setMessage("获取数据成功");
			msg.setSuccess(true);
			msg.setResult(list);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		return msg;
	}

	private void CreatStdAlpah(BaseStd baseStd){
		BaseStdAlpha stdAlphaEntity=new BaseStdAlpha();
		stdAlphaEntity.setAlphaCode(baseStd.getStdCode() + "000");
		stdAlphaEntity.setAlphaName("/");
		stdAlphaEntity.setCreateName(baseStd.getCreateName());
		stdAlphaEntity.setDeptCode(baseStd.getDeptCode());
		stdAlphaEntity.setDeptDesc(baseStd.getDeptDesc());
		stdAlphaEntity.setStdCode(baseStd.getStdCode());
		stdAlphaEntity.setSectionCode(baseStd.getSectionCode());
		stdAlphaEntity.setSectionDesc(baseStd.getSectionName());
		this.getMapper(BaseStdAlphaMapper.class).insert(stdAlphaEntity);
		
	}
	
/*	@Resource(name="tm")
	private DataSourceTransactionManager transactionManager;

	private void transactionManagers(){
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); // 事物隔离级别，开启新事务
		TransactionStatus status = transactionManager.getTransaction(def); // 获得事务状态
		try {
		    //逻辑代码
		    transactionManager.commit(status);
		} catch (Exception e) {
		    transactionManager.rollback(status);
		}	
		
	}*/
	
	@Override
	public ReturnMsg<BaseStd> SaveStd(BaseStd baseStd) {
		// TODO Auto-generated method stub
		ReturnMsg<BaseStd> msg=new ReturnMsg<>();
		msg.setResult(baseStd);
		try{
			//12.24字段限制长度
			if (baseStd==null) {
				msg.setMessage("请传入参数");
				msg.setSuccess(false);
				msg.setResult(baseStd);
				return msg;
			}
			
			
			if (baseStd.getMemo()!=null && !"".equals(baseStd.getMemo())) {
				String memo=baseStd.getMemo();
				//System.out.println(memo.length());
				if (memo.length()>200) {
					msg.setMessage("此标准备注信息超长");
					msg.setSuccess(false);
					msg.setResult(baseStd);
					return msg;
					
				}
			}
			
			
			BaseStdMapper mapper=this.getMapper(BaseStdMapper.class);
			BaseStd _baseStd=new BaseStd();
			_baseStd.setStdName(baseStd.getStdName());
			List<BaseStd> _list=mapper.find(_baseStd, 0, 0);
			if(_list.size()>0&&!_list.get(0).getStdCode().equals(baseStd.getStdCode())){
				msg.setMessage("此标准名称已存在");
				msg.setSuccess(false);
				msg.setResult(baseStd);
				return msg;
			}
			
			if(DataHelper.IsNullOrSpace(baseStd.getStdCode())){
				String maxCode=mapper.GetMaxCode();
				String code=DataHelper.InitSerial(maxCode, "STD", 10);
				baseStd.setStdCode(code);
				baseStd.setCreateTime(new Date());
				mapper.insert(baseStd);
				if(baseStd.getStdType().equals("X")||baseStd.getStdType().equals("G")){
					//默认新增一条用途码
					CreatStdAlpah(baseStd);
				}
			}else{
				baseStd.setUpdateTime(new Date());
				mapper.update(baseStd);
			}
			msg.setMessage("保存数据成功");
			msg.setSuccess(true);
			msg.setResult(baseStd);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		return msg;
	}

	@Override
	public ReturnMsg DelOrRenew(String deleteName,String stdCode, Boolean flag) {
		// TODO Auto-generated method stub
		ReturnMsg msg=new ReturnMsg<>();
		try{
			// TODO Auto-generated method stub
			BaseStd std=new BaseStd();
			std.setStdCode(stdCode);
			std.setValidflag(flag?"0":"1");
			if(flag){
				std.setDeleteTime(new Date());
				std.setDeleteName(deleteName);
			}else{
				std.setUpdateTime(new Date());
				std.setUpdateName(deleteName);
			}
			this.getMapper(BaseStdMapper.class).update(std);
			msg.setMessage(flag?"废除成功":"恢复成功");
			msg.setSuccess(true);
		}catch(Exception ex){
			msg.setMessage(ex.getMessage());
			msg.setSuccess(false);
		}
		return msg;
	}

	@Override
	public ReturnMsg<List<BaseStd>> GetUsuallyStd(RequestModel<BaseStd> baseStd) {
		ReturnMsg<List<BaseStd>> msg=new ReturnMsg<>();
		try{
			BaseStdMapper mapper=this.getMapper(BaseStdMapper.class);
			String condition=" STD_TYPE in ('X','G')";
			if(baseStd.getModel()!=null&&!DataHelper.IsNullOrSpace(baseStd.getModel().getStdName())){
				condition+=" and LOWER(STD_NAME) like '%'||LOWER('"+baseStd.getModel().getStdName()+"')||'%'";
				baseStd.getModel().setStdName(null);
			}
			List<BaseStd> list=mapper.findWithCondition(baseStd.getModel(), condition, "STD_CODE DESC", baseStd.getSkipResults(), baseStd.getPageSize());
			int count=mapper.countWithCondition(baseStd.getModel(), condition);
			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 DelOrRenewUsuallyStd(String deleteName,String stdCode, Boolean flag) {
		// TODO Auto-generated method stub
		ReturnMsg msg=new ReturnMsg<>();
		try{
			// TODO Auto-generated method stub
			BaseStd std=new BaseStd();
			std.setStdCode(stdCode);
			std.setValidflag(flag?"0":"1");
			if(flag){//作废
				std.setDeleteName(deleteName);
				Psc psc=new Psc();
				psc.setValidflag("1");
				psc.setStdCode(stdCode);
				List<Psc> list=this.getMapper(PscMapper.class).find(psc, 0, 0);
				if(list.size()>0){
					msg.setSuccess(false);
					msg.setMessage("已有产品码与其关联，不能作废");
					return msg;
				}
				std.setDeleteTime(new Date());
			}else{
				std.setUpdateName(deleteName);
				std.setUpdateTime(new Date());
			}
			this.getMapper(BaseStdMapper.class).update(std);
			msg.setMessage(flag?"废除成功":"恢复成功");
			msg.setSuccess(true);
		}catch(Exception ex){
			msg.setMessage(ex.getMessage());
			msg.setSuccess(false);
		}
		return msg;
	}

	@Override
	public ReturnMsg DelOrRenewStdDown(String deleteName,String stdCode, Boolean flag) {
		// TODO Auto-generated method stub
		ReturnMsg msg=new ReturnMsg<>();
		try{
			this.getMapper(BaseAdditionalMapper.class).DelOrRenewByStdCode(stdCode,flag?"0":"1",deleteName);
			this.getMapper(BaseStdAlphaMapper.class).DelOrRenewByStdCode(stdCode,flag?"0":"1",deleteName);
			this.getMapper(BaseStdRSpecMapper.class).DelOrRenewByStdCode(stdCode,flag?"0":"1",deleteName);
			this.getMapper(PscMapper.class).DelOrRenewByStdCode(stdCode,flag?"0":"1");
			msg.setMessage(flag?"废除成功":"恢复成功");
			msg.setSuccess(true);
		}catch(Exception ex){
			msg.setMessage(ex.getMessage());
			msg.setSuccess(false);
		}
		return msg;
	}

	@Override
	public ReturnMsg AllowOrStopUseStd(BaseStd baseStd) {
		// TODO Auto-generated method stub
		ReturnMsg msg=new ReturnMsg<>();
		try{
			BaseStd _baseStd=new BaseStd();
			_baseStd.setUpdateTime(new Date());
			_baseStd.setForbiddenOrder(baseStd.getForbiddenOrder());
			_baseStd.setStdCode(baseStd.getStdCode());
			this.getMapper(BaseStdMapper.class).update(_baseStd);
			msg.setSuccess(true);
			msg.setMessage("操作成功");
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage("操作失败："+ex.getMessage());
		}
		return msg;
	}

}
