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

import cn.ey.biz.common.exception.util.ServiceExceptionUtil;
import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.module.system.controller.admin.contract.v2.constant.FieldType;
import cn.ey.biz.module.system.controller.admin.contract.v2.vo.ContractFieldVO;
import cn.ey.biz.module.system.service.contract.ContractFieldService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

import static cn.ey.biz.module.system.enums.ErrorCodeConstants.INVALID_PARAMETER;

@Api(tags = "合同管理 - 合同字段")
@RestController
@RequestMapping("/system/contract")
@Validated
public class ContractFieldController {

    @Autowired
    private ContractFieldService contractFieldService;

    @PostMapping("/field")
    @ApiOperation("添加修改合同字段")
    @PreAuthorize("@ss.hasPermission('system:contract:modify')")
    public R<?> saveContractField(@RequestBody ContractFieldVO field) {

        if (Objects.isNull(field) || !StringUtils.hasLength(field.getFieldCode())
                || !StringUtils.hasLength(field.getFieldName())
                || !StringUtils.hasLength(field.getFieldType())
                || !StringUtils.hasLength(field.getContractModule())) {
            throw ServiceExceptionUtil.exception(INVALID_PARAMETER);
        }

        contractFieldService.saveContractField(field);
        return R.success(null);
    }

    @DeleteMapping("/field")
    @ApiOperation("删除合同字段")
    @PreAuthorize("@ss.hasPermission('system:contract:modify')")
    public R<?> deleteContractField(@RequestBody ContractFieldVO field) {

        if (Objects.isNull(field) || Objects.isNull(field.getId())) {
            throw ServiceExceptionUtil.exception(INVALID_PARAMETER);
        }

        field.setType(FieldType.FIELD.getName());

        contractFieldService.deleteContractField(field);
        return R.success(null);
    }

    @PostMapping("/field/page")
    @ApiOperation("分页获取合同字段")
    @PreAuthorize("@ss.hasPermission('system:contract:get')")
    public R<PageResult<ContractFieldVO>> getContractPage(@RequestBody ContractFieldVO page) {
        return R.success(contractFieldService.getContractFieldByPage(page));
    }

    @PostMapping("/field/sl")
    @ApiOperation("获取业务线合同字段")
    @PreAuthorize("@ss.hasPermission('system:contract:get')")
    public R<PageResult<ContractFieldVO>> getContractFieldPageBySl(@RequestBody ContractFieldVO info) {
        return R.success(contractFieldService.getContractFieldPageBySl(info));
    }

    @PostMapping("/field/show")
    @ApiOperation("修改是否展示")
    @PreAuthorize("@ss.hasPermission('system:contract:get')")
    public R<PageResult<ContractFieldVO>> changeShow(@RequestBody ContractFieldVO info) {

        if (Objects.isNull(info) || !StringUtils.hasLength(info.getSl())
                || !StringUtils.hasLength(info.getMr())
                || !StringUtils.hasLength(info.getContractType())
                || Objects.isNull(info.getFieldId())) {
            throw ServiceExceptionUtil.exception(INVALID_PARAMETER);
        }

        contractFieldService.changeShow(info);
        return R.success(null);
    }

    @PostMapping("/field/input")
    @ApiOperation("修改是否必填")
    @PreAuthorize("@ss.hasPermission('system:contract:get')")
    public R<PageResult<ContractFieldVO>> changeInput(@RequestBody ContractFieldVO info) {

        if (Objects.isNull(info) || !StringUtils.hasLength(info.getSl())
                || !StringUtils.hasLength(info.getMr())
                || !StringUtils.hasLength(info.getContractType())
                || Objects.isNull(info.getFieldId())) {
            throw ServiceExceptionUtil.exception(INVALID_PARAMETER);
        }

        contractFieldService.changeInput(info);
        return R.success(null);
    }

    @PostMapping("/field/default")
    @ApiOperation("修改默认值")
    @PreAuthorize("@ss.hasPermission('system:contract:get')")
    public R<PageResult<ContractFieldVO>> changeValue(@RequestBody ContractFieldVO info) {

        if (Objects.isNull(info) || !StringUtils.hasLength(info.getSl())
                || !StringUtils.hasLength(info.getMr())
                || !StringUtils.hasLength(info.getContractType())
                || Objects.isNull(info.getFieldId())) {
            throw ServiceExceptionUtil.exception(INVALID_PARAMETER);
        }

        contractFieldService.changeDefaultValue(info);
        return R.success(null);
    }
}
