package lingtao.net.service;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lingtao.net.bean.ExpressFee;
import lingtao.net.bean.Msg;
import lingtao.net.bean.SysUser;
import lingtao.net.dao.ExpressFeeMapper;

@Service
public class ExpressFeeService {

	@Autowired
	private ExpressFeeMapper expressFeeMapper;

	public List<ExpressFee> getExpressFees(ExpressFee expressFee) {
		return expressFeeMapper.getExpressFees(expressFee);
	}

	public Msg addExpressFee(ExpressFee expressFee, HttpSession session) {
		SysUser user = (SysUser) session.getAttribute("USER_SESSION");
		expressFee.setCreateBy(user.getRealname());
		try {
			expressFeeMapper.addExpressFee(expressFee);
			return Msg.success();
		} catch (Exception e) {
			return Msg.fail(e.toString());
		}
	}

	public Msg updateExpressFeeById(ExpressFee expressFee, HttpSession session) {
		SysUser user = (SysUser) session.getAttribute("USER_SESSION");
		expressFee.setUpdateBy(user.getRealname());
		try {
			expressFeeMapper.updateExpressFeeById(expressFee);
			return Msg.success();
		} catch (Exception e) {
			return Msg.fail();
		}

	}

	public Msg deleteExpressFeeById(Integer id) {
		try {
			expressFeeMapper.deleteExpressFeeById(id);
			return Msg.success();
		} catch (Exception e) {
			return Msg.fail();
		}

	}

	public List<ExpressFee> getShortAnswers() {
		return expressFeeMapper.getShortAnswers();
	}

	public Msg updateExpressFeeById(HttpSession session, int id, String field, String value) {
		ExpressFee expressFee = new ExpressFee();
		SysUser user = (SysUser) session.getAttribute("USER_SESSION");
		expressFee.setUpdateBy(user.getRealname());
		expressFee.setId(id);
		switch (field) {
		case "province":
			expressFee.setProvince(value);
			break;
		case "firstWeightPrice":
			if (StringUtils.isEmpty(value))
				return Msg.fail("更新失败");
			expressFee.setFirstWeightPrice(Double.valueOf(value));
			break;
		case "continuedWeightPrice":
			if (StringUtils.isEmpty(value))
				return Msg.fail("更新失败");
			expressFee.setContinuedWeightPrice(Double.valueOf(value));
			break;
		case "startPrice":
			if (StringUtils.isEmpty(value))
				return Msg.fail("更新失败");
			expressFee.setStartPrice(Double.valueOf(value));
			break;
		}
		try {
			expressFeeMapper.updateExpressFeeById(expressFee);
			return Msg.success();
		} catch (Exception e) {
			return Msg.fail();
		}

	}

	public List<ExpressFee> getAllExpressFees(ExpressFee expressFee) {
		return expressFeeMapper.getAllExpressFees(expressFee);
	}

	public List<ExpressFee> getHandBagExpressFees(ExpressFee expressFee) {
		return expressFeeMapper.getHandBagExpressFees(expressFee);
	}

	public List<ExpressFee> getTaoExpressFees(ExpressFee expressFee) {
		return expressFeeMapper.getTaoExpressFees(expressFee);
	}

	public List<ExpressFee> getOtherExpressFees(ExpressFee expressFee) {
		return expressFeeMapper.getOtherExpressFees(expressFee);
	}

}
