package com.ym.mec.web.controller;

import com.ym.mec.biz.dal.entity.SysPaymentConfig;
import com.ym.mec.biz.service.SysPaymentConfigService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.page.QueryInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RequestMapping("paymentConfig")
@Api(tags = "支付设置服务")
@RestController
public class SysPaymentConfigController extends BaseController {

    @Autowired
    private SysPaymentConfigService sysPaymentConfigService;

    @ApiOperation(value = "新增支付配置")
    @PostMapping("/add")
    @PreAuthorize("@pcs.hasPermissions('paymentConfig/add')")
    public HttpResponseResult add(SysPaymentConfig sysPaymentConfig) {
        if (sysPaymentConfig.getOrganId() == null) {
            return failed("请选择分部");
        }
        SysPaymentConfig paymentConfig = sysPaymentConfigService.findPaymentConfigByOrganId(sysPaymentConfig.getOrganId());
        if (paymentConfig != null) {
            return failed("此分部配置已经存在");
        }
        Date nowDate = new Date();
        sysPaymentConfig.setCreateTime(nowDate);
        sysPaymentConfig.setUpdateTime(nowDate);
        return succeed(sysPaymentConfigService.insert(sysPaymentConfig));
    }

    @ApiOperation(value = "删除支付配置")
    @PostMapping("/del/{id}")
    @PreAuthorize("@pcs.hasPermissions('paymentConfig/del')")
    public Object del(@ApiParam(value = "支付配置编号", required = true) @PathVariable("id") Integer id) {
        SysPaymentConfig sysPaymentConfig = sysPaymentConfigService.get(id);
        if (sysPaymentConfig == null) {
            return failed("参数错误");
        }
        sysPaymentConfigService.delete(id);
        return succeed();
    }

    @ApiOperation(value = "修改支付配置")
    @PostMapping("/update")
    @PreAuthorize("@pcs.hasPermissions('paymentConfig/update')")
    public Object update(SysPaymentConfig sysPaymentConfig) {
        sysPaymentConfig.setUpdateTime(new Date());
        sysPaymentConfigService.update(sysPaymentConfig);
        return succeed();
    }

    @ApiOperation(value = "根据支付配置编号查询支付配置")
    @GetMapping("/get/{id}")
    @PreAuthorize("@pcs.hasPermissions('paymentConfig/get')")
    public Object get(@ApiParam(value = "支付配置编号", required = true) @PathVariable("id") Integer id) {
        return succeed(sysPaymentConfigService.get(id));
    }

    @ApiOperation(value = "分页查询支付配置列表")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('paymentConfig/queryPage')")
    public Object queryPage(QueryInfo queryInfo) {
        return succeed(sysPaymentConfigService.queryPage(queryInfo));
    }
}
