package com.propertyManagement.web.controller.business;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.propertyManagement.business.domain.bo.WxCarportBo;
import com.propertyManagement.business.domain.bo.WxProprietorLedgerBo;
import com.propertyManagement.business.domain.vo.WxCarportVo;
import com.propertyManagement.business.domain.vo.WxProprietorLedgerVo;
import com.propertyManagement.business.listener.WxCarportImportListener;
import com.propertyManagement.business.service.IWxCarportService;
import com.propertyManagement.business.service.IWxProprietorLedgerService;
import com.propertyManagement.common.annotation.Log;
import com.propertyManagement.common.annotation.RepeatSubmit;
import com.propertyManagement.common.core.controller.BaseController;
import com.propertyManagement.common.core.domain.PageQuery;
import com.propertyManagement.common.core.domain.R;
import com.propertyManagement.common.core.page.TableDataInfo;
import com.propertyManagement.common.core.validate.AddGroup;
import com.propertyManagement.common.core.validate.EditGroup;
import com.propertyManagement.common.enums.BusinessType;
import com.propertyManagement.common.excel.ExcelResult;
import com.propertyManagement.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-05-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/carport")
public class WxCarportController extends BaseController {

    private final IWxCarportService iWxCarportService;
    private final IWxProprietorLedgerService proprietorLedgerService;

    /**
     * 查询业主车位列表
     */
    @SaCheckPermission("business:carport:list")
    @GetMapping("/list")
    public TableDataInfo<WxCarportVo> list(WxCarportBo bo, PageQuery pageQuery) {
        return iWxCarportService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询业主车位列表
     */
    @GetMapping("/listAll")
    public R<List<WxCarportVo>> listAll(WxCarportBo bo) {
        if (bo.getProprietorId() != null) {
            WxProprietorLedgerVo proprietorLedgerVo = proprietorLedgerService.queryById(bo.getProprietorId());
            bo.setCommunityId(proprietorLedgerVo.getCommunityId());
        }
        return R.ok(iWxCarportService.queryList(bo));
    }

    /**
     * 导出业主车位列表
     */
    @SaCheckPermission("business:carport:export")
    @Log(title = "业主车位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxCarportBo bo, HttpServletResponse response) {
        List<WxCarportVo> list = iWxCarportService.queryList(bo);
        ExcelUtil.exportExcel(list, "业主车位", WxCarportVo.class, response);
    }

    /**
     * 业主车位导入模板下载
     */
    @PostMapping("/importTemplate")
    public void importTemplate(WxProprietorLedgerBo bo, HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "业主车位模板", WxCarportVo.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, Long communityId, boolean updateSupport) throws Exception {
        ExcelResult<WxCarportVo> result = ExcelUtil.importExcel(file.getInputStream(), WxCarportVo.class, new WxCarportImportListener(communityId, updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取业主车位详细信息
     *
     * @param carportId 主键
     */
    @SaCheckPermission("business:carport:query")
    @GetMapping("/{carportId}")
    public R<WxCarportVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long carportId) {
        return R.ok(iWxCarportService.queryById(carportId));
    }

    /**
     * 新增业主车位
     */
    @SaCheckPermission("business:carport:add")
    @Log(title = "业主车位", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxCarportBo bo) {
        return toAjax(iWxCarportService.insertByBo(bo));
    }

    /**
     * 修改业主车位
     */
    @SaCheckPermission("business:carport:edit")
    @Log(title = "业主车位", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WxCarportBo bo) {
        return toAjax(iWxCarportService.updateByBo(bo));
    }

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