package cn.pluss.platform.controller.jft;


import cn.pluss.platform.api.Result;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.entity.JftBankCard;
import cn.pluss.platform.entity.JftMerchantRate;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.entity.UserLevel;
import cn.pluss.platform.jft.JftMerchantRateService;
import cn.pluss.platform.user.UserLevelService;
import cn.pluss.platform.userApp.UserAppService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 缴费通银行卡 前端控制器
 * </p>
 *
 * @author bzg
 * @since 2022-02-22
 */
@Controller
@RequestMapping("/jftMercRate")
public class JftMerchantRateController extends BaseModelController<JftMerchantRateService, JftMerchantRate> {

    @Resource
    private UserAppService userAppService;

    @Resource
    private UserLevelService userLevelService;

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/index")
    public String index(HttpServletRequest request) {
        return "jft/rateList";
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = {"/detail/{id}", "/detail"})
    public String detail(HttpServletRequest request, @PathVariable(value = "id", required = false) Integer id) {
        if (id != null) {
            JftMerchantRate entity = baseService.getOne(new QueryWrapper<JftMerchantRate>().eq("id",id));
            request.setAttribute("entity", entity);
        }
        return "jft/rateDetail";
    }

    @Override
    public Result<Page<JftMerchantRate>> page(Page<JftMerchantRate> page, JftMerchantRate condition, OrderItem orderItem) {
        orderItem.setColumn("createTime");
        orderItem.setAsc(false);
        Result<Page<JftMerchantRate>> result = super.page(page, condition, orderItem);
        Page<JftMerchantRate> pageRecords = result.getData();
        List<JftMerchantRate> records = pageRecords.getRecords();
        for (JftMerchantRate rate :records) {
            UserApp userApp = userAppService.getUserAppByUserId(rate.getUserId());
            rate.setLevelCode(userApp.getLevelCode());
            UserLevel level = userLevelService.getLevelInfoByRoleLevel(userApp.getRoleCode(), userApp.getLevelCode());
            if(level != null){
                rate.setConsultRate(level.getJftRate());
            }else{
                rate.setConsultRate(BigDecimal.ZERO);
            }
        }
        return result;
    }
}
