package market.Qms.com.service.impl;

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

import market.Com.mapper.ComBaseStationMapper;
import market.Com.vo.ComBaseStation;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.com.mapper.BaseDeliverystateMapper;
import market.Qms.com.mapper.BaseDeliverystateRPhyMapper;
import market.Qms.com.mapper.ComBaseDeliverystateRPpcMapper;
import market.Qms.com.service.BaseDeliverystateService;
import market.Qms.com.vo.BaseDeliverystate;
import market.Qms.com.vo.ComBaseDeliverystateRPpc;
import market.Qms.common.DataHelper;
import market.Qms.common.RequestModel;
import market.Qms.common.ReturnMsg;

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

@RemoteService(serviceInterface=BaseDeliverystateService.class)
public class BaseDeliverystateServiceImpl extends BaseServiceImpl<BaseDeliverystate, String> implements BaseDeliverystateService {

	@Override
	public ReturnMsg<List<BaseDeliverystate>> GetDeliveryState(RequestModel<BaseDeliverystate> delivery) {
		// TODO Auto-generated method stub
		ReturnMsg<List<BaseDeliverystate>> msg=new ReturnMsg<>();
		try{
			BaseDeliverystateMapper mapper=this.getMapper(BaseDeliverystateMapper.class);
			String condition="";
			if(delivery.getModel()!=null&&!DataHelper.IsNullOrSpace(delivery.getModel().getDeliveryStateDesc())){
				condition=" LOWER(DELIVERY_STATE_DESC) like '%'||LOWER('"+delivery.getModel().getDeliveryStateDesc()+"')||'%'";
				delivery.getModel().setDeliveryStateDesc(null);
			}
			
			List<BaseDeliverystate> list=mapper.findWithCondition(delivery.getModel(), condition,"DELIVERY_STATE_CODE Desc", delivery.getSkipResults(), delivery.getPageSize());
			int count=mapper.countWithCondition(delivery.getModel(), condition);
			msg.setTotal(count);
			msg.setMessage("获取数据成功");
			msg.setSuccess(true);
			msg.setResult(list);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		return msg;
	}

	
	public ResultModel SaveDeliveryState(CommonPage<BaseDeliverystate> sum) {
		// TODO Auto-generated method stub
		ResultModel rm = new ResultModel();
		List<BaseDeliverystate> list = sum.getObjectList();
	
		try{
			for(BaseDeliverystate delivery:list ){
			BaseDeliverystateMapper mapper=this.getMapper(BaseDeliverystateMapper.class);
			BaseDeliverystate _state=new BaseDeliverystate();
			_state.setDeliveryStateDesc(delivery.getDeliveryStateDesc());
			List<BaseDeliverystate> listDelivery=mapper.find(_state, 0, 0);
			if(listDelivery.size()>0&&!listDelivery.get(0).getDeliveryStateCode().equals(delivery.getDeliveryStateCode())){
				rm.setMsgInfo("已经存在相同名称的交货状态");
				rm.setState("500");
				return rm;
			}
			
			if(DataHelper.IsNullOrSpace(delivery.getDeliveryStateCode())){
				String maxCode=mapper.GetMaxCode();
				String code=DataHelper.InitSerial(maxCode, "", 7);
				delivery.setDeliveryStateCode(code);
				delivery.setCreateTime(new Date());
				mapper.insert(delivery);
			}else{
				delivery.setUpdateTime(new Date());
				mapper.update(delivery);
			}
			}
			rm.setMsgInfo("保存数据成功");
			rm.setState("200");
			
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo(ex.getMessage());
		}
		return rm;
	}

	@Override
	public ReturnMsg DelOrRenewDeliveryState(String deleteName,String deliveryStateCode, Boolean flag) {
		// TODO Auto-generated method stub
		ReturnMsg msg=new ReturnMsg<>();
		try{
			// TODO Auto-generated method stub
			BaseDeliverystate steel=new BaseDeliverystate();
			steel.setDeliveryStateCode(deliveryStateCode);
			steel.setValidflag(flag?"0":"1");
			if(flag){
				steel.setDeleteTime(new Date());
				steel.setDeleteName(deleteName);
				this.getMapper(BaseDeliverystateRPhyMapper.class).DelOrRenewRPhy("0", deliveryStateCode);
			}else{
				steel.setUpdateTime(new Date());
				steel.setUpdateName(deleteName);
			}
			this.getMapper(BaseDeliverystateMapper.class).update(steel);
			msg.setMessage(flag?"废除成功":"恢复成功");
			msg.setSuccess(true);
		}catch(Exception ex){
			msg.setMessage(ex.getMessage());
			msg.setSuccess(false);
		}
		return msg;
	}
	/**
	 * 查询适用工序点信息
	 * @return
	 */
    public ResultModel getStationInfo(){
    	ResultModel rm=new ResultModel();
    	try {
			List<ComBaseStation> findWithCondition = this.getMapper(ComBaseStationMapper.class).findWithCondition(null, null, null, 0, 0);
		    rm.setData(findWithCondition);
		    rm.setState("200");
    	} catch (Exception e) {
			// TODO: handle exception
    		rm.setState("500");
    		rm.setErrorInfo(e.getMessage());
		}
    	return rm;
    }
    /**
     * 保存信息
     * @param sum
     * @return
     */
    public ResultModel saveInfo(CommonPage<ComBaseDeliverystateRPpc> sum){
    	ResultModel rm=new ResultModel();
    	List<ComBaseDeliverystateRPpc> listcbdr=sum.getObjectList();
    	String username=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
    	try {
    		this.getMapper(ComBaseDeliverystateRPpcMapper.class).deleteInfo(sum.getMemo1());
    		for (ComBaseDeliverystateRPpc cbdr : listcbdr) {
    			cbdr.setCreate_name(username);
    			cbdr.setCreate_time(new Date());
    			this.getMapper(ComBaseDeliverystateRPpcMapper.class).insert(cbdr);
			}
    		rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setErrorInfo(e.getMessage());
		}
    	return rm;
    }
    /**
     * 对比适用工序是否可以打钩的信息
     * @return
     */
    public ResultModel getInfo(String state_code){
    	ResultModel rm=new ResultModel();
    	try {
			List<ComBaseDeliverystateRPpc> findWithCondition = this.getMapper(ComBaseDeliverystateRPpcMapper.class).findWithCondition(null, "DELIVERY_STATE_CODE='"+state_code+"'", null, 0, 0);
		    rm.setData(findWithCondition);
		    rm.setState("200");
    	} catch (Exception e) {
			// TODO: handle exception
    		rm.setState("500");
    		rm.setErrorInfo(e.getMessage());
		}
    	return rm;
    }
}
