package cn.superdesk.uniorder.controller.order._leaveover;

import cn.superdesk.uniorder.common.constants.ResponseMsgConstants;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.order.OrderSourceType;
import cn.superdesk.uniorder.dubbo.consumer.order._leaveover.OrderSourceTypeDubboConsumerService;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

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


@Slf4j
@RestController
@RequestMapping("/orderSourceType")
public class OrderSourceTypeController extends BaseController {

	@Autowired
	private OrderSourceTypeDubboConsumerService orderSourceTypeDubboService;


	/**
	 * 返回角色列表-分页
	 *
	 * @return
	 */
	@PostMapping("/findAll")
	@ResponseBody
	public R<List<OrderSourceType>> findAllByPage() {

		R<List<OrderSourceType>> response = new R<>();

		try {
			Map<String, Object> params = new HashMap<>();

			List<OrderSourceType> ls = orderSourceTypeDubboService.findAll(params);
			response.setData(ls);
			response.setSuccess(true);
		} catch (Exception e) {
			response.setCode(String.valueOf(e.hashCode()));
			response.setSuccess(false);
			response.setMessage("系统查找失败！");
			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}
		return response;
	}


	/**
	 * 新增修改
	 *
	 * @param model
	 * @return
	 */
	@PostMapping("/saveOrUpdate")
	@ResponseBody
	public R<?> saveOrUpdate(OrderSourceType model) {
		R<?> response = new R<>();
		try {
			if (model.getId() == null) {
				// 新增
				orderSourceTypeDubboService.add(model);
				response.setSuccess(true);

			} else {
				// 修改
				orderSourceTypeDubboService.update(model);
				response.setSuccess(true);
			}

		} catch (Exception e) {

			response.setSuccess(false);
			response.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}
		return response;
	}

	/**
	 * 删除
	 *
	 * @param id
	 * @return
	 */
	@PostMapping("/delete")
	public R<?> delete(Long id) {

		R<?> response = new R<>();

		try {
			orderSourceTypeDubboService.delete(id);
			response.setSuccess(true);

		} catch (Exception e) {

			response.setSuccess(false);
			response.setMessage(ResponseMsgConstants.ERROR_UNKNOW);

			log.error(ExceptionUtils.getMessage(e));

			e.printStackTrace();
		}

		return response;
	}
}
