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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jeequan.jeepay.core.aop.MethodLog;
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.db.entity.RouteManage;
import com.jeequan.jeepay.mch.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.RouteManageService;
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.*;

import java.math.BigDecimal;
import java.util.List;

/**
 *
 *
 * @date 2021-06-16 09:15
 */
@RestController
@RequestMapping("/api/routeManage")
public class RouteManageController extends CommonCtrl {

    @Autowired
    private RouteManageService routeManageService;

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

    @PreAuthorize("hasAuthority('ENT_ROUTE_MANAGE_ADD')")
    @MethodLog(remark = "绑定门店路由")
    @PostMapping
    public ApiRes add() {
        RouteManage entity = getObject(RouteManage.class).setMchNo(getCurrentMchNo());
        routeManageService.addSave(entity);
        return ApiRes.ok();
    }

    @PreAuthorize("hasAuthority('ENT_ROUTE_MANAGE_EDIT')")
    @MethodLog(remark = "修改路由绑定信息")
    @PutMapping("/{id}")
    public ApiRes update(@PathVariable("id") String id) {
        RouteManage entity = getObject(RouteManage.class).setStoreId(id);
        List<String> amtRange = entity.getAmtRange();
        if(amtRange != null){
            amtRange.stream().map(amt -> {
                amtRange.replaceAll(value -> new BigDecimal(value).multiply(BigDecimal.valueOf(100)).toString());
                return amtRange;
            });
        }
        routeManageService.updateById(entity);
        return ApiRes.ok();
    }


    @PreAuthorize("hasAuthority('ENT_ROUTE_MANAGE_DEL')")
    @MethodLog(remark = "删除路由绑定")
    @DeleteMapping("/{id}")
    public ApiRes delete(@PathVariable("id") String id) {
        routeManageService.removeById(id);
        return ApiRes.ok();
    }

}
