package com.ningdatech.carapi.car.equipment.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.carapi.car.equipment.model.res.SipEquipmentRes;
import com.ningdatech.carapi.car.equipment.model.vo.VehicleEquipmentVO;
import com.ningdatech.carapi.car.equipment.manage.VehicleEquipmentManage;
import com.ningdatech.carapi.car.equipment.model.dto.EquipmentSaveDTO;
import com.ningdatech.carapi.car.equipment.model.dto.EquipmentUpdateDTO;
import com.ningdatech.carapi.car.equipment.model.dto.VehicleEquipmentPageQuery;
import com.ningdatech.carapi.car.equipment.model.dto.VehicleEquipmentQueryDTO;
import com.ningdatech.carapi.car.equipment.service.INdVehicleEquipmentInfoService;
import com.ningdatech.carapi.car.vehicle.model.dto.VehicleArchivesPageQuery;
import com.ningdatech.carapi.common.util.ExcelDownUtil;
import com.ningdatech.carapi.user.util.LoginUserUtil;
import com.ningdatech.log.annotation.WebLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author PoffyZhang
 * @since 2022-10-09
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/archives/equipment")
@Api(value = "archives", tags = "车辆管理-档案管理")
@RequiredArgsConstructor
public class VehicleEquipmentController {
    private final VehicleEquipmentManage equipmentManage;
    private final INdVehicleEquipmentInfoService vehicleEquipmentInfoService;

    @ApiOperation(value = "车辆设备待添加列表", notes = "车辆设备待添加列表")
    @GetMapping("/toBeAddedList")
    public PageVo<VehicleEquipmentVO> queryToBeAdded(@ModelAttribute VehicleArchivesPageQuery vehicleArchivesPageQuery) {
        return equipmentManage.queryToBeAdded(vehicleArchivesPageQuery);
    }

    @ApiOperation(value = "车辆设备已添加列表", notes = "车辆设备已添加列表")
    @GetMapping("/addedList")
    public PageVo<VehicleEquipmentVO> queryAdded(@ModelAttribute VehicleEquipmentPageQuery vehicleArchivesPageQuery) {
        return equipmentManage.queryAdded(vehicleArchivesPageQuery);
    }

    @ApiOperation(value = "车辆设备列表导出", notes = "车辆设备列表导出")
    @GetMapping("/export")
    @WebLog
    public void export(VehicleEquipmentPageQuery req, HttpServletResponse response) {
        ExcelDownUtil.downXls(response, req, equipmentManage::export);
    }

    /**
     * 查询车辆设备信息
     * @param id  设备ID
     * @return 查询结果
     */
    @ApiOperation(value = "查询车辆设备信息-以设备ID查询", notes = "查询车辆设备信息-以设备ID查询")
    @GetMapping("/detailsbyId/{id}")
    public VehicleEquipmentQueryDTO getEquipmentById(@PathVariable("id") Long id) {
        return equipmentManage.getEquipmentById(id);
    }

    /**
     * 查询车辆设备信息
     * @param vehicleId  车辆ID
     * @return 查询结果
     */
    @ApiOperation(value = "查询车辆设备信息-以车辆ID查询", notes = "查询车辆设备信息-以车辆ID查询")
    @GetMapping("/detailsbyVehicleId/{vehicleId}")
    public List<VehicleEquipmentQueryDTO> getEquipment(@PathVariable("vehicleId") Long vehicleId) {
        return equipmentManage.getEquipment(vehicleId);
    }

    @ApiOperation(value = "批量保存车辆设备", notes = "批量保存车辆设备")
    @PostMapping("/save")
    @WebLog
    public List<VehicleEquipmentQueryDTO> handlerSave(@Valid @RequestBody List<EquipmentSaveDTO> datas) {
        vehicleEquipmentInfoService.save(datas, LoginUserUtil.getUserId());
        return CollUtils.convert(datas,w -> BeanUtil.toBean(w, VehicleEquipmentQueryDTO.class));
    }

    @ApiOperation(value = "编辑车辆设备", notes = "编辑车辆设备")
    @PostMapping("/modify")
    @WebLog
    public VehicleEquipmentQueryDTO handlerUpdate(@Valid @RequestBody EquipmentUpdateDTO data) {
        vehicleEquipmentInfoService.update(data, LoginUserUtil.getUserId());
        return BeanUtil.toBean(data, VehicleEquipmentQueryDTO.class);
    }

    @ApiOperation(value = "删除车辆设备", notes = "删除车辆设备")
    @PostMapping("/remove")
    @WebLog
    public Boolean handlerDelete(@Valid @RequestBody List<Long> ids) {
        return vehicleEquipmentInfoService.removeByIdWithCache(ids);
    }
}
