package cn.pluss.platform.controller.merchant.rate;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.merchant.MerchantRateNewRecordService;
import cn.pluss.platform.merchant.MerchantRateNewService;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * @author DJH
 */
@Controller
@RequestMapping("/merchantRate")
public class MerchantRateController {

    @Autowired
    private MerchantRateNewService merchantRateNewService;

    @Autowired
    private MerchantRateNewRecordService rateNewRecordService;

    @GetMapping("/list")
    public String list() {
        return "/merchantRate/rate_list";
    }

    @GetMapping("/page")
    @ResponseBody
    public Result<Page<Map<String, Object>>> page(Page<Object> page, @RequestParam Map<String, Object> condition) {
        page.orders().add(new OrderItem().setAsc(false).setColumn("mrnr.createTime"));
        page.orders().add(new OrderItem().setAsc(false).setColumn("mcs.updateTime"));
        val result = merchantRateNewService.pageData(page, condition);
        return ResultGenerator.genSuccessResult(result);
    }

    @PostMapping("/pass")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Result<Object> pass(String merchantId) {
        merchantRateNewService.passD0(merchantId);
        return ResultGenerator.genSuccessResult("D0费率修改已通过", null);
    }

    @PostMapping("/batchPass")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Result<Object> batchPass(@RequestParam("merchantId[]") String[] merchantIds) {
//        System.out.println(merchantIds);
        merchantRateNewService.passD0(Arrays.asList(merchantIds));
        return ResultGenerator.genSuccessResult("操作成功", null);
    }
}
