package com.goafanti.order.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.goafanti.common.bo.Result;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.common.enums.OrderState;
import com.goafanti.common.model.ConsultOrder;
import com.goafanti.order.service.ConsultOrderService;

@RestController
@RequestMapping(value = "/api/admin/consultorder")
public class AdminConsultOrderApiController extends BaseApiController {

	@Autowired
	ConsultOrderService consultOrderService;

	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Result list(String number, String toNumber, String pageNo, String pageSize, String id, String paymentId,
			String state) {
		return res().data(consultOrderService.getList(
				consultOrderService.disposeSearchParams(number, toNumber, id, paymentId, state), getPageNo(pageNo),
				getPageSize(pageSize)));
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public Result delConsultOrder(Long id) {
		ConsultOrder co = consultOrderService.selectByPrimaryKey(id);
		if (co.getOrderState() != OrderState.UNPAYED.getCode()) {
			return res().data(0);
		}
		ConsultOrder newCo = new ConsultOrder();
		newCo.setId(co.getId());
		newCo.setOrderState(OrderState.CANCELLED.getCode());
		return res().data(consultOrderService.updateByPrimaryKeySelective(newCo));
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public Result updateConsultOrder(ConsultOrder inputCO) {
		ConsultOrder co = consultOrderService.selectByPrimaryKey(inputCO.getId());
		if (co.getOrderState() != OrderState.UNPAYED.getCode()) {
			return res().data(0);
		}
		co.setPaymentId(inputCO.getPaymentId());
		co.setPaymentType(inputCO.getPaymentType());
		co.setPaymentTime(inputCO.getPaymentTime());
		co.setPaymentValue(inputCO.getPaymentValue());
		co.setOrderState(OrderState.getStatus(inputCO.getOrderState()).getCode());
		return res().data(consultOrderService.updateByPrimaryKeySelective(co));
	}
}
