package cn.pluss.platform.controller.mercSplitSetting;

import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.entity.MerchantBaseInfo;
import cn.pluss.platform.entity.MerchantChannelStatus;
import cn.pluss.platform.entity.MerchantSplitSetting;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mercSplitSetting.MerchantSplitSettingService;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantChannelStatus.MerchantChannelStatusService;
import cn.pluss.platform.util.PageUtils;
import cn.pluss.platform.vo.MerchantSplitSettingVO;
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("/mercSplitSetting")
public class MerchantSplitSettingController extends BaseModelController<MerchantSplitSettingService, MerchantSplitSetting> {


    @Resource
    private MerchantChannelStatusService merchantChannelStatusService;

    @Resource
    private MerchantBaseInfoService merchantBaseInfoService;

    @GetMapping(value = "/index/{csId}")
    public String index(HttpServletRequest request,@PathVariable("csId") Integer csId) {
        request.setAttribute("csId",csId);
        return "merchantStore/mercSplitSettingList";
    }

    @GetMapping("selectPage")
    @ResponseBody
    public Result<PageInfo<MerchantSplitSettingVO>> selectPage(Page<MerchantSplitSettingVO> page, MerchantSplitSettingVO vo) {
        Page<MerchantSplitSettingVO> pageData = baseService.pageData(page,vo);
        return ResultGenerator.genSuccessResult(PageUtils.page(pageData));
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = {"/detail/{csId}/{id}", "/detail/{csId}"})
    public String detail(HttpServletRequest request,@PathVariable(value = "csId") Integer csId,
                         @PathVariable(value = "id", required = false) Integer id) {
        MerchantChannelStatus channel = merchantChannelStatusService.getById(csId);
        MerchantBaseInfo merchant = merchantBaseInfoService.getMerchantBaseInfoByMerchantCode(channel.getMerchantCode());
        if (id != null) {
            MerchantSplitSetting entity = baseService.getById(id);
            request.setAttribute("entity", entity);
        }
        request.setAttribute("channel", channel);
        request.setAttribute("merchant", merchant);
        return "merchantStore/mercSplitSettingDetail";
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/save")
    @Override
    public Result<String> save(@RequestBody MerchantSplitSetting setting) {

        MerchantChannelStatus channel = merchantChannelStatusService.getByMerchantCode(setting.getDiviMerchantCode(), 4);
        MsgException.checkNull(channel,"商户编有误或该商户还未进件银盛通道！");
        setting.setDiviMerchantId(channel.getMerchantId());
        return super.save(setting);
    }
}
