package com.ym.mec.web.controller;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
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.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dto.TenantContractTemplateDto;
import com.ym.mec.biz.dal.entity.TenantContractTemplate;
import com.ym.mec.biz.dal.page.TenantContractTemplateQueryInfo;
import com.ym.mec.biz.service.TenantContractTemplateService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.tenant.TenantContextHolder;

@RequestMapping("tenantContractTemplate")
@Api(tags = "机构协议模板")
@RestController
public class TenantContractTemplateController extends BaseController {

	@Autowired
	private TenantContractTemplateService tenantContractTemplateService;

    @Autowired
    private SysUserFeignService sysUserFeignService;

	@ApiOperation("分页查询")
	@GetMapping(value = "/list")
	@PreAuthorize("@pcs.hasPermissions('tenantContractTemplate/list')")
	public HttpResponseResult<PageInfo<TenantContractTemplateDto>> getList(TenantContractTemplateQueryInfo queryInfo) {
		return succeed(tenantContractTemplateService.queryPageList(queryInfo));
	}

	@ApiOperation("单查询")
	@ApiImplicitParam(name = "id", value = "ID编号", required = true, dataType = "Integer", paramType = "path")
	@GetMapping(value = "/query")
	@PreAuthorize("@pcs.hasPermissions('tenantContractTemplate/query')")
	public HttpResponseResult<TenantContractTemplate> query(Integer id) {
		return succeed(tenantContractTemplateService.get(id));
	}

	@ApiOperation("查询最新启用的协议模板")
	@GetMapping(value = "/queryLatestContractTemplate")
	@PreAuthorize("@pcs.hasPermissions('tenantContractTemplate/queryLatestContractTemplate')")
	public HttpResponseResult<TenantContractTemplate> queryLatestContractTemplate() {
		SysUser sysUser = sysUserFeignService.queryUserInfo();

		if (sysUser == null || sysUser.getId() == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		return succeed(tenantContractTemplateService.queryLatestContractTemplate(TenantContextHolder.getTenantId(), null, null));
	}

	@ApiOperation("新增")
	@PostMapping(value = "/add", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@PreAuthorize("@pcs.hasPermissions('tenantContractTemplate/add')")
	public Object add(TenantContractTemplate tenantContractTemplate) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        
        if(sysUser == null || sysUser.getId() == null){
        	return failed(HttpStatus.FORBIDDEN, "请登录");
        }
		
		tenantContractTemplate.setStatus(false);
		tenantContractTemplate.setModifyBy(sysUser.getId());
		tenantContractTemplate.setTenantId(TenantContextHolder.getTenantId());
		tenantContractTemplateService.createContractTemplate(tenantContractTemplate);
		return succeed();
	}

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

	@ApiOperation("启用协议")
	@PostMapping(value = "/updateStatus", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@PreAuthorize("@pcs.hasPermissions('tenantContractTemplate/updateStatus')")
	public Object enableContract(int id) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        
		tenantContractTemplateService.enableContract(id, sysUser.getId());
		return succeed();
	}

}
