/***文档注释***********************************************
 * 作者               :                   易鑫
 * 创建日期      :                   2017.10.27
 * 描述               :        			长度基础信息
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
 ***********************************************************/
package market.Slm.BaseInformation.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import market.Com.pb.DoubleUtil;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.BaseInformation.mapper.SlmBaseUnitConversionMapper;
import market.Slm.BaseInformation.service.SlmBaseUnitConversionService;
import market.Slm.BaseInformation.vo.SlmBaseUnitConversion;

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

@RemoteService(path="SlmBaseUnitConversionService")
public class SlmBaseUnitConversionServiceImpl extends BaseServiceImpl<SlmBaseUnitConversion,Object> implements SlmBaseUnitConversionService {
	/**
	 * 查询
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<SlmBaseUnitConversion> sum) {
		ResultModel rm=new ResultModel();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		try{
			PageModel<SlmBaseUnitConversion> pm = new PageModel<SlmBaseUnitConversion>(pageIndex,pageSize);
			SlmBaseUnitConversion sb=sum.getObject();
			String sWhere="";

			pm.setRows(this.getMapper(SlmBaseUnitConversionMapper.class).findWithCondition(sb, sWhere, " create_time desc ", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(SlmBaseUnitConversionMapper.class).countWithCondition(sb,sWhere));

			rm.setState("200");
			rm.setData(pm);
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;

	}

	/**
	 * 添加数据
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doAddWeb(CommonPage<SlmBaseUnitConversion> sum) {
		ResultModel rm=new ResultModel();
		SlmBaseUnitConversion bd=sum.getObject();
		try {
			SlmBaseUnitConversion conversion=new SlmBaseUnitConversion();
			conversion.setConversion_unit_no1(bd.getConversion_unit_no1());
			conversion.setConversion_unit_no2(bd.getConversion_unit_no2());
			int count=this.getMapper(SlmBaseUnitConversionMapper.class).count(conversion);
			if(count>0){
				rm.setMsgInfo("已存在相同的换算规则");
				rm.setState("500");
				return rm;
			}
			String No=new SimpleDateFormat("yyyyMMdd").format(new Date());
			int icount=this.getMapper(SlmBaseUnitConversionMapper.class).countWithCondition(null," Conversion_no like '"+No+"%' ")+1;
			bd.setConversion_no(No+String.format("%04d", icount));

			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			bd.setCreate_man_no(userId);
			bd.setCreate_man_name(userName);
			bd.setCreate_time(new Date());
			bd.setValid_flag("1");
			this.getMapper(SlmBaseUnitConversionMapper.class).insert(bd);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，新增失败!");
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 修改数据
	 * @param bd
	 * @return
	 */
	public ResultModel doUpdateWeb(CommonPage<SlmBaseUnitConversion> sum) {
		ResultModel rm=new ResultModel();
		SlmBaseUnitConversion obd = sum.getObject();
		try {
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			obd.setUpdate_man_no(userId);
			obd.setUpdate_man_name(userName);
			obd.setUpdate_time(new Date());
			this.getMapper(SlmBaseUnitConversionMapper.class).update(obd);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("修改失败!");
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 启用、停用
	 * @param bd
	 * @return
	 */
	public ResultModel doSwitchWeb(CommonPage<SlmBaseUnitConversion> sum) throws Exception{
		ResultModel rm=new ResultModel();
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		List<SlmBaseUnitConversion> list = sum.getObjectList();
		try {
			if(list!=null){
				for(SlmBaseUnitConversion conversion:list){
					if("0".equals(conversion.getValid_flag())){
						conversion.setDelete_man_no(userId);
						conversion.setDelete_man_name(userName);
						conversion.setDelete_time(new Date());
					}
					this.getMapper(SlmBaseUnitConversionMapper.class).update(conversion);
				}
			}
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			e.printStackTrace();
			throw e;
		}
		return rm;
	}
	
	//单位转换
	public Double doUnitConversion(Double value,String unit1,String unit2){
		if(value==null)
			return value;
		Double outValue=0d;
		SlmBaseUnitConversion conversion=new SlmBaseUnitConversion();
		conversion.setConversion_unit_no1(unit1);
		conversion.setConversion_unit_no2(unit2);
		conversion.setValid_flag("1");
		List<SlmBaseUnitConversion> conversionList=this.getMapper(SlmBaseUnitConversionMapper.class).find(conversion, 0, 0);
		if(conversionList.size()>0){
			SlmBaseUnitConversion con=conversionList.get(0);
			if("*".equals(con.getConversion_sign())){
				outValue=DoubleUtil.mul(value,con.getRate());
			}else if("/".equals(con.getConversion_sign())){
				outValue=DoubleUtil.div(value,con.getRate());
			}else if("+".equals(con.getConversion_sign())){
				outValue=DoubleUtil.add(value,con.getRate());
			}else if("-".equals(con.getConversion_sign())){
				outValue=DoubleUtil.sub(value,con.getRate());
			}else{
				outValue=value;
			}
		}else{
			outValue=value;
		}
		return outValue;
	}
}
