package cn.ey.biz.module.system.controller.admin.contract;

import cn.ey.biz.common.pojo.PageParam;
import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.module.system.controller.admin.contract.vo.ContractSaveVO;
import cn.ey.biz.module.system.controller.admin.contract.vo.ContractTypeVO;
import cn.ey.biz.module.system.enums.contract.ContractTypeEnum;
import cn.ey.biz.module.system.service.contract.ContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "管理后台 - 合同规则")
@RestController
@RequestMapping("/system/contract")
@Validated
public class ContractController {

    @Autowired
    private ContractService contractService;

    @PostMapping("")
    @ApiOperation("添加合同配置项")
    @PreAuthorize("@ss.hasPermission('system:contract:modify')")
    public R<?> saveContractInfo(@RequestBody ContractSaveVO<Object> content) {
        contractService.addContractInfo(content);
        return R.success(null);
    }

    @PutMapping("")
    @ApiOperation("更新合同配置项")
    @PreAuthorize("@ss.hasPermission('system:contract:modify')")
    public R<?> updateContractInfo(@RequestBody ContractSaveVO<Object> content) {
        contractService.updateContractType(content);
        return R.success(null);
    }

    @DeleteMapping("")
    @ApiOperation("删除合同配置项")
    @PreAuthorize("@ss.hasPermission('system:contract:modify')")
    public R<?> deleteContractInfo(@RequestBody ContractSaveVO<Object> content) {
        contractService.deleteContractType(content);
        return R.success(null);
    }

    @GetMapping("/type/tree")
    @ApiOperation("获取树形结构合同类型")
    @PreAuthorize("@ss.hasPermission('system:contract:get')")
    public R<?> getTypeTree(@RequestParam("sl") String sl) {
        return R.success(contractService.getTypes(sl));
    }

    @PutMapping("/type/active")
    @ApiOperation("是否启用合同类型")
    @PreAuthorize("@ss.hasPermission('system:contract:modify')")
    public R<?> putTypeActive(@RequestBody ContractSaveVO<ContractTypeVO> content) {
        contractService.putTypeActive(content);
        return R.success(null);
    }

    @PostMapping("/page")
    @ApiOperation("根据类型获得分页内容")
    @PreAuthorize("@ss.hasPermission('system:contract:get')")
    public R<PageResult<?>> getContractPage(@Valid @RequestBody ContractSaveVO<@Valid PageParam> page) {
        PageResult<?> ret = new PageResult<>();
        if (StringUtil.isNotBlank(page.getType())
                && ContractTypeEnum.CONTRACT_TYPE.getType().equals(page.getType())
                && null != page.getContent()) {
            ret = contractService.getContractTypePage(page);
        } else if (StringUtil.isNotBlank(page.getType())
                && ContractTypeEnum.ARCHIVED_FIELD_MANAGE.getType().equals(page.getType())
                && null != page.getContent()) {
            ret = contractService.getContractArchivedFieldPage(page);
        } else if (StringUtil.isNotBlank(page.getType())
                && ContractTypeEnum.ARCHIVED_EP_MANAGE.getType().equals(page.getType())
                && null != page.getContent()) {
            ret = contractService.getContractArchivedEpPage(page);
        }
        return R.success(ret);
    }

    @GetMapping("/get/sl")
    @ApiOperation("根据name获取sl")
    @PreAuthorize("@ss.hasPermission('system:contract:get')")
    public R<?> getSl(@RequestParam("name") String name) {
        return R.success(contractService.getSl(name));
    }

}
