package com.maintain.business.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import com.maintain.business.domain.bo.ErpPostBo;
import com.maintain.business.domain.vo.*;
import com.maintain.business.listener.ErpVehicleModelImportListener;
import com.maintain.common.excel.ExcelResult;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.maintain.common.annotation.RepeatSubmit;
import com.maintain.common.annotation.Log;
import com.maintain.common.core.controller.BaseController;
import com.maintain.common.core.domain.PageQuery;
import com.maintain.common.core.domain.R;
import com.maintain.common.core.validate.AddGroup;
import com.maintain.common.core.validate.EditGroup;
import com.maintain.common.enums.BusinessType;
import com.maintain.common.utils.poi.ExcelUtil;
import com.maintain.business.domain.bo.ErpVehicleModelBo;
import com.maintain.business.service.IErpVehicleModelService;
import com.maintain.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 客户信息-车型
 *
 * @author liushuai
 * @date 2025-03-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/vehicleModel")
public class ErpVehicleModelController extends BaseController {

    private final IErpVehicleModelService iErpVehicleModelService;

    /**
     * 查询客户信息-车型列表
     */
    @SaCheckPermission("business:vehicleModel:list")
    @GetMapping("/list")
    public TableDataInfo<ErpVehicleModelVo> list(ErpVehicleModelBo bo, PageQuery pageQuery) {
        return iErpVehicleModelService.queryPageList(bo, pageQuery);
    }

    /**
     * 车型导入模板下载
     */
    @PostMapping("/importTemplate")
    public void importTemplate(ErpPostBo bo, HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "车型导入模板", ErpVehicleModelVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "客户信息-车型", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<ErpVehicleModelVo> result = ExcelUtil.importExcel(file.getInputStream(), ErpVehicleModelVo.class, new ErpVehicleModelImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 导出客户信息-车型列表
     */
    @SaCheckPermission("business:vehicleModel:export")
    @Log(title = "客户信息-车型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpVehicleModelBo bo, HttpServletResponse response) {
        List<ErpVehicleModelVo> list = iErpVehicleModelService.queryList(bo);
        ExcelUtil.exportExcel(list, "客户信息-车型", ErpVehicleModelVo.class, response);
    }

    /**
     * 获取客户信息-车型详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:vehicleModel:query")
    @GetMapping("/{id}")
    public R<ErpVehicleModelVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iErpVehicleModelService.queryById(id));
    }

    /**
     * 新增客户信息-车型
     */
    @SaCheckPermission("business:vehicleModel:add")
    @Log(title = "客户信息-车型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpVehicleModelBo bo) {
        return toAjax(iErpVehicleModelService.insertByBo(bo));
    }

    /**
     * 修改客户信息-车型
     */
    @SaCheckPermission("business:vehicleModel:edit")
    @Log(title = "客户信息-车型", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpVehicleModelBo bo) {
        return toAjax(iErpVehicleModelService.updateByBo(bo));
    }

    /**
     * 删除客户信息-车型
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:vehicleModel:remove")
    @Log(title = "客户信息-车型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iErpVehicleModelService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
