package market.Lms.FixedAssets.impl;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.FixedAssets.mapper.LmsFixedAssetsAnnexMapper;
import market.Lms.FixedAssets.mapper.LmsFixedAssetsHeatInfoMapper;
import market.Lms.FixedAssets.mapper.LmsFixedAssetsInfoMapper;
import market.Lms.FixedAssets.mapper.LmsFixedAssetsLineInfoMapper;
import market.Lms.FixedAssets.service.LmsFixedAssetsInSumService;
import market.Lms.FixedAssets.vo.LmsFixedAssetsAnnex;
import market.Lms.FixedAssets.vo.LmsFixedAssetsHeatInfo;
import market.Lms.FixedAssets.vo.LmsFixedAssetsInSum;
import market.Lms.FixedAssets.vo.LmsFixedAssetsInfo;
import market.Lms.FixedAssets.vo.LmsFixedAssetsLineInfo;
import market.Lms.FixedAssets.vo.LmsFixedAssetsSum;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

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

@RemoteService(path="LmsFixedAssetsInSum")
public class LmsFixedAssetsInSumServiceImpl extends BaseServiceImpl<LmsFixedAssetsInSum,Object> implements LmsFixedAssetsInSumService{

	private Logger logger = Logger.getLogger(LmsFixedAssetsInSumServiceImpl.class);
	//=====================================================WEB============================
	/**
	 * web端资产入库提交
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSubmitWeb(LmsFixedAssetsInSum assetsSum) throws Exception{
		ResultModel rm = new ResultModel();
		String heatNo = "";
		String lintNo = "";
		String assetsNo = "";

		List<LmsFixedAssetsLineInfo> lineList = assetsSum.getAssetsLineInfo();
		List<LmsFixedAssetsInfo> assetsList = this.getMapper(LmsFixedAssetsInfoMapper.class).find(null, 0, 0);
		if(lineList!=null){
			for(LmsFixedAssetsLineInfo line : lineList){
				String snCode = line.getSn_code();
				if(assetsList!=null){
					for(LmsFixedAssetsInfo ofa : assetsList){
						if(snCode.equals(ofa.getSn_code())){
							rm.setState("500");
							rm.setMsgInfo("产品序列号已经存在请进行核对");
							return rm;
						}
					}
				}
			}
		}
		for(int a=0;a<lineList.size()-1;a++){
			for(int b=a+1;b<lineList.size();b++){
				if(lineList.get(a).getSn_code().equals(lineList.get(b).getSn_code())){
					rm.setState("500");
					rm.setMsgInfo("输入的产品序列号有重复数据");
					return rm;
				}
			}
		}
		try{
			//入库主表新增
			if(assetsSum.getAssetsHeatInfo() != null){
				heatNo = this.getBean(LmsFixedAssetsHeatInfoServiceImpl.class).doAdd(assetsSum.getAssetsHeatInfo());
			}

			//入库副表新增
			if(lineList!=null){
				for(LmsFixedAssetsLineInfo line : lineList){
					//入库工作1  入库副表新增记录
					line.setHeat_no(heatNo);
					lintNo = this.getBean(LmsFixedAssetsLineInfoServiceImpl.class).doAdd(line);

					//入库工作2  在资产信息表中插入该记录
					LmsFixedAssetsInfo ofai = new LmsFixedAssetsInfo();
					//拷贝
					copy(line, ofai);
					ofai.setIn_heat_no(heatNo);
					ofai.setIn_line_no(lintNo);
					assetsNo = this.getBean(LmsFixedAssetsInfoServiceImpl.class).doAdd(ofai);

					//入库工作3 在附件表中插入附件信息(附件信息直接关联资产)
					if(line.getAnnexList()!=null){
						for(LmsFixedAssetsAnnex annex : line.getAnnexList()){
							annex.setNews_no(assetsNo);
							this.getBean(LmsFixedAssetsAnnexServiceImpl.class).doAdd(annex);
						}
					}
				}
			}

			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			logger.error(e.getMessage());
			throw e;
		}
		return rm;
	}

	/**Web端根据条件查询（1入库单号 2入库人 3入库类型  4购入时间）
	 * 暂定人员模糊查询
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<LmsFixedAssetsHeatInfo> sum){
		ResultModel rm = new ResultModel();
		LmsFixedAssetsHeatInfo heat = sum.getObject();
		int pageIndex = sum.getPageIndex();
		int pageSize = sum.getPageSize();
		String beginTime = sum.getStartTime();
		String endTime = sum.getEndTime();
		List<LmsFixedAssetsHeatInfo> heatList = new ArrayList<LmsFixedAssetsHeatInfo>();
		PageModel<LmsFixedAssetsHeatInfo> pm=new PageModel<LmsFixedAssetsHeatInfo>(pageIndex, pageSize);
		int count = 0;
		try{
			heatList = this.getMapper(LmsFixedAssetsHeatInfoMapper.class).doQueryList(heat,beginTime,endTime,(pageIndex-1)*pageSize, pageIndex*pageSize);
			count = this.getMapper(LmsFixedAssetsHeatInfoMapper.class).countList(heat,beginTime,endTime);
			pm.setRows(heatList);
			pm.setTotal(count);
			rm.setData(pm);
			rm.setState("200");
		}catch(Exception e){
			e.printStackTrace();
			rm.setState("500");
		}
		return rm;
	}

	/**查询详情
	 * @param sum
	 * @return
	 */
	public ResultModel doQueryDetailWeb(CommonPage<LmsFixedAssetsHeatInfo> sum){
		ResultModel rm = new ResultModel();
		String heatNo = sum.getMemo1();
		LmsFixedAssetsSum assetsSum = new LmsFixedAssetsSum();
		try{
			if(null != heatNo && !heatNo.isEmpty()){
				//查询主表
				LmsFixedAssetsHeatInfo heat = this.getMapper(LmsFixedAssetsHeatInfoMapper.class).findById(heatNo);

				//查询资产
				List<LmsFixedAssetsInfo> assetsList = new ArrayList<LmsFixedAssetsInfo>();
				LmsFixedAssetsLineInfo line = new LmsFixedAssetsLineInfo();
				line.setHeat_no(heatNo);
				List<LmsFixedAssetsLineInfo> assetsLineInfo = this.getMapper(LmsFixedAssetsLineInfoMapper.class).find(line, 0, 0);

				if(assetsLineInfo!=null){
					for(LmsFixedAssetsLineInfo lineInfo : assetsLineInfo){
						LmsFixedAssetsInfo assets = this.getMapper(LmsFixedAssetsInfoMapper.class).findById(lineInfo.getSn_code());

						//资产附件
						LmsFixedAssetsAnnex annex = new LmsFixedAssetsAnnex();
						annex.setNews_no(assets.getSn_code());
						List<LmsFixedAssetsAnnex> annexList = this.getMapper(LmsFixedAssetsAnnexMapper.class).find(annex, 0, 0);
						assets.setAnnexList(annexList);

						assetsList.add(assets);
					}
				}

				assetsSum.setHeatInfo(heat);
				assetsSum.setAssetsList(assetsList);

				rm.setData(assetsSum);
				rm.setState("200");
			}
		}catch(Exception e){
			rm.setState("500");
		}
		return rm;
	}

	//=====================================================APP============================
	//web端资产入库提交
	public ResultModel doSubmitApp(LmsFixedAssetsInSum assetsSum) throws Exception{
		ResultModel rm = new ResultModel();
		String heatNo = "";
		String lintNo = "";
		String assetsNo = "";
		List<LmsFixedAssetsLineInfo> lineList = assetsSum.getAssetsLineInfo();
		List<LmsFixedAssetsInfo> assetsList = this.getMapper(LmsFixedAssetsInfoMapper.class).find(null, 0, 0);
		if(lineList!=null){
			for(LmsFixedAssetsLineInfo line : lineList){
				String snCode = line.getSn_code();
				if(assetsList!=null){
					for(LmsFixedAssetsInfo ofa : assetsList){
						if(snCode.equals(ofa.getSn_code())){
							rm.setState("500");
							rm.setMsgInfo("产品序列号已经存在请进行核对");
							return rm;
						}
					}
				}
			}
		}
		for(int a=0;a<lineList.size()-1;a++){
			for(int b=a+1;b<lineList.size();b++){
				if(lineList.get(a).getSn_code().equals(lineList.get(b).getSn_code())){
					rm.setState("500");
					rm.setMsgInfo("输入的产品序列号有重复数据");
					return rm;
				}
			}
		}
		try{
			//入库主表新增
			if(assetsSum.getAssetsHeatInfo() != null){
				heatNo = this.getBean(LmsFixedAssetsHeatInfoServiceImpl.class).doAdd(assetsSum.getAssetsHeatInfo());
			}
			//入库副表新增
			if(lineList!=null){
				for(LmsFixedAssetsLineInfo line : lineList){
					//入库工作1  入库副表新增记录
					line.setHeat_no(heatNo);
					lintNo = this.getBean(LmsFixedAssetsLineInfoServiceImpl.class).doAdd(line);

					//入库工作2  在资产信息表中插入该记录
					LmsFixedAssetsInfo ofai = new LmsFixedAssetsInfo();
					//拷贝
					copy(line, ofai);
					ofai.setIn_heat_no(heatNo);
					ofai.setIn_line_no(lintNo);
					assetsNo = this.getBean(LmsFixedAssetsInfoServiceImpl.class).doAdd(ofai);

					//入库工作3 在附件表中插入附件信息(附件信息直接关联资产)
					if(line.getAnnexList()!=null){
						for(LmsFixedAssetsAnnex annex : line.getAnnexList()){
							annex.setNews_no(assetsNo);
							this.getBean(LmsFixedAssetsAnnexServiceImpl.class).doAdd(annex);
						}
					}
				}
			}

			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
		}
		return rm;
	}

	/**Web端根据条件查询（1入库单号 2入库人 3入库类型  4购入时间）
	 * 暂定人员模糊查询
	 * @return
	 */
	public List<LmsFixedAssetsHeatInfo> doQueryApp(CommonPage<LmsFixedAssetsHeatInfo> sum){
		LmsFixedAssetsHeatInfo heat = sum.getObject();
		int pageIndex = sum.getPageIndex();
		int pageSize = sum.getPageSize();
		String beginTime = sum.getStartTime();
		String endTime = sum.getEndTime();
		List<LmsFixedAssetsHeatInfo> heatList = new ArrayList<LmsFixedAssetsHeatInfo>();
		try{
			heatList = this.getMapper(LmsFixedAssetsHeatInfoMapper.class).doQueryList(heat,beginTime,endTime,pageIndex, pageSize);
		}catch(Exception e){
			return new ArrayList<LmsFixedAssetsHeatInfo>();
		}
		return heatList;
	}

	/**查询详情
	 * @param sum
	 * @return
	 */
	public LmsFixedAssetsSum doQueryDetailApp(String heatNo){
		LmsFixedAssetsSum assetsSum = new LmsFixedAssetsSum();
		try{
			if(null != heatNo && !heatNo.isEmpty()){
				//查询主表
				LmsFixedAssetsHeatInfo heat = this.getMapper(LmsFixedAssetsHeatInfoMapper.class).findById(heatNo);

				//查询资产
				List<LmsFixedAssetsInfo> assetsList = new ArrayList<LmsFixedAssetsInfo>();
				LmsFixedAssetsLineInfo line = new LmsFixedAssetsLineInfo();
				line.setHeat_no(heatNo);
				List<LmsFixedAssetsLineInfo> assetsLineInfo = this.getMapper(LmsFixedAssetsLineInfoMapper.class).find(line, 0, 0);

				if(assetsLineInfo!=null){
					for(LmsFixedAssetsLineInfo lineInfo : assetsLineInfo){
						LmsFixedAssetsInfo assets = this.getMapper(LmsFixedAssetsInfoMapper.class).findById(lineInfo.getSn_code());

						//资产附件
						LmsFixedAssetsAnnex annex = new LmsFixedAssetsAnnex();
						annex.setNews_no(assets.getSn_code());
						List<LmsFixedAssetsAnnex> annexList = this.getMapper(LmsFixedAssetsAnnexMapper.class).find(annex, 0, 0);
						assets.setAnnexList(annexList);

						assetsList.add(assets);
					}
				}

				assetsSum.setHeatInfo(heat);
				assetsSum.setAssetsList(assetsList);

			}
		}catch(Exception e){
			return new LmsFixedAssetsSum();
		}
		return assetsSum;
	}
	
	/**
	 * 一个对象从另一个对象赋值字段
	 * @param vv
	 * @param tt
	 * @return
	 * @throws Exception
	 */
	private static  <V, T> T  copy(V vv,T tt) throws Exception{
		BeanInfo sourceBean = Introspector.getBeanInfo(vv.getClass(),Object.class);  
		PropertyDescriptor[] sourceProperty = sourceBean.getPropertyDescriptors();  
		BeanInfo destBean = Introspector.getBeanInfo(tt.getClass(),Object.class);  
		PropertyDescriptor[] destProperty = destBean.getPropertyDescriptors();  
		try {  
			for (int i = 0; i < sourceProperty.length; i++) {  

				for (int j = 0; j < destProperty.length; j++) {  

					if (sourceProperty[i].getName().equals(destProperty[j].getName())  && sourceProperty[i].getPropertyType() == destProperty[j].getPropertyType()) {  
						destProperty[j].getWriteMethod().invoke(tt,sourceProperty[i].getReadMethod().invoke(vv));  
						break;  
					}  
				}  
			}  
		} catch (Exception e) {  

		}  
		return tt;
	}

}
