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

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.api.code.dto.CodeCustomerDTO;
import cn.ey.biz.module.system.controller.admin.code.vo.CodeCustomerVO;
import cn.ey.biz.module.system.controller.admin.code.vo.HistoryVO;
import cn.ey.biz.module.system.service.code.CodeCustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "管理后台 - 客户信息")
@RestController
@RequestMapping("/system/code/customer")
@Validated
public class CodeCustomerController {

    @Resource
    private CodeCustomerService codeCustomerService;

    @PostMapping("/submit")
    @ApiOperation("提交")
    @PreAuthorize("@ss.hasPermission('system:engage-code:edit')")
    public R<Long> submitCustomer(@Valid @RequestBody CodeCustomerVO codeCustomerVO) {
        Long codeCustomerId = codeCustomerService.submitCustomer(codeCustomerVO);
        return R.success(codeCustomerId);
    }

    @GetMapping("/get")
    @ApiOperation("获取生效的客户信息配置")
    @PreAuthorize("@ss.hasPermission('system:engage-code:query')")
    public R<CodeCustomerDTO> getCodeCustomer(@RequestParam(required = false) Long ver) {
        return R.success(codeCustomerService.getCodeCustomer(ver));
    }

    @PostMapping("/history")
    @ApiOperation("获取客户信息历史记录")
    @PreAuthorize("@ss.hasPermission('system:engage-code:query')")
    public R<PageResult<HistoryVO>> getHistory(@RequestBody PageParam pageParam) {
        PageResult<HistoryVO> histories = codeCustomerService.getHistory(pageParam);
        return R.success(histories);
    }

    @PostMapping("/revert")
    @ApiOperation("生效指定版本")
    @PreAuthorize("@ss.hasPermission('system:engage-code:edit')")
    public R<?> revertHistory(@RequestParam("ver") Long ver) {
        codeCustomerService.revertHistory(ver);
        return R.success(null);
    }
}
