package com.yonge.cooleshow.admin.controller;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.yonge.cooleshow.common.enums.ContractTemplateSourceEnum;
import com.yonge.cooleshow.common.enums.ContractTemplateTypeEnum;
import com.yonge.toolset.base.util.StringUtil;

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

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.*;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.ContractTemplateDto;
import com.yonge.cooleshow.biz.dal.entity.ContractTemplate;
import com.yonge.cooleshow.biz.dal.queryInfo.ContractTemplateQueryInfo;
import com.yonge.cooleshow.biz.dal.service.ContractTemplateService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.page.PageInfo;

@RequestMapping("contractTemplate")
@Api(tags = "协议模板")
@RestController
public class ContractTemplateController extends BaseController {

	@Autowired
	private ContractTemplateService contractTemplateService;

    @Autowired
    private SysUserFeignService sysUserFeignService;

	@ApiOperation("分页查询")
	@GetMapping(value = "/list")
	@PreAuthorize("@pcs.hasPermissions('contractTemplate/list')")
	public HttpResponseResult<PageInfo<ContractTemplateDto>> getList(ContractTemplateQueryInfo queryInfo) {
		queryInfo.setSource(ContractTemplateSourceEnum.PLATFORM.getCode());
		return succeed(contractTemplateService.queryPageList(queryInfo));
	}

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

	@ApiOperation("查询最新启用的协议模板")
	@GetMapping(value = "/queryLatestContractTemplate")
	@PreAuthorize("@pcs.hasPermissions('contractTemplate/queryLatestContractTemplate')")
	public HttpResponseResult<ContractTemplate> queryLatestContractTemplate(@ApiParam(value = "业务类型 REGISTER 注册 COURSES 课程购买 PRODUCT 产品 WITHDRAW 结算", required = true) @RequestParam("contractType") ContractTemplateTypeEnum contractType) {
		return succeed(contractTemplateService.queryLatestContractTemplate(contractType));
	}

	@ApiOperation("新增")
	@PostMapping(value = "/add", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@PreAuthorize("@pcs.hasPermissions('contractTemplate/add')")
	public HttpResponseResult<Object> add(@RequestBody ContractTemplate contractTemplate) {
		contractTemplate.setSource(ContractTemplateSourceEnum.PLATFORM.getCode());
		if(StringUtil.isEmpty(contractTemplate.getType())){
			contractTemplate.setType(ContractTemplateTypeEnum.BUY_ORDER.getCode());
		}

        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null || sysUser.getId() == null){
        	return failed(HttpStatus.FORBIDDEN, "请登录");
        }

		contractTemplate.setStatus(false);
		contractTemplate.setModifyBy(sysUser.getId());
		contractTemplateService.createContractTemplate(contractTemplate);
		return succeed();
	}

	@ApiOperation("更新")
	@PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@PreAuthorize("@pcs.hasPermissions('contractTemplate/update')")
	public HttpResponseResult<Object> update(@RequestBody ContractTemplate contractTemplate) {
		if(StringUtil.isEmpty(contractTemplate.getType())){
			contractTemplate.setType(ContractTemplateTypeEnum.BUY_ORDER.getCode());
		}
		contractTemplateService.update(contractTemplate);
		return succeed();
	}

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

}
