package cn.superdesk.uniorder.controller.fund;

import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.exception.constants.ExceptionMsg;
import cn.superdesk.uniorder.common.exception.UnifiedOrderRuntimeException;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.fund.SysFundUser;
import cn.superdesk.uniorder.dto.vo.fund.SysFundUserVoEx;
import cn.superdesk.uniorder.dubbo.consumer.fund.SysFundUserDubboConsumerService;
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("/fundUser")
public class SysFundUserController extends BaseController {


	@Autowired
	private SysFundUserDubboConsumerService sysFundUserDubboConsumerService;

	@PostMapping("/findAllViewPage")
	@ResponseBody
	public R<List<SysFundUserVoEx>> findAllViewPage() {

		R<List<SysFundUserVoEx>> result = new R<>();

		Map<String, Object> params = new HashMap<>();
		params.put("organizationName", getString("organizationName"));
		params.put("enterpriseName", getString("enterpriseName"));
		params.put("mobile", getString("mobile"));
		params.put("realname", getString("realname"));
		result = sysFundUserDubboConsumerService.findAllViewPage(params, getPageNum(), getPageSize());

		return result;
	}

	@PostMapping("/create")
	@ResponseBody
	public R<?> create() throws UnifiedOrderRuntimeException {

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

		SysFundUser sysFundUser = new SysFundUser();
		sysFundUser.setOrganizationId(getLong("organizationId"));
		sysFundUser.setEnterpriseId(getLong("enterpriseId"));
		sysFundUser.setUserId(getLong("userId"));

		sysFundUserDubboConsumerService.create(getToken(), sysFundUser);

		return result;
	}

	@PostMapping("/selectById")
	@ResponseBody
	public R<SysFundUserVoEx> selectById() throws UnifiedOrderRuntimeException {

		R<SysFundUserVoEx> result = new R<SysFundUserVoEx>();

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

		Long id = getLong("id");

		if (id == null) {
			throw new UnifiedOrderRuntimeException(ExceptionMsg.SYS_ERROR.getCode(), "id不能为空");
		}

		params.put("userId", id);

		result.setData(sysFundUserDubboConsumerService.selectById(params));

		return result;
	}
}
