package market.Qms.com.service.impl;

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

import market.Qms.com.mapper.BaseProductMapper;
import market.Qms.com.service.BaseProductService;
import market.Qms.com.vo.BaseProduct;
import market.Qms.common.DataHelper;
import market.Qms.common.RequestModel;
import market.Qms.common.ReturnMsg;

import org.springframework.transaction.annotation.Transactional;

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

@RemoteService(serviceInterface=BaseProductService.class)
@Transactional(value="tm",rollbackFor=Exception.class)
public class BaseProductServiceImpl extends BaseServiceImpl<BaseProduct, String> implements BaseProductService {

	@Override
	public ReturnMsg<List<BaseProduct>> GetProduct(RequestModel<BaseProduct> product) {
		ReturnMsg<List<BaseProduct>> msg=new ReturnMsg<>();
		List<BaseProduct> list=new ArrayList<>();
		try{
			BaseProductMapper mapper=this.getMapper(BaseProductMapper.class);
			String condition="";
			if(product.getModel()!=null&&!DataHelper.IsNullOrSpace(product.getModel().getProdName())){
				condition=" LOWER(PROD_NAME) like '%'||LOWER('"+product.getModel().getProdName()+"')||'%'";
				product.getModel().setProdName(null);
			}
			list=mapper.findWithCondition(product.getModel(), condition, "PROD_CODE DESC", product.getSkipResults(), product.getPageSize());
			int count=mapper.countWithCondition(product.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;
	}

	@Override
	public ReturnMsg<BaseProduct> SaveProduct(BaseProduct product) {
		// TODO Auto-generated method stub
		ReturnMsg<BaseProduct> msg=new ReturnMsg<>();
		msg.setResult(product);
		try{
			BaseProductMapper mapper=this.getMapper(BaseProductMapper.class);
			BaseProduct _pro=new BaseProduct();
			_pro.setProdName(product.getProdName());
			List<BaseProduct> _list=mapper.find(_pro, 0, 0);
			if(_list.size()>0&&!_list.get(0).getProdCode().equals(product.getProdCode())){
				msg.setMessage("此产品名称已存在");
				msg.setSuccess(false);
				msg.setResult(product);
				return msg;
			}
			if(DataHelper.IsNullOrSpace(product.getProdCode())){				
				String maxCode=mapper.GetMaxCode();
				String code=DataHelper.InitSerial(maxCode, "S", 7);
				product.setProdCode(code);
				product.setCreateTime(new Date());
				mapper.insert(product);
			}else{
				product.setUpdateTime(new Date());
				mapper.update(product);
			}
			msg.setMessage("保存数据成功");
			msg.setSuccess(true);
			msg.setResult(product);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		return msg;
	}

	@Override
	public ReturnMsg DeleteProduct(String deleteName,String productCode) {
		ReturnMsg msg=new ReturnMsg<>();
		try{
			BaseProduct product=new BaseProduct();
			product.setProdCode(productCode);
			product.setValidflag("0");
			product.setDeleteTime(new Date());
			product.setDeleteName(deleteName);
			BaseProductMapper mapper=this.getMapper(BaseProductMapper.class);
			mapper.update(product);
			msg.setMessage("废除成功");
			msg.setSuccess(true);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		return msg;
	}
	
	@Override
	public ReturnMsg DeleteProducts(List<String> productCode) {
		// TODO Auto-generated method stub
		BaseProductMapper mapper=this.getMapper(BaseProductMapper.class);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		for (String code : productCode) {
				BaseProduct product=new BaseProduct();
				product.setProdCode(code);
				product.setValidflag("0");
				product.setDeleteTime(new Date());
				product.setDeleteName(userName);
				mapper.update(product);
				//this.getSqlSession(null).rollback();
		}
		ReturnMsg msg=new ReturnMsg<>();
		msg.setSuccess(true);
		msg.setMessage("批量废除成功");
		return msg;
	}

	@Override
	public ReturnMsg RenewProducdt(String deleteName,String productCode) {
		ReturnMsg msg=new ReturnMsg<>();
		try{
			BaseProduct product=new BaseProduct();
			product.setProdCode(productCode);
			product.setValidflag("1");
			product.setUpdateTime(new Date());
			product.setUpdateName(deleteName);
			BaseProductMapper mapper=this.getMapper(BaseProductMapper.class);
			mapper.update(product);
			msg.setMessage("恢复成功");
			msg.setSuccess(true);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		return msg;
	}

	@Override
	public ReturnMsg RenewProducdts(List<String> productCode) {
		BaseProductMapper mapper=this.getMapper(BaseProductMapper.class);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		for (String code : productCode) {
				BaseProduct product=new BaseProduct();
				product.setProdCode(code);
				product.setValidflag("1");
				product.setUpdateTime(new Date());
				product.setUpdateName(userName);
				mapper.update(product);
				//this.getSqlSession(null).rollback();
		}
		ReturnMsg msg=new ReturnMsg<>();
		msg.setSuccess(true);
		msg.setMessage("批量恢复成功");
		return msg;
	}
}
