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

import com.alibaba.fastjson.JSON;

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.gpsboot.controller.BaseController;
import com.tofly.gpsboot.service.paln.GroupuserService;
import com.tofly.pojo.plan.Groupuser;
import com.tofly.pojo.plan.dto.InspectgroupDto;
import com.tofly.pojo.plan.vo.InspectgroupVo;

import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.pojo.plan.Inspectgroup;

import com.tofly.gpsboot.service.config.InspectgroupService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
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.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("/inspectgroup")
@Api(tags="巡查分组信息")
public class InspectgroupController extends BaseController<Inspectgroup, InspectgroupVo> {

    @Autowired
    private InspectgroupService service;
    @Autowired
    private GroupuserService groupuserService;

    @Autowired
    public InspectgroupController(InspectgroupService service) {
        init(service);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone<Page<InspectgroupVo>> getInspectgroupPage(Page page, @ModelAttribute InspectgroupDto parDto) {
        ResultRespone<Page<InspectgroupVo>> rest = new ResultRespone<>();
        try {
            return super.getInfoPage(page, JSON.parseObject(JSON.toJSONString(parDto), Inspectgroup.class));
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;

    }

    @ApiOperation(value = "新增基本信息对象")
    @PostMapping("/save")
    public ResultRespone<Object> saveInspectgroup(@Valid @RequestBody InspectgroupDto parDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        if (!Optional.ofNullable(super.validRest(rest, result)).isPresent()) {
            try {
                return Optional.ofNullable(super.validRest(rest, result)).orElseGet(() -> super.save(JSON.parseObject(JSON.toJSONString(parDto), Inspectgroup.class)));
            } catch (BusinessException e) {
                rest.setCode(e.getRetCd());
                rest.setMessage(e.getMsgDes());
            }
        }
        return rest;
    }

    @ApiOperation(value = "修改基本信息")
    @PutMapping("/updata")
    public ResultRespone<Object> updateByIdInspectgroup(@Valid @RequestBody InspectgroupDto parDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        if (!Optional.ofNullable(super.validRest(rest, result)).isPresent()) {
            try {
                return Optional.ofNullable(super.validRest(rest, result)).orElseGet(() -> super.updateById(JSON.parseObject(JSON.toJSONString(parDto), Inspectgroup.class)));
            } 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")
    })
    public ResultRespone<Inspectgroup> getByIdInspectgroup(@PathVariable Long id) {
        ResultRespone<Inspectgroup> rest = new ResultRespone<>();
        try {
            return super.getById(id);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;

    }


    @ApiOperation(value = "通过ID批量删除基本信息对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idList", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    @Transactional(rollbackFor = Exception.class)
    public ResultRespone<Object> removeByIdListInspectgroup(String idList) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            groupuserService.remove(new QueryWrapper<Groupuser>().lambda()
                    .in(Groupuser::getGroupId, Arrays.stream(idList.split(",")).map(Long::parseLong).collect(Collectors.toList())));
            return super.removeByIdList(idList);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }

    @GetMapping("getInspectGroupInfo")
    @ApiOperation(value = "根据当前用户查询对应巡检分组信息,若当前用户没绑定巡检分组,则返回所有分组信息")
    public ResultRespone<List<Inspectgroup>> getInspectGroupInfo() {
        ResultRespone<List<Inspectgroup>> rest = new ResultRespone<>();
        try {
            List<Inspectgroup> inspectGroupInfo = service.getInspectGroupInfo();
            rest.setResult(inspectGroupInfo);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;

    }

}
