package market.Slm.ContractManagement.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import market.Com.pb.PbMethod;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.ContractManagement.mapper.SlmContractIdenOrgMapper;
import market.Slm.ContractManagement.mapper.SlmContractPscMapper;
import market.Slm.ContractManagement.mapper.SlmContractPscOrgMapper;
import market.Slm.ContractManagement.service.SlmContractIdenOrgService;
import market.Slm.ContractManagement.vo.SlmContractIdenOrg;
import market.Slm.ContractManagement.vo.SlmContractPscOrg;
import market.Slm.OrderManagement.mapper.SlmProcessPlanMapper;
import market.Slm.OrderManagement.vo.SlmProcessPlan;

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

@RemoteService(path="SlmContractIdenOrgService")
public class SlmContractIdenOrgServiceImpl extends BaseServiceImpl<SlmContractIdenOrg,Object> implements SlmContractIdenOrgService {

	/**
	 * 查询多产品规范码认证机构
	 */
	public ResultModel doFindOrg(CommonPage<SlmContractIdenOrg> sum) {
		ResultModel rm=new ResultModel();
		SlmContractIdenOrg org=sum.getObject();
		try {
			List <SlmContractIdenOrg> orglist =this.getMapper(SlmContractIdenOrgMapper.class).find(org, 0, 0);
			rm.setState("200");
			rm.setData(orglist);
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败"+e.getMessage());
		}
		return rm;
	}

	/**
	 * 产品新增认证机构
	 * @throws Exception 
	 */
	public ResultModel doAddorg(CommonPage<SlmContractIdenOrg> sum) throws Exception {
		ResultModel rm=new ResultModel();
		SlmContractIdenOrg org=sum.getObject();
		try {
			SlmContractIdenOrg o =new SlmContractIdenOrg();
			o.setContract_line_no(org.getContract_line_no());
			o.setPsc(org.getPsc());
			boolean b=true;
			List<SlmContractIdenOrg> orglist =this.getMapper(SlmContractIdenOrgMapper.class).find(o, 0, 0);
			for (SlmContractIdenOrg s : orglist) {
				if (s.getContract_line_no().equals(org.getContract_line_no())) {
					if (s.getPsc().equals(org.getPsc())) {
						if(s.getOrg_code().equals(org.getOrg_code())){
							b=false;
						}
					}
				}
			}
			if (b) {
				String id=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3);
				org.setSeq(id);
				this.getMapper(SlmContractIdenOrgMapper.class).insert(org);

				//新增默认配置
				SlmContractPscOrg po=new SlmContractPscOrg();
				po.setPsc(org.getPsc());
				po.setOrg_code(org.getOrg_code());
				int count=this.getMapper(SlmContractPscOrgMapper.class).count(po);
				if(count==0){			//未曾有过的配置，加为默认配置
					po.setPsc_desc(org.getPsc_desc());
					po.setOrg_name(org.getOrg_name());
					this.getBean(SlmContractPscOrgServiceImpl.class).doAdd(po);
				}

				rm.setState("200");
			}else {
				rm.setState("500");
				rm.setMsgInfo("当前已存在");
			}
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("新增失败!");
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}

	/**
	 * 删除产品下的认证机构
	 * @throws Exception 
	 */
	public ResultModel doDeleteOrg(CommonPage<SlmContractIdenOrg> sum) throws Exception {
		ResultModel rm =new ResultModel();
		SlmContractIdenOrg org =sum.getObject();
		try {
			SlmContractIdenOrg check=this.getMapper(SlmContractIdenOrgMapper.class).findById(org.getSeq());
			this.getMapper(SlmContractPscMapper.class).deleteorg(org.getSeq());
			//删除默认配置
			if(check!=null){
				SlmContractPscOrg po=new SlmContractPscOrg();
				po.setOrg_code(check.getOrg_code());
				po.setPsc(check.getPsc());
				List<SlmContractPscOrg> po_list=this.getMapper(SlmContractPscOrgMapper.class).find(po, 0, 0);
				for(SlmContractPscOrg pos:po_list){
					this.getMapper(SlmContractPscOrgMapper.class).delete(pos.getId());
				}
			}
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("删除失败");
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}

	/**
	 * for质量
	 * 获取认证机构的接口
	 * 传入参数：合同行主键、产品规范码
	 * 返回结果：认证机构list
	 * */
	public ResultModel doQueryIdenOrg(SlmContractIdenOrg org){
		ResultModel rm =new ResultModel();
		rm.setState("500");
		if(org==null){
			rm.setMsgInfo("请传入参数");
			return rm;
		}
		try {
			if(org.getContract_line_no()==null){
				rm.setMsgInfo("请传入合同行号");
				return rm;
			}
			if(org.getPsc()==null){
				rm.setMsgInfo("请传入产品规范码");
				return rm;
			}
			SlmContractIdenOrg param=new SlmContractIdenOrg();
			param.setPsc(org.getPsc());
			SlmProcessPlan plan=this.getMapper(SlmProcessPlanMapper.class).findById(org.getContract_line_no());
			if(plan!=null)
				param.setContract_line_no(plan.getContract_line_no());
			List<SlmContractIdenOrg> org_list=this.getMapper(SlmContractIdenOrgMapper.class).find(param,0,0);
			rm.setData(org_list);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}
}
