/*
 * Author: Zev
 * 生成controller
 */
package com.tofly.gpsboot.controller.process;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;

import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.controller.BaseController;
import com.tofly.pojo.plan.dto.PointTempUpdateDto;
import com.tofly.pojo.plan.dto.PointtempDto;
import com.tofly.pojo.plan.vo.PointtempVo;

import com.tofly.pojo.plan.Pointtemp;

import com.tofly.gpsboot.service.process.PointtempService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * 巡检计划点到位信息
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@RestController
@RequestMapping("/pointtemp")
@Api(tags = "巡检计划点到位信息")
public class PointtempController extends BaseController<Pointtemp, PointtempVo> {

    @Autowired
    private PointtempService service;

    @Autowired
    public PointtempController(PointtempService service) {
        init(service);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    @ToFlyAppLog(title = "分页查询巡检计划点到位信息")
    public ResultRespone<Page<PointtempVo>> getPointtempPage(Page page, @ModelAttribute PointtempDto parDto) {
        ResultRespone<Page<PointtempVo>> rest = new ResultRespone<>();
        Page<PointtempVo> buildPage = service.getInfoPage(page, parDto);
        rest.setResult(buildPage);
        return rest;
    }

    @GetMapping("/all")
    @ApiOperation(value = "查询全部")
    @ToFlyAppLog(title = "查询全部巡检计划点到位信息")
    public ResultRespone<List<Pointtemp>> getPointtempAll(@ModelAttribute PointtempDto parDto) {
        ResultRespone<List<Pointtemp>> rest = new ResultRespone<>();
        Pointtemp pointtemp = JSONObject.parseObject(JSON.toJSONString(parDto), Pointtemp.class);
        List<Pointtemp> buildPage = service.list(new QueryWrapper<Pointtemp>().lambda().setEntity(pointtemp));
        rest.setResult(buildPage);
        return rest;
    }


    @ApiOperation(value = "新增基本信息对象")
    @PostMapping("/save")
    @ToFlyAppLog(title = "新增巡检计划点到位信息")
    public ResultRespone<Object> savePointtemp(@Valid @RequestBody PointtempDto parDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        return Optional.ofNullable(super.validRest(rest, result)).orElseGet(() -> super.save(JSON.parseObject(JSON.toJSONString(parDto), Pointtemp.class)));

    }

    @ApiOperation(value = "修改基本信息")
    @PutMapping("/updata")
    @ToFlyAppLog(title = "修改巡检计划点到位信息")
    public ResultRespone<Object> updateByIdPointtemp(@Valid @RequestBody PointtempDto parDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        return Optional.ofNullable(super.validRest(rest, result)).orElseGet(() -> super.updateById(JSON.parseObject(JSON.toJSONString(parDto), Pointtemp.class)));

    }

    @ApiOperation(value = "批量更新点到位信息")
    @PutMapping("/updatePointTempList")
    @ToFlyAppLog(title = "批量更新点到位信息")
    public ResultRespone<Object> updatePointTempList(@RequestBody PointTempUpdateDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        List<Pointtemp> pointTempList = dto.getPointTempList();
        if (null != pointTempList && pointTempList.size() > 0) {
            rest.setResult(service.updatePointTempList(pointTempList));
        } else {
            rest.setCode(Constant.UPDATE_POINT_TEMP_LIST_ERROR_CODE);
            rest.setMessage(Constant.UPDATE_POINT_TEMP_LIST_ERROR);
        }

        return rest;

    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @ToFlyAppLog(title = "通过ID查询巡检计划点到位信息")
    public ResultRespone<Pointtemp> getByIdPointtemp(@PathVariable Long id) {
        return super.getById(id);
    }

    @ApiOperation(value = "通过ID批量删除基本信息对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    @ToFlyAppLog(title = "通过ID批量删除巡检计划点到位信息")
    public ResultRespone<Object> removeByIdListPointtemp(String ids) {
        return super.removeByIdList(ids);
    }

}
