/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-08-13
 * 描述            ：采购计划行表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Pms.Demand.impl;
import java.util.Date;
import java.util.List;

import market.Com.pb.DoubleUtil;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Pms.Demand.mapper.PurPlanCMapper;
import market.Pms.Demand.mapper.PurPlanMMapper;
import market.Pms.Demand.service.PurPlanCService;
import market.Pms.Demand.vo.PurPlanC;
import market.Pms.Demand.vo.PurPlanM;

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.mapper.UserMapper;
import com.hnshituo.core.auth.service.HomeService;
import com.hnshituo.core.auth.vo.User;

@RemoteService(path="PurPlanCService")
public class PurPlanCServiceImpl extends BaseServiceImpl<PurPlanC,Object> implements PurPlanCService {

	/**
	 * 计划行信息查询
	 * 
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PurPlanC> oci) {
		String userId = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_ID);
//		String userName = (String) this.getSession().getAttribute(
//				HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		PurPlanC ppc=oci.getObject();
		try {
			User user2=this.getMapper(UserMapper.class).findById(userId);
			String sWhere=" 1=1 ";
			if("资材备件科".equals(user2.getDept_name())||"原料科".equals(user2.getDept_name())){
				sWhere+="and unit_desc = '"+user2.getDept_name()+"'";
			} 
			List<PurPlanC>  list=this.getMapper(PurPlanCMapper.class).findWithCondition(ppc, sWhere, " create_time desc,plan_sqe desc", 0, 0);
			rm.setData(list);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败，请联系管理员");
		}
		return rm;	
	}
	
	/**
	 * 计划行信息修改
	 * 
	 * @param oci
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel upAddWeb(CommonPage<PurPlanC> oci) throws Exception {
		String userId = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		if (oci.getObjectList() == null || oci.getObjectList().size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请选择数据！");
			return rm;
		}
		try {
			for (PurPlanC ppc : oci.getObjectList()) {
				if ("10".equals(ppc.getValidflag())
						&& ("00".equals(ppc.getStatus()) || "40".equals(ppc
								.getStatus()))) {
				PurPlanC ppc2=this.getMapper(PurPlanCMapper.class).findByDemandSqe(ppc.getDemand_sqe());
				if(ppc.getQty()!=null&&!"".equals(ppc.getQty())){
					PurPlanC ppc3=this.getMapper(PurPlanCMapper.class).findById(ppc.getPlan_sqe());
					if(ppc2.getDemand_sqe()!=null&&!"".equals(ppc2.getDemand_sqe())){
					    Double  approval2=DoubleUtil.sub(DoubleUtil.add(ppc.getQty(), ppc2.getQty_val()), ppc3.getQty());
					    if(approval2>ppc2.getQty()){
						rm.setState("500");
						rm.setMsgInfo(ppc.getDemand_sqe()+"累计核准数量超过计划数量，请重新编辑");
						return rm;
					  }
					}
				}	
				ppc.setUpdate_userid(userId);
				ppc.setUpdate_name(userName);
				ppc.setUpdate_time(new Date());
				this.getMapper(PurPlanCMapper.class).update(ppc);
				}else{
					rm.setState("500");
					rm.setMsgInfo("该数据不能修改！");
					return rm;
				}
				
			}
			rm.setState("200");
			rm.setMsgInfo("修改成功！");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("修改失败，请联系管理员");
			throw ex;
		}
		return rm;
	}
	
	/**
	 * 计划行信息保存
	 * 
	 * @param oci
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel upPurPlanC(CommonPage<PurPlanC> oci) throws Exception {
		String userId = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		if (oci.getObjectList() == null || oci.getObjectList().size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请选择数据！");
			return rm;
		}
		try {
			for (PurPlanC ppc : oci.getObjectList()) {
				if ("20".equals(ppc.getValidflag())
						&& ("10".equals(ppc.getStatus()) || "40".equals(ppc
								.getStatus()))) {
				ppc.setUpdate_userid(userId);
				ppc.setUpdate_name(userName);
				ppc.setUpdate_time(new Date());
				this.getMapper(PurPlanCMapper.class).update(ppc);
				}else{
					rm.setState("500");
					rm.setMsgInfo("该数据不能保存！");
					return rm;
				}
				
			}
			rm.setState("200");
			rm.setMsgInfo("保存成功！");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("保存失败，请联系管理员");
			throw ex;
		}
		return rm;
	}
	/**
	 * 计划行信息审批
	 * 
	 * @param oci
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel appPurPlanC(CommonPage<PurPlanC> oci) throws Exception {
		String userId = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		if (oci.getObjectList() == null || oci.getObjectList().size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请选择数据！");
			return rm;
		}
		try {
			for (PurPlanC ppc : oci.getObjectList()) {
				if ("10".equals(ppc.getStatus())) {
				ppc.setStatus("30");
				ppc.setValidflag("00");//数据变为无效
				ppc.setUpdate_userid(userId);
				ppc.setUpdate_name(userName);
				ppc.setUpdate_time(new Date());
				this.getMapper(PurPlanCMapper.class).update(ppc);
				String sWhere=" 1=1 ";
				if(ppc.getPlan_no()!=null&&!"".equals(ppc.getPlan_no())){
					sWhere+="and plan_no = '"+ppc.getPlan_no()+"'";
				}
				PurPlanC pc=new PurPlanC();
				List<PurPlanC> list=this.getMapper(PurPlanCMapper.class).findWithCondition(pc, sWhere, " create_time desc", 0, 0);
				boolean a=true;
				for (PurPlanC ppc2 : list) {
					if(!"30".equals(ppc2.getStatus())){//如果存在计划信息不为审批拒绝，标志符为false,否则为true；
						a=false;
					}
				}
				if(a){
					PurPlanM ppm=this.getMapper(PurPlanMMapper.class).findById(ppc.getPlan_no());
					ppm.setStatus("30");
					ppm.setValidflag("00");//数据状态变为无效
					this.getMapper(PurPlanMMapper.class).update(ppm);
				}
				}else{
					rm.setState("500");
					rm.setMsgInfo("该数据不能审批！");
					return rm;
				}
				
			}
			rm.setState("200");
			rm.setMsgInfo("审批成功！");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("审批失败，请联系管理员");
			throw ex;
		}
		return rm;
	}
	
	/**
	 * 采购单行新增信息接口查询
	 * 
	 * @param oci
	 * @return
	 */
	public ResultModel doPurPlanC(CommonPage<PurPlanC> oci) {
		ResultModel rm = new ResultModel();
		String userId = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_ID);
//		String userName = (String) this.getSession().getAttribute(
//				HomeService.SESSION_USER_NAME);
		PurPlanC ppc= oci.getObject() == null ? (new PurPlanC()) : oci
				.getObject();
		String startTime = oci.getStartTime();
		String endTime = oci.getEndTime();
		try {	
			User user2=this.getMapper(UserMapper.class).findById(userId);
			String str="";
			if("资材备件科".equals(user2.getDept_name())||"原料科".equals(user2.getDept_name())){
				str+=user2.getDept_name();
			}  
			List<PurPlanC> list = this.getMapper(PurPlanCMapper.class).findPurPlanC(ppc, startTime, endTime,str);	
			rm.setData(list);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("错误：" + ex.getMessage());
			throw ex;
		}
		return rm;
	}
}
