package com.ym.mec.web.controller;

import com.ym.mec.biz.dal.entity.SysPaymentConfig;
import com.ym.mec.biz.dal.enums.AccountType;
import com.ym.mec.biz.dal.enums.PaymentChannelEnum;
import com.ym.mec.biz.dal.page.SysPaymentConfigQueryInfo;
import com.ym.mec.biz.service.SysPaymentConfigService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
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("此分部配置已经存在");
        }
        if(StringUtils.isNotBlank(sysPaymentConfig.getHfMerNo())){
            SysPaymentConfig nowConfig = sysPaymentConfigService.findPaymentConfigByMerNo(sysPaymentConfig.getHfMerNo());
            if(nowConfig != 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) {
        if(StringUtils.isNotBlank(sysPaymentConfig.getHfMerNo())){
            SysPaymentConfig nowConfig = sysPaymentConfigService.findPaymentConfigByMerNo(sysPaymentConfig.getHfMerNo());
            if(nowConfig != null && !nowConfig.getOrganId().equals(sysPaymentConfig.getOrganId())){
                return failed("汇付商户号已配置，不同分部请勿配置相同商户号");
            }
        }

        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(SysPaymentConfigQueryInfo queryInfo) {
        if (queryInfo.getHasTypeRoute() == null) {
            queryInfo.setHasRouteScale(1);
        }
        return succeed(sysPaymentConfigService.queryPage(queryInfo));
    }

    @ApiOperation(value = "根据类型获取获取配置列表")
    @GetMapping("/getPaymentConfigs")
    @PreAuthorize("@pcs.hasPermissions('paymentConfig/getPaymentConfigs')")
    public HttpResponseResult getPaymentConfigs(PaymentChannelEnum payType) {
        return succeed(sysPaymentConfigService.getPaymentConfigs(payType));
    }


    @ApiOperation(value = "新增按费用类型配置")
    @PostMapping("/addTypeRoute")
    @PreAuthorize("@pcs.hasPermissions('paymentConfig/addTypeRoute')")
    public HttpResponseResult<SysPaymentConfig> addTypeRoute(SysPaymentConfig config) {
        if (StringUtils.isBlank(config.getTypeRouteScale())) {
            return failed("分润配置不能为空");
        }
        SysPaymentConfig paymentConfig = sysPaymentConfigService.findPaymentConfigByOrganId(config.getOrganId());
        if (paymentConfig != null && StringUtils.isNotBlank(paymentConfig.getTypeRouteScale())) {
            return failed("此分部配置存在,请核查");
        }
        Date nowDate = new Date();
        if (paymentConfig != null) {
            paymentConfig.setTypeRouteScale(config.getTypeRouteScale());
            paymentConfig.setUpdateTime(nowDate);
            sysPaymentConfigService.update(paymentConfig);
        } else {
            config.setPayType(PaymentChannelEnum.ADAPAY);
            config.setType(2);
            config.setCreateTime(nowDate);
            config.setUpdateTime(nowDate);
            sysPaymentConfigService.insert(config);
            paymentConfig = config;
        }
        return succeed(paymentConfig);
    }

    @ApiOperation(value = "更新按费用类型配置")
    @PostMapping("/updateTypeRoute")
    @PreAuthorize("@pcs.hasPermissions('paymentConfig/updateTypeRoute')")
    public HttpResponseResult<SysPaymentConfig> updateTypeRoute(SysPaymentConfig config) {
        if (StringUtils.isBlank(config.getTypeRouteScale())) {
            return failed("分润配置不能为空");
        }
        SysPaymentConfig paymentConfig = sysPaymentConfigService.get(config.getId());
        Date nowDate = new Date();
        paymentConfig.setTypeRouteScale(config.getTypeRouteScale());
        paymentConfig.setUpdateTime(nowDate);
        sysPaymentConfigService.update(paymentConfig);
        return succeed(paymentConfig);
    }

    @ApiOperation(value = "删除按费用类型配置")
    @PostMapping("/delTypeRoute")
    @PreAuthorize("@pcs.hasPermissions('paymentConfig/delTypeRoute')")
    public HttpResponseResult<SysPaymentConfig> delTypeRoute(Integer id) {
        SysPaymentConfig paymentConfig = sysPaymentConfigService.get(id);
        if (paymentConfig == null) {
            return failed("此分部配置不存在");
        }

        if (StringUtils.isBlank(paymentConfig.getHfMerNo()) && StringUtils.isBlank(paymentConfig.getRouteScale())) {
            sysPaymentConfigService.delete(id);
        } else {
            Date nowDate = new Date();
            paymentConfig.setTypeRouteScale("");
            paymentConfig.setUpdateTime(nowDate);
            sysPaymentConfigService.update(paymentConfig);
        }
        return succeed(paymentConfig);
    }

}
