package com.codvision.basic.controller.patrol;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codvision.basic.entity.patrol.PatrolPoint;
import com.codvision.basic.entity.vo.patrol.PatrolPointVO;
import com.codvision.basic.service.patrol.PatrolPointService;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.webcore.bean.QueryPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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


/**
 * @author codvision
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/patrolPoint")
@Api(tags = "巡查--点位")
public class PatrolPointController {

    private final PatrolPointService service;

    @PostMapping("/getPageResult")
    @ApiOperation("分页列表")
    public ResponseEntity<IPage<PatrolPoint>> getPageResult(@RequestBody QueryPage<Map<String, Object>> queryPage) {
        return new ResponseEntity<IPage<PatrolPoint>>().data(service.getPageResult(queryPage));
    }

    @PostMapping("/getPointByRecordId")
    @ApiOperation("根据记录获取点位列表")
    public ResponseEntity<List<PatrolPointVO>> getPointByRecordId(@ApiParam(value = "记录id", required = true) @RequestParam(value = "recordId") Integer recordId,
                                                                  @ApiParam(value = "路线id") @RequestParam(value = "routeId", required = false) Integer routeId) {
        ResponseEntity<List<PatrolPointVO>> responseEntity = new ResponseEntity<>();
        if (recordId == null) {
            throw new BusinessException(30002);
        }

        return responseEntity.data(service.getPointByRecordId(recordId, routeId));
    }


    @PostMapping("/add")
    @ApiOperation("添加")
    public ResponseEntity<String> add(@Validated @RequestBody PatrolPoint patrolpoint) {
        service.save(patrolpoint);
        return new ResponseEntity<String>().ok();
    }

    @PostMapping("/update")
    @ApiOperation("编辑")
    public ResponseEntity<String> update(@RequestBody PatrolPoint patrolpoint) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (patrolpoint.getId() == null) {
            throw new BusinessException(30002);
        }
        service.updateById(patrolpoint);
        return responseEntity.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("删除巡查点位信息")
    public ResponseEntity<String> delete(@RequestBody List<Integer> pointIds) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (CollectionUtils.isEmpty(pointIds)) {
            throw new BusinessException(30002);
        }
        service.batchDel(pointIds);
        return responseEntity.ok();
    }

}

