package com.zcloud.primeport.web;

import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.zcloud.primeport.api.MkmjPassageServiceI;
import com.zcloud.primeport.dto.MkmjPassageAddCmd;
import com.zcloud.primeport.dto.MkmjPassagePageQry;
import com.zcloud.primeport.dto.MkmjPassageUpdateCmd;
import com.zcloud.primeport.dto.clientobject.MkmjPassageCO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(tags = "口门门禁通道表")
@RequestMapping("/${application.gateway}/mkmjPassage")
@RestController
@AllArgsConstructor
public class MkmjPassageController {
    private final MkmjPassageServiceI mkmjPassageService;

    @ApiOperation("新增")
    @PostMapping("/save")
    public SingleResponse<MkmjPassageCO> add(@Validated @RequestBody MkmjPassageAddCmd cmd) {
        return mkmjPassageService.add(cmd);
    }

    @ApiOperation("分页")
    @PostMapping("/list")
    public PageResponse<MkmjPassageCO> page(@RequestBody MkmjPassagePageQry qry) {
        return mkmjPassageService.listPage(qry);
    }

    @ApiOperation("详情")
    @GetMapping("/{id}")
    public SingleResponse<MkmjPassageCO> getInfoById(@PathVariable("id") Long id) {
        return SingleResponse.of(mkmjPassageService.getInfoById(id));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public Response remove(@PathVariable("id") Long id) {
        mkmjPassageService.remove(id);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("修改")
    @PutMapping("/edit")
    public SingleResponse edit(@Validated @RequestBody MkmjPassageUpdateCmd mkmjPassageUpdateCmd) {
        mkmjPassageService.edit(mkmjPassageUpdateCmd);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("名称唯一性校验")
    @GetMapping("/check-name")
    public SingleResponse<Map<String, Boolean>> checkName(
            @RequestParam String passageName,
            @RequestParam Long mkmjId,
            @RequestParam(required = false) Long id) {
        Boolean available = mkmjPassageService.checkName(passageName, mkmjId, id);
        Map<String, Boolean> result = new HashMap<>();
        result.put("available", available);
        return SingleResponse.of(result);
    }
}


