package cn.iocoder.yudao.module.rescue.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.rescue.core.controller.BaseController;
import cn.iocoder.yudao.module.rescue.domain.RescueCarInfo;
import cn.iocoder.yudao.module.rescue.service.IRescueCarInfoService;
import cn.iocoder.yudao.module.rescue.utils.ExcelUtil;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 救援车辆信息Controller
 *
 * @author zcy
 * @date 2023-09-12
 */
@RestController
@RequestMapping("/system/rescueCar")
public class RescueCarInfoController extends BaseController {
    @Autowired
    private IRescueCarInfoService rescueCarInfoService;
    @Autowired
    private AdminUserApi adminUserApi;

    /**
     * 查询救援车辆信息列表
     */
    @GetMapping("/list")
    public CommonResult<IPage<?>> list(RescueCarInfo rescueCarInfo,
                                       @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        //获取当前登录用户
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        //所在顶级机构
        AdminUserRespDTO user = adminUserApi.getUser(userId);
        rescueCarInfo.setDeptId(user.getDeptId());
        Page<RescueCarInfo> page = new Page<>(pageNo, pageSize);
        return success(rescueCarInfoService.selectRescueCarInfoList(rescueCarInfo, page));
    }

    /**
     * 查询救援车辆信息列表
     */
    @GetMapping("/warnList")
    public CommonResult warnList(RescueCarInfo rescueCarInfo) {
        List<String> list = rescueCarInfoService.warnList(rescueCarInfo);
        return CommonResult.success(list);
    }

    /**
     * 导出救援车辆信息列表
     */
    @PreAuthorize("@ss.hasPermission('system:rescueCar:list')")
//    @Log(title = "救援车辆信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RescueCarInfo rescueCarInfo) {
        List<RescueCarInfo> list = rescueCarInfoService.list();
        ExcelUtil<RescueCarInfo> util = new ExcelUtil<RescueCarInfo>(RescueCarInfo.class);
        util.exportExcel(response, list, "救援车辆信息数据");
    }

    /**
     * 获取救援车辆信息详细信息
     */
    @GetMapping(value = "/{id}")
    public CommonResult getInfo(@PathVariable("id") Long id) {
        return success(rescueCarInfoService.selectRescueCarInfoById(id));
    }

    /**
     * 新增救援车辆信息
     */
//    @Log(title = "救援车辆信息", businessType = BusinessType.INSERT)
    @PostMapping
    public CommonResult add(@RequestBody RescueCarInfo rescueCarInfo) {
        return toAjax(rescueCarInfoService.insertRescueCarInfo(rescueCarInfo));
    }

    /**
     * 修改救援车辆信息
     */
//    @Log(title = "救援车辆信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public CommonResult edit(@RequestBody RescueCarInfo rescueCarInfo) {
        return toAjax(rescueCarInfoService.updateRescueCarInfo(rescueCarInfo));
    }

    /**
     * 删除救援车辆信息
     */
//    @Log(title = "救援车辆信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public CommonResult remove(@PathVariable Long[] ids) {
        return toAjax(rescueCarInfoService.deleteRescueCarInfoByIds(ids));
    }
}
