/***文档注释***********************************************
 * 作者               :                   邓彬
 * 创建日期      :                   2018.2.5
 * 描述               :        			检验项对照接口
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/
package market.Com.impl;

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

import market.Com.mapper.ComBasePhyRPhyMapper;
import market.Com.service.ComBasePhyRPhyService;
import market.Com.vo.ComBasePhyCode;
import market.Com.vo.ComBasePhyRPhy;
import market.Com.vo.ComBasePhyRPhyUpdate;
import market.Com.vo.CommonPage;
import market.Com.vo.Pfmc;
import market.Com.vo.ResultModel;
import market.Qms.common.DataHelper;

import org.springframework.transaction.annotation.Transactional;

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

@RemoteService(path="ComBasePhyRPhyService")
public class ComBasePhyRPhyServiceImpl extends BaseServiceImpl<ComBasePhyRPhy,Object> implements ComBasePhyRPhyService {

	/**
	 * 查询功能
	 * @param phy
	 * @return
	 */
	public ResultModel doFindPhy(CommonPage<ComBasePhyCode> phy) {
		ResultModel rm =new ResultModel();
		ComBasePhyCode code =phy.getObject();
		try {
			List<ComBasePhyCode> phylist =this.getMapper(ComBasePhyRPhyMapper.class).doFindCodeXg(code);
			rm.setData(phylist);
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("查询失败");
		}
		return rm;
	}
	
	
	
	/**
	 * 页面初始化
	 * @param phy
	 * @return
	 */
	public ResultModel doFindCbpc() {
		ResultModel rm =new ResultModel();
		try {
			List<ComBasePhyCode> cbpclist =this.getMapper(ComBasePhyRPhyMapper.class).doFindCbpc();
			for (ComBasePhyCode c : cbpclist) {
				if(c.getPhy_code_l().substring(0,1).equals("C")){
					c.setPhy_code_m(c.getPhy_code_l());
					c.setPhy_name_m(c.getPhy_name_l());
					c.setPhy_code_l(null);
					c.setPhy_name_l(null);
				}else if (c.getPhy_code_l().substring(0,1).equals("P")) {
					c.setPhy_code_s(c.getPhy_code_l());
					c.setPhy_name_s(c.getPhy_name_l());
					c.setPhy_code_l(null);
					c.setPhy_name_l(null);
				}
			}
			rm.setData(cbpclist);
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setState("初始化失败");
		}
		return rm;
	}

	/**
	 * 保存检验项代码+冷轧系统检验项名称
	 * @param sum
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doUpdate(CommonPage<ComBasePhyRPhyUpdate> sum)  {
		ResultModel rm =new ResultModel();
		ComBasePhyRPhyUpdate phy =sum.getObject();
		try {
			//如果数据库中不存在数据则进行新增操作，存在则进行修改操作
			if(phy.getPhy_id()==null||phy.getPhy_id().equals("")) {
				ComBasePhyRPhy phy1 =new ComBasePhyRPhy();
				phy1.setPhy_code_l(phy.getPhy_code_l());
				phy1.setPhy_code_m(phy.getPhy_code_m());
				phy1.setPhy_code_s(phy.getPhy_code_s());
				phy1.setItem_code_d(phy.getItem_code_d());
				phy1.setItem_code_s(phy.getItem_code_s());
				phy1.setItem_code_t(phy.getItem_code_t());
				
					//P+2位年月日+2位流水
				this.getMapper(ComBasePhyRPhyMapper.class).deleteby(phy1);
					List<Pfmc> list = phy.getList();
					for(Pfmc p:list)
					{
						phy1.setPfmc_code(p.getPfmc_code());
//						if (this.getMapper(ComBasePhyRPhyMapper.class).find(phy1, 0, 0).size()=0) {
							String id ="P"+new SimpleDateFormat("yyMMdd").format(new Date());
							String s=this.getMapper(ComBasePhyRPhyMapper.class).doFindLs();
							s=s.substring(2, 5);
							id+=s;
							phy.setPhy_id(id);
							
							ComBasePhyRPhy c = new ComBasePhyRPhy();
							if(phy.getItem_code_d() != null)
							{
								c.setItem_code_d(phy.getItem_code_d());
							}
							if(phy.getItem_code_s() != null)
							{
								c.setItem_code_s(phy.getItem_code_s());
							}
							if(phy.getItem_code_t() != null)
							{
								c.setItem_code_t(phy.getItem_code_t());
							}
							if(phy.getPhy_code_l() != null)
							{
								c.setPhy_code_l(phy.getPhy_code_l());
							}
							if(phy.getPhy_code_m() != null)
							{
								c.setPhy_code_m(phy.getPhy_code_m());
							}
							if(phy.getPhy_code_s() != null)
							{
								c.setPhy_code_s(phy.getPhy_code_s());
							}
							if(phy.getPhy_id() != null){
								c.setPhy_code_l(phy.getPhy_id());
							}
							if(p.getPfmc_code() != null)
							{
								c.setPfmc_code(p.getPfmc_code());
							}
							if(p.getPfmc_name() != null)
							{
								c.setPfmc_name(p.getPfmc_name());
							}
							//2018年1月19日修改
							//一对多关系插入修改
							String conditon = " and PFMC_CODE = '" + c.getPfmc_code() + "'";
							int num = this.getMapper(ComBasePhyRPhyMapper.class).countWithCondition(null, conditon);
							if (num > 0) {
								rm.setState("500");
								rm.setMsgInfo("保存失败，该数据已经被其它检验项目关联！");
								throw new Exception("保存失败，该数据已经被其它检验项目关联！");
							}else {
								this.getMapper(ComBasePhyRPhyMapper.class).insert(c);
							}
						}
//					}else {
//						List<Pfmc> list = phy.getList();
//						
//							ComBasePhyRPhy c = new ComBasePhyRPhy();
//							if(phy.getItem_code_d() != null)
//							{
//								c.setItem_code_d(phy.getItem_code_d());
//							}
//							if(phy.getItem_code_s() != null)
//							{
//								c.setItem_code_s(phy.getItem_code_s());
//							}
//							if(phy.getItem_code_t() != null)
//							{
//								c.setItem_code_t(phy.getItem_code_t());
//							}
//							if(phy.getPhy_code_l() != null)
//							{
//								c.setPhy_code_l(phy.getPhy_code_l());
//							}
//							if(phy.getPhy_code_m() != null)
//							{
//								c.setPhy_code_m(phy.getPhy_code_m());
//							}
//							if(phy.getPhy_code_s() != null)
//							{
//								c.setPhy_code_s(phy.getPhy_code_s());
//							}
//							if(phy.getPhy_id() != null){
//								c.setPhy_id(phy.getPhy_id());
//							}
//							if(p.getPfmc_code() != null)
//							{
//								c.setPfmc_code(p.getPfmc_code());
//							}
//							if(p.getPfmc_name() != null)
//							{
//								c.setPfmc_name(p.getPfmc_name());
//							}
//							this.getMapper(ComBasePhyRPhyMapper.class).update(c);
//						}
		//			}
						
				rm.setState("200");
				rm.setMsgInfo("修改成功");
			}
		} catch (Exception e) {
			// TODO: handle exception
			rm.setData("500");
			if("".equals(rm.getMsgInfo()))
			rm.setMsgInfo("保存失败");
		}
		return rm;
	}



	/**
	 * 获取外部材质信息
	 * 
	 * @param Phyode
	 *            
	 * @return
	 */

	public ResultModel getWbPhy(CommonPage<ComBasePhyRPhyUpdate> cp)
	{


		ResultModel rm = new ResultModel();

		try {

List<Pfmc> list = cp.getObject().getList();
if(list != null)
{
for(Pfmc p:list){
	String aa = p.getPfmc_code();
	rm.setData(this.getMapper(ComBasePhyRPhyMapper.class).QuerybySqlWbPhy(aa));
}
}
else{
	rm.setData(this.getMapper(ComBasePhyRPhyMapper.class).QuerybySqlWbPhy(null));
}
			rm.setState("200");
			rm.setMsgInfo("查询成功!");

		} catch (Exception e) {
			// TODO: handle exception

			rm.setState("500");
			rm.setMsgInfo("查询失败!");
		}

		return rm;
	}
	/**
	 * 获取已关联检验项信息
	 * 
	 * @param Phyode
	 *            
	 * @return
	 */

	public ResultModel getWbPhyGuanLian(CommonPage<ComBasePhyRPhy> cp)
	{

		ResultModel rm = new ResultModel();

		try {

			ComBasePhyRPhy rp = cp.getObject();
			String aa = cp.getObject().getPfmc_code();
             List<ComBasePhyRPhy> list1 =  this.getMapper(ComBasePhyRPhyMapper.class).find(rp, 0, 0);
			rm.setData(list1);
			rm.setState("200");


		} catch (Exception e) {
			// TODO: handle exception

			rm.setState("500");
			rm.setMsgInfo("查询失败!");
		}

		return rm;
	}
	/***
	 * 根据检验项目名称查询关联数据
	 * @param phyName
	 * @return
	 */
	public ResultModel getWbPhyByName(CommonPage<ComBasePhyRPhy> cp)
	{

		ResultModel rm = new ResultModel();

		try {
			String condition = "and phy_name_s like '%"+cp.getObject().getPhy_name_s()+"%'";
             List<ComBasePhyRPhy> list1 =  this.getMapper(ComBasePhyRPhyMapper.class).findWithCondition(null, condition, null, (cp.getPageIndex()-1)*cp.getPageSize(), cp.getPageSize());
			rm.setData(list1);
			rm.setState("200");


		} catch (Exception e) {
			// TODO: handle exception

			rm.setState("500");
			rm.setMsgInfo("查询失败!");
		}

		return rm;
	}
	/***
	 * 删除检验项关系
	 * @param cbp
	 * @return
	 */
	public ResultModel deleteWbPhyGuanLian(ComBasePhyRPhy cbp)
	{

		ResultModel rm = new ResultModel();
		try {
			if(DataHelper.IsNullOrSpace(cbp.getPhy_item_key())){
				rm.setState("500");
				rm.setMsgInfo("【检验项目拼接代码】不能为空，请确认！");
				return rm;
			}
			if(DataHelper.IsNullOrSpace(cbp.getPfmc_code())){
				rm.setState("500");
				rm.setMsgInfo("【材质检验项代码（外部系统）】不能为空，请确认！");
				return rm;
			}
            this.getMapper(ComBasePhyRPhyMapper.class).deletebyKeyCode(cbp);
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("删除失败!");
		}

		return rm;
	}
	/***
	 * 新增检验项关系
	 * @param cbp
	 * @return
	 */
	public ResultModel insertWbPhyGuanLian(ComBasePhyRPhy cbp)
	{

		ResultModel rm = new ResultModel();
		try {
			cbp.setPhy_item_key(DataHelper.GetPhyString(cbp.getPhy_code_l(), cbp.getPhy_code_m(), cbp.getPhy_code_s(), cbp.getItem_code_d(), cbp.getItem_code_t(), cbp.getItem_code_s()));
			cbp.setPhy_item_desc(DataHelper.GetPhyString(cbp.getPhy_name_l(), cbp.getPhy_name_m(), cbp.getPhy_name_s(), cbp.getItem_name_d(), cbp.getItem_name_t(), cbp.getItem_name_s()));
			cbp.setCreate_name((String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME));
			cbp.setCreate_time(new Date());
            this.getMapper(ComBasePhyRPhyMapper.class).insert(cbp);
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("新增失败!");
		}

		return rm;
	}
}
