package cn.pluss.platform.controller.sys;

import cn.pluss.platform.SxfMccInfoService;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.mcc.MccInfoYsService;
import cn.pluss.platform.mcc.MccReflectService;
import cn.pluss.platform.merchantChannel.MerchantChannelService;
import cn.pluss.platform.vo.MccReflectVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.*;

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

@Controller
@RequestMapping("/mccInfo")
public class MccInfoController {

    @Autowired
    private SxfMccInfoService sxfMccInfoService;

    @Autowired
    private MccInfoYsService mccInfoYsService;

    @Autowired
    private MccReflectService mccReflectService;

    @Resource
    private MerchantChannelService merchantChannelService;

    @GetMapping("/sxfMccList")
    public String sxfMccList() {
        return "sys/mcc/sxfMccList";
    }

    @GetMapping("/sxf/page")
    @ResponseBody
    public Result<Page<SxfMccInfo>> pageSxfMccInfo(Page<SxfMccInfo> page, SxfMccInfo condition, OrderItem orderItem) {
        if (orderItem != null && orderItem.getColumn() != null) {
            page.getOrders().add(orderItem);
        }
        page = sxfMccInfoService.page(page, new QueryWrapper<>(condition));
        return ResultGenerator.genSuccessResult("请求成功", page);
    }

    @GetMapping("/sxfMccEdit")
    public String edit(String sxfMcc, Integer channelId, HttpServletRequest request) {
        if (sxfMcc != null) {
            MccReflect mccReflect = new MccReflect();
            mccReflect.setStandardMccCode(sxfMcc);
            mccReflect.setChannelId(channelId);
            mccReflect = mccReflectService.getOne(new QueryWrapper<>(mccReflect));
            request.setAttribute("entity", mccReflect);
        }

        return "sys/mcc/mccReflectEditV2";
    }

    @GetMapping("/ysMccList")
    public String ysMccList() {
        return "sys/mcc/ysMccList";
    }

    @GetMapping("/ys/page")
    @ResponseBody
    public Result<Page<MccInfoYs>> pageYsMccInfo(Page<MccInfoYs> page, MccInfoYs condition, OrderItem orderItem) {
        if (orderItem != null && orderItem.getColumn() != null) {
            page.getOrders().add(orderItem);
        }

        page = mccInfoYsService.page(page, new QueryWrapper<>(condition));
        return ResultGenerator.genSuccessResult("请求成功", page);
    }

    @GetMapping("/mccInfoReflect")
    public String mccInfoReflect() {
        return "sys/mcc/mccReflectList";
    }

    @GetMapping("/mccReflect/page")
    @ResponseBody
    public Result<IPage<MccReflectVO>> pageMccInfoReflect(Page<MccReflectVO> page, MccReflect condition, OrderItem orderItem) {
        if (orderItem != null && orderItem.getColumn() != null) {
            page.getOrders().add(orderItem);
        }

        IPage<MccReflectVO> result = mccReflectService.pageMccReflectVO(page, condition);
        return ResultGenerator.genSuccessResult("请求成功", result);
    }

    @DeleteMapping("/mccReflect/del/{id}")
    @ResponseBody
    public Result<Object> delMccInfoReflect(@PathVariable("id") Integer id) {
        boolean flag = mccReflectService.removeById(id);
        if(flag){
            return ResultGenerator.genSuccessResult("删除成功！", null);
        }else{
            return ResultGenerator.genFailResult("删除失败！");
        }
    }

    @RequestMapping("/mccReflect/save")
    @ResponseBody
    public Result<Object> saveMccInfoReflect(@RequestBody MccReflect condition) {
        mccReflectService.saveOrUpdate(condition);
        return ResultGenerator.genSuccessResult("保存成功", null);
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = {"/mccReflect/detail/{id}", "/mccReflect/detail"})
    public String detail(HttpServletRequest request, @PathVariable(value = "id", required = false) Integer id) {
        List<MerchantChannel> channelList = merchantChannelService.getValidChannelList();
        if (id != null) {
            MccReflect entity = mccReflectService.getOne(new QueryWrapper<>(new MccReflect().setId(id)));
            request.setAttribute("entity", entity);
        }
        request.setAttribute("channelList",channelList);
        return "sys/mcc/mccReflectEdit";
    }
}
