/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.gpsboot.controller.build;

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.build.vo.BuildVo;
import com.tofly.pojo.build.dto.BuildDto;
import com.tofly.pojo.build.vo.detail.BuildDetailVo;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.service.build.BuildService;
import com.tofly.pojo.build.Build;
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 org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 工地监护上报信息表
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@RestController
@RequestMapping("/build")
@Api(tags = "工地监护上报信息表接口")
public class BuildController extends BaseController<Build, BuildVo> {
    @Autowired
    private BuildService service;

    @Autowired
    public BuildController(BuildService service) {
        init(service);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    @ToFlyAppLog(title = "分页查询工地监护上报信息")
    public ResultRespone<Page<BuildVo>> getBuildPage(Page page, @ModelAttribute BuildDto parDto) {
        ResultRespone<Page<BuildVo>> rest = new ResultRespone<>();
        Page<BuildVo> buildPage = service.getBuildPage(page, parDto);
        rest.setResult(buildPage);
        return rest;
    }

    @ApiOperation(value = "新增基本信息对象")
    @PostMapping(value = "/save")
    @ToFlyAppLog(title = "新增工地监护上报信息")
    public ResultRespone<Object> saveBuild(@Valid BuildDto parDto, BindingResult result,@RequestParam(value = "fileList", required = false) List<MultipartFile> fileList) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        if (!Optional.ofNullable(super.validRest(rest, result)).isPresent()) {
            service.savaBuildInfo(fileList, parDto);
        }
        return rest;
    }

    @ApiOperation(value = "修改基本信息")
    @PutMapping("/updata")
    @ToFlyAppLog(title = "修改工地监护上报信息")
    public ResultRespone<Object> updateByIdBuild(@Valid BuildDto parDto, BindingResult result,@RequestParam(value = "fileList", required = false) List<MultipartFile> fileList) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        if (!Optional.ofNullable(super.validRest(rest, result)).isPresent()) {
            try {
                service.updateBuildInfo(fileList, parDto);
            } catch (BusinessException e) {
                rest.setCode(e.getRetCd());
                rest.setMessage(e.getMsgDes());
            }

        }
        return rest;

    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @ToFlyAppLog(title = "通过ID查询工地监护上报信息")
    public ResultRespone<Build> getByIdBuild(@PathVariable Long id) {
        return super.getById(id);
    }


    @GetMapping("/getBuildDetail")
    @ApiOperation(value = "通过ID查询工地监护上报详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "flag", value = "是否展示部门下所有数据 是则填1 个人不填为空", required = false, dataType = "String")
    })
    @ToFlyAppLog(title = "通过ID查询工地监护上报详情")
    public ResultRespone<BuildDetailVo> getBuildDetail(Long id,String flag) {
        ResultRespone<BuildDetailVo> rest = new ResultRespone<>();
        try {
           BuildDetailVo info = service.getBuildInfoDetail(id,flag);
            rest.setResult(info);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }

    @ApiOperation(value = "通过ID批量删除基本信息对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    @ToFlyAppLog(title = "通过ID批量删除工地监护上报详情")
    public ResultRespone<Object> removeByIdListBuild(String ids) {
        ResultRespone<Object> rest = new ResultRespone<>();
        List<Long> removeId = Arrays.stream(ids.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                .collect(Collectors.toList());
        try {

            service.deleteBuildInfo(removeId);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }


    @GetMapping("/getBuildIdList")
    @ApiOperation(value = "获取工地id列表")
    @ToFlyAppLog(title = "获取工地id列表")
    public ResultRespone<List<Long>> getBuildIdList() {
        ResultRespone<List<Long>> rest = new ResultRespone<>();
        rest.setResult(service.list().stream().map(Build::getId).collect(Collectors.toList()));
        return rest;
    }


}
