package market.Lms.FixedAssets.impl;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Com.pb.PbMethod;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.FixedAssets.mapper.LmsFixedAssetsChangeInfoMapper;
import market.Lms.FixedAssets.mapper.LmsFixedAssetsChangeOldMapper;
import market.Lms.FixedAssets.mapper.LmsFixedAssetsHeatInfoMapper;
import market.Lms.FixedAssets.mapper.LmsFixedAssetsInfoMapper;
import market.Lms.FixedAssets.service.LmsFixedAssetsChangeSumService;
import market.Lms.FixedAssets.vo.LmsFixedAssetsChangeInfo;
import market.Lms.FixedAssets.vo.LmsFixedAssetsChangeOld;
import market.Lms.FixedAssets.vo.LmsFixedAssetsChangeSum;
import market.Lms.FixedAssets.vo.LmsFixedAssetsHeatInfo;
import market.Lms.FixedAssets.vo.LmsFixedAssetsInfo;
import market.Lms.FixedAssets.vo.LmsFixedAssetsSum;

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="LmsFixedAssetsChangeSum")
public class LmsFixedAssetsChangeSumServiceImpl extends BaseServiceImpl<LmsFixedAssetsSum,Object> implements LmsFixedAssetsChangeSumService{

	//==============================================web==========================================
	/**资产变更查询资产信息以及资产入库主表
	 * @param snCode
	 * @return
	 */
	public ResultModel doQueryBySnCodeWeb(CommonPage<LmsFixedAssetsSum> sum){
		ResultModel rm = new ResultModel();
		LmsFixedAssetsChangeSum changeSum = new LmsFixedAssetsChangeSum();
		String snCode = sum.getMemo1();
		try{
			LmsFixedAssetsInfo assetsInfo = this.getMapper(LmsFixedAssetsInfoMapper.class).findById(snCode);
			LmsFixedAssetsHeatInfo heatInfo = this.getMapper(LmsFixedAssetsHeatInfoMapper.class).findById(assetsInfo.getIn_heat_no());
			changeSum.setAssetsInfo(assetsInfo);
			changeSum.setHeatInfo(heatInfo);
			
			rm.setData(changeSum);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
		}
		return rm;
	}
	
	/**web端保存 （传递过来的数据为资产信息必传 SN_CODE，IN_LINE_NO，IN_HEAT_NO和变化了数据）
	 * （达到目的：将变化了的数据存储在资产变更信息表中，将原资产表中的数据存储在资产变更原纪录表中，将变更的数据更新到资产表中）
	 * @param sum
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSubmitWeb(LmsFixedAssetsSum sum) throws Exception{
		ResultModel rm = new ResultModel();
		String changeNo = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3);
		try{
			LmsFixedAssetsChangeInfo changeInfo = sum.getChangInfo();
			if(changeInfo != null){
				//1 变化的数据存储到变更表
				changeInfo.setChange_no(changeNo);
				this.getBean(LmsFixedAssetsChangeInfoServiceImpl.class).doAdd(changeInfo);

				String snCode = changeInfo.getSn_code();
				String heatNo = changeInfo.getIn_heat_no();
				LmsFixedAssetsInfo assets = this.getBean(LmsFixedAssetsInfoServiceImpl.class).doQuery(snCode);
				LmsFixedAssetsHeatInfo heat = this.getBean(LmsFixedAssetsHeatInfoServiceImpl.class).doQuery(heatNo);
				//2得到原资产信息存储到变更原纪录表中
				LmsFixedAssetsChangeOld changeOld = new LmsFixedAssetsChangeOld();
				copy(assets, changeOld);
				changeOld.setDelivery_no(heat.getDelivery_no());
				changeOld.setSupplier_no(heat.getSupplier_no());
				changeOld.setSupplier_name(heat.getSupplier_name());
				changeOld.setIn_date(heat.getIn_date());
				changeOld.setIn_type_no(heat.getIn_type_no());
				changeOld.setIn_type_name(heat.getIn_type_name());
				changeOld.setChange_no(changeNo);
				changeOld.setPrice(assets.getPrice());
				this.getBean(LmsFixedAssetsChangeOldServiceImpl.class).doAdd(changeOld);

				//3将变化的数据更新到资产表中
				LmsFixedAssetsInfo newAssets = new LmsFixedAssetsInfo();
				copy(changeInfo, newAssets);
				this.getBean(LmsFixedAssetsInfoServiceImpl.class).doUpdate(newAssets);
				
				//4修改入库单主表信息
				LmsFixedAssetsHeatInfo heatInfo = this.getBean(LmsFixedAssetsHeatInfoServiceImpl.class).doQuery(changeInfo.getIn_heat_no());
				copy(changeInfo,heatInfo);
				this.getBean(LmsFixedAssetsHeatInfoServiceImpl.class).doUpdate(heatInfo);
			}

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

	/**Web端根据条件查询（1资产类别  2资产名称  3SN序号 ）
	 * 暂定SN序号模糊查询
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<LmsFixedAssetsChangeInfo> sum){
		ResultModel rm = new ResultModel();
		LmsFixedAssetsChangeInfo changeInfo = sum.getObject();
		int pageIndex = sum.getPageIndex();
		int pageSize = sum.getPageSize();
		String beginTime = sum.getStartTime();
		String endTime = sum.getEndTime();
		List<LmsFixedAssetsChangeInfo> changeInfoList = new ArrayList<LmsFixedAssetsChangeInfo>();
		PageModel<LmsFixedAssetsChangeInfo> pm=new PageModel<LmsFixedAssetsChangeInfo>(pageIndex, pageSize);
		int count = 0;
		try{
			changeInfoList = this.getMapper(LmsFixedAssetsChangeInfoMapper.class).doQueryList(changeInfo,beginTime,endTime,(pageIndex-1)*pageSize, pageIndex*pageSize);
			count = this.getMapper(LmsFixedAssetsChangeInfoMapper.class).countList(changeInfo,beginTime,endTime);
			pm.setRows(changeInfoList);
			pm.setTotal(count);
			rm.setData(pm);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
		}
		return rm;
	}

	/**web端查询详情
	 * @param sum
	 * @return
	 */
	public ResultModel doQueryDetailWeb(CommonPage<LmsFixedAssetsChangeInfo> sum){
		ResultModel rm = new ResultModel();
		String changeNo = sum.getMemo1();
		LmsFixedAssetsSum receiveSum = new LmsFixedAssetsSum();
		try{
			//1 资产变更表信息
			LmsFixedAssetsChangeInfo changeInfo = this.getMapper(LmsFixedAssetsChangeInfoMapper.class).findById(changeNo);

			//2 资产变更原表信息
			LmsFixedAssetsChangeOld changeOld = this.getMapper(LmsFixedAssetsChangeOldMapper.class).findById(changeNo);

			receiveSum.setChangInfo(changeInfo);
			receiveSum.setChangeOld(changeOld);
			rm.setData(receiveSum);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
		}
		return rm;
	}

	//==============================================app==========================================
	/**资产变更查询资产信息以及资产入库主表
	 * @param snCode
	 * @return
	 */
	public LmsFixedAssetsChangeSum doQueryBySnCodeApp(String snCode){
		LmsFixedAssetsChangeSum changeSum = new LmsFixedAssetsChangeSum();
		try{
			LmsFixedAssetsInfo assetsInfo = this.getMapper(LmsFixedAssetsInfoMapper.class).findById(snCode);
			LmsFixedAssetsHeatInfo heatInfo = this.getMapper(LmsFixedAssetsHeatInfoMapper.class).findById(assetsInfo.getIn_heat_no());
			changeSum.setAssetsInfo(assetsInfo);
			changeSum.setHeatInfo(heatInfo);
		}catch(Exception e){
			return new LmsFixedAssetsChangeSum();
		}
		return changeSum;
	}
	
	/**web端保存 （传递过来的数据为资产信息必传 SN_CODE，IN_LINE_NO，IN_HEAT_NO和变化了数据）
	 * （达到目的：将变化了的数据存储在资产变更信息表中，将原资产表中的数据存储在资产变更原纪录表中，将变更的数据更新到资产表中）
	 *	@param sum
	 * @return
	 * @throws Exception 
	 */
	public ResultModel doSubmitApp(LmsFixedAssetsSum sum) throws Exception{
		ResultModel rm = new ResultModel();
		String changeNo = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3);
		try{
			LmsFixedAssetsChangeInfo changeInfo = sum.getChangInfo();
			if(changeInfo != null){
				//1 变化的数据存储到变更表
				changeInfo.setChange_no(changeNo);
				this.getBean(LmsFixedAssetsChangeInfoServiceImpl.class).doAdd(changeInfo);

				String snCode = changeInfo.getSn_code();
				String heatNo = changeInfo.getIn_heat_no();
				LmsFixedAssetsInfo assets = this.getBean(LmsFixedAssetsInfoServiceImpl.class).doQuery(snCode);
				LmsFixedAssetsHeatInfo heat = this.getBean(LmsFixedAssetsHeatInfoServiceImpl.class).doQuery(heatNo);
				//2得到原资产信息存储到变更原纪录表中
				LmsFixedAssetsChangeOld changeOld = new LmsFixedAssetsChangeOld();
				copy(assets, changeOld);
				changeOld.setDelivery_no(heat.getDelivery_no());
				changeOld.setSupplier_no(heat.getSupplier_no());
				changeOld.setSupplier_name(heat.getSupplier_name());
				changeOld.setIn_date(heat.getIn_date());
				changeOld.setIn_type_no(heat.getIn_type_no());
				changeOld.setIn_type_name(heat.getIn_type_name());
				changeOld.setChange_no(changeNo);
				changeOld.setPrice(assets.getPrice());
				this.getBean(LmsFixedAssetsChangeOldServiceImpl.class).doAdd(changeOld);

				//3将变化的数据更新到资产表中
				LmsFixedAssetsInfo newAssets = new LmsFixedAssetsInfo();
				copy(changeInfo, newAssets);
				this.getBean(LmsFixedAssetsInfoServiceImpl.class).doUpdate(newAssets);
				
				//4修改入库单主表信息
				LmsFixedAssetsHeatInfo heatInfo = this.getBean(LmsFixedAssetsHeatInfoServiceImpl.class).doQuery(changeInfo.getIn_heat_no());
				copy(changeInfo,heatInfo);
				this.getBean(LmsFixedAssetsHeatInfoServiceImpl.class).doUpdate(heatInfo);
			}

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

	/**Web端根据条件查询（1资产类别  2资产名称  3SN序号 ）
	 * 暂定SN序号模糊查询
	 * @return
	 */
	public List<LmsFixedAssetsChangeInfo> doQueryApp(CommonPage<LmsFixedAssetsChangeInfo> sum){
		LmsFixedAssetsChangeInfo changeInfo = sum.getObject();
		int pageIndex = sum.getPageIndex();
		int pageSize = sum.getPageSize();
		String beginTime = sum.getStartTime();
		String endTime = sum.getEndTime();
		List<LmsFixedAssetsChangeInfo> changeInfoList = new ArrayList<LmsFixedAssetsChangeInfo>();
		try{
			changeInfoList = this.getMapper(LmsFixedAssetsChangeInfoMapper.class).doQueryList(changeInfo,beginTime,endTime,pageIndex, pageSize);
		}catch(Exception e){
			return new ArrayList<LmsFixedAssetsChangeInfo>();
		}
		return changeInfoList;
	}

	/**web端查询详情
	 * @param sum
	 * @return
	 */
	public LmsFixedAssetsSum doQueryDetailApp(String changeNo){
		LmsFixedAssetsSum receiveSum = new LmsFixedAssetsSum();
		try{
			//1 资产变更表信息
			LmsFixedAssetsChangeInfo changeInfo = this.getMapper(LmsFixedAssetsChangeInfoMapper.class).findById(changeNo);

			//2 资产变更原表信息
			LmsFixedAssetsChangeOld changeOld = this.getMapper(LmsFixedAssetsChangeOldMapper.class).findById(changeNo);

			receiveSum.setChangInfo(changeInfo);
			receiveSum.setChangeOld(changeOld);
		}catch(Exception e){
			return new LmsFixedAssetsSum();
		}
		return receiveSum;
	}


	/**
	 * 一个对象从另一个对象赋值字段
	 * @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;
	}
}
