/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-01-12
 * 描述            ：供货单位信息表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Slm.BaseInformation.impl;
import java.text.SimpleDateFormat;
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.Slm.BaseInformation.mapper.SlmBaseSupplierUnitMapper;
import market.Slm.BaseInformation.service.SlmBaseSupplierUnitService;
import market.Slm.BaseInformation.vo.SlmBaseSupplierUnit;

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="SlmBaseSupplierUnitService")
public class SlmBaseSupplierUnitServiceImpl extends BaseServiceImpl<SlmBaseSupplierUnit,Object> implements SlmBaseSupplierUnitService {
	/**
	 * 查询
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<SlmBaseSupplierUnit> sum) {
		ResultModel rm=new ResultModel();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		PageModel<SlmBaseSupplierUnit> pm = new PageModel<SlmBaseSupplierUnit>(pageIndex,pageSize);
		SlmBaseSupplierUnit sb=sum.getObject();
		try{
			String sWhere=" 1=1 ";
			if(sb.getSupplier_unit_name()!=null&&!"".equals(sb.getSupplier_unit_name())){
				sWhere+=" and supplier_unit_name like concat(concat('%','"+sb.getSupplier_unit_name()+"'),'%')";
				sb.setSupplier_unit_name(null);
			}
			pm.setRows(this.getMapper(SlmBaseSupplierUnitMapper.class).findWithCondition(sb, sWhere, " supplier_unit_name,create_time desc ", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(SlmBaseSupplierUnitMapper.class).countWithCondition(sb,sWhere));
			rm.setState("200");
			rm.setData(pm);
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败"+ex.getMessage());
			ex.printStackTrace();
		}
		return rm;

	}

	/**
	 * 添加数据
	 */
	public ResultModel doAddWeb(CommonPage<SlmBaseSupplierUnit> sum) {
		ResultModel rm=new ResultModel();
		SlmBaseSupplierUnit bd=sum.getObject();
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {
			SlmBaseSupplierUnit unit=new SlmBaseSupplierUnit();
			unit.setSupplier_unit_name(bd.getSupplier_unit_name());
			List<SlmBaseSupplierUnit> list=this.getMapper(SlmBaseSupplierUnitMapper.class).find(unit,0,0);
			if(list.size()>0&&list.get(0).getSupplier_unit_name().equals(bd.getSupplier_unit_name())){
				bd.setSupplier_unit_no(list.get(0).getSupplier_unit_no());
				bd.setUpdate_man_no(userId);
				bd.setUpdate_man_name(userName);
				bd.setUpdate_time(new Date());
				bd.setValid_flag("1");
				this.getMapper(SlmBaseSupplierUnitMapper.class).update(bd);
			}else{
				String No=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3);
				bd.setSupplier_unit_no(No);
				bd.setCreate_man_no(userId);
				bd.setCreate_man_name(userName);
				bd.setCreate_time(new Date());
				bd.setValid_flag("1");
				this.getMapper(SlmBaseSupplierUnitMapper.class).insert(bd);
			}
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，新增失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 修改数据
	 * @param bd
	 * @return
	 */
	public ResultModel doUpdateWeb(CommonPage<SlmBaseSupplierUnit> sum) {
		ResultModel rm=new ResultModel();
		SlmBaseSupplierUnit 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(SlmBaseSupplierUnitMapper.class).update(obd);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("修改失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 启用、停用
	 * @param bd
	 * @return
	 */
	public ResultModel doSwitchWeb(CommonPage<SlmBaseSupplierUnit> 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<SlmBaseSupplierUnit> list = sum.getObjectList();
		try {
			if(list!=null){
				for(SlmBaseSupplierUnit conversion:list){
					if("0".equals(conversion.getValid_flag())){
						conversion.setDelete_man_no(userId);
						conversion.setDelete_man_name(userName);
						conversion.setDelete_time(new Date());
					}else{
						conversion.setUpdate_man_no(userId);
						conversion.setUpdate_man_name(userName);
						conversion.setUpdate_time(new Date());
					}
					this.getMapper(SlmBaseSupplierUnitMapper.class).update(conversion);
				}
			}
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("操作失败"+e.getMessage());
			e.printStackTrace();
			throw e;
		}
		return rm;
	}
}
