package com.ym.mec.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.biz.dal.entity.TenantApply;
import com.ym.mec.biz.dal.page.TenantApplyQueryInfo;
import com.ym.mec.biz.service.TenantApplyService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.service.IdGeneratorService;

@RequestMapping("tenantApply")
@Api(tags = "机构申请服务")
@RestController
public class TenantApplyController extends BaseController {

	@Autowired
	private TenantApplyService tenantApplyService;

	@Autowired
	private IdGeneratorService smsCodeService;

	@ApiOperation("分页查询")
	@GetMapping(value = "/list")
	@PreAuthorize("@pcs.hasPermissions('tenantApply/list')")	
	public Object getList(TenantApplyQueryInfo queryInfo) {
		return succeed(tenantApplyService.queryPage(queryInfo));
	}

	@ApiOperation("单查询")
	@ApiImplicitParam(name = "id", value = "ID编号", required = true, dataType = "Integer", paramType = "path")
	@GetMapping(value = "/query")
	@PreAuthorize("@pcs.hasPermissions('tenantApply/query')")
	public Object query(Long id) {
		return succeed(tenantApplyService.get(id));
	}

	@ApiOperation("新增")
	@PostMapping(value = "/add", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object add(TenantApply tenantApply) {
		
		boolean b = smsCodeService.verifyValidCode(tenantApply.getMobileNo(), tenantApply.getSmsCode());
		if (!b) {
			return failed("验证码不正确");
		}
		
		tenantApply.setCreateTime(new Date());
		tenantApplyService.insert(tenantApply);
		return succeed();
	}

	@ApiOperation("更新")
	@PostMapping(value = "/update", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@PreAuthorize("@pcs.hasPermissions('tenantApply/update')")
	public Object update(TenantApply tenantApply) {
		tenantApplyService.update(tenantApply);
		return succeed();
	}

}
