package cn.pluss.platform.controller.merchant;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.dto.SubMerchantDTO;
import cn.pluss.platform.subMerchant.SubMerchantService;
import cn.pluss.platform.vo.SubMerchantVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author SSS
 */
@Controller
@RequestMapping("subMerchant")
public class SubMerchantController {

    @Autowired
    private SubMerchantService smService;

    @GetMapping("/toList")
    public String toList() {
        return "subMerchant/list";
    }

    @GetMapping("/page")
    @ResponseBody
    public Result<Page<SubMerchantVO>> page(SubMerchantDTO condition, OrderItem orderItem, Page<SubMerchantVO> page) {
        if (orderItem == null) {
            orderItem = OrderItem.desc("sm.createTime");
        }

        QueryWrapper<SubMerchantDTO> qWrapper = Wrappers.query();
        qWrapper.likeRight(StringUtils.isNotEmpty(condition.getMerchantCode()), "mbi.merchantCode", condition.getMerchantCode());
        qWrapper.eq(StringUtils.isNotEmpty(condition.getUserId()), "mbi.userId", condition.getUserId());
        qWrapper.likeRight(StringUtils.isNotEmpty(condition.getMerchantId()), "mcs.merchantId", condition.getMerchantId());
        qWrapper.likeRight(StringUtils.isNotEmpty(condition.getLoginName()), "ui.loginName", condition.getLoginName());
        qWrapper.orderBy(true, orderItem.isAsc(), orderItem.getColumn());
        page = smService.pageVO(page, qWrapper);

        return ResultGenerator.genSuccessResult(page);
    }
}
