package cn.pluss.platform.controller.mercOrderSplit;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.merchantOrderSplit.MerchantOrderSplitDetailService;
import cn.pluss.platform.merchantOrderSplit.MerchantOrderSplitService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 *
 *@description: 分账订单
 *@author: bzg
 *@time: 2021/11/26 14:27
 */
@Controller
@RequiredArgsConstructor
@RequestMapping("/mercOrderSplit")
public class MerchantOrderSplitController extends BaseModelController<MerchantOrderSplitService, MerchantOrderSplit> {


    @Resource
    private MerchantOrderSplitDetailService merchantOrderSplitDetailService;

    @GetMapping(value = "/index")
    public String index(HttpServletRequest request) {
        return "mercOrderSplit/list";
    }

    @Override
    public Result<Page<MerchantOrderSplit>> page(Page<MerchantOrderSplit> page, MerchantOrderSplit condition, OrderItem orderItem) {
        orderItem.setAsc(false);
        orderItem.setColumn("id");
        return super.page(page, condition, orderItem);
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/detail/{orderNumeber}")
    public String detail(HttpServletRequest request, @PathVariable(value = "orderNumeber") String orderNumeber) {
        request.setAttribute("orderNumber",orderNumeber);
        return "mercOrderSplit/detailList";
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/detailOrder/{id}")
    public String detail(HttpServletRequest request, @PathVariable(value = "id",required = false) Integer id) {
        if(id != null){
            MerchantOrderSplitDetail entity = merchantOrderSplitDetailService.getById(id);
            request.setAttribute("entity",entity);
        }
        return "mercOrderSplit/detailOrder";
    }


    @RequestMapping(value = "/detailPage")
    @ResponseBody
    public Result<Page<MerchantOrderSplitDetail>> detailPage(Page<MerchantOrderSplitDetail> page, MerchantOrderSplitDetail entity) {
        QueryWrapper<MerchantOrderSplitDetail> queryWrapper = new QueryWrapper<>(entity);
        queryWrapper.last(" order by id desc ");
        page = merchantOrderSplitDetailService.page(page, queryWrapper);
        return ResultGenerator.genSuccessResult("获取成功",page);
    }
}
