package com.maintain.business.controller;

import java.util.Collections;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

import com.maintain.business.domain.bo.ErpSettlementMaintainRepairFormBo;
import com.maintain.business.domain.vo.ErpSettlementMaintainRepairFormVo;
import com.maintain.business.service.IErpRepairFormService;
import com.maintain.business.service.IErpSettlementMaintainRepairFormService;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
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.vo.ErpSettlementMaintainVo;
import com.maintain.business.domain.bo.ErpSettlementMaintainBo;
import com.maintain.business.service.IErpSettlementMaintainService;
import com.maintain.common.core.page.TableDataInfo;

/**
 * 结算维修单
 *
 * @author liushuai
 * @date 2025-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/settlementMaintain")
public class ErpSettlementMaintainController extends BaseController {

    private final IErpSettlementMaintainService iErpSettlementMaintainService;
    private final IErpSettlementMaintainRepairFormService iErpSettlementMaintainRepairFormService;
    private final IErpRepairFormService iErpRepairFormService;

    /**
     * 查询结算维修单列表
     */
    @SaCheckPermission("business:settlementMaintain:list")
    @GetMapping("/list")
    public TableDataInfo<ErpSettlementMaintainVo> list(ErpSettlementMaintainBo bo, PageQuery pageQuery) {
        return iErpSettlementMaintainService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出结算维修单列表
     */
    @SaCheckPermission("business:settlementMaintain:export")
    @Log(title = "结算维修单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpSettlementMaintainBo bo, HttpServletResponse response) {
        List<ErpSettlementMaintainVo> list = iErpSettlementMaintainService.queryList(bo);
        ExcelUtil.exportExcel(list, "结算维修单", ErpSettlementMaintainVo.class, response);
    }

    /**
     * 获取结算维修单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:settlementMaintain:query")
    @GetMapping("/{id}")
    public R<ErpSettlementMaintainVo> getInfo(@NotNull(message = "主键不能为空")
                                              @PathVariable Long id) {
        List<ErpSettlementMaintainRepairFormVo> queryList =
            iErpSettlementMaintainRepairFormService.queryList(
                new ErpSettlementMaintainRepairFormBo().setSettlementMaintainId(id));
        ErpSettlementMaintainVo erpSettlementMaintainVo = iErpSettlementMaintainService.queryById(id);
        erpSettlementMaintainVo.setRepairFormList(iErpRepairFormService.customQueryList(
            queryList.stream().map(ErpSettlementMaintainRepairFormVo::getRepairFormId).collect(Collectors.toList())));
        return R.ok(erpSettlementMaintainVo);
    }

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

    /**
     * 修改结算维修单
     */
    @SaCheckPermission("business:settlementMaintain:edit")
    @Log(title = "结算维修单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpSettlementMaintainBo bo) {
        iErpSettlementMaintainService.updateByBo(bo);
        iErpSettlementMaintainRepairFormService.deleteBySettlementMaintainIds(Collections.singletonList(bo.getId()), true);
        return toAjax(iErpSettlementMaintainRepairFormService.insertBatch(bo.getRepairFormIds().stream()
            .map(b -> new ErpSettlementMaintainRepairFormBo()
                .setSettlementMaintainId(b)
                .setRepairFormId(bo.getId())
            ).collect(Collectors.toList())));
    }

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

    /**
     * 结算维修单回执
     *
     * @param bo
     */
    @SaCheckPermission("business:settlementMaintain:receipt")
    @Log(title = "结算维修单回执", businessType = BusinessType.UPDATE)
    @PutMapping("/receipt")
    public R<Void> receipt(@Validated(EditGroup.class) @RequestBody ErpSettlementMaintainBo bo) {
        return toAjax(iErpSettlementMaintainService.receiptByBo(bo));
    }
}
