package com.cm.smartgate.controller.apis;

import com.cm.common.annotation.CheckRequestBodyAnnotation;
import com.cm.common.base.AbstractController;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.ParamsException;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SearchException;
import com.cm.common.pojo.ListPage;
import com.cm.common.result.ErrorResult;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultData;
import com.cm.common.result.SuccessResultList;
import com.cm.smartgate.pojo.dtos.SmartGateDTO;
import com.cm.smartgate.pojo.vos.SmartGateVO;
import com.cm.smartgate.service.ISmartGateService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: SmartGateController
 * @Description: 智慧闸机
 * @Author: WangGeng
 * @Date: 2020/8/27 9:38
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "智慧闸机接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/smartgate")
public class SmartGateController extends AbstractController {

    @Autowired
    private ISmartGateService smartGateService;

    @ApiOperation(value = "新增智慧闸机", notes = "新增智慧闸机接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("savesmartgate")
    @CheckRequestBodyAnnotation
    public SuccessResult saveSmartGate(@RequestBody SmartGateVO smartGateVO) throws Exception {
        return smartGateService.saveSmartGate(smartGateVO);
    }

    @ApiOperation(value = "删除智慧闸机(id列表)", notes = "删除智慧闸机(id列表)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("removesmartgate/{ids}")
    public SuccessResult removeSmartGate(@PathVariable("ids") String ids) throws RemoveException {
        return smartGateService.removeSmartGate(ids);
    }

    @ApiOperation(value = "修改智慧闸机", notes = "修改智慧闸机接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "smartGateId", value = "智慧闸机ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updatesmartgate/{smartGateId}")
    @CheckRequestBodyAnnotation
    public SuccessResult updateSmartGate(@PathVariable("smartGateId") String smartGateId, @RequestBody SmartGateVO smartGateVO) throws Exception {
        return smartGateService.updateSmartGate(smartGateId, smartGateVO);
    }

    @ApiOperation(value = "修改开门模式", notes = "修改开门模式接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "smartGateId", value = "智慧闸机ID", paramType = "path"),
            @ApiImplicitParam(name = "doorMode", value = "开门模式", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updatedoormode/{smartGateId}/{doorMode}")
    public SuccessResult updateDoorMode(@PathVariable("smartGateId") String smartGateId, @PathVariable("doorMode") Integer doorMode) {
        if (doorMode != 1 && doorMode != 2 && doorMode != 3) {
            throw new ParamsException("模式错误");
        }
        return smartGateService.updateDoorMode(smartGateId, doorMode);
    }

    @ApiOperation(value = "修改通行模式", notes = "修改通行模式接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "smartGateId", value = "智慧闸机ID", paramType = "path"),
            @ApiImplicitParam(name = "generalMode", value = "通行模式", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updategeneralmode/{smartGateId}/{generalMode}")
    public SuccessResult updateGeneralMode(@PathVariable("smartGateId") String smartGateId, @PathVariable("generalMode") Integer generalMode) {
        if (generalMode != 0 && generalMode != 1) {
            throw new ParamsException("模式错误");
        }
        return smartGateService.updateGeneralMode(smartGateId, generalMode);
    }

    @ApiOperation(value = "修改认证模式", notes = "修改认证模式接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "smartGateId", value = "智慧闸机ID", paramType = "path"),
            @ApiImplicitParam(name = "generalMode", value = "通行模式", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updatetrustmode/{smartGateId}/{trustMode}")
    public SuccessResult updateTrustMode(@PathVariable("smartGateId") String smartGateId, @PathVariable("trustMode") Integer trustMode) {
        if (trustMode != 1 && trustMode != 2 && trustMode != 3 && trustMode != 4) {
            throw new ParamsException("模式错误");
        }
        return smartGateService.updateTrustMode(smartGateId, trustMode);
    }

    @ApiOperation(value = "智慧闸机详情(通过ID)", notes = "智慧闸机详情(通过ID)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "smartgateId", value = "智慧闸机ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getsmartgatebyid/{smartgateId}")
    public SmartGateDTO getSmartGateById(@PathVariable("smartgateId") String smartgateId) throws SearchException {
        return smartGateService.getSmartGateById(smartgateId);
    }

    @ApiOperation(value = "检查智慧闸机设备编码(通过serialNo)", notes = "检查智慧闸机设备编码(通过serialNo)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "smartgateId", value = "智慧闸机ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("checksmartgatebyserialno/{smartGateSerialNo}")
    public SuccessResultData<String> checkSmartGateBySerialNo(@PathVariable("smartGateSerialNo") String smartGateSerialNo) throws SearchException {
        return smartGateService.checkSmartGateBySerialNo(smartGateSerialNo);
    }

    @ApiOperation(value = "检查智慧闸机设备编码(通过smartGateId, 通过serialNo)", notes = "检查智慧闸机设备编码(通过通过smartGateId, serialNo)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "smartgateId", value = "智慧闸机ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("checksmartgatebyserialno/{smartGateId}/{smartGateSerialNo}")
    public SuccessResultData<String> checkSmartGateBySerialNo(@PathVariable("smartGateId") String smartGateId, @PathVariable("smartGateSerialNo") String smartGateSerialNo) throws SearchException {
        return smartGateService.checkSmartGateBySerialNo(smartGateId, smartGateSerialNo);
    }

    @ApiOperation(value = "智慧闸机列表", notes = "智慧闸机列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listsmartgate")
    public List<SmartGateDTO> listSmartGate() throws SearchException {
        Map<String, Object> params = requestParams();
        return smartGateService.listSmartGate(params);
    }

    @ApiOperation(value = "智慧闸机分页列表", notes = "智慧闸机分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpagesmartgate")
    public SuccessResultList<List<SmartGateDTO>> listPageSmartGate(ListPage page) throws SearchException {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return smartGateService.listPageSmartGate(page);
    }

    @ApiOperation(value = "智慧闸机统计", notes = "智慧闸机统计接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("countsmartgate")
    SuccessResultData<Integer> countSmartGate() throws SearchException {
        Map<String, Object> params = requestParams();
        return smartGateService.countSmartGate(params);
    }

    @ApiOperation(value = "开门(通过ID)", notes = "开门(通过ID)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "smartGateId", value = "智慧闸机ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getopendoorbyid/{smartGateId}")
    public SuccessResult getOpenDoorById(@PathVariable("smartGateId") String smartGateId) {
        return smartGateService.getOpenDoorById(smartGateId);
    }

}
