/***文档注释***********************************************
 * 作者               :                   易鑫
 * 创建日期      :                   2017.10.24
 * 描述               :        			合同指定产线信息
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
 ***********************************************************/
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.SlmContractPointPlineMapper;
import market.Slm.ContractManagement.service.SlmContractPointPlineService;
import market.Slm.ContractManagement.vo.SlmContractPointPline;

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="SlmContractPointPlineService")
public class SlmContractPointPlineServiceImpl extends BaseServiceImpl<SlmContractPointPline,Object> implements SlmContractPointPlineService {
	
	/**
	 * 合同指定产线 - 列表查询
	 * contract_pk,contract_line_no
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmContractPointPline> sum) {
		ResultModel rm=new ResultModel();
		SlmContractPointPline line=sum.getObject();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		PageModel<SlmContractPointPline> pm=new PageModel<SlmContractPointPline>(pageIndex, pageSize);
		try {
			List<SlmContractPointPline> list=this.getMapper(SlmContractPointPlineMapper.class).findWithCondition(line, null, " create_time",(pageIndex-1)*pageSize,pageSize);
			int count=this.getMapper(SlmContractPointPlineMapper.class).count(line);
			pm.setRows(list);
			pm.setTotal(count);
			rm.setState("200");
			rm.setData(pm);
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，查询失败!"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	/**
	 * 合同指定产线 - 新增
	 * */
	public ResultModel doAddWeb(CommonPage<SlmContractPointPline> sum) {
		ResultModel rm=new ResultModel();
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		SlmContractPointPline line=sum.getObject();
		try {
			SlmContractPointPline newPline=new SlmContractPointPline();
			newPline.setContract_pk(line.getContract_pk());
			newPline.setContract_line_no(line.getContract_line_no());
			newPline.setPline_code(line.getPline_code());
			newPline.setProcess_code(line.getProcess_code());
			int count=this.getMapper(SlmContractPointPlineMapper.class).count(newPline);
			if(count>0){
				rm.setState("500");
				rm.setMsgInfo("已指定该产线");
				return rm;
			}
			String strPk=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
			line.setPoint_line_no(strPk+PbMethod.RandomCreate(3));
			line.setValid_flag("1");		//有效
			line.setCreate_man_no(userId);
			line.setCreate_man_name(userName);
			line.setCreate_time(new Date());
			this.getMapper(SlmContractPointPlineMapper.class).insert(line);
			
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，新增失败!"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 合同指定产线 - 编辑
	 * valid_flag[0：无效；1:有效
	 * */
	public ResultModel doUpdateWeb(CommonPage<SlmContractPointPline> sum) {
		ResultModel rm=new ResultModel();
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		SlmContractPointPline line=sum.getObject();
		try {
			SlmContractPointPline newPline=new SlmContractPointPline();
			newPline.setContract_pk(line.getContract_pk());
			newPline.setContract_line_no(line.getContract_line_no());
			newPline.setPline_code(line.getPline_code());
			newPline.setProcess_code(line.getProcess_code());
			List<SlmContractPointPline> plineList=this.getMapper(SlmContractPointPlineMapper.class).find(newPline,0,0);
			if(plineList.size()>0&&!plineList.get(0).getPoint_line_no().equals(line.getPoint_line_no())){
				rm.setState("500");
				rm.setMsgInfo("已指定相同产线");
				return rm;
			}
			
			line.setUpdate_man_no(userId);
			line.setUpdate_man_name(userName);
			line.setUpdate_time(new Date());
			this.getMapper(SlmContractPointPlineMapper.class).update(line);
			
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，编辑失败!"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
}
