package market.Qms.com.service.impl;

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

import market.Com.vo.CommonPage;
import market.Qms.com.mapper.PscROrgMapper;
import market.Qms.com.service.PscROrgService;
import market.Qms.com.vo.PscROrg;
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;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path="PscROrgService")
public class PscROrgServiceImpl extends BaseServiceImpl<PscROrg, String> implements PscROrgService {

	@Override
	public ReturnMsg<List<PscROrg>> GetPscROrg(RequestModel<PscROrg> org) {
		ReturnMsg<List<PscROrg>> msg=new ReturnMsg<>();
		List<PscROrg> list=new ArrayList<>();
		try{
			PscROrgMapper mapper=this.getMapper(PscROrgMapper.class);
			list=mapper.findWithCondition(org.getModel(), null, "id DESC", org.getSkipResults(), org.getPageSize());
			int count=mapper.countWithCondition(org.getModel(),null);
			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<PscROrg> SavePscROrg(PscROrg org) {
		// TODO Auto-generated method stub
		ReturnMsg<PscROrg> msg=new ReturnMsg<>();
		try{
			PscROrgMapper mapper=this.getMapper(PscROrgMapper.class);
			if(DataHelper.IsNullOrSpace(org.getId())){
				
				List<PscROrg> old=mapper.findWithCondition(null, "psc='"+org.getPsc()+"' and ORG_CODE='"+org.getOrgCode()+"' and VALIDFLAG='1'", null, 0, 0);
				if (old!=null && old.size()>0) {
					msg.setResult(org);
					msg.setMessage("新增失败：【"+org.getOrgName()+"】已存在，不能重复创建，请确认！");
					msg.setSuccess(false);
					return msg;
				}
				String maxCode=mapper.GetMaxCode();
				String code=DataHelper.InitSerial(maxCode, "", 7);
				org.setId(code);
				org.setCreateTime(new Date());
				mapper.insert(org);
			}else{
				org.setUpdateTime(new Date());
				mapper.update(org);
			}
			msg.setResult(org);
			msg.setMessage("保存数据成功");
			msg.setSuccess(true);
			msg.setResult(org);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		return msg;
	}
	
	/**
	 * 批量新增psc与认证机构关联
	 * @param sum
	 * @return
	 */
	public ReturnMsg<List<PscROrg>> SavePscROrg2(CommonPage<PscROrg> sum) {
		// TODO Auto-generated method stub
		ReturnMsg<List<PscROrg>> msg=new ReturnMsg<>();
		try{
			String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			List<PscROrg> orgs=sum.getObjectList();
			/*if (orgs==null || orgs.size()<1) {
				msg.setMessage("请传入参数！");
				msg.setSuccess(false);
				return msg;
			}*/
			if (sum.getMemo1()==null || "".equals(sum.getMemo1())) {
				msg.setMessage("请传入产品码！");
				msg.setSuccess(false);
				return msg;
			}
			
			if (orgs==null || orgs.size()<1) {
				this.getMapper(PscROrgMapper.class).deleteByPsc(sum.getMemo1());
				msg.setMessage("取消认证机构关联");
				msg.setSuccess(true);
				return msg;
			}
			
			for (PscROrg pscROrg : orgs) {
				if (pscROrg.getPsc()==null || "".equals(pscROrg.getPsc())) {
					msg.setMessage("产品码不存在，请确认！");
					msg.setSuccess(false);
					return msg;
				}
			}
			//删除已经关联的数据
			this.getMapper(PscROrgMapper.class).deleteByPsc(sum.getMemo1());
			for (PscROrg org : orgs) {
				
				PscROrgMapper mapper=this.getMapper(PscROrgMapper.class);
				if(DataHelper.IsNullOrSpace(org.getId())){
					
					List<PscROrg> old=mapper.findWithCondition(null, "psc='"+org.getPsc()+"' and ORG_CODE='"+org.getOrgCode()+"' and VALIDFLAG='1'", null, 0, 0);
					if (old!=null && old.size()>0) {
						msg.setMessage("新增失败：【"+org.getOrgName()+"】已存在，不能重复创建，请确认！");
						msg.setSuccess(false);
						return msg;
					}
					
					
					String maxCode=mapper.GetMaxCode();
					String code=DataHelper.InitSerial(maxCode, "", 7);
					org.setId(code);
					org.setCreateTime(new Date());
					org.setCreateName(username);
					mapper.insert(org);
				}
				
			}
			
			msg.setResult(orgs);
			msg.setMessage("保存数据成功");
			msg.setSuccess(true);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		return msg;
	}

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

}
