package market.Slm.ContractManagement.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
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.SlmContractLineInfoMapper;
import market.Slm.ContractManagement.mapper.SlmContractPscMapper;
import market.Slm.ContractManagement.mapper.SlmContractPscOrgMapper;
import market.Slm.ContractManagement.service.SlmContractPscService;
import market.Slm.ContractManagement.vo.SlmContractIdenOrg;
import market.Slm.ContractManagement.vo.SlmContractLineInfo;
import market.Slm.ContractManagement.vo.SlmContractPsc;
import market.Slm.ContractManagement.vo.SlmContractPscOrg;
import market.Slm.OrderManagement.mapper.SlmProcessPlanMapper;
import market.Slm.OrderManagement.vo.SlmProcessPlan;

import org.springframework.transaction.annotation.Transactional;

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

@RemoteService(path="SlmContractPscService")
public class SlmContractPscServiceImpl extends BaseServiceImpl<SlmContractPsc,Object> implements SlmContractPscService {
	
	
	/**
	 * 多产品码信息查询
	 * @param sum
	 * @return
	 */
	public ResultModel doFindPsc(CommonPage<SlmContractPsc> sum) {
		ResultModel rm=new ResultModel();
		SlmContractPsc psc=sum.getObject();
		try {
			List<SlmContractPsc> pscList =this.getMapper(SlmContractPscMapper.class).find(psc, 0, 0);
			rm.setData(pscList);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败");
		}
		return rm;
	}
	
	/**
	 * 多产品码信息删除
	 * @param sum
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doDeletePsc(CommonPage<SlmContractPsc> sum) throws Exception {
		ResultModel rm=new ResultModel();
		SlmContractPsc psc=sum.getObject();
		try {
			//查询产品信息
			psc=this.getMapper(SlmContractPscMapper.class).findById(psc.getSeq());
			//通过产品码 合同行号查询该产品下的认证机构
			SlmContractIdenOrg org=new SlmContractIdenOrg();
			org.setPsc(psc.getPsc());
			org.setContract_line_no(psc.getContract_line_no());
			List<SlmContractIdenOrg> orgList=this.getMapper(SlmContractIdenOrgMapper.class).find(org, 0, 0);
			if(orgList!=null) {
				for(SlmContractIdenOrg orgs:orgList){
					//删除认证机构
					this.getMapper(SlmContractPscMapper.class).deleteorg(orgs.getSeq());
				}
			}
			//删除产品信息
			this.getMapper(SlmContractPscMapper.class).deletepsc(psc.getSeq());
			rm.setState("200");
		} catch (Exception e) {
			e.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("删除失败");
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}
	
	/**
	 * 新增产品规范码
	 * @param sum
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAddPsc(CommonPage<SlmContractPsc> sum) throws Exception {
		ResultModel rm=new ResultModel();
		SlmContractPsc psc=sum.getObject();
		try {
			SlmContractPsc p =new SlmContractPsc();
			p.setContract_line_no(psc.getContract_line_no());
			boolean b=true;
			List<SlmContractPsc> slm=this.getMapper(SlmContractPscMapper.class).find(p, 0, 0);
			for (SlmContractPsc s : slm) {
				if (s.getPsc().equals(psc.getPsc())) {
					b=false;
				}
			}
			if (b) {
				String id=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3);
				psc.setSeq(id);
				this.getMapper(SlmContractPscMapper.class).insert(psc);
				//默认的认证机构带入
				SlmContractPscOrg po=new SlmContractPscOrg();
				po.setPsc(psc.getPsc());
				List<SlmContractPscOrg> po_list=this.getMapper(SlmContractPscOrgMapper.class).find(po,0,0);
				for(SlmContractPscOrg pos:po_list){
					SlmContractIdenOrg org=new SlmContractIdenOrg();
					String orgid=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3);
					org.setSeq(orgid);
					org.setOrg_code(pos.getOrg_code());
					org.setOrg_name(pos.getOrg_name());
					org.setPsc(pos.getPsc());
					org.setPsc_desc(pos.getPsc_desc());
					org.setContract_line_no(psc.getContract_line_no());
					this.getMapper(SlmContractIdenOrgMapper.class).insert(org);
				}
				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;
	}
	
	/**
	 * 根据加工计划号查询多产品码
	 * for质量
	 */
	public ResultModel doQueryPscByPlanNo(CommonPage<SlmProcessPlan> sum) {
		ResultModel rm=new ResultModel();
		SlmProcessPlan plan=sum.getObject();
		List<SlmContractPsc> pscList=new ArrayList<SlmContractPsc>();
		try {
			if(plan.getPlan_no()==null){
				rm.setState("500");
				rm.setMsgInfo("请传入加工计划号");
				return rm;
			}
			plan=this.getMapper(SlmProcessPlanMapper.class).findById(plan.getPlan_no());
			if(plan==null){
				rm.setState("500");
				rm.setMsgInfo("加工计划信息不存在");
				return rm;
			}
			List<SlmContractLineInfo> contract_line_list=this.getMapper(SlmContractLineInfoMapper.class).findWithCondition(null, " contract_line_no='"+plan.getContract_line_no()+"'", null, 0, 0);
			if(contract_line_list.size()==0){
				contract_line_list=this.getMapper(SlmContractLineInfoMapper.class).findWithCondition(null, " contract_line_seq='"+plan.getContract_line_seq()+"' and contract_no='"+plan.getContract_no()+"'", null, 0, 0);
			}
			if(contract_line_list.size()>0){
				SlmContractPsc psc=new SlmContractPsc();
				psc.setContract_line_no(contract_line_list.get(0).getContract_line_no());
				pscList =this.getMapper(SlmContractPscMapper.class).find(psc, 0, 0);
			}
			for(SlmContractPsc psces:pscList){
				SlmContractIdenOrg org=new SlmContractIdenOrg();
				org.setContract_line_no(psces.getContract_line_no());
				org.setPsc(psces.getPsc());
				List<SlmContractIdenOrg> org_list=this.getMapper(SlmContractIdenOrgMapper.class).find(org, 0, 0);
				psces.setOrg_list(org_list);
			}
			rm.setData(pscList);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败"+e.getMessage());
		}
		return rm;
	}
}
