package com.jeequan.jeepay.mgr.ctrl.route;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jeequan.jeepay.core.aop.MethodLog;
import com.jeequan.jeepay.core.entity.PayOrderCount;
import com.jeequan.jeepay.core.entity.SysUser;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.core.utils.SeqKit;
import com.jeequan.jeepay.db.entity.Route;
import com.jeequan.jeepay.mgr.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 商户路由管理类
 *
 * @date 2021-06-16 09:15
 */
@RestController
@RequestMapping("/api/route")
public class RouteController extends CommonCtrl {

    @Autowired
    private RouteService routeService;

    /**
     * @Description: 商户路由列表
     * @since 2024/04/19
     */
    @GetMapping
    public ApiRes list() {
        Route entity = getObject(Route.class);
        IPage<Route> pages = routeService.pageList(getIPage(true), entity);
        return ApiRes.page(pages);
    }

    @PreAuthorize("hasAuthority('ENT_ROUTE_ADD')")
    @MethodLog(remark = "新增路由策略")
    @PostMapping
    public ApiRes add() {
        Route entity = getObject(Route.class);
        SysUser sysUser = getCurrentUser().getSysUser();
        entity.setCreatedUid(sysUser.getSysUserId());
        entity.setCreatedBy(sysUser.getRealname());
        entity.setRouteId(SeqKit.genFlowNo("RU"));
        routeService.addSave(entity);
        return ApiRes.ok();
    }

    @PreAuthorize("hasAuthority('ENT_ROUTE_EDIT')")
    @MethodLog(remark = "修改路由信息")
    @PutMapping("/{id}")
    public ApiRes update(@PathVariable("id") String id) {
        Route entity = getObject(Route.class).setRouteId(id);
        entity.preCheck();
        routeService.updateById(entity);
        return ApiRes.ok();
    }


    @PreAuthorize("hasAuthority('ENT_ROUTE_DEL')")
    @MethodLog(remark = "删除路由")
    @DeleteMapping("/{id}")
    public ApiRes delete(@PathVariable("id") String id) {
        routeService.del(id);
        return ApiRes.ok();
    }

    /**
     * 数据统计
     * @return
     */
    @GetMapping("/count")
    public ApiRes count() {
        Route entity = getObject(Route.class);
        PayOrderCount count = routeService.totalCount(entity);
        return ApiRes.ok(count);
    }

    /**
     * 获取二维码
     * @return
     */
    @GetMapping("/qrcode/{id}")
    public ApiRes qrcode(@PathVariable("id") String id) {
        String url = routeService.getQrCode(id);
        return ApiRes.ok(url);
    }
}
