package com.purui.prims.headless.server.rest;

import com.purui.prims.auth.api.authentication.pojo.User;
import com.purui.prims.auth.api.authentication.utils.UserHolder;
import com.purui.prims.headless.api.pojo.request.BizConditionReq;
import com.purui.prims.headless.api.pojo.response.BizConditionResp;
import com.purui.prims.headless.server.service.BizConditionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping({"/api/semantic/condition"})
@Api(tags = "业务限定")
public class BizConditionController {

    private final BizConditionService bizConditionService;

    public BizConditionController(BizConditionService bizConditionService) {
        this.bizConditionService = bizConditionService;
    }

    @ApiOperation(value = "新建/修改业务限定")
    @PostMapping
    public boolean save(@RequestBody BizConditionReq conditionReq, HttpServletRequest request,
                        HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        return bizConditionService.saveOrUpdate(conditionReq, user);
    }

    @GetMapping("/{id}")
    public BizConditionResp getDecorator(@PathVariable int id) {
        return bizConditionService.getCondition(id);
    }

    @ApiOperation(value = "业务限定列表")
    @GetMapping("/list")
    public List<BizConditionResp> getConditions(Boolean status) {
        BizConditionReq conditionReq = new BizConditionReq();
        conditionReq.setStatus(status);
        return bizConditionService.getConditions(conditionReq);
    }

    @ApiOperation(value = "删除业务限定")
    @PostMapping("/delete/{id}")
    public void delete(@PathVariable Integer id) {
        bizConditionService.delete(id);
    }
}
