package quant.rich.emoney.controller.manage;

import java.util.Objects;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.extern.slf4j.Slf4j;
import quant.rich.emoney.controller.common.BaseController;
import quant.rich.emoney.entity.sqlite.ProtocolMatch;
import quant.rich.emoney.exception.RException;
import quant.rich.emoney.pojo.dto.LayPageReq;
import quant.rich.emoney.pojo.dto.LayPageResp;
import quant.rich.emoney.pojo.dto.R;
import quant.rich.emoney.service.sqlite.ProtocolMatchService;

@Slf4j
@Controller
@RequestMapping("/admin/v1/manage/protocolMatch")
public class ProtocolMatchControllerV1 extends BaseController {
    
    @Autowired
    ProtocolMatchService protocolMatchService;
	
	@GetMapping({"", "/", "/index"})
	public String index() {
		return "/admin/v1/manage/protocolMatch/index";
	}
	
	@GetMapping("/list")
	@ResponseBody
	public LayPageResp<?> list(LayPageReq<ProtocolMatch> pageReq) {
	    Page<ProtocolMatch> planPage = protocolMatchService.page(pageReq);
	    return new LayPageResp<>(planPage);
	}
	
	@GetMapping("/getOne")
	@ResponseBody
	public R<?> getOne(Integer protocolId) {
	    
	    // 如果 planId 是空，说明可能希望新建一个 Plan，需要返回默认实例化对象
	    if (protocolId == null) {
	        return R.ok(new ProtocolMatch());
	    }
	    
	    // 否则从数据库取
	    ProtocolMatch protocolMatch = protocolMatchService.getById(protocolId);
	    return R.judge(protocolMatch != null, protocolMatch, "无法找到对应 ID 的 ProtocolMatch");
	}
	
	@PostMapping("/save")
	@ResponseBody
	public R<?> save(@RequestBody ProtocolMatch protocolMatch) {
	    if (Objects.nonNull(protocolMatch.getProtocolId())) {
	        return R.judge(
	                protocolMatchService.saveOrUpdate(protocolMatch), "保存失败");
	    }
	    throw RException.badRequest("protocolId 不允许为空");
	}

    @PostMapping("/delete")
    @ResponseBody
    public R<?> delete(Integer protocolMatchId) {
        return R.judge(protocolMatchService.removeById(protocolMatchId), "删除失败，是否已删除？");
    }
	
}
