package market.Qms.com.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.com.mapper.BaseSteelMapper;
import market.Qms.com.mapper.BaseSteelRSpecMapper;
import market.Qms.com.service.BaseSteelService;
import market.Qms.com.vo.BaseSteel;
import market.Qms.common.DataHelper;
import market.Qms.common.ReturnMsg;

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(serviceInterface=BaseSteelService.class)
public class BaseSteelServiceImpl extends BaseServiceImpl<BaseSteel, String> implements BaseSteelService {

	public ResultModel GetSteel(CommonPage<BaseSteel> steel) {
//		ReturnMsg<List<BaseSteel>> msg=new ReturnMsg<>();
		ResultModel rm=new ResultModel();
		List<BaseSteel> list=new ArrayList<>();
		int pageIndex=steel.getPageIndex();
		int pageSize=steel.getPageSize();
		try{
			PageModel<BaseSteel> pm = new PageModel<BaseSteel>(pageIndex,pageSize);
			String condition="";
			if(steel.getObject()!=null&&!DataHelper.IsNullOrSpace(steel.getObject().getValidflag()))
			{
			 condition=" 1=1 and validflag = '"+steel.getObject().getValidflag()+"'";
			}
			if(steel.getObject()!=null&&!DataHelper.IsNullOrSpace(steel.getObject().getSteelName())){
				condition=" LOWER(STEEL_NAME) like '%'||LOWER('"+steel.getObject().getSteelName()+"')||'%'";
				steel.getObject().setSteelName(null);
			}
			String param = "STEEL_CODE DESC";
			if(steel.getMemo1() !=null){
				param = null;
				param = steel.getMemo1();
				if (steel.getMemo2().equals("1")) {
					param=param+" DESC";
				}
			}
			
			BaseSteelMapper mapper=this.getMapper(BaseSteelMapper.class);
			list=mapper.findWithCondition(steel.getObject(), condition, param, (pageIndex - 1) * pageSize, pageSize);
			int count=mapper.countWithCondition(steel.getObject(),condition);
			pm.setRows(list);
			pm.setTotal(count);
			rm.setData(pm);
			rm.setState("200");
			rm.setMsgInfo("获取数据成功");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo(ex.getMessage());
		}
		return rm;
	}

	@Override
	public ReturnMsg<BaseSteel> SaveSteel(BaseSteel steel) {
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ReturnMsg<BaseSteel> msg=new ReturnMsg<>();
		try{
			BaseSteelMapper mapper=this.getMapper(BaseSteelMapper.class);
			BaseSteel _steel=new BaseSteel();
			_steel.setSteelName(steel.getSteelName());
			List<BaseSteel> listSteel=mapper.find(_steel, 0, 0);
			if(listSteel.size()>0&&!listSteel.get(0).getSteelCode().equals(steel.getSteelCode())){
				msg.setMessage("已经存在相同名称的牌号");
				msg.setSuccess(false);
				return msg;
			}
			
			if(DataHelper.IsNullOrSpace(steel.getSteelCode())){//新增
				String maxCode=mapper.GetMaxCode();
				if(DataHelper.IsNullOrSpace(maxCode)){
					maxCode="1000";
				}else{
					maxCode=Integer.valueOf(maxCode)+1+"";
				}
				steel.setCreateName(userName);
				steel.setCreateTime(new Date());
				steel.setSteelCode(maxCode);
				mapper.insert(steel);
			}else{
				steel.setUpdateName(userName);
				steel.setUpdateTime(new Date());
				mapper.update(steel);
			}
			msg.setMessage("保存成功");
			msg.setResult(steel);
			msg.setSuccess(true);
		}catch(Exception ex){
			msg.setMessage(ex.getMessage());
			msg.setSuccess(false);
		}
		return msg;
	}

	@Override
	public ReturnMsg DelOrRenewSteel(String deleteName,String steelCode, Boolean flag) {
		ReturnMsg msg=new ReturnMsg<>();
		try{
			// TODO Auto-generated method stub
			BaseSteel steel=new BaseSteel();
			steel.setSteelCode(steelCode);
			steel.setValidflag(flag?"0":"1");
			if(flag){
				steel.setDeleteTime(new Date());
				steel.setDeleteName(deleteName);
			}else{
				steel.setUpdateTime(new Date());
				steel.setUpdateName(deleteName);
			}
			this.getMapper(BaseSteelMapper.class).update(steel);
			if(flag){
				this.getMapper(BaseSteelRSpecMapper.class).DelOrRenewSteelSpec("0", steelCode);
			}
			msg.setMessage(flag?"废除成功":"恢复成功");
			
			msg.setSuccess(true);
		}catch(Exception ex){
			msg.setMessage(ex.getMessage());
			msg.setSuccess(false);
		}
		return msg;
	}

//	@Override
//	public ReturnMsg<List<BaseSteel>> GetSteel(RequestModel<BaseSteel> steel) {
//		// TODO Auto-generated method stub
//		return null;
//	}
}
