package com.goafanti.order.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import com.goafanti.common.dao.ConsultOrderMapper;
import com.goafanti.common.dao.UserMapper;
import com.goafanti.common.enums.OrderState;
import com.goafanti.common.model.ConsultOrder;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.JDBCIdGenerator;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;

@Service
public class ConsultOrderService extends BaseMybatisDao<ConsultOrder> {

	@Autowired
	private ConsultOrderMapper	consultOrderMapper;

	@Autowired
	private UserMapper			userMapper;

	@Autowired
	private JDBCIdGenerator		idGenerator;

	public int deleteByPrimaryKey(Long id) {
		if (TokenManager.getAdminToken() == null) {
			Map<String, Object> map = new HashMap<>();
			map.put("id", id);
			map.put("uid", TokenManager.getUserId());
			return consultOrderMapper.deleteRecord(map);
		} else {
			return consultOrderMapper.deleteByPrimaryKey(id);
		}
	}

	public int insertSelective(String toUid, Double price) {
		if (userMapper.selectByPrimaryKey(toUid) == null) {
			return 0;
		}
		ConsultOrder co = new ConsultOrder();
		co.setId(idGenerator.generateId());
		co.setUserId(TokenManager.getUserId());
		co.setToUserId(toUid);
		co.setOrderState(OrderState.UNPAYED.getCode());
		co.setOrderTime(new Date());
		co.setPrice(price);
		return consultOrderMapper.insertSelective(co);
	}

	public int updateByPrimaryKeySelective(ConsultOrder record) {
		return consultOrderMapper.updateByPrimaryKeySelective(record);
	}

	public ConsultOrder selectByPrimaryKey(Long id) {
		return consultOrderMapper.selectByPrimaryKey(id);
	}

	public Pagination<ModelMap> getList(Map<String, Object> params, Integer pNo, Integer pSize) {
		Pagination<ModelMap> page = new Pagination<>();
		page.setPageNo(pNo);
		page.setPageSize(pSize);
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		params.put("page_sql", String.format(" limit  %s , %s ", offset, pSize));
		List<ModelMap> res = consultOrderMapper.findConsultOrderListByPage(params);
		for (ModelMap mm : res) {
			mm.put("id", String.valueOf(mm.get("id")));
		}
		page.setList(res);
		page.setTotalCount(consultOrderMapper.findConsultOrderCount(params));
		return page;
	}

	public Map<String, Object> disposeSearchParams(String number, String toNumber, String id, String paymentId,
			String state) {
		Map<String, Object> params = new HashMap<>();
		if (StringUtils.isNumeric(number)) {
			params.put("number", Integer.parseInt(number));
		}
		if (StringUtils.isNumeric(toNumber)) {
			params.put("tnumber", Integer.parseInt(toNumber));
		}
		if (StringUtils.isNumeric(id)) {
			params.put("id", Long.parseLong(id));
		}
		if (StringUtils.isNotBlank(paymentId)) {
			params.put("pid", paymentId);
		}
		if (state != null) {
			params.put("state", OrderState.getStatus(state).getCode());
		}
		return params;
	}
}
