package com.maintain.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.maintain.business.domain.bo.ErpCarBo;
import com.maintain.business.domain.vo.ErpCarVo;
import com.maintain.business.listener.ErpCarImportListener;
import com.maintain.business.service.IErpCarService;
import com.maintain.common.annotation.Log;
import com.maintain.common.annotation.RepeatSubmit;
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.page.TableDataInfo;
import com.maintain.common.core.validate.AddGroup;
import com.maintain.common.core.validate.EditGroup;
import com.maintain.common.enums.BusinessType;
import com.maintain.common.excel.ExcelResult;
import com.maintain.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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

    private final IErpCarService iErpCarService;

    /**
     * 查询客户信息-车辆列表
     */
    @SaCheckPermission("business:car:list")
    @GetMapping("/list")
    public TableDataInfo<ErpCarVo> list(ErpCarBo bo, PageQuery pageQuery) {
        return iErpCarService.selectPageCarList(bo, pageQuery);
    }

    /**
     * 客户信息-车辆导入模板下载
     */
    @PostMapping("/importTemplate")
    public void importTemplate(ErpCarBo bo, HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "客户信息-车辆", ErpCarVo.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<ErpCarVo> result = ExcelUtil.importExcel(file.getInputStream(), ErpCarVo.class, new ErpCarImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 导出客户信息-车辆列表
     */
    @SaCheckPermission("business:car:export")
    @Log(title = "客户信息-车辆", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpCarBo bo, HttpServletResponse response) {
        List<ErpCarVo> list = iErpCarService.selectCarList(bo);
        ExcelUtil.exportExcel(list, "车辆信息", ErpCarVo.class, response);
    }

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

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

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

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