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

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Pms.Demand.mapper.PurBudgetMMapper;
import market.Pms.Demand.service.PurBudgetMService;
import market.Pms.Demand.vo.PurBudgetM;

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="PurBudgetMService")
public class PurBudgetMServiceImpl extends BaseServiceImpl<PurBudgetM,Object> implements PurBudgetMService {
	/**
	 * 预算信息查询
	 * 
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PurBudgetM> oci) {
		ResultModel rm = new ResultModel();
		PurBudgetM pbm=oci.getObject();
//		String departname=pbm.getDepartname();
//		String year=pbm.getYear();
		try {
//			String sWhere=" 1=1 ";
//			if(departname!=null&&!"".equals(departname)){
//				sWhere+="and departname like '%"+departname+"%'";
//			}
//			if(year!=null&&!"".equals(year)){
//				sWhere+=" and year = '"+year+"'";
//			}			
//			List<PurBudgetM>  list=this.getMapper(PurBudgetMMapper.class).findWithCondition(pbm, sWhere, " year desc", 0, 0);
			List<PurBudgetM>  list=this.getMapper(PurBudgetMMapper.class).findPurBudgetM(pbm);
			rm.setData(list);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败，请联系管理员");
			throw ex;
		}
		return rm;	
	}
	
	/**
	 * 预算信息新增
	 * 
	 * @param oci
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doAddWeb(CommonPage<PurBudgetM> 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();
		if (oci.getObjectList() == null || oci.getObjectList().size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请传入需新增的数据！");
			return rm;
		}	
		try {
			for(PurBudgetM pbm:oci.getObjectList()){
				String budget_no="XQ"+new SimpleDateFormat("yyyyMM").format(new Date());
				String maxD = this.getMapper(PurBudgetMMapper.class).findMaxNo(budget_no);
				if(maxD!=null&&maxD.length()>4){
					String seq=maxD.substring(maxD.length()-4);
					int iseq=Integer.parseInt(seq)+1;
					budget_no+=String.format("%04d", iseq);
				}else{
					budget_no+="0001";
				}
				pbm.setBudget_no(budget_no);
				pbm.setValidflag("10");// 默认10草稿
				pbm.setStatus("00"); // 默认00代提报
				pbm.setCreate_userid(userId);
				pbm.setCreate_name(userName);
				pbm.setCreate_time(new Date());
				this.getMapper(PurBudgetMMapper.class).insert(pbm);
			}
					
			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 upAddWeb(CommonPage<PurBudgetM> 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();
		if (oci.getObjectList() == null || oci.getObjectList().size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请选择数据！");
			return rm;
		}	
		try {
			for(PurBudgetM pbm:oci.getObjectList()){
				if ("10".equals(pbm.getValidflag())
						&& ("00".equals(pbm.getStatus()) || "40".equals(pbm.getStatus()))) {
				pbm.setUpdate_userid(userId);
				pbm.setUpdate_name(userName);
				pbm.setUpdate_time(new Date());
				this.getMapper(PurBudgetMMapper.class).update(pbm);
				}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 upPurBudgetM(CommonPage<PurBudgetM> 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();
		if (oci.getObjectList() == null || oci.getObjectList().size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请选择数据！");
			return rm;
		}	
		try {
			for(PurBudgetM pbm:oci.getObjectList()){
				PurBudgetM pbm2=this.getMapper(PurBudgetMMapper.class).findById(pbm.getBudget_no());
				if ("10".equals(pbm2.getValidflag())
						&& ("00".equals(pbm2.getStatus()) || "40".equals(pbm2.getStatus()))) {
				pbm2.setStatus("50"); // 审批通过
				pbm2.setValidflag("20"); // 暂时生效
				pbm2.setUpdate_userid(userId);
				pbm2.setUpdate_name(userName);
				pbm2.setUpdate_time(new Date());
				this.getMapper(PurBudgetMMapper.class).update(pbm2);
				}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 disPurBudgetM(CommonPage<PurBudgetM> 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();
		if (oci.getObjectList() == null || oci.getObjectList().size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请选择数据！");
			return rm;
		}	
		try {
			for(PurBudgetM pbm:oci.getObjectList()){
				PurBudgetM pbm2=this.getMapper(PurBudgetMMapper.class).findById(pbm.getBudget_no());
				if("20".equals(pbm2.getValidflag()) && "50".equals(pbm2.getStatus())){	
				pbm2.setStatus("00"); // 审批通过置为待提报
				pbm2.setValidflag("10"); // 为草稿
				pbm2.setUpdate_userid(userId);
				pbm2.setUpdate_name(userName);
				pbm2.setUpdate_time(new Date());
				this.getMapper(PurBudgetMMapper.class).update(pbm2);
				}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;
	}
}
