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

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Pms.Demand.mapper.PurDemandCMapper;
import market.Pms.Demand.mapper.PurDemandMMapper;
import market.Pms.Demand.service.PurDemandCService;
import market.Pms.Demand.vo.PurDemandC;
import market.Pms.Demand.vo.PurDemandM;
import market.Pms.Mat.mapper.PurMatClassMapper;
import market.Pms.Mat.vo.PurMatClass;

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="PurDemandCService")
public class PurDemandCServiceImpl extends BaseServiceImpl<PurDemandC,Object> implements PurDemandCService {
	
	/**
	 * 需求行信息查询
	 * 
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PurDemandC> 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();
		PurDemandC pdc=oci.getObject();
//		String demand_no=pdc.getDemand_no();
		String receive_type_name=pdc.getReceive_type_name();
		String delivery_location=pdc.getDelivery_location();
		String budget_type_name=pdc.getBudget_type_name();
		String usage_kind_name=pdc.getUsage_kind_name();
		String expense_type_name=pdc.getExpense_type_name();
		String startTime=oci.getStartTime();
		try {
			String sWhere=" 1=1 ";
			User user2=this.getMapper(UserMapper.class).findById(userId);
			if("资材备件科".equals(user2.getDept_name())||"原料科".equals(user2.getDept_name())){
				sWhere+="and unit_desc = '"+user2.getDept_name()+"'";
			} 
			if(receive_type_name!=null&&!"".equals(receive_type_name)){
				sWhere+="and receive_type_name = '"+receive_type_name+"'";
			}
//			if(demand_no!=null&&!"".equals(demand_no)){
//				sWhere+="and demand_no = '"+demand_no+"'";
//			}
			if(delivery_location!=null&&!"".equals(delivery_location)){
				sWhere+="and delivery_location = '"+delivery_location+"'";
			}
			if(budget_type_name!=null&&!"".equals(budget_type_name)){
				sWhere+="and budget_type_name = '"+budget_type_name+"'";
			}
			if(usage_kind_name!=null&&!"".equals(usage_kind_name)){
				sWhere+="and usage_kind_name = '"+usage_kind_name+"'";
			}
			if(expense_type_name!=null&&!"".equals(expense_type_name)){
				sWhere+="and expense_type_name = '"+expense_type_name+"'";
			}
			if(startTime!=null&&!"".equals(startTime)){
				sWhere+=" and to_char(bal_year_month, 'yyyy-mm-dd hh24:mi:ss') <='"+startTime+"'";			
			}
			List<PurDemandC>  list=this.getMapper(PurDemandCMapper.class).findWithCondition(pdc, sWhere, " create_time 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 doAddWeb(CommonPage<PurDemandC> 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;
		}
//		User user2=this.getMapper(UserMapper.class).findById(userId);
		String deand_no=oci.getMemo1();
		try {
			PurDemandM pdm=this.getMapper(PurDemandMMapper.class).findById(deand_no);
			boolean  b=true;//判断需求行是否全为资材备件材料标识，是，修改主信息为资材备件科。
			if("10".equals(pdm.getValidflag())){
			for (PurDemandC pdc : oci.getObjectList()) {
				if(deand_no ==null&&"".equals(deand_no)){
					rm.setState("500");
					rm.setMsgInfo("请传入需求号！");
					return rm;
				}
				
				
				String  demand_seq=deand_no;			
				String maxD = this.getMapper(PurDemandCMapper.class)
						.findMaxDemandSeq(demand_seq);
				if (maxD != null && maxD.length() > 4) {
					String seq = maxD.substring(maxD.length() - 3);
					int iseq = Integer.parseInt(seq) + 1;
					demand_seq += String.format("%03d", iseq);
				} else {
					demand_seq += "001";
				}
				PurMatClass  pmc=this.getMapper(PurMatClassMapper.class).findById(pdc.getMat_class_code());
				if(!"资材备件科".equals(pmc.getUnit_desc())){
					b=false;
				}
				pdc.setDept_code(pmc.getDept_code());//设置部门
				pdc.setDept_desc(pmc.getDept_desc());
				pdc.setUnit_code(pmc.getUnit_code());//设置科室
				pdc.setUnit_desc(pmc.getUnit_desc());
				pdc.setDemand_sqe(demand_seq);
				pdc.setDemand_no(deand_no);
				pdc.setValidflag("10");// 默认10草稿
				pdc.setStatus("00"); // 默认00代提报
				pdc.setCreate_userid(userId);
				pdc.setCreate_name(userName);
				pdc.setCreate_time(new Date());
				this.getMapper(PurDemandCMapper.class).insert(pdc);
				
				
			}
			if(b&&"10".equals(pdm.getValidflag())){
				pdm.setUnit_desc("资材备件科");
				this.getMapper(PurDemandMMapper.class).update(pdm);
			}
			rm.setState("200");
			rm.setMsgInfo("新增成功！");
			}else{
				rm.setState("500");
				rm.setMsgInfo("需求信息不为草稿状态，请刷新界面！");
				return rm;
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("新增失败，请联系管理员");
			throw ex;
		}
		return rm;
	}
	
	/**
	 * 需求行信息修改
	 * 
	 * @param oci
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel upAddWeb(CommonPage<PurDemandC> 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 (PurDemandC pdc : oci.getObjectList()) {
				if ("10".equals(pdc.getValidflag())
						&& ("00".equals(pdc.getStatus()) || "40".equals(pdc
								.getStatus()))) {
				pdc.setUpdate_userid(userId);
				pdc.setUpdate_name(userName);
				pdc.setUpdate_time(new Date());
				this.getMapper(PurDemandCMapper.class).update(pdc);
				}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 upPurDemandC(CommonPage<PurDemandC> 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 (PurDemandC pdc : oci.getObjectList()) {
				if ("20".equals(pdc.getValidflag())
						&& ("10".equals(pdc.getStatus()) || "40".equals(pdc
								.getStatus()))) {
				pdc.setUpdate_userid(userId);
				pdc.setUpdate_name(userName);
				pdc.setUpdate_time(new Date());
				this.getMapper(PurDemandCMapper.class).update(pdc);
				}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 appPurDemandC(CommonPage<PurDemandC> 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 (PurDemandC pdc : oci.getObjectList()) {
				if ("10".equals(pdc.getStatus())) {
				pdc.setStatus("30");
				pdc.setValidflag("00");//数据为无效
				pdc.setUpdate_userid(userId);
				pdc.setUpdate_name(userName);
				pdc.setUpdate_time(new Date());
				this.getMapper(PurDemandCMapper.class).update(pdc);
				List<PurDemandC> list=this.getMapper(PurDemandCMapper.class).findByDemandno(pdc.getDemand_no());
				boolean a=true;
				for (PurDemandC pDc : list) {
					if(!"30".equals(pDc.getStatus())){//如果存在需求信息不为审批拒绝，标志符为false,否则为true；
						a=false;
					}
				}
				if(a){
					PurDemandM pdm=this.getMapper(PurDemandMMapper.class).findById(pdc.getDemand_no());
					pdm.setStatus("30");
					pdm.setValidflag("00");//数据为无效
					this.getMapper(PurDemandMMapper.class).update(pdm);
				}
				}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 doDelete(CommonPage<PurDemandC> 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 (PurDemandC pdc : oci.getObjectList()) {
				//数据状态无效或草稿状态。审批状态待提报、拒绝、退回。可进行删除操作。
				if (("00".equals(pdc.getValidflag())||"10".equals(pdc.getValidflag()))
						&& ("00".equals(pdc.getStatus()) || "30".equals(pdc
								.getStatus())|| "40".equals(pdc
								.getStatus()))) {
				this.getMapper(PurDemandCMapper.class).delete(pdc.getDemand_sqe());	
				}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;
	}
}
