/***文档注释***********************************************
 * 作者               :                   易鑫
 * 创建日期      :                   2017.10.24
 * 描述               :        			客户认证产线信息
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/

package market.Slm.CustomerInformation.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.PbModelDb;
import market.Com.vo.ResultModel;
import market.Slm.CustomerInformation.mapper.SlmCustomerIdenPlineMapper;
import market.Slm.CustomerInformation.service.SlmCustomerIdenPlineService;
import market.Slm.CustomerInformation.vo.SlmCustomerIdenPline;

import org.springframework.transaction.annotation.Transactional;

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

@RemoteService(path="SlmCustomerIdenPlineService")
public class SlmCustomerIdenPlineServiceImpl extends BaseServiceImpl<SlmCustomerIdenPline,Object> implements SlmCustomerIdenPlineService {
	/**
	 * 查询数据
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<SlmCustomerIdenPline> sum) {
		ResultModel rm=new ResultModel();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		try{
			PageModel<SlmCustomerIdenPline> pm = new PageModel<SlmCustomerIdenPline>(pageIndex,pageSize);
			SlmCustomerIdenPline sb=sum.getObject()==null?(new SlmCustomerIdenPline()):sum.getObject();
			
			pm.setRows(this.getMapper(SlmCustomerIdenPlineMapper.class).findWithCondition(sb,null," product_code,procedure_no", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(SlmCustomerIdenPlineMapper.class).count(sb));
	
			rm.setState("200");
			rm.setData(pm);
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 查看详情
	 * */
	public ResultModel doQueryDetailWeb(CommonPage<SlmCustomerIdenPline> sum){
		ResultModel rm=new ResultModel();
		try{
			rm.setState("200");
			rm.setData(this.getMapper(SlmCustomerIdenPlineMapper.class).findById(sum.getObject().getIden_no()));
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	

	/**
	 * 添加数据
	 * 
	 * @param bd
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAddWeb(CommonPage<SlmCustomerIdenPline> sum) throws Exception {
		ResultModel rm=new ResultModel();
		try {
			SlmCustomerIdenPline bd=sum.getObject();
			SlmCustomerIdenPline newPline=new SlmCustomerIdenPline();
			newPline.setCustomer_no(bd.getCustomer_no());
			newPline.setProduct_code(bd.getProduct_code());
			newPline.setProcedure_no(bd.getProcedure_no());
			newPline.setPline_no(bd.getPline_no());
			int count=this.getMapper(SlmCustomerIdenPlineMapper.class).count(newPline);
			if(count>0){
				rm.setState("500");
				rm.setMsgInfo("该产线已存在，无法新增");
				return rm;
			}
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			bd.setIden_no(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3));
			bd.setCreate_man_no(userId);
			bd.setCreate_man_name(userName);
			bd.setValid_flag("1");
			bd.setCreate_time(new Date());
			
			this.getMapper(SlmCustomerIdenPlineMapper.class).insert(bd);
			rm.setState("200");
			
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，新增失败!");
			e.printStackTrace();
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}

	/**
	 * 修改数据
	 * 
	 * @param bd
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doUpdateWeb(CommonPage<SlmCustomerIdenPline> sum) throws Exception {
		ResultModel rm=new ResultModel();
		SlmCustomerIdenPline bd = sum.getObject();
		try {
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			
			SlmCustomerIdenPline check=this.getMapper(SlmCustomerIdenPlineMapper.class).findById(bd.getIden_no());
			if(check!=null&&"0".equals(check.getValid_flag())){
				rm.setMsgInfo("无法修改已失效的数据，请先启用");
				rm.setState("500");
				return rm;
			}
			SlmCustomerIdenPline newPline=new SlmCustomerIdenPline();
			newPline.setCustomer_no(bd.getCustomer_no());
			newPline.setProduct_code(bd.getProduct_code());
			newPline.setProcedure_no(bd.getProcedure_no());
			newPline.setPline_no(bd.getPline_no());
			newPline.setValid_flag("1");
			List<SlmCustomerIdenPline> plineList=this.getMapper(SlmCustomerIdenPlineMapper.class).find(newPline,0,0);
			if(plineList.size()>0&&!plineList.get(0).getIden_no().equals(bd.getIden_no())){
				rm.setState("500");
				rm.setMsgInfo("已存在相同的认证产线");
				return rm;
			}
			
			bd.setUpdate_man_no(userId);
			bd.setUpdate_man_name(userName);
			bd.setUpdate_time(new Date());
			this.getMapper(SlmCustomerIdenPlineMapper.class).update(bd);
			rm.setState("200");
			
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("修改失败!");
			e.printStackTrace();
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}

	/**
	 * 启用、停用数据
	 * @param bd
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doUpdateFlagWeb(CommonPage<SlmCustomerIdenPline> sum) {
		ResultModel rm=new ResultModel();
		List<SlmCustomerIdenPline> obd = sum.getObjectList();
		for(SlmCustomerIdenPline sci :obd){
			this.getMapper(SlmCustomerIdenPlineMapper.class).update(sci);
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 指定产线 - 查询工序&指定产线下拉列表
	 * customer_no查工序，customer_no+procedure_no查指定产线
	 * @throws Exception 
	 * */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doQueryProcedureWeb(CommonPage<SlmCustomerIdenPline> sum) throws Exception {
		ResultModel rm=new ResultModel();
		SlmCustomerIdenPline pline = sum.getObject();
		List<PbModelDb> pmdList=new ArrayList<PbModelDb>();
		try {
			List<SlmCustomerIdenPline> plineList=this.getMapper(SlmCustomerIdenPlineMapper.class).doQueryProcedure(pline);
			for(SlmCustomerIdenPline plines:plineList){
				PbModelDb pmd=new PbModelDb();
				pmd.setId(plines.getProcedure_no());
				pmd.setText(plines.getProcedure_name());
				if(pline.getProcedure_no()!=null&&!"".equals(pline.getProcedure_no())){
					pmd.setId(plines.getPline_no());
					pmd.setText(plines.getPline_name());
				}
				pmdList.add(pmd);
			}
			rm.setState("200");
			rm.setData(pmdList);
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			e.printStackTrace();
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}
}
