package cn.pluss.platform.controller.common;

import cn.jiguang.common.utils.StringUtils;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.operationRecord.OperationRecordService;
import cn.pluss.platform.vo.OperationRecordVO;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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;

@Controller
@RequestMapping("operationRecord")
public class OperationRecordController {

    @Autowired
    private OperationRecordService opRecordService;

    @GetMapping("/index")
    public String index() {
        return "/operationRecord/index";
    }

    @GetMapping("/page")
    @ResponseBody
    public Result<Object> page(Page<OperationRecordVO> page, OrderItem orderItem, OperationRecordVO condition) {
        page.getOrders().clear();
        if (StringUtils.isNotEmpty(orderItem.getColumn())) {
            switch (orderItem.getColumn()) {
                case "userName":
                    orderItem.setColumn("ua.userName");
                    break;
                case "count":
                    orderItem.setColumn("t.count");
                    break;
                default:
                    orderItem.setColumn("opr." + orderItem.getColumn());
                    break;
            }
        }
        page.getOrders().add(orderItem);

        Page<OperationRecordVO> data = opRecordService.pageVO(page, condition);
        return ResultGenerator.genSuccessResult(data);
    }
}
