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

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.api.DepartmentClient;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.gpsboot.controller.BaseController;
import com.tofly.pojo.User;
import com.tofly.pojo.plan.dto.PlanpathDto;
import com.tofly.pojo.plan.vo.PlanpathVo;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.pojo.plan.Planpath;

import com.tofly.gpsboot.service.daily.PlanpathService;
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.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 巡检线信息
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@RestController
@RequestMapping("/planpath")
@Api(tags="巡检线信息")
public class PlanpathController extends BaseController<Planpath, PlanpathVo> {
    @Autowired
    private PlanpathService service;
    @Autowired
    private DepartmentClient departmentClient;
    
    @Autowired
    public PlanpathController(PlanpathService service) {
        init(service);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    @ToFlyAppLog(title = "分页查询巡检线信息")
    public ResultRespone<Page<PlanpathVo>> getPlanpathPage(Page page, @ModelAttribute PlanpathDto parDto) {
        ResultRespone<Page<PlanpathVo>> rest = new ResultRespone<>();
        try {
            // 获取前端传入部门id
            Long deptId = parDto.getDeptId();
            // 是否跨部门
            Set<Long> userIdList = departmentClient.getDeptUserList(String.valueOf(null == deptId ? SecurityUtils.getUser().getDepartmentId() : deptId)).getResult().stream().map(User::getId).collect(Collectors.toSet());
            parDto.setUserIdList(userIdList);
            Page<PlanpathVo> planPathPage = service.getPlanPathPage(page, parDto);
            rest.setResult(planPathPage);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;


    }

    @ApiOperation(value = "新增基本信息对象")
    @PostMapping("/save")
    @ToFlyAppLog(title = "新增巡检线信息")
    public ResultRespone<Object> savePlanpath(@Valid @RequestBody PlanpathDto 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), Planpath.class)));   } catch (BusinessException e) {
                rest.setCode(e.getRetCd());
                rest.setMessage(e.getMsgDes());
            }
        }
        return rest;
    }

    @ApiOperation(value = "修改基本信息")
    @PutMapping("/updata")
    @ToFlyAppLog(title = "修改巡检线信息")
    public ResultRespone<Object> updateByIdPlanpath(@Valid @RequestBody PlanpathDto 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), Planpath.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")
    })
    @ToFlyAppLog(title = "通过ID查询巡检线信息")
    public ResultRespone<Planpath> getByIdPlanpath(@PathVariable Long id) {
        ResultRespone<Planpath> 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")
    @ToFlyAppLog(title = "通过ID批量删除巡检线信息")
    public ResultRespone<Object> removeByIdListPlanpath(String idList) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            return super.removeByIdList(idList);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }


}
