/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-10-15
 * 描述            ：原材料贸易卷收发存报表的期末数据
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Lms.Warehouse.impl;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import market.Cm.vo.BaseCostItem;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Warehouse.mapper.YdmReportFinalResultMapper;
import market.Lms.Warehouse.service.YdmReportFinalResultService;
import market.Lms.Warehouse.vo.YdmReportFinalResult;

import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;

@RemoteService(path="YdmReportFinalResultService")
public class YdmReportFinalResultServiceImpl extends BaseServiceImpl<YdmReportFinalResult,Object> implements YdmReportFinalResultService {
	
	/**
	 * (获取牌号信息)原材料收发存，贸易卷收发存
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQaurySteelInfoForReport(CommonPage<YdmReportFinalResult> cp) throws Exception {
		ResultModel rm = new ResultModel();		
		List<BaseCostItem> bciList = this.getMapper(YdmReportFinalResultMapper.class).doQaurySteelInfoForReport();
		rm.setData(bciList);
		rm.setState("200");
		rm.setMsgInfo("查询牌号成功");
		return rm;
	}
	
	/**
	 * 收发存的本月新增
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQauryAddInfoForReport(CommonPage<YdmReportFinalResult> cp) throws Exception {
		ResultModel rm = new ResultModel();
		YdmReportFinalResult yrf = cp.getObject();
		String pageType = yrf.getPage_type();
		List<YdmReportFinalResult> yrfList = null;
		if ("0".equals(pageType)) {
			//原材料本月新增
			yrfList = this.getMapper(YdmReportFinalResultMapper.class).doQauryAddMaterialForReport(yrf);			
		}else if ("1".equals(pageType)) {
			//贸易卷本月新增
			yrfList = this.getMapper(YdmReportFinalResultMapper.class).doQauryAddTradeForReport(yrf);
		}
		rm.setData(yrfList);
		rm.setState("200");
		rm.setMsgInfo("查询本月增加成功");
		return rm;
	}
	
	/**
	 * 收发存的本月减少
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQauryReduceInfoForReport(CommonPage<YdmReportFinalResult> cp) throws Exception {
		ResultModel rm = new ResultModel();
		YdmReportFinalResult yrf = cp.getObject();
		String pageType = yrf.getPage_type();
		List<YdmReportFinalResult> yrfList = null;
		if ("0".equals(pageType)) {
			//原材料本月减少
			yrfList = this.getMapper(YdmReportFinalResultMapper.class).doQauryReduceMaterialForReport(yrf);			
		}else if ("1".equals(pageType)) {
			//贸易卷本月减少
			yrfList = this.getMapper(YdmReportFinalResultMapper.class).doQauryReduceTradeForReport(yrf);
		}
		rm.setData(yrfList);
		rm.setState("200");
		rm.setMsgInfo("查询本月减少成功");
		return rm;
	}
	
	/**
	 * 获取上月库存（即本月期初）
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQauryStartInfoForReport(CommonPage<YdmReportFinalResult> cp) throws Exception {
		ResultModel rm = new ResultModel();
		YdmReportFinalResult yrf = cp.getObject();
		//获取上月的成本期间
		//原材料本月期初
		String condition = "";
		if (yrf.getPrd_num()!=null && !"".equals(yrf.getPrd_num())) {
			SimpleDateFormat sd = new SimpleDateFormat("yyyyMM");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(sd.parse(yrf.getPrd_num()));
			calendar.add(Calendar.MONTH, -1);
			String lastPrd_num = sd.format(calendar.getTime());
			condition += " and prd_num = '"+lastPrd_num+"'";
		}
		if (yrf.getPage_type()!=null && !"".equals(yrf.getPage_type())) {
			condition += " and page_type = '"+yrf.getPage_type()+"'";
		}
		List<YdmReportFinalResult> yrfList = this.getMapper(YdmReportFinalResultMapper.class).findWithCondition(null, condition, "materailtype", 0, 0);			
		rm.setData(yrfList);
		rm.setState("200");
		rm.setMsgInfo("查询本月期初成功");
		return rm;
	}
	
	/**
	 * 保存本月的期末数据
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doInsertYdmReportFinalResult(CommonPage<YdmReportFinalResult> cp) throws Exception {
		ResultModel rm = new ResultModel();
		List<YdmReportFinalResult> yrfList = cp.getObjectList();
		if (yrfList==null || yrfList.size()<=0) {
			throw new Exception("请传入参数");
		}
		String prdnum = yrfList.get(0).getPrd_num();
		String pageType = yrfList.get(0).getPage_type();
				
		//判断是否已经进行了核算
		String condition = " and prd_num = '"+prdnum+"' and page_type = '"+pageType+"'";
		int count = this.getMapper(YdmReportFinalResultMapper.class).countWithCondition(null, condition);
		if (count>0) {
			throw new Exception("成本期间【"+prdnum+"】已经进行核算");
		}
		for (YdmReportFinalResult yrf : yrfList) {
			this.getMapper(YdmReportFinalResultMapper.class).insert(yrf);
		}
		rm.setState("200");		
		rm.setMsgInfo("保存本月期末数据成功");
		return rm;
	}
	
	/**
	 * 撤销本月的期末数据
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doDeleteYdmReportFinalResult(CommonPage<YdmReportFinalResult> cp) throws Exception {
		ResultModel rm = new ResultModel();
		YdmReportFinalResult yrf = cp.getObject();
		if (yrf==null) {
			throw new Exception("请传入参数");
		}
		this.getMapper(YdmReportFinalResultMapper.class).doDeleteYdmReportFinalResult(yrf);		
		rm.setState("200");		
		rm.setMsgInfo("删除本月期末数据成功");
		return rm;
	}
	
	
}
