package com.maintain.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.maintain.business.domain.bo.ErpRepairFormBo;
import com.maintain.business.domain.vo.ErpRepairFormVo;
import com.maintain.business.domain.vo.ErpRepairSettlementFormVo;
import com.maintain.business.service.IErpRepairFormService;
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.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 报修单
 *
 * @author liushuai
 * @date 2025-04-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/repairForm")
public class ErpRepairFormController extends BaseController {

    private final IErpRepairFormService iErpRepairFormService;

    /**
     * 查询报修单列表
     */
    @SaCheckPermission("business:repairForm:list")
    @GetMapping("/list")
    public TableDataInfo<ErpRepairFormVo> list(ErpRepairFormBo bo, PageQuery pageQuery) {
        return iErpRepairFormService.customQueryPageList(bo, pageQuery);
    }

    /**
     * 导出报修单列表
     */
    @SaCheckPermission("business:repairForm:export")
    @Log(title = "报修单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpRepairFormBo bo, HttpServletResponse response) {
        List<ErpRepairFormVo> list = iErpRepairFormService.queryList(bo);
        ExcelUtil.exportExcel(list, "报修单", ErpRepairFormVo.class, response);
    }

    /**
     * 导出待结算报修单列表
     */
    @Log(title = "报修单", businessType = BusinessType.EXPORT)
    @PostMapping("/settlement/export")
    public void settlementExport(ErpRepairFormBo bo, HttpServletResponse response) {
        List<ErpRepairSettlementFormVo> list = iErpRepairFormService
            .customQueryList(bo).stream()
            .map(item -> {
                ErpRepairSettlementFormVo settlementFormVo = BeanUtil.toBean(item, ErpRepairSettlementFormVo.class);
                settlementFormVo.setClientType(item.getClient().getType());
                settlementFormVo.setClientName(item.getClient().getName());
                settlementFormVo.setClientPhone(item.getClient().getPhone());
                settlementFormVo.setPlateNumber(item.getCar().getPlateNumber());
                return settlementFormVo;
            })
            .collect(Collectors.toList());
        ExcelUtil.exportExcel(list, "待结算报修单", ErpRepairSettlementFormVo.class, response);
    }

    /**
     * 获取报修单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:repairForm:query")
    @GetMapping("/{id}")
    public R<ErpRepairFormVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iErpRepairFormService.customQueryById(id));
    }

    /**
     * 新增报修单
     */
    @SaCheckPermission("business:repairForm:add")
    @Log(title = "报修单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpRepairFormBo bo) {
        return toAjax(iErpRepairFormService.insertByBo(bo));
    }

    /**
     * 修改报修单
     */
    @SaCheckPermission("business:repairForm:edit")
    @Log(title = "报修单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpRepairFormBo bo) {
        return toAjax(iErpRepairFormService.updateByBo(bo));
    }

    /**
     * 作废报修单
     */
    @SaCheckPermission("business:repairForm:abolish")
    @Log(title = "作废报修单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/abolish/{id}")
    public R<Void> abolish(@PathVariable Long id) {
        return toAjax(iErpRepairFormService.abolishByBo(id));
    }

    /**
     * 删除报修单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:repairForm:remove")
    @Log(title = "报修单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iErpRepairFormService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
